Material cache support for the character.

This commit is contained in:
Relintai 2021-08-26 00:15:30 +02:00
parent 44a2507f4c
commit 4f02de36d9
4 changed files with 57 additions and 64 deletions

View File

@ -1,12 +1,10 @@
[gd_scene load_steps=44 format=2]
[gd_scene load_steps=42 format=2]
[ext_resource path="res://modules/core/model_visuals/test_base_v3.tres" type="ModelVisual" id=1]
[ext_resource path="res://player/CharacterSkeletonAttachPoint.gd" type="Script" id=2]
[ext_resource path="res://modules/core/materials/3_standard_character_material.tres" type="Material" id=3]
[ext_resource path="res://modules/species/Human/models/dead.tres" type="Animation" id=4]
[ext_resource path="res://modules/species/Human/models/armature.gd" type="Script" id=5]
[ext_resource path="res://modules/species/Human/models/armature.gltf" type="PackedScene" id=6]
[ext_resource path="res://modules/core/materials/9_standard_character_material_notex.tres" type="Material" id=7]
[ext_resource path="res://player/CharacterSkeletonGD.gd" type="Script" id=8]
[ext_resource path="res://modules/species/Human/models/idle-loop.tres" type="Animation" id=9]
[ext_resource path="res://modules/species/Human/models/rest.tres" type="Animation" id=10]
@ -133,7 +131,7 @@ attach_point_paths/2_torso = NodePath("armature/Armature/Skeleton/NeckAttachment
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("armature/Armature/Skeleton/BoneAttachment/Spine2AP")
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")
@ -143,7 +141,6 @@ script = ExtResource( 8 )
automatic_build = true
mesh_instance_path = NodePath("armature/Armature/Skeleton/MeshInstance")
skeleton_path = NodePath("armature/Armature/Skeleton")
materials = [ ExtResource( 3 ), ExtResource( 3 ), ExtResource( 7 ) ]
viss = [ ExtResource( 1 ) ]
[node name="armature" parent="." instance=ExtResource( 6 )]
@ -163,7 +160,7 @@ bones/27/bound_children = [ NodePath("NeckAttachment") ]
visible = false
[node name="LeftHandAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="1"]
transform = Transform( 0.166537, 0.128508, -0.977625, 0.0478394, -0.991356, -0.122164, -0.984873, -0.0264241, -0.171246, 0.236768, 0.767375, -0.00607332 )
transform = Transform( 0.190725, 0.147102, -0.970559, 0.0931773, -0.986957, -0.131278, -0.97721, -0.065396, -0.201944, 0.244939, 0.774361, -0.0217047 )
bone_name = "left_hand"
[node name="LeftHandAP" type="Spatial" parent="armature/Armature/Skeleton/LeftHandAttachment"]
@ -171,14 +168,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.0184403, 0.10685, 0.994104, -0.034023, -0.993765, 0.106183, 0.99925, -0.0318643, 0.0219607, 0.211053, 1.00654, 0.00159548 )
transform = Transform( -0.0433176, 0.125638, 0.99113, -0.0774754, -0.989496, 0.122045, 0.996052, -0.0715015, 0.0525965, 0.214702, 1.0125, -0.00449639 )
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.123816, -0.0747699, -0.989484, -0.0158104, -0.996882, 0.0773075, -0.99218, 0.025216, 0.122248, -0.247354, 0.768532, 0.00305579 )
transform = Transform( -0.0270232, -0.0924788, -0.995348, -0.0997119, -0.990496, 0.0947352, -0.99465, 0.101808, 0.0175453, -0.248237, 0.77238, -0.0111511 )
bone_name = "right_hand"
[node name="RightHandAP" type="Spatial" parent="armature/Armature/Skeleton/RightHandAttachment"]
@ -194,7 +191,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.999962, -0.008224, -0.00275978, 0.00839928, 0.99744, 0.0710206, 0.00216864, -0.0710411, 0.997471, 0.000932109, 1.31296, -0.00128597 )
transform = Transform( 0.999992, 0.00218317, -0.00333621, -0.00195909, 0.997835, 0.0657461, 0.00347252, -0.065739, 0.997831, 0.000876503, 1.31821, 0.00123351 )
bone_name = "neck"
[node name="TorsoAP" type="Spatial" parent="armature/Armature/Skeleton/NeckAttachment"]
@ -202,28 +199,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.999866, 0.0163955, 7.77678e-09, -0.0163955, 0.999866, 4.71791e-07, -4.04814e-11, -4.71855e-07, 1, 0.00597917, 0.771599, 0 )
transform = Transform( 0.999504, 0.0314799, 1.49316e-08, -0.0314799, 0.999504, 4.7162e-07, -7.76685e-11, -4.71856e-07, 1, 0, 0.776724, 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.999866, 0.0163955, 7.77678e-09, -0.0163955, 0.999866, 4.71791e-07, -4.04814e-11, -4.71855e-07, 1, 0.00597917, 0.771599, 0 )
transform = Transform( 0.999504, 0.0314799, 1.49316e-08, -0.0314799, 0.999504, 4.7162e-07, -7.76685e-11, -4.71856e-07, 1, 0, 0.776724, 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.99996, -0.00867777, -0.00202415, 0.00867132, 0.999957, -0.00318031, 0.00205166, 0.00316263, 0.999993, 0.00256003, 1.12537, -0.00187927 )
transform = Transform( 0.999993, 0.00165794, -0.00337115, -0.00168643, 0.999963, -0.00847085, 0.00335698, 0.00847647, 0.999959, 0.000565477, 1.13062, -0.000356666 )
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.99996, -0.00867777, -0.00202415, 0.00867132, 0.999957, -0.00318031, 0.00205166, 0.00316263, 0.999993, 0.00256003, 1.12537, -0.00187927 )
transform = Transform( 0.999993, 0.00165794, -0.00337115, -0.00168643, 0.999963, -0.00847085, 0.00335698, 0.00847647, 0.999959, 0.000565477, 1.13062, -0.000356666 )
bone_name = "spine_2"
[node name="LeftBackAP" type="Spatial" parent="armature/Armature/Skeleton/LeftBackAttachment"]

