-
Notifications
You must be signed in to change notification settings - Fork 4
/
skeletal_bone.py
126 lines (107 loc) · 5.07 KB
/
skeletal_bone.py
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
from runeblend.mat_4 import Matrix4f
from runeblend.vec_3 import Vector3f
from mathutils import Euler
###########################################
# Majority of data here is not used, and has only been left as a reference
# local matrices is the only one we need for multiplying it with it's parent bones
# to get it's world-space position. In future most of this can be removed when we
# use the actual blender Matrix class
###########################################
class SkeletalBone:
def __init__(self):
self.parent_id = 0
self.id = 0
self.parent = None
self.unused = None
self.local_matrices = []
self.model_matrices = []
self.inverted_model_matrices = []
self.anim_matrix = Matrix4f()
self.update_anim_model_matrix = False
self.anim_matrix = Matrix4f()
self.update_final_matrix = False
self.final_matrix = Matrix4f()
self.rotations = []
self.translations = []
self.scalings = []
self.local_translation = Vector3f()
def decode(self, pose_id, compact_matrix, buffer):
self.parent_id = buffer.read_short()
self.local_matrices = [None for _ in range(pose_id)]
self.model_matrices = [None for _ in range(len(self.local_matrices))]
self.inverted_model_matrices = [None for _ in range(len(self.local_matrices))]
self.unused = [[0.0] * 3 for _ in range(len(self.local_matrices))]
for index in range(len(self.local_matrices)):
local_matrix = Matrix4f()
self.read_mat_4(buffer, local_matrix, compact_matrix)
self.local_matrices[index] = local_matrix
# These values are never used in the actual game engine, they're usually extremely
# high or extremely small values, and always the same for every bone within a skeleton
# But will always have a different value per skeleton. I suspect these hold additional data
# that is used by runescapes internal tooling, and perhaps have some logical operations done on them
# with their parent/local bone id to get something more meaningful?
self.unused[index][0] = buffer.read_float()
self.unused[index][1] = buffer.read_float()
self.unused[index][2] = buffer.read_float()
def read_mat_4(self, buffer, matrix, compact_matrix):
if compact_matrix:
pass
else:
for index in range(16):
matrix.m[index] = buffer.read_float()
# this isn't really needed
def extract_transformations(self):
pose_count = len(self.local_matrices)
self.rotations = []
self.translations = []
self.scalings = []
inverted_local_matrix = Matrix4f.take()
for pose in range(pose_count):
local_matrix = self.get_local_matrix(pose)
inverted_local_matrix.copy(local_matrix)
inverted_local_matrix.invert()
self.rotations.append(inverted_local_matrix.get_euler_angles_yxz_inverse())
self.translations.append(local_matrix.get_translation())
self.scalings.append(local_matrix.get_scale())
inverted_local_matrix.release()
def get_local_matrix(self, bone_index):
return self.local_matrices[bone_index]
def get_model_matrix(self, pose_id):
if self.model_matrices[pose_id] is None:
self.model_matrices[pose_id] = Matrix4f.from_arr(self.local_matrices[pose_id].m)
if self.parent:
self.model_matrices[pose_id].multiply(self.parent.get_model_matrix(pose_id))
else:
self.model_matrices[pose_id].multiply(Matrix4f.IDENTITY)
return self.model_matrices[pose_id]
def get_inverted_model_matrix(self, pose_id):
if self.inverted_model_matrices[pose_id] is None:
self.inverted_model_matrices[pose_id] = self.get_model_matrix(pose_id)
self.inverted_model_matrices[pose_id].invert()
return self.inverted_model_matrices[pose_id]
def set_anim_matrix(self, anim_matrix):
self.anim_matrix.copy(anim_matrix)
self.update_anim_model_matrix = True
self.update_final_matrix = True
def get_anim_matrix(self):
return self.anim_matrix
def get_anim_model_matrix(self):
if self.update_anim_model_matrix:
self.update_anim_model_matrix = False
if self.parent:
self.anim_matrix.multiply(self.parent.get_anim_model_matrix())
else:
self.anim_matrix.copy(self.get_anim_matrix())
return self.anim_matrix
def get_final_matrix(self, pose_id):
if self.update_final_matrix:
self.update_final_matrix = False
self.final_matrix.copy(self.get_inverted_model_matrix(pose_id))
self.final_matrix.multiply(self.get_anim_model_matrix())
return self.final_matrix
def get_rotation(self, pose_id):
return self.rotations[pose_id]
def get_translation(self, pose_id):
return self.translations[pose_id]
def get_scaling(self, pose_id):
return self.scalings[pose_id]