I am trying use a render texture to allow painting of a coloured line based on mouse position.
The drawing part is working fine, however I would like the drawing to fade back to the base colour over time, much like a fading trail.
My approach has been to apply the line in one shader, then in another to continually try and apply a solid colour that matches the background, to every pixel.
I thought that in the frag function, returning col * _Color * 0.5, where _Color is the same as background colour, would work, but actually this only ever dims a white line to grey.
Does anyone have a better suggestion for how to achieve this trail effect behind a drawn line… Ultimately I want the trail to fade very slowly so the rate of fade needs to be configurable.
If you’re storing a high precision value in a low bit data type, then sure it’ll quantize. Without seeing your code, couldn’t you do something like this;
color = lerp(foregroundColor, backgroundColor, time);```
Where *time* is a high precision value stored instead of pixel color?
I can’t Lerp because I need to apply a uniform Color delta to every pixel regardless of its current Color.
I need to always be trying to bring every pixel back to black by reducing each component of each pixel by a steady fixed amount - the effect is exactly what I need, its just too fast.
If fixed4 Color was high enough precision I could just do col.r -= 0.0001 for example and that would probably be slow enough. Since it is not high enough precision, I have to instead decrease value by a larger amount, less frequently, to achieve same effect.
Seems like a simple thing, apply changes to Color at a slow rate…
RGBAHalf is a texture format. Change your texture to something higher. RGBAHalf or if you’re only using the single red channel you could use RHalf to save memory.
You’ll also need to set half types in the shader, including the sampler; sampler2D_half _MainTex;
For completeness, if you’ve not updated the precision in the shader then it’s because you’re targeting desktop where all types are high precision (32 bit); Be aware if you deploy to mobile where low precision is used.
Also, if you’re rendering in sync with frame rate then the speed of the animation will vary. You might want to replace the constant with something like this;