View File

@ -36,9 +36,6 @@ var mesh_instance : MeshInstance = null
export(NodePath) var skeleton_path : NodePath
var skeleton : Skeleton
export(Array, Material) var materials : Array
var _materials : Array = Array()
export(Array, ModelVisual) var viss : Array
var meshes : Array
@ -48,6 +45,7 @@ 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",
@ -102,12 +100,6 @@ func _enter_tree():
skeleton = get_node(skeleton_path) as Skeleton
mesh_instance = get_node(mesh_instance_path) as MeshInstance
if _materials.size() != materials.size():
for m in materials:
_materials.append(m.duplicate())
_mesh_job.materials = _materials
set_process(false)
if Engine.editor_hint:
@ -219,6 +211,19 @@ func build():
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()
@ -250,7 +255,6 @@ func finish_build_mesh() -> void:
func job_finished():
meshes = _mesh_job.meshes
_texture = _mesh_job._texture
mesh_instance.mesh = meshes[_current_lod_level]
if !mesh_instance.is_software_skinning_enabled():

View File

@ -30,21 +30,14 @@ var meshes : Array
var _generating : bool = false
var _texture_packer : TexturePacker
var _textures : Array
var _texture : Texture
var material_cache : ESSMaterialCache = null
var materials : Array = Array()
var data : Array
signal finished
func _init():
_texture_packer = TexturePacker.new()
_texture_packer.texture_flags = 0
# _texture_packer.texture_flags = Texture.FLAG_FILTER
_texture_packer.max_atlas_size = 512
func _execute():
prepare_textures()
@ -69,20 +62,11 @@ func _execute():
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 atlas_texture : AtlasTexture = ddict["atlas_texture"]
var transform : Transform = ddict["transform"]
var mesh : MeshDataResource = ddict["mesh"]
var rect : Rect2
if atlas_texture != null and texture != null:
var otw : float = _texture.get_width()
var oth : float = _texture.get_height()
rect.position.x = atlas_texture.region.position.x / otw
rect.position.y = atlas_texture.region.position.y / oth
rect.size.x = atlas_texture.region.size.x / otw
rect.size.y = atlas_texture.region.size.y / oth
var rect : Rect2 = material_cache.texture_get_uv_rect(texture)
bones[0] = bone_idx
@ -92,21 +76,32 @@ func _execute():
var mesh : ArrayMesh = ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
mesh.surface_set_material(0, materials[0])
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, materials[1])
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)
arr = MeshUtils.bake_mesh_array_uv(arr, _texture)
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, materials[2])
meshl3.surface_set_material(0, material_cache.material_lod_get(2))
meshes.append(meshl3)
emit_signal("finished")
@ -114,7 +109,13 @@ func _execute():
complete = true
func prepare_textures() -> void:
_texture_packer.clear()
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()
@ -146,22 +147,11 @@ func prepare_textures() -> void:
ddict["texture"] = texture
if texture != null:
ddict["atlas_texture"] = _texture_packer.add_texture(texture)
if !material_cache.initialized:
material_cache.texture_add(texture)
data[i] = ddict
_texture_packer.merge()
var tex : Texture = _texture_packer.get_generated_texture(0)
# var mat : SpatialMaterial = _material as SpatialMaterial
# mat.albedo_texture = tex
var mat : ShaderMaterial = materials[0] as ShaderMaterial
mat.set_shader_param("texture_albedo", tex)
if use_lod:
var mat2 : ShaderMaterial = materials[1] as ShaderMaterial
mat2.set_shader_param("texture_albedo", tex)
# mat.albedo_texture = tex
_texture = tex
if !material_cache.initialized:
material_cache.refresh_rects()
material_cache.mutex_unlock()

View File

@ -302,6 +302,8 @@ enums/stats="Agility,Strength,Stamina,Intellect,Spirit,Health,Mana,Speed,Global
enums/skeletons_bones=PoolStringArray( "", "root,pelvis,spine,spine_1,spine_2,neck,head,left_clavicle,left_upper_arm,left_forearm,left_hand,left_thumb_base,left_thumb_end,left_fingers_base,left_fingers_end,right_clavicle,right_upper_arm,right_forearm,right_hand,right_thumb_base,right_thumb_end,right_fingers_base,right_fingers_end,left_thigh,left_calf,left_foot,right_thigh,right_calf,right_foot" )
enums/skeletons_bone_attachment_points=PoolStringArray( "", "left_hand,right_hand,torso,root,right_hip,left_hip,spine_2,weapon_left,weapon_right,weapon_left_back,weapon_right_back,weapon_shield_left" )
xp/character_xps=PoolIntArray( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )
material_cache/texture_flags=1
material_cache/material_paths=PoolStringArray( "res://modules/core/materials/3_standard_character_material.tres", "res://modules/core/materials/3_standard_character_material.tres", "res://modules/core/materials/9_standard_character_material_notex.tres" )
profiles/automatic_save=true
data/ess_entity_spawner_path="res://player/bs_entity_spawner.tres"
level/max_character_level=60