summaryrefslogtreecommitdiff
path: root/extractor/rsm_format.gd
blob: cf83af9c48c18bb862bc3a925b523623a910f251 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
class_name RSMFormat


## Byte Length: 4 [br]
## GRAT
var signature: String = "GRSM"

## Byte Type: u8 [br]
## Byte Length: 2
var version: Version

## Byte Type: u32 [br]
## Byte Length: 4
var animation_length: int

## Byte Type: u32 [br]
## Byte Length: 4
var shade_type: int

## Byte Type: u8 [br]
## Byte Length: 1
## Versions: [>=1.4]
var alpha: int

## Byte Type: u8 [br]
## Byte Length: 16
## Versions: [<2.2]
var reserved: PackedByteArray

## Byte Type: f32 [br]
## Byte Length: 4
## Versions: [>=2.2]
var frames_per_second: float

## Byte Type: u32 [br]
## Byte Length: 4
## Versions: [<2.3]
var texture_count: int

## Length: [member texture_count]
## Byte Length: 40
var texture_names: Array[String]

## Byte Type: u8
## Byte Length: 40
var root_node_name: String

## Byte Type: u32 [br]
## Byte Length: 4
## Versions: [>=2.2]
var root_node_count: int

## Length: [member root_node_count]
## Byte Length: 40
var root_node_names: Array[String]

## Byte Type: u32 [br]
## Byte Length: 4
var node_count: int

## Length: [member node_count]
var nodes: Array[ModelNode]


static func from_bytes(bytes: ByteStream) -> RSMFormat:
	var rsm_format = RSMFormat.new()
	
	bytes.advance(rsm_format.signature.length())
	
	@warning_ignore("shadowed_variable")
	var version = Version.new()
	version.major = bytes.decode_u8()
	version.minor = bytes.decode_u8()
	rsm_format.version = version
	
	rsm_format.animation_length = bytes.decode_u32()
	rsm_format.shade_type = bytes.decode_u32()
	
	if version.higher_than(1, 3): # >= 1.4
		rsm_format.alpha = bytes.decode_u8()
	
	if version.lower_than(2, 2): # < 2.2
		rsm_format.reserved = bytes.get_buffer(16).bytes
	
	if version.higher_than(2, 1): # >= 2.2
		rsm_format.frames_per_second = bytes.decode_float()
	
	if version.lower_than(2, 3): # < 2.3
		rsm_format.texture_count = bytes.decode_u32()
		
		rsm_format.texture_names = [] as Array[String]
		for _n in rsm_format.texture_count:
			rsm_format.texture_names.append(bytes.get_string_from_utf8(40))
	
	rsm_format.root_node_name = bytes.get_string_from_utf8(40)
	
	if version.higher_than(2, 1): # >= 2.2
		rsm_format.root_node_count = bytes.decode_u32()
		
		rsm_format.root_node_names = [] as Array[String]
		for _n in rsm_format.root_node_count:
			rsm_format.root_node_names.append(bytes.get_string_from_utf8(40))
	
	rsm_format.node_count = bytes.decode_u32()
	rsm_format.nodes = [] as Array[ModelNode]
	for _n in rsm_format.node_count:
		rsm_format.nodes.append(ModelNode.from_bytes(bytes, version))
	
	print(inst_to_dict(rsm_format))
	#print(inst_to_dict(rsm_format.nodes[0].texture_coordinates[0]))
	#rsm_format.nodes[0].texture_coordinates.clear()
	#print(inst_to_dict(rsm_format.nodes[0].faces[0]))
	#rsm_format.nodes[0].faces.clear()
	#print(inst_to_dict(rsm_format.nodes[0]))
	return rsm_format


func convert() -> Node3D:
	var node := Node3D.new()
	node.name = root_node_name
	#node.set_script(load("res://extractor/model.gd"))
	
	return node


