Added a few helper methods to mesh add utils.

This commit is contained in:
Relintai 2022-01-14 13:03:11 +01:00
parent da5992fc94
commit 6dfb97cb39

View File

@ -1,6 +1,85 @@
tool
extends Object
static func get_face_normal_arr_ti(verts : PoolVector3Array, indices : PoolIntArray, triangle_index : int, flipped : bool = false) -> Vector3:
return get_face_normal_arr(verts, indices, triangle_index * 3, flipped)
static func get_face_normal_arr(verts : PoolVector3Array, indices : PoolIntArray, index : int, flipped : bool = false) -> Vector3:
var v0 : Vector3 = verts[indices[index]]
var v1 : Vector3 = verts[indices[index + 1]]
var v2 : Vector3 = verts[indices[index + 2]]
return get_face_normal(v0, v1, v2, flipped)
static func get_face_normal(v0 : Vector3, v1 : Vector3, v2 : Vector3, flipped : bool = false) -> Vector3:
if !flipped:
return Plane(v0, v1, v2).normal
else:
return Plane(v2, v1, v0).normal
static func should_triangle_flip(v0 : Vector3, v1 : Vector3, v2 : Vector3, similar_dir_normal : Vector3) -> bool:
var normal : Vector3 = get_face_normal(v0, v1, v2)
var ndns : float = normal.dot(similar_dir_normal)
return ndns < 0
static func is_normal_similar(v0 : Vector3, v1 : Vector3, v2 : Vector3, similar_dir_normal : Vector3) -> bool:
var normal : Vector3 = get_face_normal(v0, v1, v2)
var ndns : float = normal.dot(similar_dir_normal)
return ndns >= 0
static func flip_triangle_ti(mdr : MeshDataResource, triangle_index : int) -> void:
flip_triangle(mdr, triangle_index * 3)
static func flip_triangle(mdr : MeshDataResource, index : int) -> 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 indices : PoolIntArray = arrays[ArrayMesh.ARRAY_INDEX]
var i0 : int = indices[index]
var i2 : int = indices[index + 2]
indices[index] = i2
indices[index + 2] = i0
arrays[ArrayMesh.ARRAY_INDEX] = indices
mdr.set_array(arrays)
static func add_triangle_at(mdr : MeshDataResource, v0 : Vector3, v1 : Vector3, v2 : Vector3, flip : bool = false) -> void:
var st : SurfaceTool = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.add_uv(Vector2(0, 1))
st.add_vertex(v0)
st.add_uv(Vector2(0.5, 0))
st.add_vertex(v1)
st.add_uv(Vector2(1, 1))
st.add_vertex(v2)
if !flip:
st.add_index(0)
st.add_index(1)
st.add_index(2)
else:
st.add_index(2)
st.add_index(1)
st.add_index(0)
st.generate_normals()
merge_in_surface_tool(mdr, st)
static func add_triangle(mdr : MeshDataResource) -> void:
var st : SurfaceTool = SurfaceTool.new()