2020-05-30 19:25:32 +02:00
|
|
|
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
|
|
|
|
|
2021-04-15 11:52:32 +02:00
|
|
|
# Copyright (c) 2019-2021 Péter Magyar
|
2020-05-30 19:25:32 +02: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.
|
|
|
|
|
|
|
|
export(bool) var use_lod : bool = true
|
|
|
|
|
|
|
|
var meshes : Array
|
|
|
|
|
2022-02-16 20:52:50 +01:00
|
|
|
#var debug_arrays : Array
|
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
var _generating : bool = false
|
|
|
|
|
|
|
|
var _textures : Array
|
2021-08-26 00:15:30 +02:00
|
|
|
var material_cache : ESSMaterialCache = null
|
2020-05-30 19:25:32 +02:00
|
|
|
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"]
|
2021-08-26 00:15:30 +02:00
|
|
|
#var atlas_texture : AtlasTexture = ddict["atlas_texture"]
|
2020-05-30 19:25:32 +02:00
|
|
|
var transform : Transform = ddict["transform"]
|
|
|
|
var mesh : MeshDataResource = ddict["mesh"]
|
|
|
|
|
2021-08-26 00:15:30 +02:00
|
|
|
var rect : Rect2 = material_cache.texture_get_uv_rect(texture)
|
2022-02-16 20:52:50 +01:00
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
bones[0] = bone_idx
|
|
|
|
|
|
|
|
mm.add_mesh_data_resource_bone(mesh, bones, bonew, transform, rect)
|
|
|
|
|
|
|
|
var arr : Array = mm.build_mesh()
|
2021-07-19 18:56:59 +02:00
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
var mesh : ArrayMesh = ArrayMesh.new()
|
2022-03-24 21:58:15 +01:00
|
|
|
# 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)
|
2022-02-16 20:52:50 +01:00
|
|
|
|
|
|
|
#debug_arrays = arr.duplicate(true)
|
|
|
|
|
2021-08-26 00:15:30 +02:00
|
|
|
mesh.surface_set_material(0, material_cache.material_lod_get(0))
|
2020-05-30 19:25:32 +02:00
|
|
|
meshes.append(mesh)
|
|
|
|
|
|
|
|
if use_lod:
|
|
|
|
arr = MeshUtils.merge_mesh_array(arr)
|
|
|
|
var meshl2 : ArrayMesh = ArrayMesh.new()
|
2022-03-24 21:58:15 +01:00
|
|
|
meshl2.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)
|
2021-08-26 00:15:30 +02:00
|
|
|
meshl2.surface_set_material(0, material_cache.material_lod_get(1))
|
2020-05-30 19:25:32 +02:00
|
|
|
meshes.append(meshl2)
|
2021-08-26 00:15:30 +02:00
|
|
|
|
|
|
|
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")
|
2020-05-30 19:25:32 +02:00
|
|
|
|
2021-08-26 00:15:30 +02:00
|
|
|
arr = MeshUtils.bake_mesh_array_uv(arr, texture)
|
2020-05-30 19:25:32 +02:00
|
|
|
arr[VisualServer.ARRAY_TEX_UV] = null
|
|
|
|
var meshl3 : ArrayMesh = ArrayMesh.new()
|
2022-03-24 21:58:15 +01:00
|
|
|
meshl3.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)
|
2021-08-26 00:15:30 +02:00
|
|
|
meshl3.surface_set_material(0, material_cache.material_lod_get(2))
|
2020-05-30 19:25:32 +02:00
|
|
|
meshes.append(meshl3)
|
2020-05-30 23:23:38 +02:00
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
emit_signal("finished")
|
2020-05-30 23:23:38 +02:00
|
|
|
# call_deferred("emit_signal", "finished")
|
|
|
|
complete = true
|
2020-05-30 19:25:32 +02:00
|
|
|
|
|
|
|
func prepare_textures() -> void:
|
2022-02-16 20:52:50 +01:00
|
|
|
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:
|
2021-08-26 00:15:30 +02:00
|
|
|
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()
|
2022-02-16 20:52:50 +01:00
|
|
|
return
|
2020-05-30 19:25:32 +02:00
|
|
|
|
2020-09-15 11:36:08 +02:00
|
|
|
var lmerger : TextureLayerMerger = TextureLayerMerger.new()
|
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
for i in range(data.size()):
|
|
|
|
var ddict : Dictionary = data[i]
|
2020-09-15 11:36:08 +02:00
|
|
|
var textures : Array = ddict["textures"]
|
|
|
|
|
|
|
|
var texture : Texture = null
|
|
|
|
var tcount : int = 0
|
|
|
|
for j in range(textures.size()):
|
|
|
|
if textures[j]:
|
|
|
|
tcount += 1
|
2020-05-30 19:25:32 +02:00
|
|
|
|
2020-09-15 11:36:08 +02:00
|
|
|
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
|
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
if texture != null:
|
2022-02-16 20:52:50 +01:00
|
|
|
material_cache.texture_add(texture)
|
|
|
|
|
|
|
|
material_cache.refresh_rects()
|
|
|
|
material_cache.mutex_unlock()
|
|
|
|
|
2020-05-30 19:25:32 +02:00
|
|
|
|