class ModelNode:
	## Byte Type: u8 [br]
	## Byte Length: 40
	var node_name: String
	
	## Byte Type: u8 [br]
	## Byte Length: 40
	var parent_node_name: String
	
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	## Versions: [<2.3]
	var texture_count: int
	
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	## Length: [member texture_count] [br]
	## Versions: [<2.3]
	var texture_indices: Array[int]
	
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	## Versions: [>=2.3]
	var texture_name_count: int
	
	## Byte Length: 40 [br]
	## Length: [member texture_name_count] [br]
	## Versions: [>=2.3]
	var texture_names: Array[String]
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 9 [br]
	## Length: 9 [br]
	## 3 x 3 Matrix. Each element represents a column.
	var offset_matrix: Array[Vector3]
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 3 [br]
	## Versions: [<2.2]
	var translation_1: Vector3
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 3 [br]
	var translation_2: Vector3
	
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	## Versions: [<2.2]
	var rotation_angle: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 3 [br]
	## Versions: [<2.2]
	var rotation_axis: Vector3
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 3 [br]
	## Versions: [<2.2]
	var scale: Vector3
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var vertex_position_count: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 3 * [member vertex_position_count] [br]
	## Length: [member vertex_position_count]
	var vertex_positions: Array[Vector3]
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var texture_coordinate_count: int
	
	## Length: [member texture_coordinate_count]
	var texture_coordinates: Array[TextureCoordinate]
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var face_count: int
	
	## Length: [member face_count]
	var faces: Array[Face]
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	## Versions: [>=1.6]
	var scale_keyframe_count: int
	
	## Length: [member scale_keyframe_count]
	## Versions: [>=1.6]
	var scale_keyframes: Array[ScaleKeyframe]
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var rotation_keyframe_count: int
	
	## Length: [member scale_keyframe_count]
	var rotation_keyframes: Array[RotationKeyframe]
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	## Versions: [>=2.2]
	var translation_keyframe_count: int
	
	## Length: [member scale_keyframe_count]
	## Versions: [>=2.2]
	var translation_keyframes: Array[TranslationKeyframe]
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	## Versions: [>=2.3]
	var textures_keyframe_count: int
	
	## Length: [member scale_keyframe_count]
	## Versions: [>=2.3]
	var textures_keyframes: Array[TexturesKeyframe]
	
	
	static func from_bytes(bytes: ByteStream, version: Version) -> ModelNode:
		var node = ModelNode.new()
		
		node.node_name = bytes.get_string_from_utf8(40)
		node.parent_node_name = bytes.get_string_from_utf8(40)
		
		if version.lower_than(2, 3): # < 2.3
			node.texture_count = bytes.decode_u32()
			
			node.texture_indices = [] as Array[int]
			for _n in node.texture_count:
				node.texture_indices.append(bytes.decode_u32())
		
		if version.higher_than(2, 2): # >= 2.3
			node.texture_name_count = bytes.decode_u32()
			
			node.texture_names = [] as Array[String]
			for _n in node.texture_name_count:
				node.texture_names.append(bytes.get_string_from_utf8(40))
		
		node.offset_matrix = [] as Array[Vector3]
		for _in in 3:
			node.offset_matrix.append(Vector3(
				bytes.decode_float(),
				bytes.decode_float(),
				bytes.decode_float()
			))
		
		if version.lower_than(2, 2):
			node.translation_1 = Vector3(
				bytes.decode_float(),
				bytes.decode_float(),
				bytes.decode_float()
			)
		
		node.translation_2 = Vector3(
				bytes.decode_float(),
				bytes.decode_float(),
				bytes.decode_float()
			)
		
		if version.lower_than(2, 2):
			node.rotation_angle = bytes.decode_float()
			node.rotation_axis = Vector3(
				bytes.decode_float(),
				bytes.decode_float(),
				bytes.decode_float()
			)
			node.scale = Vector3(
				bytes.decode_float(),
				bytes.decode_float(),
				bytes.decode_float()
			)
		
		node.vertex_position_count = bytes.decode_u32()
		node.vertex_positions = [] as Array[Vector3]
		
		for _n in node.vertex_position_count:
			node.vertex_positions.append(Vector3(
				bytes.decode_float(),
				bytes.decode_float(),
				bytes.decode_float()
			))
		
		node.texture_coordinate_count = bytes.decode_u32()
		node.texture_coordinates = [] as Array[TextureCoordinate]
		
		for _n in node.texture_coordinate_count:
			node.texture_coordinates.append(TextureCoordinate.from_bytes(bytes, version))
		
		node.face_count = bytes.decode_u32()
		node.faces = [] as Array[Face]
		
		for _n in node.face_count:
			node.faces.append(Face.from_bytes(bytes, version))
		
		if version.higher_than(1, 5): # >= 1.6
			node.scale_keyframe_count = bytes.decode_u32()
			node.scale_keyframes = [] as Array[ScaleKeyframe]
			
			for _n in node.scale_keyframe_count:
				node.scale_keyframes.append(ScaleKeyframe.from_bytes(bytes))
		
		node.rotation_keyframe_count = bytes.decode_u32()
		node.rotation_keyframes = [] as Array[RotationKeyframe]
		
		for _n in node.rotation_keyframe_count:
			node.rotation_keyframes.append(RotationKeyframe.from_bytes(bytes))
		
		if version.higher_than(2, 1): # >= 2.2
			node.translation_keyframe_count = bytes.decode_u32()
			node.translation_keyframes = [] as Array[ScaleKeyframe]
			
			for _n in node.translation_keyframe_count:
				node.translation_keyframes.append(TranslationKeyframe.from_bytes(bytes))
		
		if version.higher_than(2, 2): # >= 2.3
			node.textures_keyframe_count = bytes.decode_u32()
			node.textures_keyframes = [] as Array[TexturesKeyframe]
			
			for _n in node.textures_keyframe_count:
				node.textures_keyframes.append(TexturesKeyframe.from_bytes(bytes))
		
		return node


