Refactored the mesh outline generator a bit. Also made it offset the outline along the normal if available to reduce z-fighting.

This commit is contained in:
Relintai 2022-01-18 21:34:25 +01:00
parent 302e0a9680
commit 272e34a06a

View File

@ -6,71 +6,85 @@ var _mdr : MeshDataResource
var lines : PoolVector3Array var lines : PoolVector3Array
var seam_lines : PoolVector3Array var seam_lines : PoolVector3Array
var _vertices : PoolVector3Array = PoolVector3Array()
var _normals : PoolVector3Array = PoolVector3Array()
var _indices : PoolIntArray = PoolIntArray()
func setup(mdr : MeshDataResource) -> void: func setup(mdr : MeshDataResource) -> void:
_mdr = mdr _mdr = mdr
func generate(): func reset() -> void:
lines.resize(0) lines.resize(0)
seam_lines.resize(0) seam_lines.resize(0)
_normals.resize(0)
_vertices.resize(0)
_indices.resize(0)
func initialize() -> bool:
if !_mdr: if !_mdr:
return return false
if _mdr.array.size() != ArrayMesh.ARRAY_MAX: if _mdr.array.size() != ArrayMesh.ARRAY_MAX:
return return false
if _mdr.array[ArrayMesh.ARRAY_VERTEX] == null || _mdr.array[ArrayMesh.ARRAY_INDEX] == null: if _mdr.array[ArrayMesh.ARRAY_VERTEX] == null || _mdr.array[ArrayMesh.ARRAY_INDEX] == null:
return return false
var arr : Array = _mdr.array var arr : Array = _mdr.array
var vertices : PoolVector3Array = arr[ArrayMesh.ARRAY_VERTEX] _vertices = arr[ArrayMesh.ARRAY_VERTEX]
var indices : PoolIntArray = arr[ArrayMesh.ARRAY_INDEX]
if vertices.size() == 0: if _mdr.array[ArrayMesh.ARRAY_NORMAL] != null:
_normals = arr[ArrayMesh.ARRAY_NORMAL]
_indices = arr[ArrayMesh.ARRAY_INDEX]
if _vertices.size() == 0:
return false
return true
func get_vertex(index : int) -> Vector3:
var v : Vector3 = _vertices[index]
# This should reduce z fighting
if _normals.size() > 0:
var n : Vector3 = _normals[index]
v += n * 0.001
return v
func generate():
reset()
if !initialize():
return return
if indices.size() % 3 == 0: for i in range(0, _indices.size(), 3):
for i in range(0, indices.size(), 3):
for j in range(3): for j in range(3):
lines.append(vertices[indices[i + j]]) lines.append(get_vertex(_indices[i + j]))
lines.append(vertices[indices[i + ((j + 1) % 3)]]) lines.append(get_vertex(_indices[i + ((j + 1) % 3)]))
var seams : PoolIntArray = _mdr.seams var seams : PoolIntArray = _mdr.seams
for i in range(0, seams.size(), 2): for i in range(0, seams.size(), 2):
seam_lines.append(vertices[seams[i]]) seam_lines.append(get_vertex(seams[i]))
seam_lines.append(vertices[seams[i + 1]]) seam_lines.append(get_vertex(seams[i + 1]))
func generate_mark_edges(): func generate_mark_edges():
lines.resize(0) reset()
seam_lines.resize(0)
if !_mdr: if !initialize():
return return
if _mdr.array.size() != ArrayMesh.ARRAY_MAX: for i in range(0, _indices.size(), 3):
return
if _mdr.array[ArrayMesh.ARRAY_VERTEX] == null || _mdr.array[ArrayMesh.ARRAY_INDEX] == null:
return
var arr : Array = _mdr.array
var vertices : PoolVector3Array = arr[ArrayMesh.ARRAY_VERTEX]
var indices : PoolIntArray = arr[ArrayMesh.ARRAY_INDEX]
if vertices.size() == 0:
return
if indices.size() % 3 == 0:
for i in range(0, indices.size(), 3):
for j in range(3): for j in range(3):
var i0 : int = indices[i + j] var i0 : int = _indices[i + j]
var i1 : int = indices[i + ((j + 1) % 3)] var i1 : int = _indices[i + ((j + 1) % 3)]
var v0 : Vector3 = vertices[i0] var v0 : Vector3 = get_vertex(i0)
var v1 : Vector3 = vertices[i1] var v1 : Vector3 = get_vertex(i1)
lines.append(v0) lines.append(v0)
lines.append(v1) lines.append(v1)
@ -89,46 +103,28 @@ func generate_mark_edges():
var seams : PoolIntArray = _mdr.seams var seams : PoolIntArray = _mdr.seams
for i in range(0, seams.size(), 2): for i in range(0, seams.size(), 2):
seam_lines.append(vertices[seams[i]]) seam_lines.append(get_vertex(seams[i]))
seam_lines.append(vertices[seams[i + 1]]) seam_lines.append(get_vertex(seams[i + 1]))
func generate_mark_faces(): func generate_mark_faces():
lines.resize(0) reset()
seam_lines.resize(0)
if !_mdr: if !initialize():
return return
if _mdr.array.size() != ArrayMesh.ARRAY_MAX: for i in range(0, _indices.size(), 3):
return
if _mdr.array[ArrayMesh.ARRAY_VERTEX] == null || _mdr.array[ArrayMesh.ARRAY_INDEX] == null:
return
var arr : Array = _mdr.array
var vertices : PoolVector3Array = arr[ArrayMesh.ARRAY_VERTEX]
var indices : PoolIntArray = arr[ArrayMesh.ARRAY_INDEX]
if vertices.size() == 0:
return
if indices.size() % 3 != 0:
return
for i in range(0, indices.size(), 3):
for j in range(3): for j in range(3):
lines.append(vertices[indices[i + j]]) lines.append(get_vertex(_indices[i + j]))
lines.append(vertices[indices[i + ((j + 1) % 3)]]) lines.append(get_vertex(_indices[i + ((j + 1) % 3)]))
for i in range(0, indices.size(), 3): for i in range(0, _indices.size(), 3):
var i0 : int = indices[i + 0] var i0 : int = _indices[i + 0]
var i1 : int = indices[i + 1] var i1 : int = _indices[i + 1]
var i2 : int = indices[i + 2] var i2 : int = _indices[i + 2]
var v0 : Vector3 = vertices[i0] var v0 : Vector3 = get_vertex(i0)
var v1 : Vector3 = vertices[i1] var v1 : Vector3 = get_vertex(i1)
var v2 : Vector3 = vertices[i2] var v2 : Vector3 = get_vertex(i2)
var pmid : Vector3 = v0 + v1 + v2 var pmid : Vector3 = v0 + v1 + v2
pmid /= 3 pmid /= 3
@ -145,5 +141,5 @@ func generate_mark_faces():
var seams : PoolIntArray = _mdr.seams var seams : PoolIntArray = _mdr.seams
for i in range(0, seams.size(), 2): for i in range(0, seams.size(), 2):
seam_lines.append(vertices[seams[i]]) seam_lines.append(get_vertex(seams[i]))
seam_lines.append(vertices[seams[i + 1]]) seam_lines.append(get_vertex(seams[i + 1]))