Implemented the Add Box button for the mesh data resource editor.

This commit is contained in:
Relintai 2021-12-29 00:00:28 +01:00
parent 290f8e39f1
commit 6337d16a54
5 changed files with 292 additions and 2 deletions

View File

@ -1,7 +1,7 @@
tool
extends PanelContainer
var plugin
var plugin : EditorPlugin
export var uv_editor_path : NodePath
@ -46,7 +46,7 @@ func _on_Extrude_pressed():
pass # Replace with function body.
func _on_AddBox_pressed():
pass # Replace with function body.
plugin.add_box()
func _on_UnwrapButton_pressed():
plugin.uv_unwrap()

View File

@ -206,6 +206,18 @@ margin_right = 146.0
margin_bottom = 20.0
text = "UV Unwrap"
[node name="GenNormals" type="Button" parent="VBoxContainer/Operations/Operations"]
margin_left = 150.0
margin_right = 246.0
margin_bottom = 20.0
text = "Gen Normals"
[node name="GenTangents" type="Button" parent="VBoxContainer/Operations/Operations"]
margin_left = 250.0
margin_right = 349.0
margin_bottom = 20.0
text = "Gen Tangents"
[node name="HSeparator3" type="HSeparator" parent="VBoxContainer"]
margin_top = 140.0
margin_right = 1010.0

View File

@ -1,6 +1,8 @@
tool
extends EditorPlugin
const MeshAddUtils = preload("res://addons/mesh_data_resource_editor/utilities/mesh_add_utils.gd")
const MdiGizmoPlugin = preload("res://addons/mesh_data_resource_editor/MDIGizmoPlugin.gd")
const MDIEdGui = preload("res://addons/mesh_data_resource_editor/MDIEd.tscn")
@ -112,6 +114,12 @@ func set_axis_z(on : bool) -> void:
for g in active_gizmos:
g.set_axis_z(on)
func get_mdr() -> MeshDataResource:
if current_mesh_data_instance:
return current_mesh_data_instance.mesh_data
return null
func uv_unwrap() -> void:
var mdr : MeshDataResource = null
@ -147,3 +155,9 @@ func forward_spatial_gui_input(index, camera, event):
return true
return false
func add_box() -> void:
var mdr : MeshDataResource = get_mdr()
if mdr:
MeshAddUtils.add_box(mdr)

View File

