1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using SPICA . Formats . CtrH3D . Model ;
5+ using SPICA . Formats . CtrH3D . Model . Material ;
6+ using SPICA . PICA . Commands ;
7+ using SPICA . PICA . Converters ;
8+ using UnityEngine ;
9+
10+ namespace ExtensionMethods
11+ {
12+ public static class H3DMaterialExtensions
13+ {
14+ public static int GetTextureIndex ( this H3DMaterial h3DMaterial , string name )
15+ {
16+ return h3DMaterial . Texture0Name == name ? 0 : h3DMaterial . Texture1Name == name ? 1 : 2 ;
17+ }
18+
19+ public static IEnumerable < string > TextureNames ( this H3DMaterial h3DMaterial )
20+ {
21+ return new List < string > {
22+ h3DMaterial . Texture0Name , h3DMaterial . Texture1Name , h3DMaterial . Texture2Name
23+ } ;
24+ }
25+ }
26+ }
27+
28+ public static class TextureUtils
29+ {
30+ public static Texture2D FlipTexture ( Texture2D original )
31+ {
32+ var flipped = new Texture2D ( original . width , original . height ) ;
33+ var xN = original . width ;
34+ var yN = original . height ;
35+
36+ for ( var i = 0 ; i < xN ; i ++ )
37+ for ( var j = 0 ; j < yN ; j ++ )
38+ flipped . SetPixel ( xN - i - 1 , j , original . GetPixel ( i , j ) ) ;
39+ flipped . Apply ( ) ;
40+ return flipped ;
41+ }
42+
43+ public static TextureWrapMode PicaToUnityTextureWrapMode ( PICATextureWrap picaTextureWrap )
44+ {
45+ // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
46+ switch ( picaTextureWrap ) {
47+ case PICATextureWrap . Repeat :
48+ return TextureWrapMode . Repeat ;
49+ case PICATextureWrap . Mirror :
50+ return TextureWrapMode . Mirror ;
51+ case PICATextureWrap . ClampToEdge :
52+ return TextureWrapMode . Clamp ;
53+ default :
54+ return TextureWrapMode . Mirror ;
55+ }
56+ }
57+
58+ public class H3DTextureRepresentation
59+ {
60+ public H3DTextureCoord TextureCoord ;
61+ public H3DTextureMapper TextureMapper ;
62+ }
63+ }
64+
65+ public static class MeshUtils
66+ {
67+ public static IEnumerable < Vector3 > PicaToUnityVertex ( IEnumerable < PICAVertex > picaVertices )
68+ {
69+ return picaVertices . Select ( picaVertex =>
70+ new Vector3 ( picaVertex . Position . X , picaVertex . Position . Y , picaVertex . Position . Z ) ) . ToList ( ) ;
71+ }
72+
73+ public static IEnumerable < Vector4 > PicaToUnityTangents ( IEnumerable < PICAVertex > picaVertices )
74+ {
75+ return picaVertices . Select ( picaVertex =>
76+ new Vector4 ( picaVertex . Tangent . X , picaVertex . Tangent . Y , picaVertex . Tangent . Z , picaVertex . Tangent . W ) )
77+ . ToList ( ) ;
78+ }
79+
80+ public static IEnumerable < Vector2 > PicaToUnityUV ( IEnumerable < PICAVertex > picaVertices )
81+ {
82+ return picaVertices . Select ( picaVertex => new Vector2 ( picaVertex . TexCoord0 . X , picaVertex . TexCoord0 . Y ) )
83+ . ToList ( ) ;
84+ }
85+
86+ public static IEnumerable < Vector3 > PicaToUnityNormals ( IEnumerable < PICAVertex > picaVertices )
87+ {
88+ return picaVertices
89+ . Select ( picaVertex => new Vector3 ( picaVertex . Normal . X , picaVertex . Normal . Y , picaVertex . Normal . Z ) )
90+ . ToList ( ) ;
91+ }
92+ }
93+
94+ public static class VectorExtensions
95+ {
96+ public static Vector3 CastNumericsVector3 ( System . Numerics . Vector3 newValues )
97+ {
98+ var vector3 = new Vector3 { x = newValues . X , y = newValues . Y , z = newValues . Z } ;
99+ return vector3 ;
100+ }
101+
102+ public static Vector3 CastNumericsVector3 ( System . Numerics . Vector4 newValues )
103+ {
104+ var vector3 = new Vector3 { x = newValues . X , y = newValues . Y , z = newValues . Z } ;
105+ return vector3 ;
106+ }
107+
108+ public static Vector4 CastNumericsVector4 ( System . Numerics . Vector4 newValues )
109+ {
110+ var vector4 = new Vector4 { x = newValues . X , y = newValues . Y , z = newValues . Z , w = newValues . W } ;
111+ return vector4 ;
112+ }
113+
114+ public static Vector3 GetAxisFromRotation ( Vector4 vector4 )
115+ {
116+ return new Vector3 ( vector4 . x * - 1 , vector4 . y * - 1 , vector4 . z * - 1 ) ;
117+ }
118+
119+ public static float GetScalarFromRotation ( Vector4 vector4 )
120+ {
121+ return vector4 . w ;
122+ }
123+ }
124+
125+ public static class SkeletonUtils
126+ {
127+ private const float RadToDegConstant = ( float ) ( 1 / Math . PI * 180 ) ;
128+
129+ public static SkeletonNode GenerateSkeletonForModel ( H3DModel mdl )
130+ {
131+ if ( ( mdl . Skeleton ? . Count ?? 0 ) <= 0 ) return null ;
132+ var rootNode = new SkeletonNode ( ) ;
133+ var childBones = new Queue < Tuple < H3DBone , SkeletonNode > > ( ) ;
134+
135+ childBones . Enqueue ( Tuple . Create ( mdl . Skeleton [ 0 ] , rootNode ) ) ;
136+
137+ while ( childBones . Count > 0 ) {
138+ var ( item1 , item2 ) = childBones . Dequeue ( ) ;
139+
140+ var bone = item1 ;
141+
142+ item2 . Name = bone . Name ;
143+ item2 . SetBoneEuler (
144+ VectorExtensions . CastNumericsVector3 ( bone . Translation ) ,
145+ VectorExtensions . CastNumericsVector3 ( bone . Rotation ) ,
146+ VectorExtensions . CastNumericsVector3 ( bone . Scale )
147+ ) ;
148+
149+ foreach ( var b in mdl . Skeleton ) {
150+ if ( b . ParentIndex == - 1 ) continue ;
151+
152+ var parentBone = mdl . Skeleton [ b . ParentIndex ] ;
153+
154+ if ( parentBone != bone ) continue ;
155+
156+ var node = new SkeletonNode ( ) ;
157+
158+ childBones . Enqueue ( Tuple . Create ( b , node ) ) ;
159+
160+ if ( item2 . Nodes == null ) item2 . Nodes = new List < SkeletonNode > ( ) ;
161+
162+ item2 . Nodes . Add ( node ) ;
163+ }
164+ }
165+
166+ return rootNode ;
167+ }
168+
169+ private static float RadToDeg ( float radians )
170+ {
171+ return radians * RadToDegConstant ;
172+ }
173+
174+ public class SkeletonNode
175+ {
176+ public string Name ;
177+
178+ public List < SkeletonNode > Nodes ;
179+ public Vector4 [ ] Rotation ;
180+ public Vector3 Scale ;
181+
182+ public Vector3 Translation ;
183+
184+ public void SetBoneEuler ( Vector3 t , Vector3 r , Vector3 s )
185+ {
186+ Rotation = new Vector4 [ 3 ] ;
187+ Translation = t ;
188+ Rotation [ 0 ] = new Vector4 ( 0 , 0 , 1 , RadToDeg ( r . z ) ) ;
189+ Rotation [ 1 ] = new Vector4 ( 0 , 1 , 0 , RadToDeg ( r . y ) ) ;
190+ Rotation [ 2 ] = new Vector4 ( 1 , 0 , 0 , RadToDeg ( r . x ) ) ;
191+ Scale = s ;
192+ }
193+ }
194+ }
0 commit comments