Skip to content

Commit fcb96c3

Browse files
committed
Fixed performance issues on quieter-behind tracers
1 parent 9f84e26 commit fcb96c3

File tree

6 files changed

+122
-78
lines changed

6 files changed

+122
-78
lines changed

lua/entities/base_streamradio.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,8 @@ function ENT:ShowDebug()
692692
end
693693

694694
if CLIENT then
695-
function ENT:DrawTranslucent()
696-
self:DrawModel()
695+
function ENT:DrawTranslucent(flags)
696+
self:DrawModel(flags)
697697

698698
if not g_isWiremodLoaded then return end
699699
Wire_Render(self)

lua/entities/base_streamradio_gui.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,8 +1097,8 @@ if CLIENT then
10971097
cam.End3D2D( )
10981098
end
10991099

1100-
function ENT:DrawTranslucent( )
1101-
BaseClass.DrawTranslucent( self )
1100+
function ENT:DrawTranslucent(...)
1101+
BaseClass.DrawTranslucent(self, ...)
11021102
self.isseen = true
11031103
self:DrawGUI()
11041104
end

lua/entities/sent_streamradio/cl_init.lua

Lines changed: 83 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -190,30 +190,47 @@ function ENT:OnModelSetup()
190190
end
191191

192192
function ENT:GetWallTraceParamenters()
193-
if not self.WallTraceParamenters then
194-
self.WallTraceParamenters = {
195-
mask = MASK_SHOT_PORTAL,
196-
filter = function(ent)
197-
if not IsValid(ent) then return false end
198-
if not IsValid(self) then return false end
199-
200-
if ent == self then return false end
201-
if ent.__IsRadio then return false end
202-
if ent:IsPlayer() then return false end
203-
if ent:IsVehicle() then return false end
204-
if ent:IsNPC() then return false end
205-
206-
local camera = StreamRadioLib.GetCameraEnt()
207-
if IsValid(camera) and ent == camera then return false end
208-
209-
return true
210-
end
211-
}
193+
local wallTraceParamenters = self.WallTraceParamenters
194+
195+
if not wallTraceParamenters then
196+
self.WallTraceParamenters = {}
197+
198+
wallTraceParamenters = self.WallTraceParamenters
199+
wallTraceParamenters.mask = MASK_SHOT_PORTAL
200+
wallTraceParamenters.filter = {}
212201
end
213202

214-
self.WallTraceParamenters.output = self.WallTraceParamenters.output or {}
203+
wallTraceParamenters.output = wallTraceParamenters.output or {}
204+
205+
local camera = StreamRadioLib.GetCameraEnt(ent)
206+
local cameraVehicle = false
207+
208+
if IsValid(camera) then
209+
cameraVehicle = camera.GetVehicle and camera:GetVehicle() or false
210+
else
211+
camera = false
212+
end
213+
214+
local tmp = {}
215+
216+
tmp[self] = self
217+
tmp[camera] = camera
218+
tmp[cameraVehicle] = cameraVehicle
215219

216-
return self.WallTraceParamenters
220+
local filter = wallTraceParamenters.filter
221+
table.Empty(filter)
222+
223+
for _, filterEnt in pairs(tmp) do
224+
if not IsValid(filterEnt) then continue end
225+
table.insert(filter, filterEnt)
226+
end
227+
228+
for _, filterEnt in pairs(StreamRadioLib.SpawnedRadios) do
229+
table.insert(filter, filterEnt)
230+
end
231+
232+
wallTraceParamenters.filter = filter
233+
return wallTraceParamenters
217234
end
218235

219236
function ENT:TraceToCamera(frompos)
@@ -228,18 +245,13 @@ function ENT:TraceToCamera(frompos)
228245
local result = traceparams.output
229246

230247
-- Tracers Debug
231-
-- debugoverlay.Line(frompos, result.HitPos or endpos, 0.1, color_white, false)
232-
-- debugoverlay.Line(result.HitPos or endpos, endpos, 0.1, color_black, false)
248+
-- debugoverlay.Line(frompos, result.HitPos or endpos, 0.5, color_white, false)
249+
-- debugoverlay.Line(result.HitPos or endpos, endpos, 0.5, color_black, false)
233250

