13
|
1 from io import BytesIO
|
|
2 import os
|
|
3 import struct
|
|
4 from itertools import chain
|
|
5
|
|
6 from pytouhou.utils.matrix import Matrix
|
|
7 from pytouhou.utils.interpolator import Interpolator
|
|
8
|
|
9 from pytouhou.formats.std import Stage
|
|
10 from pytouhou.formats.anm0 import Animations
|
|
11
|
|
12
|
|
13 class Background(object):
|
|
14 def __init__(self, archive, stage_num):
|
|
15 self.stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)))
|
|
16 self.anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num)))
|
|
17 self.objects = []
|
|
18 self.object_instances = []
|
|
19 self._uvs = b''
|
|
20 self._vertices = b''
|
|
21 self.build_objects()
|
|
22 self.build_object_instances()
|
|
23
|
|
24
|
|
25 def build_object_instances(self):
|
|
26 self.object_instances = []
|
|
27 for obj, ox, oy, oz in self.stage.object_instances:
|
|
28 obj_id = self.stage.objects.index(obj)
|
|
29
|
|
30 obj_instance = []
|
|
31 for face_vertices, face_uvs in self.objects[obj_id]:
|
|
32 obj_instance.append((tuple((x + ox, y + oy, z + oz)
|
|
33 for x, y, z in face_vertices),
|
|
34 face_uvs))
|
|
35 self.object_instances.append(obj_instance)
|
|
36 # Z-sorting
|
|
37 def keyfunc(obj):
|
|
38 return min(z for face in obj for x, y, z in face[0])
|
|
39 self.object_instances.sort(key=keyfunc, reverse=True)
|
|
40
|
|
41
|
|
42 def object_instances_to_vertices_uvs(self):
|
|
43 vertices = tuple(vertex for obj in self.object_instances
|
|
44 for face in obj for vertex in face[0])
|
|
45 uvs = tuple(uv for obj in self.object_instances
|
|
46 for face in obj for uv in face[1])
|
|
47 return vertices, uvs
|
|
48
|
|
49
|
|
50 def build_objects(self):
|
|
51 self.objects = []
|
|
52 for i, obj in enumerate(self.stage.objects):
|
|
53 faces = []
|
|
54 for script_index, x, y, z, width_override, height_override in obj.quads:
|
|
55 #TODO: refactor
|
|
56 vertices = []
|
|
57 uvs = []
|
|
58 vertmat = Matrix()
|
|
59 vertmat.data[0][0] = -.5
|
|
60 vertmat.data[1][0] = -.5
|
|
61
|
|
62 vertmat.data[0][1] = .5
|
|
63 vertmat.data[1][1] = -.5
|
|
64
|
|
65 vertmat.data[0][2] = .5
|
|
66 vertmat.data[1][2] = .5
|
|
67
|
|
68 vertmat.data[0][3] = -.5
|
|
69 vertmat.data[1][3] = .5
|
|
70
|
|
71 for i in range(4):
|
|
72 vertmat.data[2][i] = 0.
|
|
73 vertmat.data[3][i] = 1.
|
|
74
|
|
75 properties = {}
|
|
76 for time, instr_type, data in self.anim.scripts[script_index]:
|
|
77 if instr_type == 15:
|
|
78 properties[15] = b''
|
|
79 break
|
|
80 elif time == 0: #TODO
|
|
81 properties[instr_type] = data
|
|
82 #if 15 not in properties: #TODO: Skip properties
|
|
83 # continue
|
|
84
|
|
85 #TODO: properties 3 and 4
|
|
86 if 1 in properties:
|
|
87 tx, ty, tw, th = self.anim.sprites[struct.unpack('<I', properties[1])[0]]
|
|
88 width, height = 1., 1.
|
|
89 if 2 in properties:
|
|
90 width, height = struct.unpack('<ff', properties[2])
|
|
91 width = width_override or width * tw
|
|
92 height = height_override or height * th
|
|
93 transform = Matrix.get_scaling_matrix(width, height, 1.)
|
|
94 if 7 in properties:
|
|
95 transform = Matrix.get_scaling_matrix(-1., 1., 1.).mult(transform)
|
|
96 if 9 in properties:
|
|
97 rx, ry, rz = struct.unpack('<fff', properties[9])
|
|
98 transform = Matrix.get_rotation_matrix(-rx, 'x').mult(transform)
|
|
99 transform = Matrix.get_rotation_matrix(ry, 'y').mult(transform)
|
|
100 transform = Matrix.get_rotation_matrix(-rz, 'z').mult(transform) #TODO: minus, really?
|
|
101 if 23 in properties: # Reposition
|
|
102 transform = Matrix.get_translation_matrix(width / 2., height / 2., 0.).mult(transform)
|
|
103 vertmat = transform.mult(vertmat)
|
|
104
|
|
105 uvs = [(tx / self.anim.size[0], 1. - (ty / self.anim.size[1])),
|
|
106 ((tx + tw) / self.anim.size[0], 1. - (ty / self.anim.size[1])),
|
|
107 ((tx + tw) / self.anim.size[0], 1. - ((ty + th) / self.anim.size[1])),
|
|
108 (tx / self.anim.size[0], 1. - ((ty + th) / self.anim.size[1]))]
|
|
109
|
|
110 for i in xrange(4):
|
|
111 w = vertmat.data[3][i]
|
|
112 vertices.append((vertmat.data[0][i] / w + x,
|
|
113 vertmat.data[1][i] / w + y,
|
|
114 vertmat.data[2][i] / w + z))
|
|
115 faces.append((vertices, uvs))
|
|
116 self.objects.append(faces)
|
|
117
|
|
118
|
|
119 def update(self, frame):
|
|
120 if not self._uvs or not self._vertices:
|
|
121 vertices, uvs = self.object_instances_to_vertices_uvs()
|
|
122 self.nb_vertices = len(vertices)
|
|
123 vertices_format = 'f' * (3 * self.nb_vertices)
|
|
124 uvs_format = 'f' * (2 * self.nb_vertices)
|
|
125 self._vertices = struct.pack(vertices_format, *chain(*vertices))
|
|
126 self._uvs = struct.pack(uvs_format, *chain(*uvs))
|
|
127
|
|
128 self.position_interpolator = Interpolator((0, 0, 0))
|
|
129 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0))
|
|
130 self.position2_interpolator = Interpolator((0, 0, 0))
|
|
131
|
|
132 for frame_num, message_type, args in self.stage.script:
|
|
133 if frame_num == frame:
|
14
|
134 if message_type == 0:
|
|
135 self.position_interpolator.set_interpolation_start(frame_num, args)
|
|
136 elif message_type == 1:
|
13
|
137 self.fog_interpolator.set_interpolation_end_values(args)
|
14
|
138 elif message_type == 2:
|
|
139 self.position2_interpolator.set_interpolation_end_values(args)
|
13
|
140 elif message_type == 3:
|
|
141 duration, = args
|
|
142 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration)
|
|
143 elif message_type == 4:
|
|
144 duration, = args
|
|
145 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration)
|
|
146 if frame_num > frame and message_type == 0:
|
|
147 self.position_interpolator.set_interpolation_end(frame_num, args)
|
|
148 break
|
|
149
|
|
150 self.position2_interpolator.update(frame)
|
|
151 self.fog_interpolator.update(frame)
|
|
152 self.position_interpolator.update(frame)
|
|
153
|