class TextureCoordinate:
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	## Versions: [>=1.2]
	var color: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 2 [br]
	## Note: possibly wrong if version < 1.2
	var coordinates: Vector2
	
	
	static func from_bytes(bytes: ByteStream, version: Version) -> TextureCoordinate:
		var data = TextureCoordinate.new()
		
		if version.higher_than(1, 1):
			data.color = bytes.decode_u32()
		
		data.coordinates = Vector2(
			bytes.decode_float(),
			bytes.decode_float()
		)
		
		return data


class Face:
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	## Versions: [>=2.2]
	var length: int
	
	## Byte Type: u16 [br]
	## Byte Length: 2 * 3
	## Length: 3
	var vertex_position_indices: Array[int]
	
	## Byte Type: u16 [br]
	## Byte Length: 2 * 3
	## Length: 3
	var texture_coordinate_indices: Array[int]
	
	## Byte Type: u16 [br]
	## Byte Length: 2
	var texture_index: int
	
	## Byte Type: u16 [br]
	## Byte Length: 2
	var padding: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	var two_sided: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	var smooth_group: int
	
	## Byte Type: i32 [br]
	## Length: ([member length] - 24) / 4 [br]
	## Versions: [>=2.2]
	# TODO: saturating_sub?
	var smooth_group_extra: Array[int]
	
	
	static func from_bytes(bytes: ByteStream, version: Version) -> Face:
		var data = Face.new()
		
		if version.higher_than(2, 1):
			data.length = bytes.decode_u32()
		
		data.vertex_position_indices = [] as Array[int]
		for _in in 3:
			data.vertex_position_indices.append(bytes.decode_u16())
		
		data.texture_coordinate_indices = [] as Array[int]
		for _in in 3:
			data.texture_coordinate_indices.append(bytes.decode_u16())
		
		data.texture_index = bytes.decode_u16()
		data.padding = bytes.decode_u16()
		data.two_sided = bytes.decode_s32()
		data.smooth_group = bytes.decode_s32()
		
		if version.higher_than(2, 1):
			data.smooth_group_extra = [] as Array[int]
			
			for _n in ((data.length - 24) / 4):
				data.smooth_group_extra.append(bytes.decode_s32())
		
		return data


