-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
156 lines (115 loc) · 4.44 KB
/
Main.cs
File metadata and controls
156 lines (115 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using Godot;
public partial class Main : Node3D
{
private const float _MOUSE_SENSITIVITY = 0.005f;
private const float _SCROLL_SENSITIVITY = 0.05f;
private Camera3D camera; //set in ready
private Node3D cameraPivot;
public override void _Input(InputEvent theEvent)
{
if (theEvent is InputEventMouseMotion inputEventMouseMotion)
{
//middle mouse button
if (Input.IsActionPressed("camera_rotate"))
{
//rotate camera
cameraPivot.RotateY(-inputEventMouseMotion.Relative.X * _MOUSE_SENSITIVITY);
cameraPivot.RotateObjectLocal(camera.Basis.X,-inputEventMouseMotion.Relative.Y * _MOUSE_SENSITIVITY);
cameraPivot.Rotation = new Vector3(Mathf.Clamp(cameraPivot.Rotation.X,-Mathf.Pi / 2, Mathf.Pi / 2), cameraPivot.Rotation.Y, cameraPivot.Rotation.Z);
}
}
if (theEvent is InputEventMouseButton mouseButtonEvent)
{
//scrolling
if (mouseButtonEvent.ButtonIndex == MouseButton.WheelUp)
{
cameraPivot.Scale -= new Vector3(_SCROLL_SENSITIVITY, _SCROLL_SENSITIVITY,_SCROLL_SENSITIVITY) ;
}
else if (mouseButtonEvent.ButtonIndex == MouseButton.WheelDown)
{
cameraPivot.Scale += new Vector3(_SCROLL_SENSITIVITY, _SCROLL_SENSITIVITY, _SCROLL_SENSITIVITY);
}
}
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
//consts
const float SCALE_FACTOR = 1.01f;
//nodes
cameraPivot = GetNode<Node3D>("CameraPivot");
camera = GetNode<Camera3D>("CameraPivot/Camera3D");
MeshInstance3D cubeMeshInstance3D = GetNode<MeshInstance3D>("Props/Cube");
Mesh cubeMesh = cubeMeshInstance3D.Mesh;
Vector3[] vertices = cubeMesh.GetFaces();
ImmediateMesh immediateMesh = new();
StandardMaterial3D material = new();
material.ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded;
material.VertexColorUseAsAlbedo = true; //commment and uncomment below to remove override and apply yellow via material
//material.AlbedoColor = Colors.Brown;
immediateMesh.SurfaceBegin(Mesh.PrimitiveType.Lines);
immediateMesh.SurfaceSetColor(Colors.Yellow); //use this line with alternate colours to change w/out need of new material
for (int i = 0; i < vertices.Length; i+=3)
{
//tri line a-b
immediateMesh.SurfaceAddVertex(vertices[i]);
immediateMesh.SurfaceAddVertex(vertices[i + 1]);
//tri line b-c
immediateMesh.SurfaceAddVertex(vertices[i + 1]);
immediateMesh.SurfaceAddVertex(vertices[i + 2]);
//tri line c-a
immediateMesh.SurfaceAddVertex(vertices[i + 2]);
immediateMesh.SurfaceAddVertex(vertices[i]);
}
immediateMesh.SurfaceEnd();
immediateMesh.SurfaceSetMaterial(0,material);
MeshInstance3D immediateMeshInstance = new();
immediateMeshInstance.Scale = new Vector3(SCALE_FACTOR, SCALE_FACTOR, SCALE_FACTOR);
immediateMeshInstance.Mesh = immediateMesh;
cubeMeshInstance3D.AddChild(immediateMeshInstance);
ImmediateMesh axisImmediateMesh = new();
StandardMaterial3D axisMaterial = new();
axisMaterial.ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded;
axisMaterial.VertexColorUseAsAlbedo = true;
axisImmediateMesh.SurfaceBegin(Mesh.PrimitiveType.Lines);
axisImmediateMesh.SurfaceSetColor(Colors.Red);
const float axisLength = 10f;
axisImmediateMesh.SurfaceAddVertex(new Vector3(0, 0, 0));
axisImmediateMesh.SurfaceAddVertex(new Vector3(axisLength, 0, 0));
axisImmediateMesh.SurfaceEnd();
axisImmediateMesh.SurfaceSetMaterial(0,axisMaterial);
MeshInstance3D axisMeshInstance = new();
axisMeshInstance.Mesh = axisImmediateMesh;
AddChild(axisMeshInstance);
}
//signals
private void _on_static_body_3d_mouse_entered()
{
GD.Print("Mouse Entered");
}
private void _on_static_body_3d_mouse_exited()
{
GD.Print("Mouse Exited");
}
private void _on_static_body_3d_input_event(Camera3D camera , InputEvent theEvent, Vector3 clickPosition, Vector3 clickNormal, int shapeIndex)
{
switch (theEvent)
{
case InputEventMouseButton inputEventMouseButton:
switch (inputEventMouseButton.ButtonIndex)
{
case MouseButton.Left:
if (inputEventMouseButton.IsPressed())
GD.Print("Clicked at" + clickPosition);
break;
case MouseButton.Right:
break;
}
break;
}
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
//public override void _Process(double delta)
//{
//}
}