Completed

This commit is contained in:
William Tilton 2020-10-22 15:49:14 -07:00
parent 163d5cae20
commit 7ab26ab492
3 changed files with 125 additions and 44 deletions

View File

@ -1,43 +1,27 @@
[gd_scene load_steps=7 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/Camera.gd" type="Script" id=2]
[sub_resource type="SpatialMaterial" id=1]
albedo_color = Color( 0.0627451, 0.8, 0.25098, 1 )
[sub_resource type="CubeMesh" id=2]
material = SubResource( 1 )
[sub_resource type="BoxShape" id=5]
[sub_resource type="SpatialMaterial" id=3]
[sub_resource type="SpatialMaterial" id=4]
albedo_color = Color( 0.592157, 0.905882, 0.0862745, 1 )
[sub_resource type="CubeMesh" id=4]
material = SubResource( 3 )
[sub_resource type="CubeMesh" id=5]
material = SubResource( 4 )
[node name="Main" type="Spatial"]
transform = Transform( 1, 0, 0, 0, 0.99977, 0.021431, 0, -0.021431, 0.99977, 0, 0, 0 )
script = ExtResource( 1 )
show_wireframe = false
show_axes = false
show_face_normals = false
[node name="Meshes" type="Node" parent="."]
[node name="Cube" type="MeshInstance" parent="Meshes"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.07597, 0 )
mesh = SubResource( 2 )
material/0 = null
[node name="StaticBody" type="StaticBody" parent="Meshes/Cube"]
[node name="CollisionShape" type="CollisionShape" parent="Meshes/Cube/StaticBody"]
shape = SubResource( 5 )
[node name="Floor" type="MeshInstance" parent="Meshes"]
transform = Transform( 6.88346, 0, 0, 0, 0.0447118, 0, 0, 0, 7.16078, 0, 0, 0 )
mesh = SubResource( 4 )
visible = false
mesh = SubResource( 5 )
material/0 = null
[node name="Lights" type="Node" parent="."]
@ -45,11 +29,12 @@ material/0 = null
[node name="DirectionalLight" type="DirectionalLight" parent="Lights"]
transform = Transform( 1, 0, 0, 0, 0.778528, 0.62761, 0, -0.62761, 0.778528, 0, 3.77816, 3.77808 )
[node name="CameraAnchor" type="Spatial" parent="."]
transform = Transform( 0.391454, -0.0197208, 0.919986, 0.0197208, 0.999721, 0.0130388, -0.919987, 0.0130388, 0.391734, 0, 0, 0 )
[node name="CameraGimbal" type="Spatial" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )
[node name="Camera" type="Camera" parent="CameraAnchor"]
transform = Transform( 0.871787, -0.177322, 0.456666, -9.31323e-10, 0.932191, 0.361967, -0.489885, -0.315558, 0.812672, 2.14561, 3.89777, 4.89912 )
[connection signal="input_event" from="Meshes/Cube/StaticBody" to="." method="_on_StaticBody_input_event"]
[connection signal="mouse_entered" from="Meshes/Cube/StaticBody" to="." method="_on_StaticBody_mouse_entered"]
[connection signal="mouse_exited" from="Meshes/Cube/StaticBody" to="." method="_on_StaticBody_mouse_exited"]
[node name="InnerGimbal" type="Spatial" parent="CameraGimbal"]
transform = Transform( 1, -4.65661e-10, 0, 9.31323e-10, 1, 9.31323e-10, 0, 9.31323e-10, 1, 0, 0, 0 )
[node name="Camera" type="Camera" parent="CameraGimbal/InnerGimbal"]
transform = Transform( 0.999999, -2.3283e-09, 2.98023e-08, 1.86264e-09, 1, -7.45058e-09, -2.98023e-08, 5.58794e-09, 1, -0.855393, 0.263348, 5.88796 )
script = ExtResource( 2 )

17
Scripts/Camera.gd Normal file
View File

@ -0,0 +1,17 @@
extends Camera
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
var cube = get_node("/root/Main/Meshes/Cube")
look_at(cube.global_transform.origin, Vector3(0,1,0))
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

View File

@ -3,11 +3,15 @@ extends Spatial
var is_rotate_enabled = false
var is_zoom_enabled = false
var input_start_position = Vector2()
var camera_anchor
var camera_gimble
var inner_gimbal
var rotation_speed = 0.2
var zoom_step = 0.002
var camera
var zoom_min = 3
export var invert_x = false
export var invert_y = false
export var mouse_sensitivity = 0.005
export var show_wireframe = true
export var show_axes = true
@ -29,24 +33,27 @@ func _input(event):
if event.is_action_pressed("ui_exit"):
get_tree().quit()
if event is InputEventMouse:
var delta = input_start_position - event.get_position()
if event is InputEventMouseMotion:
if is_rotate_enabled:
print("Delta x:", delta.x, " y: ", delta.y)
camera_anchor.rotate_y(deg2rad(delta.x) * rotation_speed)
#camera_anchor.rotate_z(deg2rad(delta.y) * rotation_speed) # Hmm
input_start_position = event.get_position()
if event.relative.x != 0:
var dir = 1 if invert_x else -1
camera_gimble.rotate_object_local(Vector3.UP, dir * event.relative.x * mouse_sensitivity)
if event.relative.y != 0:
var dir = 1 if invert_y else -1
inner_gimbal.rotate_object_local(Vector3.RIGHT, dir * event.relative.y * mouse_sensitivity)
elif is_zoom_enabled:
var camera_to_anchor = camera_anchor.get_global_transform().origin - camera.get_global_transform().origin
var camera_to_anchor = camera_gimble.get_global_transform().origin - camera.get_global_transform().origin
var length = camera_to_anchor.length()
if(length > zoom_min):
camera.global_translate((delta.y * zoom_step) * camera_to_anchor)
input_start_position = event.get_position()
if length > zoom_min or event.relative.y < 0:
camera.global_translate((event.relative.y * zoom_step) * camera_to_anchor)
func _ready():
camera_anchor = get_node("CameraAnchor")
camera = get_node("CameraAnchor/Camera")
camera_gimble = get_node("CameraGimbal")
inner_gimbal = get_node("CameraGimbal/InnerGimbal")
camera = get_node("CameraGimbal/InnerGimbal/Camera")
createAndAssignCubeMesh()
if show_wireframe:
drawWireframe()
if show_axes:
@ -54,6 +61,72 @@ func _ready():
if show_face_normals:
drawSurfaceNormals()
func createAndAssignCubeMesh():
var uniqueCubeVertices = [
Vector3(-0.5,0.5,0.5),
Vector3(0.5,0.5,0.5),
Vector3(0.5,-0.5,0.5),
Vector3(-0.5,-0.5,0.5),
Vector3(-0.5,0.5,-0.5),
Vector3(0.5,0.5,-0.5),
Vector3(0.5,-0.5,-0.5),
Vector3(-0.5,-0.5,-0.5),
]
var front_face = [
uniqueCubeVertices[0], uniqueCubeVertices[1], uniqueCubeVertices[2],
uniqueCubeVertices[0], uniqueCubeVertices[2], uniqueCubeVertices[3]
]
var back_face = [
uniqueCubeVertices[4], uniqueCubeVertices[6], uniqueCubeVertices[5],
uniqueCubeVertices[4], uniqueCubeVertices[7], uniqueCubeVertices[6]
]
var left_face = [
uniqueCubeVertices[0], uniqueCubeVertices[7], uniqueCubeVertices[4],
uniqueCubeVertices[0], uniqueCubeVertices[3], uniqueCubeVertices[7]
]
var right_face = [
uniqueCubeVertices[1], uniqueCubeVertices[5], uniqueCubeVertices[6],
uniqueCubeVertices[1], uniqueCubeVertices[6], uniqueCubeVertices[2]
]
var top_face = [
uniqueCubeVertices[0], uniqueCubeVertices[4], uniqueCubeVertices[1],
uniqueCubeVertices[4], uniqueCubeVertices[5], uniqueCubeVertices[1]
]
var bottom_face = [
uniqueCubeVertices[7], uniqueCubeVertices[3], uniqueCubeVertices[2],
uniqueCubeVertices[6], uniqueCubeVertices[7], uniqueCubeVertices[2]
]
var cube_faces = front_face + back_face + left_face + right_face + top_face + bottom_face
var sTool = SurfaceTool.new()
sTool.begin(Mesh.PRIMITIVE_TRIANGLES)
for x in cube_faces:
sTool.add_vertex(x)
sTool.generate_normals()
var cube = $Meshes/Cube
cube.mesh = sTool.commit()
var material = SpatialMaterial.new()
material.albedo_color = Color("36c92a")
cube.material_override = material
cube.create_convex_collision()
var cubesStaticBody = cube.get_child(0)
cubesStaticBody.connect("input_event", get_node("/root/Main"), "_on_StaticBody_input_event")
cubesStaticBody.connect("mouse_exited", get_node("/root/Main"), "_on_StaticBody_mouse_exited")
cubesStaticBody.connect("mouse_entered", get_node("/root/Main"), "_on_StaticBody_mouse_entered")
func drawSurfaceNormals():
var cubeMeshInstance = get_node("Meshes/Cube")
var cubeMesh = cubeMeshInstance.get_mesh()
@ -144,6 +217,12 @@ func drawWireframe():
var sf = 1.005
ig.set_scale(Vector3(sf, sf, sf))
cubeMeshInstance.add_child(ig)
static func rotate_vector3_around(var v3_pos,var v3_pivot,var y_angle):
var dir = v3_pos - v3_pivot
dir = Quat(Vector3(0,1,0),y_angle) * dir
var point = dir - v3_pivot
return point
func _on_StaticBody_mouse_entered():
print("In cube")