-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDrawLine3D.gd
97 lines (82 loc) · 3.29 KB
/
DrawLine3D.gd
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
extends Node2D
class Line:
var Start
var End
var LineColor
var time
func _init(Start, End, LineColor, time):
self.Start = Start
self.End = End
self.LineColor = LineColor
self.time = time
var Lines = []
var RemovedLine = false
func _process(delta):
for i in range(len(Lines)):
Lines[i].time -= delta
if(len(Lines) > 0 || RemovedLine):
queue_redraw() #Calls _draw
RemovedLine = false
func _draw():
var Cam = get_viewport().get_camera_3d()
for i in range(len(Lines)):
var ScreenPointStart = Cam.unproject_position(Lines[i].Start)
var ScreenPointEnd = Cam.unproject_position(Lines[i].End)
#Dont draw line if either start or end is considered behind the camera
#this causes the line to not be drawn sometimes but avoids a bug where the
#line is drawn incorrectly
if(Cam.is_position_behind(Lines[i].Start) ||
Cam.is_position_behind(Lines[i].End)):
continue
draw_line(ScreenPointStart, ScreenPointEnd, Lines[i].LineColor)
#Remove lines that have timed out
var i = Lines.size() - 1
while (i >= 0):
if(Lines[i].time < 0.0):
Lines.remove_at(i)
RemovedLine = true
i -= 1
func DrawLine(Start, End, LineColor, time = 0.0):
Lines.append(Line.new(Start, End, LineColor, time))
func DrawRay(Start, Ray, LineColor, time = 0.0):
Lines.append(Line.new(Start, Start + Ray, LineColor, time))
func DrawCube(Center, HalfExtents, LineColor, time = 0.0):
#Start at the 'top left'
var LinePointStart = Center
LinePointStart.x -= HalfExtents
LinePointStart.y += HalfExtents
LinePointStart.z -= HalfExtents
#Draw top square
var LinePointEnd = LinePointStart + Vector3(0, 0, HalfExtents * 2.0)
DrawLine(LinePointStart, LinePointEnd, LineColor, time);
LinePointStart = LinePointEnd
LinePointEnd = LinePointStart + Vector3(HalfExtents * 2.0, 0, 0)
DrawLine(LinePointStart, LinePointEnd, LineColor, time);
LinePointStart = LinePointEnd
LinePointEnd = LinePointStart + Vector3(0, 0, -HalfExtents * 2.0)
DrawLine(LinePointStart, LinePointEnd, LineColor, time);
LinePointStart = LinePointEnd
LinePointEnd = LinePointStart + Vector3(-HalfExtents * 2.0, 0, 0)
DrawLine(LinePointStart, LinePointEnd, LineColor, time);
#Draw bottom square
LinePointStart = LinePointEnd + Vector3(0, -HalfExtents * 2.0, 0)
LinePointEnd = LinePointStart + Vector3(0, 0, HalfExtents * 2.0)
DrawLine(LinePointStart, LinePointEnd, LineColor, time);
LinePointStart = LinePointEnd
LinePointEnd = LinePointStart + Vector3(HalfExtents * 2.0, 0, 0)
DrawLine(LinePointStart, LinePointEnd, LineColor, time);
LinePointStart = LinePointEnd
LinePointEnd = LinePointStart + Vector3(0, 0, -HalfExtents * 2.0)
DrawLine(LinePointStart, LinePointEnd, LineColor, time);
LinePointStart = LinePointEnd
LinePointEnd = LinePointStart + Vector3(-HalfExtents * 2.0, 0, 0)
DrawLine(LinePointStart, LinePointEnd, LineColor, time);
#Draw vertical lines
LinePointStart = LinePointEnd
DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), LineColor, time)
LinePointStart += Vector3(0, 0, HalfExtents * 2.0)
DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), LineColor, time)
LinePointStart += Vector3(HalfExtents * 2.0, 0, 0)
DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), LineColor, time)
LinePointStart += Vector3(0, 0, -HalfExtents * 2.0)
DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), LineColor, time)