Implemented generate normals, remove doubles, merge optimize and generate tangent actions.

This commit is contained in:
Relintai 2022-01-15 15:53:08 +01:00
parent 76d5818c34
commit 69ba7ac15a
5 changed files with 202 additions and 0 deletions

View File

@ -162,3 +162,15 @@ func _oncreate_face_pressed():
func _on_delete_pressed():
plugin.delete_selected()
func _on_GenNormals_pressed():
plugin.generate_normals()
func _on_RemDoubles_pressed():
plugin.remove_doubles()
func _on_MergeOptimize_pressed():
plugin.merge_optimize()
func _on_GenTangents_pressed():
plugin.generate_tangents()

View File

@ -357,6 +357,12 @@ margin_right = 1010.0
margin_bottom = 92.0
text = "Rem Doubles"
[node name="MergeOptimize" type="Button" parent="VBoxContainer/Operations/Operations"]
margin_top = 96.0
margin_right = 1010.0
margin_bottom = 116.0
text = "MergeOptim"
[node name="HSeparator3" type="HSeparator" parent="VBoxContainer"]
margin_top = 350.0
margin_right = 1010.0
@ -483,6 +489,10 @@ script = ExtResource( 3 )
[connection signal="pressed" from="VBoxContainer/FaceOps/Operations/Connect" to="." method="_on_connect_pressed"]
[connection signal="pressed" from="VBoxContainer/FaceOps/Operations/Disconnect" to="." method="_on_disconnect_pressed"]
[connection signal="pressed" from="VBoxContainer/Operations/Operations/UnwrapButton" to="." method="_on_UnwrapButton_pressed"]
[connection signal="pressed" from="VBoxContainer/Operations/Operations/GenNormals" to="." method="_on_GenNormals_pressed"]
[connection signal="pressed" from="VBoxContainer/Operations/Operations/GenTangents" to="." method="_on_GenTangents_pressed"]
[connection signal="pressed" from="VBoxContainer/Operations/Operations/RemDoubles" to="." method="_on_RemDoubles_pressed"]
[connection signal="pressed" from="VBoxContainer/Operations/Operations/MergeOptimize" to="." method="_on_MergeOptimize_pressed"]
[connection signal="pressed" from="VBoxContainer/Add/Add/AddBox" to="." method="_on_AddBox_pressed"]
[connection signal="pressed" from="VBoxContainer/Add/Add/Tri/AddTriangle" to="." method="_on_add_triangle_pressed"]
[connection signal="pressed" from="VBoxContainer/Add/Add/Tri/At" to="." method="_on_add_triangle_at_pressed"]

View File

@ -745,3 +745,42 @@ func delete_selected() -> void:
on_mdr_changed()
func generate_normals():
if !_mdr:
return
MDRMeshUtils.generate_normals(_mdr)
func generate_tangents():
if !_mdr:
return
MDRMeshUtils.generate_tangents(_mdr)
func remove_doubles():
if !_mdr:
return
var mdr_arr : Array = _mdr.array
if mdr_arr.size() != ArrayMesh.ARRAY_MAX || mdr_arr[ArrayMesh.ARRAY_VERTEX] == null || mdr_arr[ArrayMesh.ARRAY_VERTEX].size() == 0:
return
var merged_arrays : Array = MeshUtils.remove_doubles(mdr_arr)
_mdr.array = merged_arrays
func merge_optimize():
if !_mdr:
return
var mdr_arr : Array = _mdr.array
if mdr_arr.size() != ArrayMesh.ARRAY_MAX || mdr_arr[ArrayMesh.ARRAY_VERTEX] == null || mdr_arr[ArrayMesh.ARRAY_VERTEX].size() == 0:
return
var merged_arrays : Array = MeshUtils.merge_mesh_array(mdr_arr)
_mdr.array = merged_arrays

View File

@ -213,3 +213,19 @@ func create_face():
func delete_selected():
for g in active_gizmos:
g.delete_selected()
func generate_normals():
for g in active_gizmos:
g.generate_normals()
func remove_doubles():
for g in active_gizmos:
g.remove_doubles()
func merge_optimize():
for g in active_gizmos:
g.merge_optimize()
func generate_tangents():
for g in active_gizmos:
g.generate_tangents()

