Smoothing out values

So I’m trying to make it so that when the client receives a net message it will render a screen space effect, this being DrawSobel (as a test). I am trying to make it so that it smoothly draws the sobel but I am having issues. I have tried Lerp and math.Approach but both seem instant.

My code:

[lua]
– In the HUDDraw Hook
net.Receive( “ghostie_effects”, function()
hook.Add( “RenderScreenspaceEffects”, “GhostieEffects”, DrawGhostieEffects )
timer.Create( “StopGhostieEffects”, 10, 1, function()
hook.Remove( “RenderScreenspaceEffects”, “GhostieEffects” )
end )
end )

– A seperate function
function DrawGhostieEffects()
local sexysmoothness = math.Approach( 0, 0.8, FrameTime() * 50 )
DrawSobel( sexysmoothness )
end
[/lua]

Any help is appreciated!

I think you need to put the sexysmoothness variable OUTSIDE the DrawGhostieEffects function. That’s the thing with variables, if you make them in a function then they’ll be reset every time that function is run rather than being changed over time

I tried moving the variable but it was still instant.

Try making the * 50 thing a smaller number

[lua]local smooth = math.Approach( 0, 0.8, FrameTime() * 0.02 )[/lua]

Okay, I made it smaller, but now when the function is called my entire screen goes black.

Read the wiki:

DrawSobel

Your starting value is 0

I noticed.

But the value is supposed to go from 0 to 0.8 D:

Maybe making the value a little bit above zero (like 0.00000000000001 or something) might work?

Okay so now it draws the sobel at a high value then smooths down to a lower one, I don’t think the game likes me today.

[lua]
– Defined at the top of cl_init.lua
local smooth = 0

– The updated function
function DrawGhostieEffects()
smooth = Lerp( 0.01, smooth, 0.8 )
DrawSobel( smooth )
end
[/lua]

That is the correct way of doing what you want to do. [del]Just swap the start and end values around and it should work.[/del]

Also do not forget about trivial math such as ( 1 - x ) or rather ( max - current ) etc to reverse the way the count increases or decreases.

Okay so I swapped the values around, now when the function is called it’s as if I just called DrawSobel( 0.8 )

[lua]
function DrawGhostieEffects()
smooth = Lerp( 0.005, 0.8, smooth )
DrawSobel( smooth )
end
[/lua]

Ignore the first part of my post, use the math.

Okay, I managed to get it to work finally, thanks!

One more thing - don’t put net.Receive in any hook. Put it outside of any function.

It still works even if it’s on it’s own?