Implemented handle normal calculation, and facing checks based on them for clicking. This doesn't work well, so it will be removed, but committing it because the normal calculations might come in handy later,.

This commit is contained in:
Relintai 2022-01-25 15:52:51 +01:00
parent a5cb7ab767
commit 78993c214f
2 changed files with 77 additions and 2 deletions

View File

@ -60,6 +60,7 @@ var _mdr : MeshDataResource = null
var _vertices : PoolVector3Array
var _indices : PoolIntArray
var _handle_points : PoolVector3Array
var _handle_normals : PoolVector3Array
var _handle_to_vertex_map : Array
var _selected_points : PoolIntArray
@ -228,18 +229,26 @@ func selection_click_select_front_or_back(index, camera, event):
var closest_dist : float = 1e10
for i in range(_handle_points.size()):
var is_dir_similar : bool = MDRMeshUtils.is_direction_similar(_handle_normals[i], _last_known_camera_facing)
if handle_selection_type == HandleSelectionType.HANDLE_SELECTION_TYPE_FRONT:
if is_dir_similar:
continue
elif handle_selection_type == HandleSelectionType.HANDLE_SELECTION_TYPE_BACK:
if !is_dir_similar:
continue
var vert_pos_3d : Vector3 = gt.xform(_handle_points[i])
var vert_pos_2d : Vector2 = camera.unproject_position(vert_pos_3d)
var dist_3d : float = ray_from.distance_to(vert_pos_3d)
var dist_2d : float = gpoint.distance_to(vert_pos_2d)
if (dist_2d < grab_threshold && dist_3d < closest_dist):
closest_dist = dist_3d
closest_idx = i
if (closest_idx >= 0):
for si in range(_selected_points.size()):
if _selected_points[si] == closest_idx:
if event.alt || event.control:
_selected_points.remove(si)
@ -598,6 +607,7 @@ func recalculate_handle_points() -> void:
if !_mdr:
_handle_points.resize(0)
_handle_to_vertex_map.resize(0)
_handle_normals.resize(0)
return
var mdr_arr : Array = _mdr.array
@ -605,6 +615,7 @@ func recalculate_handle_points() -> void:
if mdr_arr.size() != ArrayMesh.ARRAY_MAX || mdr_arr[ArrayMesh.ARRAY_VERTEX] == null || mdr_arr[ArrayMesh.ARRAY_VERTEX].size() == 0:
_handle_points.resize(0)
_handle_to_vertex_map.resize(0)
_handle_normals.resize(0)
return
var arr : Array = Array()
@ -626,6 +637,66 @@ func recalculate_handle_points() -> void:
_handle_points = result[0]
_handle_to_vertex_map = result[1]
recalculate_handle_normals()
func recalculate_handle_normals() -> void:
_handle_normals.resize(0)
var mdr_arr : Array = _mdr.array
if mdr_arr[ArrayMesh.ARRAY_NORMAL] == null || mdr_arr[ArrayMesh.ARRAY_NORMAL].size() != mdr_arr[ArrayMesh.ARRAY_VERTEX].size():
recalculate_handle_normals_no_vert_normals()
return
var normals : PoolVector3Array = mdr_arr[ArrayMesh.ARRAY_NORMAL]
for i in range(_handle_points.size()):
var ps : PoolIntArray = _handle_to_vertex_map[i]
if ps.size() == 0:
_handle_normals.push_back(Vector3())
continue
var handle_normal : Vector3 = normals[ps[0]]
for ind in range(1, ps.size()):
handle_normal += normals[ps[ind]]
handle_normal /= ps.size()
_handle_normals.push_back(handle_normal.normalized())
func recalculate_handle_normals_no_vert_normals() -> void:
var mdr_arr : Array = _mdr.array
var vertices : PoolVector3Array = mdr_arr[ArrayMesh.ARRAY_VERTEX]
var indices : PoolIntArray = mdr_arr[ArrayMesh.ARRAY_INDEX]
for i in range(_handle_points.size()):
var ps : PoolIntArray = _handle_to_vertex_map[i]
if ps.size() == 0:
_handle_normals.push_back(Vector3())
continue
var handle_normal : Vector3 = Vector3()
for ind in range(ps.size()):
var n : Vector3 = Vector3()
for iind in range(indices.size()):
var indx : int = indices[iind]
if ind == iind:
var tri_start_ind : int = iind / 3
tri_start_ind *= 3
var p : Plane = Plane(vertices[tri_start_ind], vertices[tri_start_ind + 1], vertices[tri_start_ind + 2])
n += p.normal
handle_normal += n.normalized()
handle_normal /= ps.size()
_handle_normals.push_back(handle_normal.normalized())
func on_mesh_data_resource_changed(mdr : MeshDataResource) -> void:
if _mdr:

View File

@ -512,6 +512,10 @@ static func is_normal_similar(v0 : Vector3, v1 : Vector3, v2 : Vector3, similar_
return ndns >= 0
static func is_direction_similar(d0 : Vector3, d1 : Vector3) -> bool:
var ndns : float = d0.dot(d1)
return ndns >= 0
static func flip_triangle_ti(mdr : MeshDataResource, triangle_index : int) -> void:
flip_triangle(mdr, triangle_index * 3)