This commit is contained in:
William Tilton 2020-10-22 18:21:55 -07:00
parent 40ba16fec3
commit 7f99bdbef5
2 changed files with 38 additions and 5 deletions

View File

@ -1,10 +1,8 @@
[gd_scene load_steps=6 format=2] [gd_scene load_steps=5 format=2]
[ext_resource path="res://Scripts/Main.gd" type="Script" id=1] [ext_resource path="res://Scripts/Main.gd" type="Script" id=1]
[ext_resource path="res://Scripts/Camera.gd" type="Script" id=2] [ext_resource path="res://Scripts/Camera.gd" type="Script" id=2]
[sub_resource type="CubeMesh" id=6]
[sub_resource type="SpatialMaterial" id=4] [sub_resource type="SpatialMaterial" id=4]
albedo_color = Color( 0.592157, 0.905882, 0.0862745, 1 ) albedo_color = Color( 0.592157, 0.905882, 0.0862745, 1 )
@ -14,12 +12,13 @@ material = SubResource( 4 )
[node name="Main" type="Spatial"] [node name="Main" type="Spatial"]
transform = Transform( 1, 0, 0, 0, 0.99977, 0.021431, 0, -0.021431, 0.99977, 0, 0, 0 ) transform = Transform( 1, 0, 0, 0, 0.99977, 0.021431, 0, -0.021431, 0.99977, 0, 0, 0 )
script = ExtResource( 1 ) script = ExtResource( 1 )
show_wireframe = false
show_axes = false
show_face_normals = false
[node name="Meshes" type="Node" parent="."] [node name="Meshes" type="Node" parent="."]
[node name="Cube" type="MeshInstance" parent="Meshes"] [node name="Cube" type="MeshInstance" parent="Meshes"]
mesh = SubResource( 6 )
material/0 = null
[node name="Floor" type="MeshInstance" parent="Meshes"] [node name="Floor" type="MeshInstance" parent="Meshes"]
transform = Transform( 6.88346, 0, 0, 0, 0.0447118, 0, 0, 0, 7.16078, 0, 0, 0 ) transform = Transform( 6.88346, 0, 0, 0, 0.0447118, 0, 0, 0, 7.16078, 0, 0, 0 )

View File

@ -253,3 +253,37 @@ func _on_StaticBody_mouse_exited():
func _on_StaticBody_input_event(camera, event, click_position, click_normal, shape_idx): func _on_StaticBody_input_event(camera, event, click_position, click_normal, shape_idx):
if event.is_action_pressed("left_mouse"): if event.is_action_pressed("left_mouse"):
print("Pos ", click_position) print("Pos ", click_position)
var face_index = get_hit_mesh_triangle_face_index(click_position)
if face_index > -1:
drawSelectedOutline(face_index)
func get_hit_mesh_triangle_face_index(hitVector):
var cubeMeshInstance = get_node("Meshes/Cube")
var cubeMesh = cubeMeshInstance.get_mesh()
var vertices = cubeMesh.get_faces()
var arrayMesh = ArrayMesh.new()
var arrays = []
arrays.resize(ArrayMesh.ARRAY_MAX)
arrays[ArrayMesh.ARRAY_VERTEX] = vertices
arrayMesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
var meshDataTool = MeshDataTool.new()
meshDataTool.create_from_surface(arrayMesh, 0)
var camera_origin = camera.get_global_transform().origin
var purple_arrow = hitVector - camera_origin
var i = 0
while i < vertices.size():
var face_index = i / 3
var a = cubeMeshInstance.to_global(vertices[i])
var b = cubeMeshInstance.to_global(vertices[i + 1])
var c = cubeMeshInstance.to_global(vertices[i + 2])
var intersects_triangle = Geometry.ray_intersects_triangle(camera_origin, purple_arrow, a, b, c)
if intersects_triangle != null:
var angle = rad2deg(purple_arrow.angle_to(meshDataTool.get_face_normal(face_index)))
if angle > 90 and angle < 180:
return face_index
i += 3
return -1