Added SkeletonMeshJob and CharacterSkeleton3D form the 3d project.

This commit is contained in:
Relintai 2021-11-22 22:24:51 +01:00
parent 7bb8124ec7
commit d7a94ba74f
5 changed files with 506 additions and 14 deletions

View File

@ -12,7 +12,6 @@ collision_layer = 2147483650
collision_mask = 3
script = ExtResource( 1 )
max_visible_distance = 70.0
model_path = NodePath("../HumanFemale/Rotation_Helper/Model")
character_skeleton_path = NodePath("Rotation_Helper/Model/character2")
[node name="Body_CollisionShape" type="CollisionShape" parent="."]

View File

@ -137,18 +137,27 @@ attach_point_paths/9_weapon_left_back = NodePath("armature/Armature/Skeleton/Lef
attach_point_paths/10_weapon_right_back = NodePath("armature/Armature/Skeleton/RightWeaponBack")
attach_point_paths/11_weapon_shield_left = NodePath("armature/Armature/Skeleton/Shield")
script = ExtResource( 8 )
mesh_instance_path = NodePath("armature/Armature/Skeleton/MeshInstance")
skeleton_path = NodePath("armature/Armature/Skeleton")
[node name="armature" parent="." instance=ExtResource( 6 )]
script = ExtResource( 5 )
[node name="Skeleton" parent="armature/Armature" index="0"]
transform = Transform( 1, -6.84064e-18, 1.54087e-25, 6.84064e-18, 1, 1.05378e-42, -1.54087e-25, 0, 1, 0, 0, 0 )
bones/0/bound_children = [ NodePath("RootAttachment") ]
bones/1/bound_children = [ NodePath("RightHipAttachment"), NodePath("LeftHipAttachment") ]
bones/10/bound_children = [ NodePath("RightBackAttachment"), NodePath("LeftBackAttachment") ]
bones/14/bound_children = [ NodePath("RightHandAttachment") ]
bones/21/bound_children = [ NodePath("ShieldAttachment") ]
bones/22/bound_children = [ NodePath("LeftHandAttachment") ]
bones/27/bound_children = [ NodePath("NeckAttachment") ]
[node name="MeshInstance" type="MeshInstance" parent="armature/Armature/Skeleton" index="0"]
visible = false
[node name="LeftHandAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="1"]
transform = Transform( 0.188157, 0.145046, -0.971369, 0.0882702, -0.98753, -0.130361, -0.978163, -0.0612146, -0.198614, 0.244036, 0.773574, -0.0200296 )
transform = Transform( 0.178242, 0.137293, -0.974361, 0.0695386, -0.9895, -0.126706, -0.981525, -0.0451713, -0.185918, 0.240629, 0.77064, -0.0135979 )
bone_name = "left_hand"
[node name="LeftHandAP" type="Spatial" parent="armature/Armature/Skeleton/LeftHandAttachment"]
@ -156,14 +165,14 @@ transform = Transform( -0.0646502, 0.683772, 0.726832, -0.148514, -0.726783, 0.6
script = ExtResource( 2 )
[node name="ShieldAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="2"]
transform = Transform( -0.0406676, 0.12356, 0.991503, -0.0727625, -0.990056, 0.120395, 0.996519, -0.0672481, 0.0492538, 0.214299, 1.01185, -0.00384494 )
transform = Transform( -0.0304567, 0.115726, 0.992814, -0.0547942, -0.991975, 0.113947, 0.998032, -0.05093, 0.0365534, 0.212777, 1.00938, -0.00134053 )
bone_name = "left_forearm"
[node name="LeftHandShield" type="Spatial" parent="armature/Armature/Skeleton/ShieldAttachment"]
transform = Transform( 0.122585, 0.992132, -0.0242311, 0.987746, -0.124342, -0.0941345, -0.0964273, -0.0123912, -0.995239, 0.0157415, 0.108918, 0.0213161 )
[node name="RightHandAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="3"]
transform = Transform( -0.0370837, -0.0893548, -0.995309, -0.0909409, -0.99156, 0.0924068, -0.995166, 0.0939413, 0.0286449, -0.248013, 0.771704, -0.00950206 )
transform = Transform( -0.0765276, -0.0800615, -0.993848, -0.0567859, -0.994803, 0.0845112, -0.99545, 0.062904, 0.0715836, -0.247435, 0.769703, -0.00345938 )
bone_name = "right_hand"
[node name="RightHandAP" type="Spatial" parent="armature/Armature/Skeleton/RightHandAttachment"]
@ -179,7 +188,7 @@ transform = Transform( 1, 0, 0, 0, -1.62921e-07, 1, 0, -1, -1.62921e-07, 0, 0, 0
script = ExtResource( 2 )
[node name="NeckAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="5"]
transform = Transform( 0.999994, 0.00106162, -0.00327271, -0.000842232, 0.997799, 0.0663152, 0.00333591, -0.066312, 0.997793, 0.000883431, 1.31766, 0.000961275 )
transform = Transform( 0.99999, -0.00322995, -0.00303278, 0.00343013, 0.997646, 0.0684912, 0.00280441, -0.0685009, 0.997647, 0.000907734, 1.31551, -7.88745e-05 )
bone_name = "neck"
[node name="TorsoAP" type="Spatial" parent="armature/Armature/Skeleton/NeckAttachment"]
@ -187,28 +196,28 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.04, -0.09 )
script = ExtResource( 2 )
[node name="RightHipAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="6"]
transform = Transform( 0.999554, 0.0298487, 1.41579e-08, -0.0298487, 0.999554, 4.71643e-07, -7.36469e-11, -4.71856e-07, 1, 0.000646701, 0.77617, 0 )
transform = Transform( 0.999721, 0.0236193, 1.12032e-08, -0.0236193, 0.999721, 4.71722e-07, -5.82903e-11, -4.71855e-07, 1, 0.00311605, 0.774054, 0 )
bone_name = "pelvis"
[node name="Spatial" type="Spatial" parent="armature/Armature/Skeleton/RightHipAttachment"]
transform = Transform( 0.993372, -0.0110498, 0.114411, 0.0109836, 0.999939, 0.00120905, -0.114417, 5.56017e-05, 0.993433, -0.141825, 0.176866, 0.16621 )
[node name="LeftHipAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="7"]
transform = Transform( 0.999554, 0.0298487, 1.41579e-08, -0.0298487, 0.999554, 4.71643e-07, -7.36469e-11, -4.71856e-07, 1, 0.000646701, 0.77617, 0 )
transform = Transform( 0.999721, 0.0236193, 1.12032e-08, -0.0236193, 0.999721, 4.71722e-07, -5.82903e-11, -4.71855e-07, 1, 0.00311605, 0.774054, 0 )
bone_name = "pelvis"
[node name="Spatial" type="Spatial" parent="armature/Armature/Skeleton/LeftHipAttachment"]
transform = Transform( 0.99343, -0.00240871, -0.114417, 0.00240855, 0.999997, -0.000139626, 0.114417, -0.00013687, 0.993433, 0.144429, 0.18077, 0.165634 )
[node name="RightBackAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="8"]
transform = Transform( 0.999994, 0.000544192, -0.00322462, -0.000569631, 0.999969, -0.00790054, 0.00322021, 0.00790234, 0.999964, 0.000781342, 1.13007, -0.000521188 )
transform = Transform( 0.999989, -0.00371775, -0.00266693, 0.00370247, 0.999977, -0.00571864, 0.00268812, 0.00570872, 0.99998, 0.00160517, 1.12792, -0.00114982 )
bone_name = "spine_2"
[node name="RightBackAP" type="Spatial" parent="armature/Armature/Skeleton/RightBackAttachment"]
transform = Transform( 0.999997, -0.00244424, -0.000479204, 0.00243884, 0.999966, -3.47705e-05, 0.000479292, 3.44458e-05, 1, 0.000560207, -0.00363708, -0.120332 )
[node name="LeftBackAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="9"]
transform = Transform( 0.999994, 0.000544192, -0.00322462, -0.000569631, 0.999969, -0.00790054, 0.00322021, 0.00790234, 0.999964, 0.000781342, 1.13007, -0.000521188 )
transform = Transform( 0.999989, -0.00371775, -0.00266693, 0.00370247, 0.999977, -0.00571864, 0.00268812, 0.00570872, 0.99998, 0.00160517, 1.12792, -0.00114982 )
bone_name = "spine_2"
[node name="LeftBackAP" type="Spatial" parent="armature/Armature/Skeleton/LeftBackAttachment"]

View File

@ -4,7 +4,7 @@ extends CharacterSkeleton3D
# Copyright Péter Magyar relintai@gmail.com
# MIT License, functionality from this class needs to be protable to the entity spell system
# Copyright (c) 2019 Péter Magyar
# 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
@ -24,3 +24,333 @@ extends CharacterSkeleton3D
# 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"] = skeleton.get_bone_global_pose(bone_idx)
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("rest")
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]
if !mesh_instance.is_software_skinning_enabled():
mesh_instance.initialize_skinning(true, true)
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]
if !mesh_instance.is_software_skinning_enabled():
mesh_instance.initialize_skinning(true, true)
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)

View File

@ -0,0 +1,157 @@
tool
extends ThreadPoolJob
# Copyright Péter Magyar relintai@gmail.com
# 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.
export(bool) var use_lod : bool = true
var meshes : Array
var _generating : bool = false
var _textures : Array
var material_cache : ESSMaterialCache = null
var materials : Array = Array()
var data : Array
signal finished
func _execute():
prepare_textures()
meshes.clear()
var mm : MeshMerger = MeshMerger.new()
mm.format = ArrayMesh.ARRAY_FORMAT_VERTEX | ArrayMesh.ARRAY_FORMAT_COLOR | ArrayMesh.ARRAY_FORMAT_BONES | ArrayMesh.ARRAY_FORMAT_INDEX | ArrayMesh.ARRAY_FORMAT_NORMAL | ArrayMesh.ARRAY_FORMAT_TEX_UV | ArrayMesh.ARRAY_FORMAT_WEIGHTS
var bones : PoolIntArray = PoolIntArray()
bones.resize(4)
bones[0] = 1
bones[1] = 0
bones[2] = 0
bones[3] = 0
var bonew : PoolRealArray = PoolRealArray()
bonew.resize(4)
bonew[0] = 1
bonew[1] = 0
bonew[2] = 0
bonew[3] = 0
for ddict in data:
var bone_name : String = ddict["bone_name"]
var bone_idx : int = ddict["bone_idx"]
var texture : Texture = ddict["texture"]
#var atlas_texture : AtlasTexture = ddict["atlas_texture"]
var transform : Transform = ddict["transform"]
var mesh : MeshDataResource = ddict["mesh"]
var rect : Rect2 = material_cache.texture_get_uv_rect(texture)
bones[0] = bone_idx
mm.add_mesh_data_resource_bone(mesh, bones, bonew, transform, rect)
var arr : Array = mm.build_mesh()
var mesh : ArrayMesh = ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
mesh.surface_set_material(0, material_cache.material_lod_get(0))
meshes.append(mesh)
if use_lod:
arr = MeshUtils.merge_mesh_array(arr)
var meshl2 : ArrayMesh = ArrayMesh.new()
meshl2.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
meshl2.surface_set_material(0, material_cache.material_lod_get(1))
meshes.append(meshl2)
var texture : Texture = null
var ml : Material = material_cache.material_lod_get(0)
if ml is SpatialMaterial:
var spml : SpatialMaterial = ml
texture = spml.get_texture(SpatialMaterial.TEXTURE_ALBEDO)
elif ml is ShaderMaterial:
var sm : ShaderMaterial = ml
texture = sm.get_shader_param("texture_albedo")
arr = MeshUtils.bake_mesh_array_uv(arr, texture)
arr[VisualServer.ARRAY_TEX_UV] = null
var meshl3 : ArrayMesh = ArrayMesh.new()
meshl3.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
meshl3.surface_set_material(0, material_cache.material_lod_get(2))
meshes.append(meshl3)
emit_signal("finished")
# call_deferred("emit_signal", "finished")
complete = true
func prepare_textures() -> void:
if !material_cache.initialized:
material_cache.mutex_lock()
#got initialized before we got the lock
#No need to have the lock anymore
if material_cache.initialized:
material_cache.mutex_unlock()
var lmerger : TextureLayerMerger = TextureLayerMerger.new()
for i in range(data.size()):
var ddict : Dictionary = data[i]
var textures : Array = ddict["textures"]
var texture : Texture = null
var tcount : int = 0
for j in range(textures.size()):
if textures[j]:
tcount += 1
if tcount > 1:
for j in range(textures.size() - 1, -1, -1):
if textures[j]:
lmerger.add_texture(textures[j])
break
lmerger.merge()
texture = lmerger.get_result_as_texture()
lmerger.clear()
else:
for j in range(textures.size() - 1, -1, -1):
if textures[j]:
texture = textures[j]
break
ddict["texture"] = texture
if texture != null:
if !material_cache.initialized:
material_cache.texture_add(texture)
data[i] = ddict
if !material_cache.initialized:
material_cache.refresh_rects()
material_cache.mutex_unlock()

View File

@ -3,8 +3,6 @@
[ext_resource path="res://testsave.png" type="Texture" id=1]
[ext_resource path="res://spritesheet_generator/SpriteMover.gd" type="Script" id=2]
[sub_resource type="SpatialMaterial" id=1]
flags_transparent = true
params_billboard_mode = 1
@ -16,7 +14,6 @@ cast_shadow = 0
flip_v = true
pixel_size = 0.064
texture = ExtResource( 1 )
vframes = 16
hframes = 16
vframes = 16
script = ExtResource( 2 )