2020SOFTWARE.
2121'''
2222
23- from .IOTools import unpackStream , readNullTerminatedString
23+ from .IOTools import unpackStream , readNullTerminatedString , readLengthPrefixedString , calculatePadding
2424
2525class A3DMaterial :
2626 def __init__ (self ):
@@ -36,10 +36,17 @@ def read2(self, stream):
3636 print (f"[A3DMaterial name: { self .name } color: { self .color } diffuse map: { self .diffuseMap } ]" )
3737
3838 def read3 (self , stream ):
39- pass
39+ self .name = readLengthPrefixedString (stream )
40+ self .color = unpackStream ("<3f" , stream )
41+ self .diffuseMap = readLengthPrefixedString (stream )
42+
43+ print (f"[A3DMaterial name: { self .name } color: { self .color } diffuse map: { self .diffuseMap } ]" )
4044
4145class A3DMesh :
4246 def __init__ (self ):
47+ self .name = ""
48+ self .bboxMax = None
49+ self .bboxMin = None
4350 self .vertexBuffers = []
4451 self .submeshes = []
4552
@@ -58,7 +65,31 @@ def read2(self, stream):
5865 submesh .read2 (stream )
5966 self .submeshes .append (submesh )
6067
61- print (f"[A3DMesh vertex buffers: { len (self .vertexBuffers )} submeshes: { len (self .submeshes )} ]" )
68+ print (f"[A3DMesh name: { self .name } bbox max: { self .bboxMax } bbox min: { self .bboxMin } vertex buffers: { len (self .vertexBuffers )} submeshes: { len (self .submeshes )} ]" )
69+
70+ def read3 (self , stream ):
71+ # Read mesh info
72+ self .name = readLengthPrefixedString (stream )
73+ # XXX: bbox order maybe incorrect, check this (might be min then max and not max then min)
74+ self .bboxMax = unpackStream ("<3f" , stream )
75+ self .bboxMin = unpackStream ("<3f" , stream )
76+ stream .read (4 ) # XXX: Unknown float value
77+
78+ # Read vertex buffers
79+ vertexCount , bufferCount = unpackStream ("<2I" , stream )
80+ for _ in range (bufferCount ):
81+ vertexBuffer = A3DVertexBuffer ()
82+ vertexBuffer .read2 (vertexCount , stream )
83+ self .vertexBuffers .append (vertexBuffer )
84+
85+ # Read submeshes
86+ submeshCount , = unpackStream ("<I" , stream )
87+ for _ in range (submeshCount ):
88+ submesh = A3DSubmesh ()
89+ submesh .read3 (stream )
90+ self .submeshes .append (submesh )
91+
92+ print (f"[A3DMesh name: { self .name } bbox max: { self .bboxMax } bbox min: { self .bboxMin } vertex buffers: { len (self .vertexBuffers )} submeshes: { len (self .submeshes )} ]" )
6293
6394A3D_VERTEXTYPE_COORDINATE = 1
6495A3D_VERTEXTYPE_UV1 = 2
@@ -93,6 +124,7 @@ def read2(self, vertexCount, stream):
93124
94125class A3DSubmesh :
95126 def __init__ (self ):
127+ self .indices = []
96128 self .faces = []
97129 self .smoothingGroups = []
98130 self .materialID = None
@@ -107,10 +139,24 @@ def read2(self, stream):
107139 self .smoothingGroups .append (smoothingGroup )
108140 self .materialID , = unpackStream ("<H" , stream )
109141
110- print (f"[A3DSubmesh faces: { len (self .faces )} smoothing groups: { len (self .smoothingGroups )} materialID: { self .materialID } ]" )
142+ print (f"[A3DSubmesh indices: { len (self .indices )} faces: { len (self .faces )} smoothing groups: { len (self .smoothingGroups )} materialID: { self .materialID } ]" )
143+
144+ def read3 (self , stream ):
145+ # Read indices
146+ indexCount , = unpackStream ("<I" , stream )
147+ for _ in range (indexCount ):
148+ index , = unpackStream ("<H" , stream )
149+ self .indices .append (index )
150+
151+ # Padding
152+ padding = calculatePadding (indexCount * 2 ) # Each index is 2 bytes
153+ stream .read (padding )
154+
155+ print (f"[A3DSubmesh indices: { len (self .indices )} faces: { len (self .faces )} smoothing groups: { len (self .smoothingGroups )} materialID: { self .materialID } ]" )
111156
112157class A3DTransform :
113158 def __init__ (self ):
159+ self .name = ""
114160 self .position = (0.0 , 0.0 , 0.0 )
115161 self .rotation = (0.0 , 0.0 , 0.0 , 0.0 )
116162 self .scale = (0.0 , 0.0 , 0.0 )
@@ -122,14 +168,33 @@ def read2(self, stream):
122168
123169 print (f"[A3DTransform position: { self .position } rotation: { self .rotation } scale: { self .scale } ]" )
124170
171+ def read3 (self , stream ):
172+ self .name = readLengthPrefixedString (stream )
173+ self .position = unpackStream ("<3f" , stream )
174+ self .rotation = unpackStream ("<4f" , stream )
175+ self .scale = unpackStream ("<3f" , stream )
176+
177+ print (f"[A3DTransform name: { self .name } position: { self .position } rotation: { self .rotation } scale: { self .scale } ]" )
178+
125179class A3DObject :
126180 def __init__ (self ):
127181 self .name = ""
128182 self .meshID = None
129183 self .transformID = None
184+ self .materialIDs = []
130185
131186 def read2 (self , stream ):
132187 self .name = readNullTerminatedString (stream )
133188 self .meshID , self .transformID = unpackStream ("<2I" , stream )
134189
135- print (f"[A3DObject name: { self .name } meshID: { self .meshID } transformID: { self .transformID } ]" )
190+ print (f"[A3DObject name: { self .name } meshID: { self .meshID } transformID: { self .transformID } materialIDs: { len (self .materialIDs )} ]" )
191+
192+ def read3 (self , stream ):
193+ self .meshID , self .transformID , materialCount = unpackStream ("<3I" , stream )
194+
195+ # Read material IDs
196+ for _ in range (materialCount ):
197+ materialID , = unpackStream ("<I" , stream )
198+ self .materialIDs .append (materialID )
199+
200+ print (f"[A3DObject name: { self .name } meshID: { self .meshID } transformID: { self .transformID } materialIDs: { len (self .materialIDs )} ]" )
0 commit comments