@ -0,0 +1,263 @@
extends Object
static func add_box(mdr : MeshDataResource) -> void:
var st : SurfaceTool = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
var sgn : float = 1
#z
for i in range(2):
st.add_uv(Vector2())
st.add_vertex(Vector3(-0.5, -0.5, sgn * 0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(-0.5, 0.5, sgn * 0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(0.5, 0.5, sgn * 0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(0.5, -0.5, sgn * 0.5))
sgn *= -1
#x
for i in range(2):
st.add_uv(Vector2())
st.add_vertex(Vector3(sgn * 0.5, -0.5, 0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(sgn * 0.5, 0.5, 0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(sgn * 0.5, 0.5, -0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(sgn * 0.5, -0.5, -0.5))
sgn *= -1
#y
for i in range(2):
st.add_uv(Vector2())
st.add_vertex(Vector3(-0.5, sgn * 0.5, 0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(-0.5, sgn * 0.5, -0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(0.5, sgn * 0.5, -0.5))
st.add_uv(Vector2())
st.add_vertex(Vector3(0.5, sgn * 0.5, 0.5))
sgn *= -1
var ind : int = 0
for i in range(3):
st.add_index(ind + 0)
st.add_index(ind + 1)
st.add_index(ind + 2)
st.add_index(ind + 2)
st.add_index(ind + 3)
st.add_index(ind + 0)
ind += 4
st.add_index(ind + 0)
st.add_index(ind + 2)
st.add_index(ind + 1)
st.add_index(ind + 0)
st.add_index(ind + 3)
st.add_index(ind + 2)
ind += 4
st.generate_normals()
merge_in_surface_tool(mdr, st)
static func merge_in_surface_tool(mdr : MeshDataResource, st : SurfaceTool, generate_normals_if_needed : bool = true, generate_tangents_if_needed : bool = true) -> void:
var arrays : Array = mdr.get_array()
if arrays.size() != ArrayMesh.ARRAY_MAX:
arrays.resize(ArrayMesh.ARRAY_MAX)
if generate_normals_if_needed && arrays[ArrayMesh.ARRAY_NORMAL] == null:
st.generate_normals()
if generate_tangents_if_needed && arrays[ArrayMesh.ARRAY_TANGENT] == null:
st.generate_tangents()
merge_in_arrays(mdr, st.commit_to_arrays())
static func merge_in_arrays(mdr : MeshDataResource, merge : Array) -> 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]
var merge_vertices : PoolVector3Array
var merge_normals : PoolVector3Array
var merge_tangents : PoolRealArray
var merge_colors : PoolColorArray
var merge_uv : PoolVector2Array
var merge_uv2 : PoolVector2Array
var merge_bones : PoolRealArray
var merge_weights : PoolRealArray
var merge_indices : PoolIntArray
if merge[ArrayMesh.ARRAY_VERTEX] != null:
merge_vertices = merge[ArrayMesh.ARRAY_VERTEX]
if merge[ArrayMesh.ARRAY_NORMAL] != null:
merge_normals = merge[ArrayMesh.ARRAY_NORMAL]
if merge[ArrayMesh.ARRAY_TANGENT] != null:
merge_tangents = merge[ArrayMesh.ARRAY_TANGENT]
if merge[ArrayMesh.ARRAY_COLOR] != null:
merge_colors = merge[ArrayMesh.ARRAY_COLOR]
if merge[ArrayMesh.ARRAY_TEX_UV] != null:
merge_uv = merge[ArrayMesh.ARRAY_TEX_UV]
if merge[ArrayMesh.ARRAY_TEX_UV2] != null:
merge_uv2 = merge[ArrayMesh.ARRAY_TEX_UV2]
if merge[ArrayMesh.ARRAY_BONES] != null:
merge_bones = merge[ArrayMesh.ARRAY_BONES]
if merge[ArrayMesh.ARRAY_WEIGHTS] != null:
merge_weights = merge[ArrayMesh.ARRAY_WEIGHTS]
if merge[ArrayMesh.ARRAY_INDEX] != null:
merge_indices = merge[ArrayMesh.ARRAY_INDEX]
#merge
var ovc : int = vertices.size()
vertices.append_array(merge_vertices)
if arrays[ArrayMesh.ARRAY_NORMAL] != null:
if merge_vertices.size() != merge_normals.size():
for i in range(merge_vertices.size()):
normals.append(Vector3())
else:
normals.append_array(merge_normals)
if arrays[ArrayMesh.ARRAY_TANGENT] != null:
if merge_vertices.size() != merge_tangents.size() * 4:
for i in range(merge_vertices.size()):
merge_tangents.append(0)
merge_tangents.append(0)
merge_tangents.append(0)
merge_tangents.append(0)
else:
tangents.append_array(merge_tangents)
if arrays[ArrayMesh.ARRAY_COLOR] != null:
if merge_vertices.size() != merge_colors.size():
for i in range(merge_colors.size()):
colors.append(Color())
else:
colors.append_array(merge_colors)
if arrays[ArrayMesh.ARRAY_TEX_UV] != null:
if merge_vertices.size() != merge_uv.size():
for i in range(merge_vertices.size()):
uv.append(Vector2())
else:
uv.append_array(merge_uv)
if arrays[ArrayMesh.ARRAY_TEX_UV2] != null:
if merge_vertices.size() != merge_uv2.size():
for i in range(merge_vertices.size()):
uv2.append(Vector2())
else:
uv2.append_array(merge_uv2)
if arrays[ArrayMesh.ARRAY_BONES] != null:
if merge_vertices.size() != merge_bones.size() * 4:
for i in range(merge_vertices.size()):
bones.append(0)
bones.append(0)
bones.append(0)
bones.append(0)
else:
bones.append_array(merge_bones)
if arrays[ArrayMesh.ARRAY_WEIGHTS] != null:
if merge_vertices.size() != merge_weights.size() * 4:
for i in range(merge_vertices.size()):
weights.append(0)
weights.append(0)
weights.append(0)
weights.append(0)
else:
weights.append_array(merge_weights)
for i in range(merge_indices.size()):
merge_indices[i] += ovc
indices.append_array(merge_indices)
#write back
arrays[ArrayMesh.ARRAY_VERTEX] = vertices
if arrays[ArrayMesh.ARRAY_NORMAL] != null:
arrays[ArrayMesh.ARRAY_NORMAL] = normals
if arrays[ArrayMesh.ARRAY_TANGENT] != null:
arrays[ArrayMesh.ARRAY_TANGENT] = tangents
if arrays[ArrayMesh.ARRAY_COLOR] != null:
arrays[ArrayMesh.ARRAY_COLOR] = colors
if arrays[ArrayMesh.ARRAY_TEX_UV] != null:
arrays[ArrayMesh.ARRAY_TEX_UV] = uv
if arrays[ArrayMesh.ARRAY_TEX_UV2] != null:
arrays[ArrayMesh.ARRAY_TEX_UV2] = uv2
if arrays[ArrayMesh.ARRAY_BONES] != null:
arrays[ArrayMesh.ARRAY_BONES] = bones
if arrays[ArrayMesh.ARRAY_WEIGHTS] != null:
arrays[ArrayMesh.ARRAY_WEIGHTS] = weights
arrays[ArrayMesh.ARRAY_INDEX] = indices
mdr.set_array(arrays)

View File

@ -3,6 +3,7 @@
[ext_resource path="res://modules/planets/test_planet/voxel_library/textures/grass_mossy_albedo.png" type="Texture" id=1]
[sub_resource type="MeshDataResource" id=1]
aabb = AABB( -0.5, -0.5, -1.025, 1, 1, 1.525 )
[sub_resource type="SpatialMaterial" id=2]
albedo_texture = ExtResource( 1 )