diff --git a/game/materials/3_standard_character_material.tres b/game/materials/3_standard_character_material.tres index aaf3b261..74e9cc80 100644 --- a/game/materials/3_standard_character_material.tres +++ b/game/materials/3_standard_character_material.tres @@ -2,8 +2,6 @@ [ext_resource path="res://shaders/4_standard_character_shader.tres" type="Shader" id=1] - - [resource] shader = ExtResource( 1 ) shader_param/albedo = Color( 1, 1, 1, 1 ) diff --git a/game/materials/9_standard_character_material_notex.tres b/game/materials/9_standard_character_material_notex.tres index f1ac538b..a7bfa134 100644 --- a/game/materials/9_standard_character_material_notex.tres +++ b/game/materials/9_standard_character_material_notex.tres @@ -2,8 +2,6 @@ [ext_resource path="res://shaders/7_standard_character_shader_notex.tres" type="Shader" id=1] - - [resource] shader = ExtResource( 1 ) shader_param/albedo = Color( 1, 1, 1, 1 ) diff --git a/game/models/entities/human/models/armature_editor.gd b/game/models/entities/human/models/armature_editor.gd new file mode 100644 index 00000000..de0e886d --- /dev/null +++ b/game/models/entities/human/models/armature_editor.gd @@ -0,0 +1,324 @@ +tool +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-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/CharacterSkeletonMeshJobSimple.gd") + +export(bool) var build : bool = false setget editor_build + +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 mesh : ArrayMesh + +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.connect("finished", self, "job_finished") + + skeleton = get_node(skeleton_path) as Skeleton + mesh_instance = get_node(mesh_instance_path) as MeshInstance + + set_process(false) + + call_deferred("sheath", sheathed) + toggle_build() + +func toggle_build(): + if build: + call_deferred("setup_and_build") + else: + call_deferred("clear_all") + +func setup_and_build(): + for iv in viss: + add_model_visual(iv as ModelVisual) + + call_deferred("build_model") + +func clear_all(): + for iv in viss: + remove_model_visual(iv as ModelVisual) + + clear_mesh() + _mesh_job.reset() + +func _build_model(): + if _generating: + return + + _generating = true + + if not 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.reset() + _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) + +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 + + if get_animation_tree() != null: + get_animation_tree().active = true + + if mesh_instance != null: + mesh_instance.show() + + +func job_finished(): + mesh = _mesh_job.final_mesh + mesh_instance.mesh = mesh + + _generating = false + +func clear_mesh() -> void: + mesh = null + + 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 + + build = val + + toggle_build() + +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 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) diff --git a/game/models/entities/human/models/armature_editor.tscn b/game/models/entities/human/models/armature_editor.tscn new file mode 100644 index 00000000..5428748d --- /dev/null +++ b/game/models/entities/human/models/armature_editor.tscn @@ -0,0 +1,135 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://models/entities/human/models/armature_editor.gd" type="Script" id=1] +[ext_resource path="res://player/CharacterSkeletonAttachPoint.gd" type="Script" id=2] +[ext_resource path="res://core/model_visuals/test_base_v3.tres" type="ModelVisual" id=5] +[ext_resource path="res://models/entities/human/models/armature.tscn" type="PackedScene" id=6] + +[node name="character" type="CharacterSkeleton3D"] +entity_type = 1 +model_dirty = true +animation_player_path = NodePath("armature/AnimationPlayer") +attach_point_paths/0_left_hand = NodePath("armature/Armature/Skeleton/LeftHandAttachment/LeftHandAP") +attach_point_paths/1_right_hand = NodePath("armature/Armature/Skeleton/RightHandAttachment/RightHandAP") +attach_point_paths/2_torso = NodePath("armature/Armature/Skeleton/NeckAttachment/TorsoAP") +attach_point_paths/3_root = NodePath("armature/Armature/Skeleton/RootAttachment/RootAP") +attach_point_paths/4_right_hip = NodePath("armature/Armature/Skeleton/RightHipAttachment/Spatial") +attach_point_paths/5_left_hip = NodePath("") +attach_point_paths/6_spine_2 = NodePath("") +attach_point_paths/7_weapon_left = NodePath("armature/Armature/Skeleton/LeftWeapon") +attach_point_paths/8_weapon_right = NodePath("armature/Armature/Skeleton/RightWeapon") +attach_point_paths/9_weapon_left_back = NodePath("armature/Armature/Skeleton/LeftWeaponBack") +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( 1 ) +mesh_instance_path = NodePath("armature/Armature/Skeleton/MeshInstance") +skeleton_path = NodePath("armature/Armature/Skeleton") +viss = [ ExtResource( 5 ) ] + +[node name="armature" parent="." instance=ExtResource( 6 )] + +[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"] + +[node name="LeftHandAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="1"] +transform = Transform( -0.0261654, 0.991076, 0.130703, 0.130666, 0.133017, -0.982463, -0.99108, -0.00862805, -0.13298, 0.633677, 1.24697, -0.00282459 ) +bone_name = "left_hand" + +[node name="LeftHandAP" type="Spatial" parent="armature/Armature/Skeleton/LeftHandAttachment"] +transform = Transform( -0.0646502, 0.683772, 0.726832, -0.148514, -0.726783, 0.670508, 0.986758, -0.0646467, 0.148575, -0.0226043, 0.107657, 0.0283146 ) +script = ExtResource( 2 ) + +[node name="ShieldAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="2"] +transform = Transform( 0.0185141, 0.99935, 0.0309251, 0.0309237, -0.0314875, 0.999026, 0.999349, -0.0175398, -0.0314865, 0.393162, 1.25454, 0.00139672 ) +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.0297752, -0.995028, 0.0950463, 0.0829777, 0.0972214, 0.991798, -0.996108, -0.0216441, 0.0854607, -0.634219, 1.24856, -0.00479734 ) +bone_name = "right_hand" + +[node name="RightHandAP" type="Spatial" parent="armature/Armature/Skeleton/RightHandAttachment"] +transform = Transform( 0.0789363, 0.493983, 0.86583, -0.0571054, -0.864836, 0.498656, 0.995268, -0.0888088, -0.0400836, -0.0131918, 0.0790432, -0.0425565 ) +script = ExtResource( 2 ) + +[node name="RootAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="4"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -3.23883e-05, 0, 0.000216131 ) +bone_name = "root" + +[node name="RootAP" type="Spatial" parent="armature/Armature/Skeleton/RootAttachment"] +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( 1, 0.000278968, -9.65792e-05, -0.000269675, 0.996335, 0.0855512, 0.000120043, -0.0855511, 0.996334, 0.0010334, 1.32424, -0.00440288 ) +bone_name = "neck" + +[node name="TorsoAP" type="Spatial" parent="armature/Armature/Skeleton/NeckAttachment"] +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( 1, 0, 0, 0, 1, 4.71856e-07, 0, -4.71856e-07, 1, 0, 0.782716, 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( 1, 0, 0, 0, 1, 4.71856e-07, 0, -4.71856e-07, 1, 0, 0.782716, 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( 1, -6.44075e-07, 8.31194e-07, 6.43385e-07, 0.999936, 0.0113912, -8.86884e-07, -0.0113911, 0.999935, 0.00103352, 1.13666, -0.00226594 ) +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( 1, -6.44075e-07, 8.31194e-07, 6.43385e-07, 0.999936, 0.0113912, -8.86884e-07, -0.0113911, 0.999935, 0.00103352, 1.13666, -0.00226594 ) +bone_name = "spine_2" + +[node name="LeftBackAP" type="Spatial" parent="armature/Armature/Skeleton/LeftBackAttachment"] +transform = Transform( 0.999994, -0.00350821, -0.000687771, 0.00350043, 0.999949, -5.02995e-05, 0.000687948, 4.91154e-05, 1, 0.000822697, -0.00513983, -0.097967 ) + +[node name="LeftWeapon" type="Spatial" parent="armature/Armature/Skeleton" index="10"] +transform = Transform( 0.99343, -0.00240871, -0.114417, 0.0024086, 0.999997, -0.000139157, 0.114417, -0.000137342, 0.993433, 0.144429, 0.963486, 0.165634 ) +script = ExtResource( 2 ) +positions = [ NodePath("../LeftHipAttachment/Spatial"), NodePath("../LeftHandAttachment/LeftHandAP") ] + +[node name="RightWeapon" type="Spatial" parent="armature/Armature/Skeleton" index="11"] +transform = Transform( 0.993372, -0.0110498, 0.114411, 0.0109835, 0.999939, 0.00120952, -0.114417, 5.51299e-05, 0.993433, -0.141825, 0.959582, 0.16621 ) +script = ExtResource( 2 ) +positions = [ NodePath("../RightHipAttachment/Spatial"), NodePath("../RightHandAttachment/RightHandAP") ] + +[node name="LeftWeaponBack" type="Spatial" parent="armature/Armature/Skeleton" index="12"] +transform = Transform( 0.999994, -0.00350885, -0.00068694, 0.00350868, 0.999885, 0.0113409, 0.000647143, -0.0113414, 0.999936, 0.00185614, 1.1304, -0.100168 ) +script = ExtResource( 2 ) +positions = [ NodePath("../LeftBackAttachment/LeftBackAP"), NodePath("../LeftHandAttachment/LeftHandAP") ] + +[node name="Shield" type="Spatial" parent="armature/Armature/Skeleton" index="13"] +transform = Transform( 0.999994, -0.00350885, -0.00068694, 0.00350868, 0.999885, 0.0113409, 0.000647143, -0.0113414, 0.999936, 0.00185614, 1.1304, -0.100168 ) +script = ExtResource( 2 ) +positions = [ NodePath("../LeftBackAttachment/LeftBackAP"), NodePath("../ShieldAttachment/LeftHandShield") ] + +[node name="RightWeaponBack" type="Spatial" parent="armature/Armature/Skeleton" index="14"] +transform = Transform( 0.999997, -0.00244488, -0.000478373, 0.00244479, 0.999902, 0.0113564, 0.000450593, -0.0113563, 0.999935, 0.00159363, 1.13165, -0.122549 ) +script = ExtResource( 2 ) +positions = [ NodePath("../RightBackAttachment/RightBackAP"), NodePath("../RightHandAttachment/RightHandAP") ] + +[editable path="armature"] diff --git a/game/player/CharacterSkeletonMeshJobSimple.gd b/game/player/CharacterSkeletonMeshJobSimple.gd new file mode 100644 index 00000000..6914a3c5 --- /dev/null +++ b/game/player/CharacterSkeletonMeshJobSimple.gd @@ -0,0 +1,165 @@ +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. + +var final_mesh : ArrayMesh + +var _textures : Array +var material_cache : ESSMaterialCache = null +var material : Material + +var data : Array + +signal finished + +func _execute(): + prepare_textures() + + final_mesh = null + + 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() + # No octahedral compression for skinned meshes for now. Can cause bugs on some gpus + mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr, [], ArrayMesh.ARRAY_COMPRESS_NORMAL | ArrayMesh.ARRAY_COMPRESS_TANGENT | ArrayMesh.ARRAY_COMPRESS_COLOR | ArrayMesh.ARRAY_COMPRESS_TEX_UV | ArrayMesh.ARRAY_COMPRESS_TEX_UV2 | ArrayMesh.ARRAY_COMPRESS_WEIGHTS) + + #debug_arrays = arr.duplicate(true) + + mesh.surface_set_material(0, material_cache.material_lod_get(0)) + final_mesh = mesh + + emit_signal("finished") +# call_deferred("emit_signal", "finished") + complete = true + +func prepare_textures() -> void: + if !material_cache.initialized: + initialize_material_cache() + + var curr_tex_index : int = 0 + 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: + # merged texture. We need to get the texture itself + texture = material_cache.texture_get(curr_tex_index) + else: + for j in range(textures.size() - 1, -1, -1): + if textures[j]: + texture = textures[j] + break + + ddict["texture"] = texture + data[i] = ddict + + if texture != null: + curr_tex_index += 1 + +func initialize_material_cache() -> 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() + return + + 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 + + if texture != null: + material_cache.texture_add(texture) + + material_cache.refresh_rects() + material_cache.mutex_unlock() + +func reset(): + complete = false + final_mesh = null + _textures.clear() + material_cache = null + material = null + data.clear()