234251
return result
235252
end
236253

237254
function ENT:TraceWalls(radius)
238-
local coveredvol = StreamRadioLib.GetCoveredVolume()
239-
if coveredvol >= 1 then
240-
return 1
241-
end
242-
243255
local startpos = self.SoundPos
244256

245257
local camtrace = self:TraceToCamera(startpos)
@@ -251,7 +263,7 @@ function ENT:TraceWalls(radius)
251263

252264
traceparams.start = startpos
253265

254-
local traces = StreamRadioLib.StarTrace(traceparams, radius, 16, 16)
266+
local traces = StreamRadioLib.StarTrace(traceparams, radius)
255267

256268
local blockcount = 0
257269
local wallcount = 0
@@ -285,7 +297,9 @@ function ENT:TraceWalls(radius)
285297

286298
local f = blockcount / wallcount
287299

288-
local volfactor = math.Clamp((1 - f) * 2, coveredvol, 1)
300+
f = (1 - f) * 2
301+
302+
local volfactor = math.Clamp(f, 0, 1)
289303
return volfactor
290304
end
291305

@@ -300,24 +314,53 @@ function ENT:GetWallVolumeFactor()
300314
return 0
301315
end
302316

303-
if StreamRadioLib.GetCoveredVolume() >= 1 then
317+
local coveredvol = StreamRadioLib.GetCoveredVolume()
318+
319+
if coveredvol >= 1 then
320+
self.wallvolcache = nil
321+
return 1
322+
end
323+
324+
local streamingRadioCount = StreamRadioLib.GetStreamingRadioCount()
325+
if streamingRadioCount <= 0 then
304326
self.wallvolcache = nil
305327
return 1
306328
end
307329

308330
local now = RealTime()
309331

310332
self.wallvolcache = self.wallvolcache or {}
311-
if (self.wallvolcache.nexttime or 0) >= now then
312-
return self.wallvolcache.value or 0
333+
local cache = self.wallvolcache
334+
local oldvalue = cache.value or 0
335+
336+
if cache.nexttime and cache.nexttime > now then
337+
return math.max(oldvalue, coveredvol)
338+
end
339+
340+
local startTime = SysTime()
341+
342+
local value = self:TraceWalls(self.Radius) or 1
343+
344+
local endTime = SysTime()
345+
local runtime = endTime - startTime
346+
347+
local mintime = math.max(RealFrameTime() * 30, runtime * streamingRadioCount * 50, 0.1)
348+
349+
if oldvalue <= 0 and value <= 0 then
350+
-- already quiet radios should retest less often
351+
mintime = math.max(mintime * 3, 0.5)
352+
elseif oldvalue >= 1 and value >= 1 then
353+
-- already clear radios should retest less often
354+
mintime = math.max(mintime * 3, 0.5)
313355
end
314356

315-
local mintime = math.max(FrameTime() * 3, 0.075)
357+
mintime = math.Rand(mintime, mintime * 2)
358+
mintime = math.min(mintime, 3)
316359

317-
self.wallvolcache.value = self:TraceWalls(self.Radius)
318-
self.wallvolcache.nexttime = now + math.Rand(mintime, mintime * 4)
360+
cache.nexttime = now + mintime
319361

320-
return self.wallvolcache.value or 1
362+
cache.value = value
363+
return math.max(value, coveredvol)
321364
end
322365

323366
function ENT:GetWallVolumeFactorSmoothed()
@@ -635,9 +678,9 @@ function ENT:OnMasterradioChange(masterradio, oldmasterradio)
635678
end
636679
end
637680

638-
function ENT:DrawTranslucent()
639-
BaseClass.DrawTranslucent(self)
640-
self:CallModelFunction("Draw")
681+
function ENT:DrawTranslucent(...)
682+
BaseClass.DrawTranslucent(self, ...)
683+
self:CallModelFunction("Draw", ...)
641684
end
642685

643686
function ENT:PostFakeRemove()

lua/streamradio_core/lib.lua

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ function StreamRadioLib.Trace(ent)
226226
table.insert(filter, filterEnt)
227227
end
228228

