@@ -39,7 +39,7 @@ def get_image(sprite, texture, alpha_texture) -> Image:
3939def get_image_from_sprite (m_Sprite ) -> Image :
4040 atlas = None
4141 if m_Sprite .m_SpriteAtlas :
42- atlas = m_Sprite .m_SpriteAtlas
42+ atlas = m_Sprite .m_SpriteAtlas . read ()
4343 elif m_Sprite .m_AtlasTags :
4444 # looks like the direct pointer is empty, let's try to find the Atlas via its name
4545 for obj in m_Sprite .assets_file .objects .values ():
@@ -84,44 +84,37 @@ def get_image_from_sprite(m_Sprite) -> Image:
8484 # Tight
8585
8686 # create mask to keep only the polygon
87- points = get_points (m_Sprite .m_RD )
88- points = normalize_points (points , m_Sprite .m_PixelsToUnits )
8987 mask = Image .new ("1" , sprite_image .size , color = 0 )
9088 draw = ImageDraw .ImageDraw (mask )
91- draw .polygon (points , fill = 1 )
89+ for triangle in get_triangles (m_Sprite ):
90+ draw .polygon (triangle , fill = 1 )
9291
9392 # apply the mask
94- empty_img = Image .new (sprite_image .mode , sprite_image .size , color = 0 )
95- sprite_image = Image .composite (sprite_image , empty_img , mask )
93+ sprite_image .putalpha (mask )
9694
9795 return sprite_image .transpose (Image .FLIP_TOP_BOTTOM )
9896
9997
100- def normalize_points (points , mod ):
101- min_x = min (p .X for p in points )
102- min_y = min (p .Y for p in points )
103- return [
104- ((p .X - min_x ) * mod , (p .Y - min_y ) * mod )
105- for p in points
106- ]
107-
108-
109- def get_points (m_RD ):
98+ def get_triangles (m_Sprite ):
11099 """
111- returns the vertices of the sprite
100+ returns the triangles of the sprite polygon
112101 """
102+ m_RD = m_Sprite .m_RD
103+
104+ # read the raw points
105+ points = []
113106 if hasattr (m_RD , "vertices" ): # 5.6 down
114107 vertices = [v .pos for v in m_RD .vertices ]
115- # points = [
116- # vertices[index]
117- # for index in range(m_RD.indices)
118- # ]
108+ points = [
109+ vertices [index ]
110+ for index in range (m_RD .indices )
111+ ]
119112 else : # 5.6 and up
120113 m_Channel = m_RD .m_VertexData .m_Channels [0 ] # kShaderChannelVertex
121114 m_Stream = m_RD .m_VertexData .m_Streams [m_Channel .stream ]
122115
123116 vertexReader = EndianBinaryReader (m_RD .m_VertexData .m_DataSize , endian = '<' )
124- # indexReader = EndianBinaryReader(m_RD.m_IndexBuffer, endian='<')
117+ indexReader = EndianBinaryReader (m_RD .m_IndexBuffer , endian = '<' )
125118
126119 for subMesh in m_RD .m_SubMeshes :
127120 vertexReader .Position = m_Stream .offset + subMesh .firstVertex * m_Stream .stride + m_Channel .offset
@@ -131,9 +124,24 @@ def get_points(m_RD):
131124 vertices .append (vertexReader .read_vector3 ())
132125 vertexReader .Position = vertexReader .Position + m_Stream .stride - 12
133126
134- # indexReader.Position = subMesh.firstByte
127+ indexReader .Position = subMesh .firstByte
135128
136- # for _ in range(subMesh.indexCount):
137- # points.append(vertices[indexReader.read_u_short() - subMesh.firstVertex])
129+ for _ in range (subMesh .indexCount ):
130+ points .append (vertices [indexReader .read_u_short () - subMesh .firstVertex ])
138131
139- return vertices
132+ # normalize the points
133+ # shift the whole point matrix into the positive space
134+ # multiply them with a factor to scale them to the image
135+ min_x = min (p .X for p in points )
136+ min_y = min (p .Y for p in points )
137+ factor = m_Sprite .m_PixelsToUnits
138+ points = [
139+ ((p .X - min_x ) * factor , (p .Y - min_y ) * factor )
140+ for p in points
141+ ]
142+
143+ # generate triangles from the given points
144+ return [
145+ points [i :i + 3 ]
146+ for i in range (0 , len (points ), 3 )
147+ ]
0 commit comments