-
Notifications
You must be signed in to change notification settings - Fork 3
/
ray_tracer2.lua
169 lines (118 loc) · 4.31 KB
/
ray_tracer2.lua
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
157
158
159
160
161
162
163
164
165
166
167
168
169
local R = Color( 230, 30, 30 )
local G = Color( 30, 230, 30 )
local B = Color( 30, 30, 230 )
local P = Color( 230, 30, 230 )
local X = Vector( 1, 0, 0 )
local Y = Vector( 0, 1, 0 )
local Z = Vector( 0, 0, 1 )
local function drawLine( a, b, color )
render.DrawLine( a, b, color or W, false )
end
local function drawCross( v, size, color )
size = size or 4
drawLine( v + X*size, v - X*size, color or R )
drawLine( v + Y*size, v - Y*size, color or G )
drawLine( v + Z*size, v - Z*size, color or B )
end
local function drawNormal( v, normal, len, color )
len = len or 32
drawLine( v, v + normal * len, color )
drawCross( v, 1, color )
end
local function drawText( v, text, color )
local scr = v:ToScreen()
if ( scr.visible ) then
cam.Start2D()
draw.SimpleText( tostring(text), "BudgetLabel", scr.x, scr.y, color or W, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
cam.End2D()
end
end
local function getAnchor()
local prop = ents.FindByClass( "prop_physics" )[1]
if IsValid( prop ) then
return prop:GetPos(), prop:GetUp()
end
end
local abs = math.abs
local floor = math.floor
local min = math.min
local function sign( n )
return n > 0 and 1 or n < 0 and -1 or 0
end
local function trace( origin, normal, limit, grid, cback )
local fX = floor( origin.x /grid )*grid + ( normal.x > 0 and grid or 0 )
local fY = floor( origin.y /grid )*grid + ( normal.y > 0 and grid or 0 )
local fZ = floor( origin.z /grid )*grid + ( normal.z > 0 and grid or 0 )
local dX = ( fX - origin.x )/normal.x
local dY = ( fY - origin.y )/normal.y
local dZ = ( fZ - origin.z )/normal.z
local sX = grid/ abs( normal.x )
local sY = grid/ abs( normal.y )
local sZ = grid/ abs( normal.z )
local cX = dX
local cY = dY
local cZ = dZ
local dist = 0
for i = 0, limit do
local step = min( cX, cY, cZ )
cX = cX - step
cY = cY - step
cZ = cZ - step
dist = dist + step
local hit = origin + normal * dist
local dir = nil
if ( cX == 0 ) then dir = X * sign( normal.x ) cX = sX end
if ( cY == 0 ) then dir = Y * sign( normal.y ) cY = sY end
if ( cZ == 0 ) then dir = Z * sign( normal.z ) cZ = sZ end
local x = floor( hit.x / grid - dir.x * 0.5 )
local y = floor( hit.y / grid - dir.y * 0.5 )
local z = floor( hit.z / grid - dir.z * 0.5 )
local pos = Vector(x,y,z)*grid + Vector(grid,grid,grid)/2
render.DrawWireframeBox( pos, Angle(), -Vector(grid,grid,grid)/2, Vector(grid,grid,grid)/2, color, true )
drawNormal( hit, dir, 6, color )
end
end
local S = 32
hook.Add( "PostDrawOpaqueRenderables", "TraceTest", function()
local origin, normal = getAnchor()
if ( !origin ) then return end
trace( origin, normal, 25, S )
--[[
drawNormal( origin, normal, 512, P )
local fX = floor( origin.x /S )*S + ( normal.x > 0 and 1 or 0 ) *S
local fY = floor( origin.y /S )*S + ( normal.y > 0 and 1 or 0 ) *S
local fZ = floor( origin.z /S )*S + ( normal.z > 0 and 1 or 0 ) *S
-- drawNormal( Vector( fX, origin.y, origin.z ), X * sign( normal.x ), S, R )
-- drawNormal( Vector( origin.x, fY, origin.z ), Y * sign( normal.y ), S, G )
-- drawNormal( Vector( origin.x, origin.y, fZ ), Z * sign( normal.z ), S, B )
local dX = ( fX - origin.x )/normal.x
local dY = ( fY - origin.y )/normal.y
local dZ = ( fZ - origin.z )/normal.z
local sX = S/ abs( normal.x )
local sY = S/ abs( normal.y )
local sZ = S/ abs( normal.z )
local cX = dX
local cY = dY
local cZ = dZ
local dist = 0
for i = 0, 12 do
local step = min( cX, cY, cZ )
cX = cX - step
cY = cY - step
cZ = cZ - step
dist = dist + step
local hit = origin + normal * dist
local dir = nil
local color = nil
if ( cX == 0 ) then color = R dir = X * -sign( normal.x ) cX = sX end
if ( cY == 0 ) then color = G dir = Y * -sign( normal.y ) cY = sY end
if ( cZ == 0 ) then color = B dir = Z * -sign( normal.z ) cZ = sZ end
local box = hit - dir*S/2
local hX, hY, hZ = floor( box.x /S )*S, floor( box.y /S )*S, floor( box.z /S )*S
local pos = Vector( hX, hY, hZ ) + Vector(S,S,S)/2
render.DrawWireframeBox( pos, Angle(), -Vector(S,S,S)/2, Vector(S,S,S)/2, color, true )
drawNormal( hit, dir, 6, color )
-- drawText( hit, i, color )
end
]]--
end )