229-
g_PlayerTrace.filter = filter
230-
231229
local trace = util.TraceLine(g_PlayerTrace)
232230

233231
-- prevent the cache from overflowing
@@ -247,27 +245,17 @@ end
247245

248246
local g_PI = math.pi
249247
local g_TAU = g_PI * 2
248+
local g_starTracePoses = {}
250249

251-
function StreamRadioLib.StarTrace(traceparams, size, edges, layers)
252-
traceparams = traceparams or {}
253-
254-
local centerpos = traceparams.start or Vector()
255-
256-
size = math.abs(size or 0)
257-
edges = math.abs(edges or 0)
250+
local function buildStarTracePoses(layers, edges)
258251
layers = math.abs(layers or 0)
252+
edges = math.abs(edges or 0)
259253

260-
traceparams.start = centerpos
261-
traceparams.output = nil
262-
263-
local traceposes = {}
264-
local traces = {}
265-
266-
for e = 1, edges do
267-
local u = g_TAU / edges * e
254+
for l = 1, layers do
255+
local u = g_TAU / layers * l
268256

269-
for l = 1, layers do
270-
local v = g_TAU / layers * l
257+
for e = 1, edges do
258+
local v = g_TAU / edges * e
271259

272260
local x = math.cos(u) * math.cos(v)
273261
local y = math.cos(u) * math.sin(v)
@@ -276,28 +264,43 @@ function StreamRadioLib.StarTrace(traceparams, size, edges, layers)
276264
local v = Vector(x, y, z)
277265
v:Normalize()
278266

279-
if traceposes[v] then continue end
280-
traceposes[v] = true
267+
if g_starTracePoses[v] then continue end
268+
g_starTracePoses[v] = true
281269
end
282270
end
283271

284-
traceposes[Vector(0, 0, 1)] = true
285-
traceposes[Vector(0, 1, 0)] = true
286-
traceposes[Vector(1, 0, 0)] = true
272+
g_starTracePoses[Vector(0, 0, 1)] = true
273+
g_starTracePoses[Vector(0, 1, 0)] = true
274+
g_starTracePoses[Vector(1, 0, 0)] = true
287275

288-
traceposes[Vector(0, 0, -1)] = true
289-
traceposes[Vector(0, -1, 0)] = true
290-
traceposes[Vector(-1, 0, 0)] = true
276+
g_starTracePoses[Vector(0, 0, -1)] = true
277+
g_starTracePoses[Vector(0, -1, 0)] = true
278+
g_starTracePoses[Vector(-1, 0, 0)] = true
279+
end
280+
281+
buildStarTracePoses(10, 6)
282+
283+
function StreamRadioLib.StarTrace(traceparams, size)
284+
traceparams = traceparams or {}
285+
286+
local centerpos = traceparams.start or Vector()
287+
288+
size = math.abs(size or 0)
289+
290+
traceparams.start = centerpos
291+
traceparams.output = nil
292+
293+
local traces = {}
291294

292-
for v, _ in pairs(traceposes) do
295+
for v, _ in pairs(g_starTracePoses) do
293296
local endpos = centerpos + v * size
294297
traceparams.endpos = endpos
295298

296299
local trace = util.TraceLine(traceparams)
297300

298301
-- Tracers Debug
299-
-- debugoverlay.Line(centerpos, trace.HitPos or endpos, 0.1, color_white, false)
300-
-- debugoverlay.Line(trace.HitPos or endpos, endpos, 0.1, color_black, false)
302+
-- debugoverlay.Line(centerpos, trace.HitPos or endpos, 0.5, color_white, false)
303+
-- debugoverlay.Line(trace.HitPos or endpos, endpos, 0.5, color_black, false)
301304

302305
table.insert(traces, trace)
303306
end

lua/streamradio_core/vr.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,6 @@ function LIB.TraceHand()
215215
table.insert(filter, filterEnt)
216216
end
217217

218-
g_PlayerHandTrace.filter = filter
219-
220218
util.TraceLine(g_PlayerHandTrace)
221219
g_PlayerHandTraceCache = g_PlayerHandTrace.output
222220

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
442
2-
1704655335
1+
443
2+
1708179919

0 commit comments

Comments
 (0)