I’ve been working on this over the last week in my spare time and I think I came up with a pretty solid solution. Big thanks to Daniel for setting me straight on the shader. Attach this script to your camera and you should be good to go. If you’re using image effects then this script obviously needs to be tweaked a bit. I’d also love to see any improvements people come up with to make this script a truly general anaglyph rendering solution.
public var eyeDistance = 0.03;
public var focalDistance = 10.0;
var anaglyphMat;
var leftEyeRT;
var rightEyeRT;
var leftEye;
var rightEye;
function Start ()
{
leftEye = new GameObject ("leftEye", Camera);
rightEye = new GameObject ("rightEye", Camera);
leftEye.camera.CopyFrom (camera);
rightEye.camera.CopyFrom (camera);
leftEyeRT = new RenderTexture (Screen.width, Screen.height, 24);
rightEyeRT = new RenderTexture (Screen.width, Screen.height, 24);
anaglyphMat = new Material
(
"Shader \"Hidden/Anaglyph\"" +
"{" +
" Properties" +
" {" +
" _Color (\"Main Color, Alpha\", Color) = (1,1,1,1)" +
" _LeftTex (\"Left (RGB)\", RECT) = \"white\" {}" +
" _RightTex (\"Right (RGB)\", RECT) = \"white\" {}" +
" }" +
" Category" +
" {" +
" ZWrite Off" +
" Lighting On" +
" Tags {Queue=Transparent}" +
" SubShader" +
" {" +
" Pass" +
" {" +
" ColorMask R" +
" Cull Off" +
" Material" +
" {" +
" Emission [_Color]" +
" }" +
"" +
" SetTexture [_LeftTex]" +
" {" +
" Combine texture * primary, texture + primary" +
" }" +
" }" +
"" +
" Pass" +
" {" +
" ColorMask GB" +
" Cull Off" +
" Material" +
" {" +
" Emission [_Color]" +
" }" +
"" +
" SetTexture [_RightTex]" +
" {" +
" Combine texture * primary, texture + primary" +
" }" +
" }" +
" }" +
" }" +
"}"
);
leftEye.camera.targetTexture = leftEyeRT;
rightEye.camera.targetTexture = rightEyeRT;
anaglyphMat.SetTexture ("_LeftTex", leftEyeRT);
anaglyphMat.SetTexture ("_RightTex", rightEyeRT);
leftEye.camera.depth = camera.depth -2;
rightEye.camera.depth = camera.depth -1;
leftEye.transform.position = transform.position + transform.TransformDirection(-eyeDistance, 0, 0);
rightEye.transform.position = transform.position + transform.TransformDirection(eyeDistance, 0, 0);
leftEye.transform.rotation = transform.rotation;
rightEye.transform.rotation = transform.rotation;
leftEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * focalDistance));
rightEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * focalDistance));
leftEye.transform.parent = transform;
rightEye.transform.parent = transform;
camera.cullingMask = 0;
camera.backgroundColor = Color (0,0,0,0);
camera.Render();
camera.clearFlags = CameraClearFlags.Nothing;
}
function OnPostRender ()
{
GL.PushMatrix ();
GL.LoadOrtho ();
anaglyphMat.SetPass (0);
GL.Begin (GL.QUADS);
GL.TexCoord2 (0, 0); GL.Vertex3 (0, 0, 1);
GL.TexCoord2 (1, 0); GL.Vertex3 (1, 0, 1);
GL.TexCoord2 (1, 1); GL.Vertex3 (1, 1, 1);
GL.TexCoord2 (0, 1); GL.Vertex3 (0, 1, 1);
GL.End ();
GL.PopMatrix ();
GL.PushMatrix ();
GL.LoadOrtho ();
anaglyphMat.SetPass (1);
GL.Begin (GL.QUADS);
GL.TexCoord2 (0, 0); GL.Vertex3 (0, 0, 1);
GL.TexCoord2 (1, 0); GL.Vertex3 (1, 0, 1);
GL.TexCoord2 (1, 1); GL.Vertex3 (1, 1, 1);
GL.TexCoord2 (0, 1); GL.Vertex3 (0, 1, 1);
GL.End ();
GL.PopMatrix ();
}