forked from StarsMmd/Blender-Addon-Gamecube-Models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_hsd.py
More file actions
3006 lines (2636 loc) · 119 KB
/
Copy pathimport_hsd.py
File metadata and controls
3006 lines (2636 loc) · 119 KB
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import math
import struct
if "bpy" in locals():
import importlib
if "hsd" in locals():
importlib.reload(hsd)
if "gx" in locals():
importlib.reload(gx)
if "img" in locals():
importlib.reload(img)
from . import hsd, gx, img
import time
import bpy
from mathutils import Matrix, Euler, Vector
"""
import hsd
import gx
"""
#TODO list
#features:
#implement comp tev
#implement texture animations
#animations of other properties
#culling?
#figure out how the skyboxes are rendered shadeless (just no lights assigned?)
#needed optimizations (bottlenecks):
#image conversion
#bugs:
#fix custom normals #done?
#fix texture transforms
#why are the shadows in pyrite white?
#misc:
#deprecate blender internal material
ikhack = True
bone_count = 0
armature_count = 0
light_count = 0
image_count = 0
anim_max_frame = 1000
def error_output(string):
print('Error: ' + string)
return {'CANCELLED'}
def notice_output(string):
print(string)
def load_hsd(filepath, context = None, offset = 0, scene_name = 'scene_data', data_type = 'SCENE', import_animation = True):
data = None
try:
file = open( filepath, 'rb')
data = bytearray(os.path.getsize( filepath))
file.readinto(data)
data = memoryview(data)
file.close()
except:
error_output("Couldn't read file")
return
print ("Read File " + filepath)
hsd.HSD_reset_created_structs()
if len(data) - offset < hsd.HSD_get_struct_size('HSD_ArchiveHeader'):
error_output("Invalid data: Smaller than Header size")
return
if filepath[-4:] == '.pkx':
# check for byte pattern unique to Colosseum pkx models
isXDModel = data[0] != data[0x40]
pkx_header_size = 0xE60 if isXDModel else 0x40
gpt1SizeOffset = 8 if isXDModel else 4
gpt1Size = struct.unpack('>I', data[gpt1SizeOffset:gpt1SizeOffset+4])[0]
if (gpt1Size > 0) and isXDModel:
pkx_header_size += gpt1Size + ((0x20 - (gpt1Size % 0x20)) % 0x20)
data = data[pkx_header_size:]
#this is where our header should be
header_data = data[offset:]
hsd.HSD_reset_created_structs()
header, _ = hsd.HSD_read_struct('HSD_ArchiveHeader', header_data, -1)
reloc_size = header.nb_reloc * hsd.HSD_get_struct_size('HSD_ArchiveRelocationInfo')
public_size = header.nb_public * hsd.HSD_get_struct_size('HSD_ArchivePublicInfo')
extern_size = header.nb_extern * hsd.HSD_get_struct_size('HSD_ArchiveExternInfo')
header_size = 32
data_size = header.data_size + header_size + reloc_size + public_size + extern_size
print(header.file_size)
print(data_size)
if not (header.file_size <= len(header_data) and data_size <= header.file_size):
return error_output("Invalid data: file_size greater than read data")
#location of the info array
info = data[offset + header_size + header.data_size:]
scene_info = hsd.HSD_get_archive_section(info[reloc_size:], header, scene_name)
if not scene_info:
return error_output("Couldn't find Scene")
#pointer offsets are relative to the end of the header, since we already have the header object
#and the info arrays we'll adjust our memoryview to only cover the main data
data = data[offset + header_size:offset + header_size + header.data_size]
#make sure the current selection doesn't mess with anything
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
if data_type == 'BONE':
load_bone(data, scene_info, info[0:], filepath)
else:
load_scene(data, scene_info, info[0:], filepath, import_animation)
return {'FINISHED'}
def load_bone(data, info, rel, filepath):
root_joint, valid = hsd.HSD_init_Joint(data, info.entry)
mesh_dict, material_dict = init_geometry()
if valid:
armature = load_model(root_joint, mesh_dict, material_dict)
else:
error_output("Invalid Armature")
def load_scene(data, scene_info, rel, filepath, import_animation = True):
scene = hsd.HSD_initialize_scene(data, scene_info, rel)
if not scene:
return error_output("Invalid Scene")
scenedescs = hsd.HSD_get_struct_dict('HSD_SceneDesc')
scene = list(scenedescs.values())[0]
#do geometry here because the way it's currently implemented it initializes geometry from all models
mesh_dict, material_dict = init_geometry()
for k, modelset in enumerate(scene.modelsets):
root_joint = modelset.joint
if not root_joint:
notice_output("Empty Model")
continue
armature = load_model(root_joint, mesh_dict, material_dict)
if import_animation:
n_a = len(modelset.animjoints) if modelset.animjoints else 0
n_m = len(modelset.matanimjoints) if modelset.matanimjoints else 0
n_s = len(modelset.shapeanimjoints) if modelset.shapeanimjoints else 0
print('ANIMS: %d %d %d' % (n_a, n_m, n_s))
anim_count = max(n_a, n_m, n_s)
for i in range(anim_count):
if modelset.animjoints:
global cur_anim
cur_anim = i
animjoint = modelset.animjoints[i]
if animjoint.aobjdesc or animjoint.child or animjoint.next:
action = bpy.data.actions.new(os.path.basename(filepath) + '_Anim' + '' + ' ' + str(k) + ' ' + str(i))
action.use_fake_user = True
bpy.types.PoseBone.custom_40 = bpy.props.FloatProperty(name="40")
add_bone_animation_total(armature, root_joint, modelset.animjoints[i], action)
#TODO: figure out how to pack this into a single track with the above or something
#if modelset.matanimjoints:
# add_material_animation(material_dict, modelset.matanimjoints[i], action)
#if modelset.shapeanimjoints:
# add_shape_animation(mesh_dict, modelset.shapeanimjoints[i], action)
for lightset in scene.lightsets:
light = lightset.lightdesc
if not light:
notice_output("Empty Light")
continue
light = load_light(light)
cur_anim = 0
#####################################
def add_bone_animation_total(armature, root_joint, animation, action):
for bone in armature.pose.bones:
bone.rotation_mode = 'XYZ'
for bone in armature.data.bones:
bone.use_local_location = True
armature.animation_data_create()
armature.animation_data.action = action
trav_animjoints_total(root_joint, animation, action, armature.pose)
def trav_animjoints_total(joint, animjoint, action, pose):
if joint.robj:
robj = joint.robj
if robj:
print(joint.temp_name + ':')
if joint.flags & hsd.JOBJ_TYPE_MASK == hsd.JOBJ_JOINT1:
print('JOBJ_JOINT1')
if joint.flags & hsd.JOBJ_TYPE_MASK == hsd.JOBJ_JOINT2:
print('JOBJ_JOINT2')
if joint.flags & hsd.JOBJ_TYPE_MASK == hsd.JOBJ_EFFECTOR:
print('JOBJ_EFFECTOR')
while robj:
print('ROBJ: %.8X TYPE: %.8X SUBTYPE: %.8X' % (robj.id, robj.flags & 0x70000000, robj.flags & 0x0FFFFFFF))
if (robj.flags & 0x70000000 == 0x10000000):
print(' JointRef: ' + robj.u.temp_name)
elif (robj.flags & 0x70000000 == 0x40000000):
print(' VAL0: %f VAL1: %f' % (robj.val0, robj.val1))
robj = robj.next
if animjoint.robjanim:
print('ROBJANIM')
if animjoint.aobjdesc:
add_jointanim_to_armature_total(joint, animjoint.aobjdesc, action, pose)
if animjoint.child:
trav_animjoints_total(joint.child, animjoint.child, action, pose)
if animjoint.next:
trav_animjoints_total(joint.next, animjoint.next, action, pose)
TRANSFORMCOUNT = (hsd.HSD_A_J_SCAZ - hsd.HSD_A_J_ROTX) + 1
t_jointanim_type_dict = {
hsd.HSD_A_J_ROTX: ('r', 0),
hsd.HSD_A_J_ROTY: ('r', 1),
hsd.HSD_A_J_ROTZ: ('r', 2),
#hsd.HSD_A_J_PATH: '',
hsd.HSD_A_J_TRAX: ('l', 0),
hsd.HSD_A_J_TRAY: ('l', 1),
hsd.HSD_A_J_TRAZ: ('l', 2),
hsd.HSD_A_J_SCAX: ('s', 0),
hsd.HSD_A_J_SCAY: ('s', 1),
hsd.HSD_A_J_SCAZ: ('s', 2),
}
def add_jointanim_to_armature_total(joint, aobjdesc, action, pose):
t0 = time.perf_counter()
#trans, rot, scale = joint.temp_matrix_local.decompose()
trans, rot, scale = joint.position, joint.rotation, joint.scale
#rot = rot.to_euler('XYZ')
if aobjdesc.flags & hsd.AOBJ_NO_ANIM:
return
fobj = aobjdesc.fobjdesc
invmtx = joint.temp_matrix_local.inverted()
#invmtx = invmtx * Matrix.Translation(Vector((0.0,1.0,0.0)))
transform_list = [0] * (TRANSFORMCOUNT)
while fobj:
#print(hsd_a_j_dict[fobj.type])
if fobj.type == hsd.HSD_A_J_PATH:
pass #TODO: implement paths
print('HSD_A_J_PATH')
elif fobj.type >= hsd.HSD_A_J_ROTX and fobj.type <= hsd.HSD_A_J_SCAZ:
data_type, component = t_jointanim_type_dict[fobj.type]
data_path = 'pose.bones["' + joint.temp_name + '"]' + '.' + data_type
curve = action.fcurves.new(data_path, index=component)
transform_list[fobj.type - hsd.HSD_A_J_ROTX] = curve
#total values for testing
curve_bias = 0
curve_scale = 1
read_fobjdesc(fobj, curve, curve_bias, curve_scale, False)
if aobjdesc.flags & hsd.AOBJ_ANIM_LOOP:
curve.modifiers.new('CYCLES')
else:
print('Unknown A Type: %.2X JOINT: %s' % (fobj.type, joint.temp_name))
fobj = fobj.next
for i in range(3):
if not transform_list[i]:
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].r', index=i)
curve.keyframe_points.insert(0, joint.rotation[i])
transform_list[i] = curve
if not transform_list[i+4]:
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].l', index=i)
curve.keyframe_points.insert(0, joint.position[i])
transform_list[i+4] = curve
if not transform_list[i+7]:
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].s', index=i)
curve.keyframe_points.insert(0, joint.scale[i])
transform_list[i+7] = curve
new_transform_list = [0] * 10
for i in range(3):
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].rotation_euler', index=i)
new_transform_list[i] = curve
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].location', index=i)
new_transform_list[i+4] = curve
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].scale', index=i)
new_transform_list[i+7] = curve
global anim_max_frame
for frame in range(min(int(aobjdesc.endframe), anim_max_frame)):
scale_x = Matrix.Scale(transform_list[7].evaluate(frame), 4, [1.0,0.0,0.0])
scale_y = Matrix.Scale(transform_list[8].evaluate(frame), 4, [0.0,1.0,0.0])
scale_z = Matrix.Scale(transform_list[9].evaluate(frame), 4, [0.0,0.0,1.0])
rotation_x = Matrix.Rotation(transform_list[0].evaluate(frame), 4, 'X')
rotation_y = Matrix.Rotation(transform_list[1].evaluate(frame), 4, 'Y')
rotation_z = Matrix.Rotation(transform_list[2].evaluate(frame), 4, 'Z')
translation = Matrix.Translation(Vector((transform_list[4].evaluate(frame), transform_list[5].evaluate(frame), transform_list[6].evaluate(frame))))
# T * R * S
rotation = rotation_z @ rotation_y @ rotation_x
scale = scale_z @ scale_y @ scale_x
mtx = translation @ rotation @ scale
Bmtx = invmtx @ mtx
trans, rot, scale = Bmtx.decompose()
rot = rot.to_euler()
new_transform_list[0].keyframe_points.insert(frame, rot[0]).interpolation = 'BEZIER'
new_transform_list[1].keyframe_points.insert(frame, rot[1]).interpolation = 'BEZIER'
new_transform_list[2].keyframe_points.insert(frame, rot[2]).interpolation = 'BEZIER'
new_transform_list[4].keyframe_points.insert(frame, trans[0]).interpolation = 'BEZIER'
new_transform_list[5].keyframe_points.insert(frame, trans[1]).interpolation = 'BEZIER'
new_transform_list[6].keyframe_points.insert(frame, trans[2]).interpolation = 'BEZIER'
new_transform_list[7].keyframe_points.insert(frame, scale[0]).interpolation = 'BEZIER'
new_transform_list[8].keyframe_points.insert(frame, scale[1]).interpolation = 'BEZIER'
new_transform_list[9].keyframe_points.insert(frame, scale[2]).interpolation = 'BEZIER'
for c in transform_list:
if c:
action.fcurves.remove(c)
"""transform *= Matrix.Translation(Vector(position))
posebone = pose.bones[joint.temp_name]
by.ops.screen.frame_set(frame)
posebone.matrix_local = transform
posebone.keyframe_insert(data_path = 'location')
posebone.keyframe_insert(data_path = 'rotation')
posebone.keyframe_insert(data_path = 'scale')
"""
t1 = time.perf_counter()
#print (('anim %s: ' % joint.temp_name) + str(t1 - t0))
####################################
def add_bone_animation(armature, root_joint, animation, action):
for bone in armature.pose.bones:
bone.rotation_mode = 'XYZ'
for bone in armature.data.bones:
bone.use_local_location = False
#bone.use_inherit_rotation = False
armature.animation_data_create()
armature.animation_data.action = action
trav_animjoints(root_joint, animation, action)
def trav_animjoints(joint, animjoint, action):
if animjoint.flags:
print('Joint: %s\t AnimJointFlags: %.8X' % (joint.temp_name, animjoint.flags))
if animjoint.aobjdesc:
add_jointanim_to_armature(joint, animjoint.aobjdesc, action)
if animjoint.child:
trav_animjoints(joint.child, animjoint.child, action)
if animjoint.next:
trav_animjoints(joint.next, animjoint.next, action)
def add_jointanim_to_armature(joint, aobjdesc, action):
trans, rot, scale = joint.position, joint.rotation, joint.scale
#if aobjdesc.flags & hsd.AOBJ_NO_ANIM:
# return
printd = (joint.temp_name == 'Bone57')
if printd:
print(action.name)
print('AOBJ FLAGS: %.8X' % aobjdesc.flags)
fobj = aobjdesc.fobjdesc
transform_list = [0] * 10
while fobj:
#print(hsd_a_j_dict[fobj.type])
if fobj.type == hsd.HSD_A_J_PATH:
#TODO: implement paths
print('HSD_A_J_PATH')
elif (fobj.type >= hsd.HSD_A_J_ROTX and fobj.type <= hsd.HSD_A_J_SCAZ):
data_type, component = jointanim_type_dict[fobj.type]
data_path = 'pose.bones["' + joint.temp_name + '"]' + '.' + data_type
curve = action.fcurves.new(data_path, index=component)
transform_list[fobj.type - hsd.HSD_A_J_ROTX] = curve
#HSD curves give absolute transform values while the Pose's are relative to the EditBones'
#This means we'll have to adjust all the values accordingly
curve_bias = 0
curve_scale = 1
if fobj.type == hsd.HSD_A_J_ROTX:
curve_bias = -rot[0]
elif fobj.type == hsd.HSD_A_J_ROTY:
curve_bias = -rot[1]
elif fobj.type == hsd.HSD_A_J_ROTZ:
curve_bias = -rot[2]
elif fobj.type == hsd.HSD_A_J_TRAX:
curve_bias = -trans[0]
elif fobj.type == hsd.HSD_A_J_TRAY:
curve_bias = -trans[1]
elif fobj.type == hsd.HSD_A_J_TRAZ:
curve_bias = -trans[2]
elif fobj.type == hsd.HSD_A_J_SCAX:
curve_scale = 1 / scale[0]
elif fobj.type == hsd.HSD_A_J_SCAY:
curve_scale = 1 / scale[1]
elif fobj.type == hsd.HSD_A_J_SCAZ:
curve_scale = 1 / scale[2]
if printd:
print(hsd_a_j_dict[fobj.type])
read_fobjdesc(fobj, curve, curve_bias, curve_scale, printd)
#read_fobjdesc(fobj, curve, 0, 1, printd)
if aobjdesc.flags & hsd.AOBJ_ANIM_LOOP:
curve.modifiers.new('CYCLES')
else:
print('Unknown A Type: %.2X' % fobj.type)
if fobj.type in hsd_a_j_dict:
data_type = hsd_a_j_dict[fobj.type]
else:
data_type = 'custom_%d' % fobj.type
data_path = 'pose.bones["' + joint.temp_name + '"]' + '.' + data_type
curve = action.fcurves.new(data_path, index=0)
read_fobjdesc(fobj, curve, 0, 1, printd)
fobj = fobj.next
for i in range(3):
if not transform_list[i]:
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].rotation_euler', index=i)
transform_list[i] = curve
curve.keyframe_points.insert(0, 0)
if not transform_list[i+4]:
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].location', index=i)
transform_list[i+4] = curve
curve.keyframe_points.insert(0, 0)
if not transform_list[i+7]:
curve = action.fcurves.new('pose.bones["' + joint.temp_name + '"].scale', index=i)
transform_list[i+7] = curve
curve.keyframe_points.insert(0, 1)
bonemtxinv = joint.temp_matrix_local.inverted()
for frame in range(int(aobjdesc.endframe)):
scale = list(tuple(joint.scale))
rotation = list(tuple(joint.rotation))
position = list(tuple(joint.position))
for i in range(3):
rotation[i] += transform_list[i].evaluate(frame)
position[i] += transform_list[i+4].evaluate(frame)
scale[i] *= transform_list[i+7].evaluate(frame)
posetargetmtx = compileSRTmtx(scale, rotation, position)
posemtx = bonemtxinv @ posetargetmtx
trans, rot, scale = posemtx.decompose()
for i in range(3):
transform_list[i].keyframe_points.insert(frame, rot[i])
transform_list[i+4].keyframe_points.insert(frame, trans[i])
transform_list[i+7].keyframe_points.insert(frame, scale[i])
hsd_a_j_dict = {
hsd.HSD_A_J_ROTX: 'HSD_A_J_ROTX',
hsd.HSD_A_J_ROTY: 'HSD_A_J_ROTY',
hsd.HSD_A_J_ROTZ: 'HSD_A_J_ROTZ',
hsd.HSD_A_J_PATH: 'HSD_A_J_PATH',
hsd.HSD_A_J_TRAX: 'HSD_A_J_TRAX',
hsd.HSD_A_J_TRAY: 'HSD_A_J_TRAY',
hsd.HSD_A_J_TRAZ: 'HSD_A_J_TRAZ',
hsd.HSD_A_J_SCAX: 'HSD_A_J_SCAX',
hsd.HSD_A_J_SCAY: 'HSD_A_J_SCAY',
hsd.HSD_A_J_SCAZ: 'HSD_A_J_SCAZ',
hsd.HSD_A_J_NODE: 'HSD_A_J_NODE',
hsd.HSD_A_J_BRANCH: 'HSD_A_J_BRANCH',
hsd.HSD_A_J_SETBYTE0: 'HSD_A_J_SETBYTE0',
hsd.HSD_A_J_SETBYTE1: 'HSD_A_J_SETBYTE1',
hsd.HSD_A_J_SETBYTE2: 'HSD_A_J_SETBYTE2',
hsd.HSD_A_J_SETBYTE3: 'HSD_A_J_SETBYTE3',
hsd.HSD_A_J_SETBYTE4: 'HSD_A_J_SETBYTE4',
hsd.HSD_A_J_SETBYTE5: 'HSD_A_J_SETBYTE5',
hsd.HSD_A_J_SETBYTE6: 'HSD_A_J_SETBYTE6',
hsd.HSD_A_J_SETBYTE7: 'HSD_A_J_SETBYTE7',
hsd.HSD_A_J_SETBYTE8: 'HSD_A_J_SETBYTE8',
hsd.HSD_A_J_SETBYTE9: 'HSD_A_J_SETBYTE9',
hsd.HSD_A_J_SETFLOAT0: 'HSD_A_J_SETFLOAT0',
hsd.HSD_A_J_SETFLOAT1: 'HSD_A_J_SETFLOAT1',
hsd.HSD_A_J_SETFLOAT2: 'HSD_A_J_SETFLOAT2',
hsd.HSD_A_J_SETFLOAT3: 'HSD_A_J_SETFLOAT3',
hsd.HSD_A_J_SETFLOAT4: 'HSD_A_J_SETFLOAT4',
hsd.HSD_A_J_SETFLOAT5: 'HSD_A_J_SETFLOAT5',
hsd.HSD_A_J_SETFLOAT6: 'HSD_A_J_SETFLOAT6',
hsd.HSD_A_J_SETFLOAT7: 'HSD_A_J_SETFLOAT7',
hsd.HSD_A_J_SETFLOAT8: 'HSD_A_J_SETFLOAT8',
hsd.HSD_A_J_SETFLOAT9: 'HSD_A_J_SETFLOAT9',
}
def read_fobjdesc(fobjdesc, curve, bias, scale, printd):
printa = printd
printd = 0
current_frame = 0 - fobjdesc.startframe // 1
cur_pos = 0
ad = fobjdesc.ad
if printd:
print('DATA: %s' % ''.join(['%.2X' % b for b in ad[:fobjdesc.length]]))
value_type = (fobjdesc.frac_value & hsd.HSD_A_FRAC_TYPE_MASK)
frac_value = (fobjdesc.frac_value & hsd.HSD_A_FRAC_MASK)
slope_type = (fobjdesc.frac_slope & hsd.HSD_A_FRAC_TYPE_MASK)
frac_slope = (fobjdesc.frac_slope & hsd.HSD_A_FRAC_MASK)
if printa:
print('Value: %s %d' % (frac_type_dict[value_type], frac_value))
print('Slope: %s %d' % (frac_type_dict[slope_type], frac_slope))
keyframes = []
slopes = []
cur_slope = 0
if printd:
print('LENGTH %d' % fobjdesc.length)
while cur_pos < fobjdesc.length:
if printd:
print('CURPOS %d' % cur_pos)
opcode = ad[cur_pos] & hsd.HSD_A_OP_MASK
node_count = (ad[cur_pos] & hsd.HSD_A_PACK0_MASK) >> hsd.HSD_A_PACK0_SHIFT
shift = 0
while ad[cur_pos] & hsd.HSD_A_PACK_EXT:
cur_pos += 1
node_count += (ad[cur_pos] & hsd.HSD_A_PACK1_MASK) << (hsd.HSD_A_PACK1_BIT * shift + 3)
shift += 1
cur_pos += 1
#there's always at least one node
node_count += 1
if opcode == hsd.HSD_A_OP_SLP:
for i in range(node_count):
_, cur_slope, cur_pos = read_node_values(opcode, value_type, frac_value, slope_type, frac_slope, ad, cur_pos)
else:
for i in range(node_count):
val, slope, cur_pos = read_node_values(opcode, value_type, frac_value, slope_type, frac_slope, ad, cur_pos)
if printd:
print(val)
slopes.append((cur_slope, slope))
cur_slope = slope
keyframe = curve.keyframe_points.insert(current_frame, (val + bias) * scale)
if printd:
print(opcode)
keyframe.interpolation = interpolation_dict[opcode]
keyframes.append(keyframe)
if not opcode == hsd.HSD_A_OP_NONE:
shift = 0
wait = 0
while True:
wait += (ad[cur_pos] & hsd.HSD_A_WAIT_MASK) << (hsd.HSD_A_WAIT_BIT * shift)
if printd:
print('WaitByte %.2X' % ad[cur_pos])
shift += 1
if not ad[cur_pos] & hsd.HSD_A_WAIT_EXT:
break
cur_pos += 1
if printd:
print('WAIT %d' % wait)
cur_pos += 1
#TODO: Is there always at least one wait frame ?
current_frame += wait
for i in range(len(keyframes)):
#TODO: I don't know whether this is the correct conversion
#try either normalized tangent vectors or x = 1
#assuming x = 1
if i > 0:
l_delta = keyframe.co[0] - keyframes[i - 1].co[0]
keyframe.handle_left[:] = (keyframe.co[0] - l_delta / 3, keyframe.co[1] - slopes[i][0] * l_delta / 3)
if i < len(keyframes) - 1:
r_delta = keyframes[i + 1].co[0] - keyframe.co[0]
keyframe.handle_right[:] = (keyframe.co[0] + r_delta / 3, keyframe.co[1] + slopes[i][1] * r_delta / 3)
frac_type_dict = {
hsd.HSD_A_FRAC_FLOAT: 'HSD_A_FRAC_FLOAT',
hsd.HSD_A_FRAC_S16: 'HSD_A_FRAC_S16',
hsd.HSD_A_FRAC_U16: 'HSD_A_FRAC_U16',
hsd.HSD_A_FRAC_S8: 'HSD_A_FRAC_S8',
hsd.HSD_A_FRAC_U8: 'HSD_A_FRAC_U8',
}
opcode_dict = {
hsd.HSD_A_OP_NONE: 'HSD_A_OP_NONE',
hsd.HSD_A_OP_CON: 'HSD_A_OP_CON',
hsd.HSD_A_OP_LIN: 'HSD_A_OP_LIN',
hsd.HSD_A_OP_SPL0: 'HSD_A_OP_SPL0',
hsd.HSD_A_OP_SPL: 'HSD_A_OP_SPL',
hsd.HSD_A_OP_SLP: 'HSD_A_OP_SLP',
hsd.HSD_A_OP_KEY: 'HSD_A_OP_KEY',
}
def read_node_values(opcode, value_type, frac_value, slope_type, frac_slope, ad, cur_pos):
slope = 0
val = 0
#frac_value += 1
if opcode == hsd.HSD_A_OP_NONE:
return 0, 0, cur_pos
if not opcode == hsd.HSD_A_OP_SLP:
if value_type == hsd.HSD_A_FRAC_FLOAT:
val = struct.unpack('f', ad[cur_pos:cur_pos + 4])[0]
cur_pos += 4
elif value_type == hsd.HSD_A_FRAC_S16:
val = struct.unpack('h', ad[cur_pos:cur_pos + 2])[0] / (1 << frac_value)
cur_pos += 2
elif value_type == hsd.HSD_A_FRAC_U16:
val = struct.unpack('H', ad[cur_pos:cur_pos + 2])[0] / (1 << frac_value)
cur_pos += 2
elif value_type == hsd.HSD_A_FRAC_S8:
val = struct.unpack('b', ad[cur_pos:cur_pos + 1])[0] / (1 << frac_value)
cur_pos += 1
elif value_type == hsd.HSD_A_FRAC_U8:
val = struct.unpack('B', ad[cur_pos:cur_pos + 1])[0] / (1 << frac_value)
cur_pos += 1
if (opcode == hsd.HSD_A_OP_SPL or
opcode == hsd.HSD_A_OP_SLP):
if slope_type == hsd.HSD_A_FRAC_FLOAT:
slope = struct.unpack('f', ad[cur_pos:cur_pos + 4])[0]
cur_pos += 4
elif slope_type == hsd.HSD_A_FRAC_S16:
slope = struct.unpack('h', ad[cur_pos:cur_pos + 2])[0] / (1 << frac_slope)
cur_pos += 2
elif slope_type == hsd.HSD_A_FRAC_U16:
slope = struct.unpack('H', ad[cur_pos:cur_pos + 2])[0] / (1 << frac_slope)
cur_pos += 2
elif slope_type == hsd.HSD_A_FRAC_S8:
slope = struct.unpack('b', ad[cur_pos:cur_pos + 1])[0] / (1 << frac_slope)
cur_pos += 1
elif slope_type == hsd.HSD_A_FRAC_U8:
slope = struct.unpack('B', ad[cur_pos:cur_pos + 1])[0] / (1 << frac_slope)
cur_pos += 1
return val, slope, cur_pos
interpolation_dict = {
hsd.HSD_A_OP_NONE: 'CONSTANT',
hsd.HSD_A_OP_CON: 'CONSTANT',
hsd.HSD_A_OP_LIN: 'LINEAR',
hsd.HSD_A_OP_SPL0: 'BEZIER',
hsd.HSD_A_OP_SPL: 'BEZIER',
#hsd.HSD_A_OP_SLP: '',
hsd.HSD_A_OP_KEY: 'LINEAR', #?
}
jointanim_type_dict = {
hsd.HSD_A_J_ROTX: ('rotation_euler', 0),
hsd.HSD_A_J_ROTY: ('rotation_euler', 1),
hsd.HSD_A_J_ROTZ: ('rotation_euler', 2),
#hsd.HSD_A_J_PATH: '',
hsd.HSD_A_J_TRAX: ('location', 0),
hsd.HSD_A_J_TRAY: ('location', 1),
hsd.HSD_A_J_TRAZ: ('location', 2),
hsd.HSD_A_J_SCAX: ('scale', 0),
hsd.HSD_A_J_SCAY: ('scale', 1),
hsd.HSD_A_J_SCAZ: ('scale', 2),
}
def add_material_animation(material_dict, animations, action):
pass
def add_shape_animation(mesh_dict, animations, action):
pass
def init_geometry():
hsd_textures = hsd.HSD_get_struct_dict('HSD_TObjDesc')
texture_dict = {}
image_dict = make_textures(hsd_textures.values(), texture_dict)
hsd_materials = hsd.HSD_get_struct_dict('HSD_MObjDesc')
material_dict = {}
for hsd_material in hsd_materials.values():
material_dict[hsd_material.id] = make_approx_cycles_material(hsd_material, image_dict)
hsd_meshes = hsd.HSD_get_struct_dict('HSD_DObjDesc')
mesh_dict = {}
for hsd_mesh in hsd_meshes.values():
pobj = hsd_mesh.pobj
while pobj:
ob = make_mesh(pobj, mesh_dict)
#add material
#mat = make_material(hsd_mesh.mobj, texture_dict)
mat = material_dict[hsd_mesh.mobj.id]
ob.data.materials.append(mat)
pobj = pobj.next
return mesh_dict, material_dict
#normalize u8 to float
#only used for color so we can do srgb conversion here
def normcolor(x):
if len(x) > 2:
color = [c / 255 for c in x]
return tolin(color)
else:
type = x[1]
val = x[0] / 255
if type == 'R' or type == 'G' or type == 'B':
return tolin([val])[0]
elif type == 'A':
return val
def norm8bit(x):
if hasattr(x, '__iter__'):
return [c / 255 for c in x]
else:
return x / 255
def make_approx_cycles_material(mobj, image_dict):
material = mobj.mat
mat = bpy.data.materials.new('')
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
#diff = nodes['Diffuse BSDF']
#output = nodes['Material Output']
for node in nodes:
nodes.remove(node)
output = nodes.new('ShaderNodeOutputMaterial')
#nodes.remove(diff)
mat_diffuse_color = normcolor(material.diffuse)
#XXX: Print material flags etc
print(mat.name)
notice_output('MOBJ FLAGS:\nrendermode: %.8X' % mobj.rendermode)
if mobj.pedesc:
pedesc = mobj.pedesc
notice_output('PEDESC FLAGS:\nflags: %.2X\nref0: %.2X\nref1: %.2X\ndst_alpha: %.2X\ntype: %.2X\nsrc_factor: %.2X\ndst_factor: %.2X\nlogic_op: %.2X\nz_comp: %.2X\nalpha_comp0: %.2X\nalpha_op: %.2X\nalpha_comp1: %.2X' % \
(pedesc.flags, pedesc.ref0, pedesc.ref1, pedesc.dst_alpha, pedesc.type, pedesc.src_factor, pedesc.dst_factor, pedesc.logic_op, pedesc.z_comp, pedesc.alpha_comp0, pedesc.alpha_op, pedesc.alpha_comp1))
textures = []
toon = None
tex_num = 0
texdesc = mobj.texdesc
while texdesc:
#if texdesc.flag & hsd.TEX_COORD_TOON:
# toon = texdesc
#XXX:
notice_output('TOBJ FLAGS:\nid: %.8X\nsrc: %.8X\nflag: %.8X' % (texdesc.texid, texdesc.src, texdesc.flag))
if texdesc.tev:
tev = texdesc.tev
notice_output('TEV FLAGS:\ncolor_op: %.2X\nalpha_op: %.2X\ncolor_bias: %.2X\nalpha_bias: %.2X\n\
color_scale: %.2X\nalpha_scale: %.2X\ncolor_clamp: %.2X\nalpha_clamp: %.2X\n\
color_a: %.2X color_b: %.2X color_c: %.2X color_d: %.2X\n\
alpha_a: %.2X alpha_b: %.2X alpha_c: %.2X alpha_d: %.2X\n\
konst: %.2X%.2X%.2X%.2X tev0: %.2X%.2X%.2X%.2X tev1: %.2X%.2X%.2X%.2X\n\
active: %.8X' % ((tev.color_op, tev.alpha_op, tev.color_bias, tev.alpha_bias,\
tev.color_scale, tev.alpha_scale, tev.color_clamp, tev.alpha_clamp, \
tev.color_a, tev.color_b, tev.color_c, tev.color_d, \
tev.alpha_a, tev.alpha_b, tev.alpha_c, tev.alpha_d) + \
tuple(tev.konst) + tuple(tev.tev0) + tuple(tev.tev1) + \
(tev.active,)))
print('%.8X' % texdesc.flag)
#if texdesc.flag & (hsd.TEX_LIGHTMAP_DIFFUSE | hsd.TEX_LIGHTMAP_AMBIENT):
if mobj.rendermode & (1 << (tex_num + 4)): #is this texture enabled in the material?
textures.append(texdesc)
texdesc = texdesc.next
tex_num += 1
if tex_num > 7:
break
print('textures: %d' % len(textures))
diffuse_flags = mobj.rendermode & hsd.RENDER_DIFFUSE_BITS
if diffuse_flags == hsd.RENDER_DIFFUSE_MAT0:
diffuse_flags = hsd.RENDER_DIFFUSE_MAT
alpha_flags = mobj.rendermode & hsd.RENDER_ALPHA_BITS
if alpha_flags == hsd.RENDER_ALPHA_COMPAT:
alpha_flags = diffuse_flags << hsd.RENDER_ALPHA_SHIFT
if mobj.rendermode & hsd.RENDER_DIFFUSE:
color = nodes.new('ShaderNodeRGB')
if diffuse_flags == hsd.RENDER_DIFFUSE_VTX:
color.outputs[0].default_value[:] = [1,1,1,1]
else:
color.outputs[0].default_value[:] = mat_diffuse_color
alpha = nodes.new('ShaderNodeValue')
if alpha_flags == hsd.RENDER_ALPHA_VTX:
alpha.outputs[0].default_value = 1
else:
alpha.outputs[0].default_value = material.alpha
else:
if diffuse_flags == hsd.RENDER_DIFFUSE_MAT:
color = nodes.new('ShaderNodeRGB')
color.outputs[0].default_value[:] = mat_diffuse_color
else:
#Toon not supported
#if toon:
# color = nodes.new('ShaderNodeTexImage')
# color.image = image_dict[toon.id]
# #TODO: add the proper texture mapping
#else:
color = nodes.new('ShaderNodeAttribute')
color.attribute_name = 'color_0'
if not (diffuse_flags == hsd.RENDER_DIFFUSE_VTX):
diff = nodes.new('ShaderNodeRGB')
diff.outputs[0].default_value[:] = mat_diffuse_color
mix = nodes.new('ShaderNodeMixRGB')
mix.blend_type = 'ADD'
mix.inputs[0].default_value = 1
links.new(color.outputs[0], mix.inputs[1])
links.new(diff.outputs[0], mix.inputs[2])
color = mix
if alpha_flags == hsd.RENDER_ALPHA_MAT:
alpha = nodes.new('ShaderNodeValue')
alpha.outputs[0].default_value = material.alpha
else:
alpha = nodes.new('ShaderNodeAttribute')
alpha.attribute_name = 'alpha_0'
if not (alpha_flags == hsd.RENDER_ALPHA_VTX):
mat_alpha = nodes.new('ShaderNodeValue')
mat_alpha.outputs[0].default_value = material.alpha
mix = nodes.new('ShaderNodeMath')
mix.operation = 'MULTIPLY'
links.new(alpha.outputs[0], mix.inputs[0])
links.new(mat_alpha.outputs[0], mix.inputs[1])
alpha = mix
last_color = color.outputs[0]
last_alpha = alpha.outputs[0]
last_specular = None
last_bump = None
if mobj.rendermode & hsd.RENDER_SPECULAR:
spec = nodes.new('ShaderNodeRGB')
spec.outputs[0].default_value[:] = normcolor(material.specular)
last_specular = spec.outputs[0]
for texdesc in textures:
if (texdesc.flag & hsd.TEX_COORD_MASK) == hsd.TEX_COORD_UV:
uv = nodes.new('ShaderNodeUVMap')
uv.uv_map = 'uvtex_' + str(texdesc.src - 4)
uv_output = uv.outputs[0]
elif (texdesc.flag & hsd.TEX_COORD_MASK) == hsd.TEX_COORD_REFLECTION:
uv = nodes.new('ShaderNodeTexCoord')
uv_output = uv.outputs[6]
else:
print('UV Type not supported: %X' % (texdesc.flag & hsd.TEX_COORD_MASK))
uv_output = None
mapping = nodes.new('ShaderNodeMapping')
mapping.vector_type = 'TEXTURE'
mapping.inputs[1].default_value = texdesc.translate #mapping.translation[:]
mapping.inputs[2].default_value = texdesc.rotate #mapping.rotate[:]
mapping.inputs[3].default_value = texdesc.scale #mapping.scale[:]
#blender UV coordinates are relative to the bottom left so we need to account for that
mapping.inputs[1].default_value[1] = 1 - (texdesc.scale[1] * (texdesc.translate[1] + 1))
#TODO: Is this correct?
if (texdesc.flag & hsd.TEX_COORD_MASK) == hsd.TEX_COORD_REFLECTION:
mapping.inputs[2].default_value[0] -= math.pi/2
texture = nodes.new('ShaderNodeTexImage')
texture.image = image_dict[texdesc.id]
texture.name = ("0x%X" % texdesc.id)
texture.name += ' flag: %X' % texdesc.flag
texture.name += (' image: 0x%X ' % (texdesc.imagedesc.image_ptr_id if texdesc.imagedesc else -1))
texture.name += (' tlut: 0x%X' % (texdesc.tlutdesc.id if texdesc.tlutdesc else -1))
texture.extension = 'EXTEND'
if texdesc.wrap_t == gx.GX_REPEAT:
texture.extension = 'REPEAT'
interp_dict = {
gx.GX_NEAR: 'Closest',
gx.GX_LINEAR: 'Linear',
gx.GX_NEAR_MIP_NEAR: 'Closest',
gx.GX_LIN_MIP_NEAR: 'Linear',
gx.GX_NEAR_MIP_LIN: 'Closest',
gx.GX_LIN_MIP_LIN: 'Cubic' #XXX use CUBIC?
}
if texdesc.lod:
texture.interpolation = interp_dict[texdesc.lod.minFilt]
if uv_output:
links.new(uv_output, mapping.inputs[0])
links.new(mapping.outputs[0], texture.inputs[0])
cur_color = texture.outputs[0]
cur_alpha = texture.outputs[1]
#do tev
if texdesc.tev:
tev = texdesc.tev
if tev.active & hsd.TOBJ_TEVREG_ACTIVE_COLOR_TEV:
inputs = [make_tev_input(nodes, texture, tev, i, True) for i in range(4)]
cur_color = make_tev_op(nodes, links, inputs, tev, True)
if tev.active & hsd.TOBJ_TEVREG_ACTIVE_ALPHA_TEV:
inputs = [make_tev_input(nodes, texture, tev, i, False) for i in range(4)]
cur_alpha = make_tev_op(nodes, links, inputs, tev, False)
texture.name += ' tev'
if texdesc.flag & hsd.TEX_BUMP:
#bumpmap
if last_bump:
#idk, just do blending for now to keep the nodes around
mix = nodes.new('ShaderNodeMixRGB')
mix.blend_type = 'MIX'
mix.inputs[0].default_value = texdesc.blending
links.new(last_bump, mix.inputs[1])
links.new(cur_color, mix.inputs[2])
last_bump = mix.outputs[0]
else:
last_bump = cur_color
else:
#do color
#technically there is an inaccuracy with this since the game engine ensures that specular maps are evaluated last
#this only has potential effects on the alpha channel, but I haven't seen a specular map use alpha yet
if (texdesc.flag & hsd.TEX_LIGHTMAP_MASK) & (hsd.TEX_LIGHTMAP_DIFFUSE | hsd.TEX_LIGHTMAP_AMBIENT | hsd.TEX_LIGHTMAP_SPECULAR | hsd.TEX_LIGHTMAP_EXT):
if texdesc.flag & hsd.TEX_LIGHTMAP_SPECULAR and not mobj.rendermode & hsd.RENDER_SPECULAR:
continue
colormap = texdesc.flag & hsd.TEX_COLORMAP_MASK
if not (colormap == hsd.TEX_COLORMAP_NONE or
colormap == hsd.TEX_COLORMAP_PASS):
mix = nodes.new('ShaderNodeMixRGB')
mix.blend_type = map_col_op_dict[colormap]
mix.inputs[0].default_value = 1
###
colormap_name_dict = {
hsd.TEX_COLORMAP_NONE: 'TEX_COLORMAP_NONE',
hsd.TEX_COLORMAP_PASS: 'TEX_COLORMAP_PASS',
hsd.TEX_COLORMAP_REPLACE: 'TEX_COLORMAP_REPLACE',
hsd.TEX_COLORMAP_ALPHA_MASK: 'TEX_COLORMAP_ALPHA_MASK',
hsd.TEX_COLORMAP_RGB_MASK: 'TEX_COLORMAP_RGB_MASK',
hsd.TEX_COLORMAP_BLEND: 'TEX_COLORMAP_BLEND',
hsd.TEX_COLORMAP_ADD: 'TEX_COLORMAP_ADD',
hsd.TEX_COLORMAP_SUB: 'TEX_COLORMAP_SUB',
hsd.TEX_COLORMAP_MODULATE: 'TEX_COLORMAP_MODULATE'
}
mix.name = colormap_name_dict[colormap] + ' ' + str(texdesc.blending)
###
if not colormap == hsd.TEX_COLORMAP_REPLACE:
if texdesc.flag & hsd.TEX_LIGHTMAP_SPECULAR:
links.new(last_specular, mix.inputs[1])
else:
links.new(last_color, mix.inputs[1])
links.new(cur_color, mix.inputs[2])
if colormap == hsd.TEX_COLORMAP_ALPHA_MASK:
links.new(cur_alpha, mix.inputs[0])
elif colormap == hsd.TEX_COLORMAP_RGB_MASK:
links.new(cur_color, mix.inputs[0])
elif colormap == hsd.TEX_COLORMAP_BLEND:
mix.inputs[0].default_value = texdesc.blending
elif colormap == hsd.TEX_COLORMAP_REPLACE:
links.new(cur_color, mix.inputs[1])
mix.inputs[0].default_value = 0.0
if texdesc.flag & hsd.TEX_LIGHTMAP_SPECULAR:
last_specular = mix.outputs[0]
else:
last_color = mix.outputs[0]
#do alpha
alphamap = texdesc.flag & hsd.TEX_ALPHAMAP_MASK
if not (alphamap == hsd.TEX_ALPHAMAP_NONE or
alphamap == hsd.TEX_ALPHAMAP_PASS):
mix = nodes.new('ShaderNodeMixRGB')
mix.blend_type = map_alpha_op_dict[alphamap]
mix.inputs[0].default_value = 1
###
alphamap_name_dict = {
hsd.TEX_ALPHAMAP_NONE: 'TEX_ALPHAMAP_NONE',
hsd.TEX_ALPHAMAP_PASS: 'TEX_ALPHAMAP_PASS',
hsd.TEX_ALPHAMAP_REPLACE: 'TEX_ALPHAMAP_REPLACE',