View File

@ -527,3 +527,128 @@ static func flip_triangle(mdr : MeshDataResource, index : int) -> void:
mdr.set_array(arrays)
static func add_into_surface_tool(mdr : MeshDataResource, st : SurfaceTool) -> void:
var arrays : Array = mdr.get_array()
if arrays.size() != ArrayMesh.ARRAY_MAX:
arrays.resize(ArrayMesh.ARRAY_MAX)
var vertices : PoolVector3Array
var normals : PoolVector3Array
var tangents : PoolRealArray
var colors : PoolColorArray
var uv : PoolVector2Array
var uv2 : PoolVector2Array
var bones : PoolRealArray
var weights : PoolRealArray
var indices : PoolIntArray
if arrays[ArrayMesh.ARRAY_VERTEX] != null:
vertices = arrays[ArrayMesh.ARRAY_VERTEX]
if arrays[ArrayMesh.ARRAY_NORMAL] != null:
normals = arrays[ArrayMesh.ARRAY_NORMAL]
if arrays[ArrayMesh.ARRAY_TANGENT] != null:
tangents = arrays[ArrayMesh.ARRAY_TANGENT]
if arrays[ArrayMesh.ARRAY_COLOR] != null:
colors = arrays[ArrayMesh.ARRAY_COLOR]
if arrays[ArrayMesh.ARRAY_TEX_UV] != null:
uv = arrays[ArrayMesh.ARRAY_TEX_UV]
if arrays[ArrayMesh.ARRAY_TEX_UV2] != null:
uv2 = arrays[ArrayMesh.ARRAY_TEX_UV2]
if arrays[ArrayMesh.ARRAY_BONES] != null:
bones = arrays[ArrayMesh.ARRAY_BONES]
if arrays[ArrayMesh.ARRAY_WEIGHTS] != null:
weights = arrays[ArrayMesh.ARRAY_WEIGHTS]
if arrays[ArrayMesh.ARRAY_INDEX] != null:
indices = arrays[ArrayMesh.ARRAY_INDEX]
for i in range(vertices.size()):
if normals.size() > 0:
st.add_normal(normals[i])
if tangents.size() > 0:
var ti : int = i * 4
st.add_tangent(Plane(tangents[ti], tangents[ti + 1], tangents[ti + 2], tangents[ti + 3]))
if colors.size() > 0:
st.add_color(colors[i])
if uv.size() > 0:
st.add_uv(uv[i])
if uv2.size() > 0:
st.add_uv2(uv2[i])
if bones.size() > 0:
var bi : int = i * 4
var pia : PoolIntArray = PoolIntArray()
pia.append(bones[bi])
pia.append(bones[bi + 1])
pia.append(bones[bi + 1])
pia.append(bones[bi + 1])
st.add_bones(pia)
if weights.size() > 0:
var bi : int = i * 4
var pia : PoolIntArray = PoolIntArray()
pia.append(bones[bi])
pia.append(bones[bi + 1])
pia.append(bones[bi + 2])
pia.append(bones[bi + 3])
st.add_weight(pia)
st.add_vertex(vertices[i])
for ind in indices:
st.add_index(ind)
static func generate_normals(mdr : MeshDataResource) -> void:
var arrays : Array = mdr.get_array()
if arrays.size() != ArrayMesh.ARRAY_MAX:
arrays.resize(ArrayMesh.ARRAY_MAX)
if arrays[ArrayMesh.ARRAY_INDEX] == null:
return
var st : SurfaceTool = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
add_into_surface_tool(mdr, st)
st.generate_normals()
mdr.array = st.commit_to_arrays()
static func generate_tangents(mdr : MeshDataResource) -> void:
var arrays : Array = mdr.get_array()
if arrays.size() != ArrayMesh.ARRAY_MAX:
arrays.resize(ArrayMesh.ARRAY_MAX)
if arrays[ArrayMesh.ARRAY_INDEX] == null:
return
var st : SurfaceTool = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
add_into_surface_tool(mdr, st)
st.generate_tangents()
mdr.array = st.commit_to_arrays()