2019-11-20 14:05:35 +01:00
|
|
|
tool
|
|
|
|
extends CharacterSkeleton3D
|
|
|
|
|
2019-11-21 13:10:00 +01:00
|
|
|
# Copyright Péter Magyar relintai@gmail.com
|
|
|
|
# MIT License, functionality from this class needs to be protable to the entity spell system
|
2019-11-20 14:05:35 +01:00
|
|
|
|
2021-04-15 11:52:32 +02:00
|
|
|
# Copyright (c) 2019-2021 Péter Magyar
|
2019-12-01 23:21:48 +01:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
var job_script = preload("res://player/CharacterSkeletonMeshJob.gd")
|
|
|
|
|
2019-11-20 14:05:35 +01:00
|
|
|
export(bool) var refresh_in_editor : bool = false setget editor_build
|
|
|
|
export(bool) var automatic_build : bool = false
|
2020-03-30 23:18:28 +02:00
|
|
|
export(bool) var use_lod : bool = true
|
|
|
|
|
2019-11-20 14:05:35 +01:00
|
|
|
export(NodePath) var mesh_instance_path : NodePath
|
|
|
|
var mesh_instance : MeshInstance = null
|
|
|
|
|
|
|
|
export(NodePath) var skeleton_path : NodePath
|
|
|
|
var skeleton : Skeleton
|
|
|
|
|
2020-05-19 21:10:07 +02:00
|
|
|
export(Array, ModelVisual) var viss : Array
|
2019-11-20 14:05:35 +01:00
|
|
|
|
2020-03-30 23:18:28 +02:00
|
|
|
var meshes : Array
|
|
|
|
|
|
|
|
var _current_lod_level : int = 0
|
|
|
|
|
|
|
|
var _generating : bool = false
|
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
var _mesh_job : ThreadPoolJob = null
|
2021-08-26 00:15:30 +02:00
|
|
|
var _material_cache : ESSMaterialCache = null
|
2020-05-30 19:25:32 +02:00
|
|
|
|
2019-12-24 03:10:29 +01:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-20 14:05:35 +01:00
|
|
|
var _textures : Array
|
|
|
|
var _texture : Texture
|
|
|
|
|
|
|
|
var _editor_built : bool = false
|
2020-06-16 18:44:42 +02:00
|
|
|
var sheathed : bool = true
|
2019-11-20 14:05:35 +01:00
|
|
|
|
2020-02-16 16:23:45 +01:00
|
|
|
func _enter_tree():
|
2020-05-30 19:25:32 +02:00
|
|
|
_mesh_job = job_script.new()
|
|
|
|
_mesh_job.use_lod = use_lod
|
|
|
|
_mesh_job.connect("finished", self, "job_finished")
|
|
|
|
|
|
|
|
meshes.resize(3)
|
2019-11-20 14:05:35 +01:00
|
|
|
|
|
|
|
skeleton = get_node(skeleton_path) as Skeleton
|
|
|
|
mesh_instance = get_node(mesh_instance_path) as MeshInstance
|
|
|
|
|
|
|
|
set_process(false)
|
2020-06-16 19:10:52 +02:00
|
|
|
|
|
|
|
if Engine.editor_hint:
|
|
|
|
return
|
2019-11-20 14:05:35 +01:00
|
|
|
|
|
|
|
# if not Engine.is_editor_hint():
|
|
|
|
for iv in viss:
|
2020-05-19 21:10:07 +02:00
|
|
|
add_model_visual(iv as ModelVisual)
|
2020-06-16 18:44:42 +02:00
|
|
|
|
2020-07-10 10:34:25 +02:00
|
|
|
call_deferred("sheath", sheathed)
|
2020-07-10 10:29:07 +02:00
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
if automatic_build:
|
2020-07-10 10:31:09 +02:00
|
|
|
call_deferred("build_model")
|
2020-06-16 18:44:42 +02:00
|
|
|
|
2019-11-20 14:05:35 +01:00
|
|
|
func _build_model():
|
2020-03-30 23:18:28 +02:00
|
|
|
if _generating:
|
|
|
|
return
|
|
|
|
|
|
|
|
_generating = true
|
|
|
|
|
2019-11-20 14:05:35 +01:00
|
|
|
if Engine.is_editor_hint() and not refresh_in_editor:
|
|
|
|
set_process(false)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not automatic_build:
|
|
|
|
set_process(false)
|
|
|
|
return
|
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
build()
|
|
|
|
set_process(false)
|
2019-11-20 14:05:35 +01:00
|
|
|
|
|
|
|
model_dirty = false
|
|
|
|
|
|
|
|
func build():
|
|
|
|
setup_build_mesh()
|
2020-05-30 14:04:37 +02:00
|
|
|
|
2020-09-15 10:54:50 +02:00
|
|
|
sort_layers()
|
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
var data : Array = Array()
|
2020-05-23 19:56:03 +02:00
|
|
|
|
2020-06-16 13:40:16 +02:00
|
|
|
for skele_point in range(ESS.skeletons_bones_index_get(entity_type).count(',') + 1):
|
2019-12-24 03:10:29 +01:00
|
|
|
var bone_name : String = get_bone_name(skele_point)
|
2020-06-16 13:40:16 +02:00
|
|
|
|
2019-12-24 03:10:29 +01:00
|
|
|
if bone_name == "":
|
|
|
|
print("Bone name error")
|
|
|
|
continue
|
|
|
|
|
|
|
|
var bone_idx : int = skeleton.find_bone(bone_name)
|
2022-08-20 02:44:58 +02:00
|
|
|
|
2021-01-29 16:33:27 +01:00
|
|
|
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)
|
2020-05-17 16:28:31 +02:00
|
|
|
|
2020-09-15 10:54:50 +02:00
|
|
|
var ddict : Dictionary = Dictionary()
|
2019-12-24 03:10:29 +01:00
|
|
|
for j in range(get_model_entry_count(skele_point)):
|
|
|
|
var entry : SkeletonModelEntry = get_model_entry(skele_point, j)
|
2020-05-30 19:25:32 +02:00
|
|
|
|
2020-09-15 10:54:50 +02:00
|
|
|
for k in range(entry.entry.size):
|
|
|
|
if entry.entry.get_mesh(k):
|
|
|
|
ddict["bone_name"] = bone_name
|
|
|
|
ddict["bone_idx"] = bone_idx
|
|
|
|
|
2021-01-29 16:33:27 +01:00
|
|
|
var global_pose = skeleton.get_bone_global_pose(bone_idx)
|
|
|
|
|
2022-08-20 02:44:58 +02:00
|
|
|
#ddict["transform"] = global_pose
|
2021-01-29 16:33:27 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-09-15 10:54:50 +02:00
|
|
|
ddict["mesh"] = entry.entry.get_mesh(k)
|
2020-09-15 11:36:08 +02:00
|
|
|
|
|
|
|
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
|
2019-11-20 14:05:35 +01:00
|
|
|
|
2020-09-15 10:54:50 +02:00
|
|
|
if !ddict.empty():
|
|
|
|
data.append(ddict)
|
2020-03-30 23:18:28 +02:00
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
_mesh_job.data = data
|
2021-08-26 00:15:30 +02:00
|
|
|
_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
|
2020-07-11 01:00:25 +02:00
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
finish_build_mesh()
|
2020-07-11 01:00:25 +02:00
|
|
|
|
|
|
|
ThreadPool.add_job(_mesh_job)
|
|
|
|
# _mesh_job.execute()
|
2019-11-20 14:05:35 +01:00
|
|
|
|
|
|
|
func setup_build_mesh() -> void:
|
2020-05-23 18:12:53 +02:00
|
|
|
if mesh_instance != null:
|
|
|
|
mesh_instance.hide()
|
|
|
|
|
2019-11-20 14:05:35 +01:00
|
|
|
if get_animation_tree() != null:
|
|
|
|
get_animation_tree().active = false
|
|
|
|
|
|
|
|
if get_animation_player() != null:
|
2022-08-20 02:44:58 +02:00
|
|
|
get_animation_player().play("RESET")
|
2019-11-20 14:05:35 +01:00
|
|
|
get_animation_player().seek(0, true)
|
|
|
|
|
|
|
|
func finish_build_mesh() -> void:
|
2020-06-18 22:46:24 +02:00
|
|
|
mesh_instance.mesh = null
|
|
|
|
# mesh_instance.mesh = meshes[_current_lod_level]
|
2019-11-20 14:05:35 +01:00
|
|
|
|
|
|
|
if get_animation_tree() != null:
|
|
|
|
get_animation_tree().active = true
|
2020-05-23 18:12:53 +02:00
|
|
|
|
|
|
|
if mesh_instance != null:
|
|
|
|
mesh_instance.show()
|
2020-03-30 23:18:28 +02:00
|
|
|
|
|
|
|
_generating = false
|
2020-05-30 19:25:32 +02:00
|
|
|
|
|
|
|
func job_finished():
|
|
|
|
meshes = _mesh_job.meshes
|
|
|
|
mesh_instance.mesh = meshes[_current_lod_level]
|
2019-11-20 14:05:35 +01:00
|
|
|
|
|
|
|
func clear_mesh() -> void:
|
2020-03-30 23:18:28 +02:00
|
|
|
meshes.clear()
|
2020-05-30 19:25:32 +02:00
|
|
|
meshes.resize(3)
|
2019-11-20 14:05:35 +01:00
|
|
|
|
|
|
|
if mesh_instance != null:
|
|
|
|
mesh_instance.mesh = null
|
|
|
|
|
|
|
|
func editor_build(val : bool) -> void:
|
2020-05-17 16:28:31 +02:00
|
|
|
if not is_inside_tree():
|
2019-11-20 14:05:35 +01:00
|
|
|
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
|
2019-12-24 03:10:29 +01:00
|
|
|
|
|
|
|
func get_bone_name(skele_point : int) -> String:
|
|
|
|
if bone_names.has(skele_point):
|
|
|
|
return bone_names[skele_point]
|
|
|
|
|
|
|
|
return ""
|
2020-03-30 23:18:28 +02:00
|
|
|
|
2020-06-16 13:40:16 +02:00
|
|
|
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
|
2020-06-16 18:44:42 +02:00
|
|
|
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_LEFT:
|
|
|
|
return 7
|
|
|
|
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_LEFT_BACK:
|
|
|
|
return 9
|
2020-06-16 19:10:52 +02:00
|
|
|
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_RIGHT:
|
|
|
|
return 8
|
2020-06-16 18:44:42 +02:00
|
|
|
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_RIGHT_BACK:
|
|
|
|
return 10
|
2020-06-16 19:10:52 +02:00
|
|
|
elif point == EntityEnums.COMMON_SKELETON_POINT_WEAPON_LEFT_SHIELD:
|
|
|
|
return 11
|
2020-06-16 13:40:16 +02:00
|
|
|
|
|
|
|
return 3
|
|
|
|
|
2020-03-30 23:18:28 +02:00
|
|
|
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]
|
2022-03-20 14:16:46 +01:00
|
|
|
|
2020-06-16 19:10:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
func toggle_sheath():
|
|
|
|
sheathed = not sheathed
|
|
|
|
sheath(sheathed)
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 18:44:42 +02:00
|
|
|
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)
|