class ScaleKeyframe:
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	var frame: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 3 [br]
	var scale: Vector3
	
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	var reserved: float
	
	
	static func from_bytes(bytes: ByteStream) -> ScaleKeyframe:
		var data = ScaleKeyframe.new()
		
		data.frame = bytes.decode_u32()
		data.scale = Vector3(
			bytes.decode_float(),
			bytes.decode_float(),
			bytes.decode_float()
		)
		data.reserved = bytes.decode_float()
		
		return data


class RotationKeyframe:
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	var frame: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 3 [br]
	var rotation: Vector4
	
	
	static func from_bytes(bytes: ByteStream) -> RotationKeyframe:
		var data = RotationKeyframe.new()
		
		data.frame = bytes.decode_u32()
		data.rotation = Vector4(
			bytes.decode_float(),
			bytes.decode_float(),
			bytes.decode_float(),
			bytes.decode_float()
		)
		
		return data


class TranslationKeyframe:
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	var frame: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 * 3 [br]
	var translation: Vector3
	
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	var reserved: float
	
	
	static func from_bytes(bytes: ByteStream) -> ScaleKeyframe:
		var data = ScaleKeyframe.new()
		
		data.frame = bytes.decode_u32()
		data.translation = Vector3(
			bytes.decode_float(),
			bytes.decode_float(),
			bytes.decode_float()
		)
		data.reserved = bytes.decode_float()
		
		return data


enum TextureOperation {
	## Texture translation on the X axis. The texture is tiled.
	Translation_X,
	
	## Texture translation on the Y axis. The texture is tiled.
	Translation_Y,
	
	## Texture multiplication on the X axis. The texture is tiled.
	Scale_X,
	
	## Texture multiplication on the Y axis. The texture is tiled.
	Scale_Y,
	
	## Texture rotation around (0, 0). The texture is not tiled.
	Rotation,
}


class TexturesKeyframe:
	## Byte Type: u32 [br]
	## Byte Length: 4
	var texture_index: int
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var texture_keyframe_count: int
	
	## Length: [member texture_keyframe_count]
	var texture_keyframes: Array[TextureKeyframe]
	
	
	static func from_bytes(bytes: ByteStream) -> TexturesKeyframe:
		var data = TexturesKeyframe.new()
		
		data.texture_index = bytes.decode_u32()
		data.texture_keyframe_count = bytes.decode_u32()
		
		data.texture_keyframes = [] as Array[TextureKeyframe]
		for _n in data.texture_keyframe_count:
			data.texture_keyframes.append(TextureKeyframe.from_bytes(bytes))
		
		return data


class TextureKeyframe:
	## Byte Type: u8
	var operation_type: TextureOperation
	
	## Byte Type: u32 [br]
	## Byte Length: 4
	var frame_count: int
	
	## Length: [member frame_count]
	var texture_frames: Array[TextureFrame]
	
	
	static func from_bytes(bytes: ByteStream) -> TextureKeyframe:
		var data = TextureKeyframe.new()
		
		data.operation_type = bytes.decode_u8()
		data.frame_count = bytes.decode_u32()
		
		data.texture_frames = [] as Array[TextureFrame]
		for _n in data.frame_count:
			data.texture_frames.append(TextureFrame.from_bytes(bytes))
		
		return data


class TextureFrame:
	## Byte Type: u32 [br]
	## Byte Length: 4 [br]
	var frame: int
	
	## Byte Type: f32 [br]
	## Byte Length: 4 [br]
	var translation: float
	
	
	static func from_bytes(bytes: ByteStream) -> TextureFrame:
		var data = TextureFrame.new()
		
		data.frame = bytes.decode_u32()
		data.translation = bytes.decode_float()
		
		return data