1+ using System ;
2+ using UnityEditor ;
3+ using UnityEngine ;
4+ using Object = UnityEngine . Object ;
5+
6+ namespace Hitsub . ExpressionFaceIconGenerator . Scripts . Editor
7+ {
8+ public class AnimationViewerGenerator
9+ {
10+ private GameObject m_animatedRoot ;
11+ private Camera m_camera ;
12+
13+ public void Begin ( GameObject animatedRoot )
14+ {
15+ m_animatedRoot = animatedRoot ;
16+
17+ m_camera = new GameObject ( ) . AddComponent < Camera > ( ) ;
18+
19+ var sceneCamera = SceneView . lastActiveSceneView . camera ;
20+ m_camera . transform . position = sceneCamera . transform . position ;
21+ m_camera . transform . rotation = sceneCamera . transform . rotation ;
22+ var whRatio = ( 1f * sceneCamera . pixelWidth / sceneCamera . pixelHeight ) ;
23+ m_camera . fieldOfView = whRatio < 1 ? sceneCamera . fieldOfView * whRatio : sceneCamera . fieldOfView ;
24+ m_camera . orthographic = sceneCamera . orthographic ;
25+ m_camera . nearClipPlane = sceneCamera . nearClipPlane ;
26+ m_camera . farClipPlane = sceneCamera . farClipPlane ;
27+ m_camera . orthographicSize = sceneCamera . orthographicSize ;
28+ m_camera . clearFlags = CameraClearFlags . SolidColor ;
29+ m_camera . backgroundColor = Color . gray ;
30+
31+ }
32+
33+ public void ParentCameraTo ( Transform newParent )
34+ {
35+ m_camera . transform . parent = newParent ;
36+ }
37+
38+ public void Terminate ( )
39+ {
40+ Object . DestroyImmediate ( m_camera . gameObject ) ;
41+ }
42+
43+ public void Render ( AnimationClip clip , Texture2D element , float normalizedTime )
44+ {
45+ var initPos = m_animatedRoot . transform . position ;
46+ var initRot = m_animatedRoot . transform . rotation ;
47+ try
48+ {
49+ AnimationMode . StartAnimationMode ( ) ;
50+ AnimationMode . BeginSampling ( ) ;
51+ AnimationMode . SampleAnimationClip ( m_animatedRoot . gameObject , clip , normalizedTime * clip . length ) ;
52+ AnimationMode . EndSampling ( ) ;
53+ // This is a workaround for an issue where for some reason, the animator moves to the origin
54+ // after sampling despite the animation having no RootT/RootQ properties.
55+ m_animatedRoot . transform . position = initPos ;
56+ m_animatedRoot . transform . rotation = initRot ;
57+
58+ var renderTexture = RenderTexture . GetTemporary ( element . width , element . height , 24 ) ;
59+ renderTexture . wrapMode = TextureWrapMode . Clamp ;
60+
61+ RenderCamera ( renderTexture , m_camera ) ;
62+ RenderTextureTo ( renderTexture , element ) ;
63+ RenderTexture . ReleaseTemporary ( renderTexture ) ;
64+ }
65+ catch ( Exception e )
66+ {
67+ Debug . LogError ( e ) ;
68+ }
69+ finally
70+ {
71+ AnimationMode . StopAnimationMode ( ) ;
72+ m_animatedRoot . transform . position = initPos ;
73+ m_animatedRoot . transform . rotation = initRot ;
74+ }
75+ }
76+
77+ private static void RenderCamera ( RenderTexture renderTexture , Camera camera )
78+ {
79+ var originalRenderTexture = camera . targetTexture ;
80+ var originalAspect = camera . aspect ;
81+ try
82+ {
83+ camera . targetTexture = renderTexture ;
84+ camera . aspect = ( float ) renderTexture . width / renderTexture . height ;
85+ camera . Render ( ) ;
86+ }
87+ catch ( Exception e )
88+ {
89+ Debug . LogError ( e ) ;
90+ }
91+ finally
92+ {
93+ camera . targetTexture = originalRenderTexture ;
94+ camera . aspect = originalAspect ;
95+ }
96+ }
97+
98+ private static void RenderTextureTo ( RenderTexture renderTexture , Texture2D texture2D )
99+ {
100+ RenderTexture . active = renderTexture ;
101+ texture2D . ReadPixels ( new Rect ( 0 , 0 , renderTexture . width , renderTexture . height ) , 0 , 0 ) ;
102+ texture2D . Apply ( ) ;
103+ RenderTexture . active = null ;
104+ }
105+ }
106+ }
0 commit comments