Processing style 'trace' rendering camera

Has anyone found a way to render with Unity in a way that’s similar to how Processing does? A common use is to render a line that moves and leaves a trail based on where it was in the previous frame.

In Processing ( and Unity I believe), the background must be redrawn each frame, erasing the object and redrawing it in its new position. If you simply set your background color to a semi-transparent color, it will only partially erase the previous frame, creating a trace image of where the object was and gradually fade out over time. Similar to Motion blur, but the object remains crisp.

I was thinking that using the camera clear flag set to Don’t Clear is similar, but I can’t find a way to simulate the effect. I’ve seen suggestions to render to texture, and I suppose if I layered several semitransparent renders it might work, but that seems like a lot of overhead and horribly inefficient compared to Processing’s method.

The manual mentions using don’t clear with a shader, but I can find no such shaders anywhere and I’m not even sure where I would apply them. Any pointers or suggestions would be helpful.

If you’re not familiar with the look, check out the Proximity of needs on page 9 or many of the other examples from Processing-

http://processing.org/exhibition/

The clear colour is used for clearing the screen, and does not get blended in any way regardless of its alpha value.

I would recommend setting the camera clear mode to depth only. Then put a plane in front of the camera and give it a shader that draws what you want. Make sure the shader has a Queue tag of less than 1000 so that it draws before everything else, and turn of ZWrite so it doesn’t occlude anything rendered later. Something like this:

Shader "Early Blend" {
	Properties {
		_Color ("Main Color", Color) = (0.5, 0.5, 0.5, 0.5)
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader {
		Tags { "Queue"="0" "IgnoreProjector"="True" "RenderType"="Transparent" }
		ZWrite Off
		ColorMask RGB
		Blend SrcAlpha OneMinusSrcAlpha
		Pass {
			SetTexture [_MainTex] {
				constantColor [_Color]
				combine constant * texture DOUBLE
			}
		}
	}
}

I think Daniel is right. You could probably also combine this idea with a skybox shader, see: Cg Programming/Unity/Skyboxes - Wikibooks, open books for an open world . That page also explains how to apply a skybox shader (either with front-face culling to a box around the camera or as a skybox material for Unity’s skybox system).

Thanks Daniel!

I was able to get a good approximation of the effect and it seems to render pretty fast. I’d like to compare this to the speed of Processing, which can be fast but I’ve noticed when I increase the render size in Processing there is a much bigger render hit compared to Unity. OTOH, Unity does not like as many objects as Processing. I’m trying to decide which program to use for a new project and wanted the look but couldn’t get it without your help!

Here’s the adjusted shader code:

Shader "Custom/EarlyBlend" {
    Properties {

        _Color ("Main Color", Color) = (0.5, 0.5, 0.5, 0.5)

        _MainTex ("Texture", 2D) = "white" {}

    }

    SubShader {

        Tags { "Queue"="Background-1000" "IgnoreProjector"="True" "RenderType"="Transparent" }

        ZWrite Off

        ColorMask RGB

        Blend SrcAlpha OneMinusSrcAlpha

        Pass {

            SetTexture [_MainTex] {

                constantColor [_Color]

                combine constant * texture DOUBLE

            }

        }

    }

}

For whatever reason, using 0 for the background queue resulted in it actually being set to 2000.

Here’a a complete scene package with a script that builds a cube in front of the camera and applies your shader to it. You have to Build it to a Player to see the final effect. Using Free Aspect in the Game view of the Editor works OK, but when you switch to an Aspect Ratio the effect goes away completely.

I also threw in an outline shader to see how that would work for spheres and it works pretty good. Anyone trying to do the same thing might want to download the package. Improvements or suggestions are welcome!

Screenshot:

1089860–40895–$ProcessingRenderScene.unitypackage (56.2 KB)

Thanks, I’ll give this a try also. What do you see as the advantage to adding a Skybox shader?

Only performance in case you have a skybox already. Also, you mentioned that you didn’t know where to apply the shader and skyboxes have to be applied in the same way.

As far as I understand it, this is a bug of Unity, which ignores the camera settings and clears the framebuffer each frame if an aspect ratio is specified. (I found this bug during a lecture but didn’t know what was going on - it wasn’t fun.)