-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCharacterSkeletonGD.gd
350 lines (253 loc) · 8.71 KB
/
CharacterSkeletonGD.gd
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
tool
extends CharacterSkeleton3D
# Copyright Péter Magyar [email protected]
# MIT License, functionality from this class needs to be protable to the entity spell system
# Copyright (c) 2019-2021 Péter Magyar
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
var job_script = preload("res://player/CharacterSkeletonMeshJob.gd")
export(bool) var refresh_in_editor : bool = false setget editor_build
export(bool) var automatic_build : bool = false
export(bool) var use_lod : bool = true
export(NodePath) var mesh_instance_path : NodePath
var mesh_instance : MeshInstance = null
export(NodePath) var skeleton_path : NodePath
var skeleton : Skeleton
export(Array, ModelVisual) var viss : Array
var meshes : Array
var _current_lod_level : int = 0
var _generating : bool = false
var _mesh_job : ThreadPoolJob = null
var _material_cache : ESSMaterialCache = null
var bone_names = {
0: "root",
1: "pelvis",
2: "spine",
3: "spine_1",
4: "spine_2",
5: "neck",
6: "head",
7: "left_clavicle",
8: "left_upper_arm",
9: "left_forearm",
10: "left_hand",
11: "left_thunb_base",
12: "left_thumb_end",
13: "left_fingers_base",
14: "left_fingers_end",
15: "right_clavicle",
16: "right_upper_arm",
17: "right_forearm",
18: "right_hand",
19: "right_thumb_base",
20: "right_thumb_head",
21: "right_fingers_base",
22: "right_fingers_head",
23: "left_thigh",
24: "left_calf",
25: "left_foot",
26: "right_thigh",
27: "right_calf",
28: "right_foot",
}
var _textures : Array
var _texture : Texture
var _editor_built : bool = false
var sheathed : bool = true
func _enter_tree():
_mesh_job = job_script.new()
_mesh_job.use_lod = use_lod
_mesh_job.connect("finished", self, "job_finished")
meshes.resize(3)
skeleton = get_node(skeleton_path) as Skeleton
mesh_instance = get_node(mesh_instance_path) as MeshInstance
set_process(false)
if Engine.editor_hint:
return
# if not Engine.is_editor_hint():
for iv in viss:
add_model_visual(iv as ModelVisual)
call_deferred("sheath", sheathed)
if automatic_build:
call_deferred("build_model")
func _build_model():
if _generating:
return
_generating = true
if Engine.is_editor_hint() and not refresh_in_editor:
set_process(false)
return
if not automatic_build:
set_process(false)
return
build()
set_process(false)
model_dirty = false
func build():
setup_build_mesh()
sort_layers()
var data : Array = Array()
for skele_point in range(ESS.skeletons_bones_index_get(entity_type).count(',') + 1):
var bone_name : String = get_bone_name(skele_point)
if bone_name == "":
print("Bone name error")
continue
var bone_idx : int = skeleton.find_bone(bone_name)
var abi_dict : Dictionary = Dictionary()
for abti in range(bone_additional_mesh_transform_count):
var obi : int = bone_additional_mesh_transform_bone_index_get(abti)
var bin = get_bone_name(obi)
var bi : int = skeleton.find_bone(bin)
abi_dict[bi] = bone_additional_mesh_transform_transform_get(abti) * bone_additional_mesh_transform_user_transform_get(abti)
var ddict : Dictionary = Dictionary()
for j in range(get_model_entry_count(skele_point)):
var entry : SkeletonModelEntry = get_model_entry(skele_point, j)
for k in range(entry.entry.size):
if entry.entry.get_mesh(k):
ddict["bone_name"] = bone_name
ddict["bone_idx"] = bone_idx
var global_pose = skeleton.get_bone_global_pose(bone_idx)
#ddict["transform"] = global_pose
if abi_dict.has(bone_idx):
global_pose *= abi_dict[bone_idx]
# for abti in range(bone_model_additional_mesh_transform_count):
# var bin = get_bone_name(bone_model_additional_mesh_transform_bone_index_get(abti))
# var bi : int = skeleton.find_bone(bin)
#
# if bone_idx == bi:
# global_pose *= bone_model_additional_mesh_transform_bone_transform_get(abti)
# break
#
ddict["transform"] = global_pose
ddict["mesh"] = entry.entry.get_mesh(k)
var texture_layer_array : Array = Array()
texture_layer_array.resize(ESS.texture_layers.count(",") + 1)
var texture_used : bool = false
for j in range(get_model_entry_count(skele_point)):
var entry : SkeletonModelEntry = get_model_entry(skele_point, j)
var layer : int = entry.entry.override_layer
if texture_layer_array.size() <= layer || texture_layer_array[layer]:
continue
for k in range(entry.entry.size):
if entry.entry.get_texture(k):
texture_layer_array[layer] = entry.entry.get_texture(k)
texture_used = true
break
if texture_used:
ddict["textures"] = texture_layer_array
if !ddict.empty():
data.append(ddict)
_mesh_job.data = data
_material_cache= ESS.material_cache_get(data.hash())
if _material_cache.material_get_num() == 0:
#lock just in case
_material_cache.mutex_lock()
if _material_cache.material_get_num() == 0:
#this has to be done on the main thread!
_material_cache.initial_setup_default()
_material_cache.mutex_unlock()
_mesh_job.material_cache = _material_cache
finish_build_mesh()
ThreadPool.add_job(_mesh_job)
# _mesh_job.execute()
func setup_build_mesh() -> void:
if mesh_instance != null:
mesh_instance.hide()
if get_animation_tree() != null:
get_animation_tree().active = false
if get_animation_player() != null:
get_animation_player().play("RESET")
get_animation_player().seek(0, true)
func finish_build_mesh() -> void:
mesh_instance.mesh = null
# mesh_instance.mesh = meshes[_current_lod_level]
if get_animation_tree() != null:
get_animation_tree().active = true
if mesh_instance != null:
mesh_instance.show()
_generating = false
func job_finished():
meshes = _mesh_job.meshes
mesh_instance.mesh = meshes[_current_lod_level]
func clear_mesh() -> void:
meshes.clear()
meshes.resize(3)
if mesh_instance != null:
mesh_instance.mesh = null
func editor_build(val : bool) -> void:
if not is_inside_tree():
return
skeleton = get_node(skeleton_path) as Skeleton
mesh_instance = get_node(mesh_instance_path) as MeshInstance
if val:
_editor_built = true
build()
else:
clear_mesh()
_editor_built = false
refresh_in_editor = val
func get_bone_name(skele_point : int) -> String:
if bone_names.has(skele_point):
return bone_names[skele_point]
return ""
func _common_attach_point_index_get(point):
if point == EntityEnums.COMMON_SKELETON_POINT_LEFT_HAND:
return 0
elif point == EntityEnums.COMMON_SKELETON_POINT_ROOT:
return 3
elif point == EntityEnums.COMMON_SKELETON_POINT_SPINE_2:
return 6
elif point == EntityEnums.COMMON_SKELETON_POINT_RIGHT_HAND:
return 1
elif point == EntityEnums.COMMON_SKELETON_POINT_BACK:
return 6
elif point == EntityEnums.COMMON_SKELETON_POINT_RIGHT_HIP:
return 4
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_LEFT:
return 7
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_LEFT_BACK:
return 9
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_RIGHT:
return 8
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_RIGHT_BACK:
return 10
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_LEFT_SHIELD:
return 11
return 3
func set_lod_level(level : int) -> void:
if _current_lod_level == level:
return
if meshes.size() == 0:
return
if level < 0:
return
if level >= meshes.size():
level = meshes.size() - 1
_current_lod_level = level
mesh_instance.mesh = meshes[_current_lod_level]
func toggle_sheath():
sheathed = not sheathed
sheath(sheathed)
func sheath(on : bool) -> void:
var pos = 0
if not on:
pos = 1
attach_point_node_get(7).set_node_position(pos)
attach_point_node_get(8).set_node_position(pos)
attach_point_node_get(9).set_node_position(pos)
attach_point_node_get(10).set_node_position(pos)
attach_point_node_get(11).set_node_position(pos)