I’ve found a pretty reasonable solution for active stereoscopy that I’ve been using with my Mac using Quartz Composer and now am making it work in Unity.
I bought a brand new 60" Mitsubishi DLP (3d Ready) TV for $900. That is the smallest one believe it or not. They have a 73" for $1500 and even an 82".
They use the checkerboard interlaced method which is more forgiving if frame rates drop since both right and left and drawn at the same time in my Mac and the DLP TV takes care of the rest. Video input standard is 1920x1080p @60hz.
I also bought a pair of shutter glasses and emitter. These work fine but are a little heavier than I would like and eventually bother the bridge of my nose.
http://www.i-glassesstore.com/dlp3d-wireless-2set.html
I’m still quite new to Unity but have got Unity outputting the correct format thanks to a post and project upload by Ekzuzy.
http://forum.unity3d.com/viewtopic.php?t=33761&highlight=stereoscopy
His project outputted stereo in interlaced format. I just tweaked his shader to make it a checkerboard pattern which works with my DLP TV.
from this (interlaced)
int x = fmod( IN.uv.y, 2 );
to this (checkerboard)
int x = fmod(floor( IN.uv.x) + floor(IN.uv.y),2) <1;
Although this method works, it doesn’t appear to be drawing each camera in sync with the other which makes panning in the scene hard to look at. I believe every other frame is left eye and every other is the right.
I’ve currently been experimenting with the Anaglyph Camera script that has been building in the “Stereo - Anagylph Rendering HELP!!! ;)” thread and have been pretty happy with it since it now has the correction for the projection matrix and is quite elegant how it just has to be attached to one camera and handles the rest. Big props to all those who contributed to that thread
here’s the current Anaglyph script I’m using
var anaglyphMat;
var leftEyeRT;
var rightEyeRT;
var leftEye;
var rightEye;
var enableKeys : boolean = true;
var downEyeDistance : KeyCode = KeyCode.O;
var upEyeDistance : KeyCode = KeyCode.P;
var downFocalDistance : KeyCode = KeyCode.K;
var upFocalDistance : KeyCode = KeyCode.L;
var zvalue : float = 0.0; // original: 1.0
class S3DV extends System.Object {
static var eyeDistance = 0.035;
static var focalDistance = 6.5;
};
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" +
" ZTest Always" +
" 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(-S3DV.eyeDistance, 0, 0);
rightEye.transform.position = transform.position + transform.TransformDirection(S3DV.eyeDistance, 0, 0);
leftEye.transform.rotation = transform.rotation;
rightEye.transform.rotation = transform.rotation;
leftEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * S3DV.focalDistance));
rightEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * S3DV.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;
//camera.enabled = false;
}
function Stop () {
}
function UpdateView() {
leftEye.camera.depth = camera.depth -2;
rightEye.camera.depth = camera.depth -1;
leftEye.transform.position = transform.position + transform.TransformDirection(-S3DV.eyeDistance, 0, 0);
rightEye.transform.position = transform.position + transform.TransformDirection(S3DV.eyeDistance, 0, 0);
leftEye.transform.rotation = transform.rotation;
rightEye.transform.rotation = transform.rotation;
leftEye.camera.projectionMatrix = projectionMatrix(true);
rightEye.camera.projectionMatrix = projectionMatrix(false);
leftEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * S3DV.focalDistance));
rightEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * S3DV.focalDistance));
leftEye.transform.parent = transform;
rightEye.transform.parent = transform;
}
function LateUpdate() {
UpdateView();
if (enableKeys) {
// o and p
var eyeDistanceAdjust : float = 0.01;
if (Input.GetKeyDown(upEyeDistance)) {
S3DV.eyeDistance += eyeDistanceAdjust;
} else if (Input.GetKeyDown(downEyeDistance)) {
S3DV.eyeDistance -= eyeDistanceAdjust;
}
// k and l
var focalDistanceAdjust : float = 0.5;
if (Input.GetKeyDown(upFocalDistance)) {
//Debug.Log("focal up");
S3DV.focalDistance += focalDistanceAdjust;
} else if (Input.GetKeyDown(downFocalDistance)) {
S3DV.focalDistance -= focalDistanceAdjust;
}
}
}
function OnRenderImage (source:RenderTexture, destination:RenderTexture) {
RenderTexture.active = destination;
GL.PushMatrix();
GL.LoadOrtho();
for(var i:int = 0; i < anaglyphMat.passCount; i++) {
anaglyphMat.SetPass(i);
DrawQuad();
}
GL.PopMatrix();
}
private function DrawQuad() {
GL.Begin (GL.QUADS);
GL.TexCoord2( 0.0, 0.0 ); GL.Vertex3( 0.0, 0.0, zvalue );
GL.TexCoord2( 1.0, 0.0 ); GL.Vertex3( 1.0, 0.0, zvalue );
GL.TexCoord2( 1.0, 1.0 ); GL.Vertex3( 1.0, 1.0, zvalue );
GL.TexCoord2( 0.0, 1.0 ); GL.Vertex3( 0.0, 1.0, zvalue );
GL.End();
}
function PerspectiveOffCenter(
left : float, right : float,
bottom : float, top : float,
near : float, far : float ) : Matrix4x4
{
var x = (2.0 * near) / (right - left);
var y = (2.0 * near) / (top - bottom);
var a = (right + left) / (right - left);
var b = (top + bottom) / (top - bottom);
var c = -(far + near) / (far - near);
var d = -(2.0 * far * near) / (far - near);
var e = -1.0;
var m : Matrix4x4;
m[0,0] = x; m[0,1] = 0; m[0,2] = a; m[0,3] = 0;
m[1,0] = 0; m[1,1] = y; m[1,2] = b; m[1,3] = 0;
m[2,0] = 0; m[2,1] = 0; m[2,2] = c; m[2,3] = d;
m[3,0] = 0; m[3,1] = 0; m[3,2] = e; m[3,3] = 0;
return m;
}
function projectionMatrix(isLeftEye : boolean) : Matrix4x4 {
var left : float;
var right : float;
var a : float;
var b : float;
var fov : float;
fov = camera.fieldOfView / 180.0 * Mathf.PI; // convert FOV to radians
var aspect : float = camera.aspect;
a = camera.nearClipPlane * Mathf.Tan(fov * 0.5);
b = camera.nearClipPlane / S3DV.focalDistance;
if (isLeftEye) // left camera
{
left = - aspect * a + (S3DV.eyeDistance) * b;
right = aspect * a + (S3DV.eyeDistance) * b;
}
else // right camera
{
left = - aspect * a - (S3DV.eyeDistance) * b;
right = aspect * a - (S3DV.eyeDistance) * b;
}
return PerspectiveOffCenter(left, right, -a, a, camera.nearClipPlane, camera.farClipPlane);
}
My hope is to embed the CG shader fragment from the one project with the Anaglyph Camera script and I’ve made several attempts. Here’s what I have currently. I does not run correctly and has the following errors. (see attachment) Unfortunately I’m still very new to Unity, Shaderlab, and shader writing in general and can’t get figure out the errors.
var checkerMat;
var leftEyeRT;
var rightEyeRT;
var leftEye;
var rightEye;
var enableKeys : boolean = true;
var downEyeDistance : KeyCode = KeyCode.O;
var upEyeDistance : KeyCode = KeyCode.P;
var downFocalDistance : KeyCode = KeyCode.K;
var upFocalDistance : KeyCode = KeyCode.L;
var zvalue : float = 0.0; // original: 1.0
class S3DV extends System.Object {
static var eyeDistance = 0.035;
static var focalDistance = 6.5;
};
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);
checkerMat = new Material
(
"Shader \"Hidden/CheckerStereocopy\"" +
"{" +
" Properties" +
" {" +
" _LeftTex (\"Left (RGB)\", RECT) = \"white\" {}" +
" _RightTex (\"Right (RGB)\", RECT) = \"white\" {}" +
" }" +
" SubShader" +
" {" +
" Pass " +
" { " +
" ZTest Always " +
" Cull Off " +
" ZWrite Off" +
" Fog " +
" { " +
" Mode off " +
" } "+
""+
" CGPROGRAM" +
""+
" #include \"UnityCG.cginc\" "+
" #pragma vertex vert_img "+
" #pragma fragment frag "+
" #pragma fragmentoption ARB_precision_hint_fastest "+
""+
" uniform samplerRECT _LeftTex; "+
" uniform samplerRECT _RightTex; "+
""+
" float4 frag( v2f_img IN ) : COLOR0 "+
" { "+
" float3 left = texRECT( _LeftTex, IN.uv ).rgb; "+ // Sample scene texture
" float3 right = texRECT( _RightTex, IN.uv ).rgb; "+ // Sample scene texture
""+
" int x = fmod(floor( IN.uv.x) + floor(IN.uv.y),2) <1; "+
""+
" return float4( lerp( left, right, x ), 0.0 ); "+
" } "+
""+
" ENDCG "+
""+
" } "+
" }"+
" Fallback Off "+
"}"
);
leftEye.camera.targetTexture = leftEyeRT;
rightEye.camera.targetTexture = rightEyeRT;
checkerMat.SetTexture ("_LeftTex", leftEyeRT);
checkerMat.SetTexture ("_RightTex", rightEyeRT);
leftEye.camera.depth = camera.depth -2;
rightEye.camera.depth = camera.depth -1;
leftEye.transform.position = transform.position + transform.TransformDirection(-S3DV.eyeDistance, 0, 0);
rightEye.transform.position = transform.position + transform.TransformDirection(S3DV.eyeDistance, 0, 0);
leftEye.transform.rotation = transform.rotation;
rightEye.transform.rotation = transform.rotation;
leftEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * S3DV.focalDistance));
rightEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * S3DV.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;
//camera.enabled = false;
}
function Stop () {
}
function UpdateView() {
leftEye.camera.depth = camera.depth -2;
rightEye.camera.depth = camera.depth -1;
leftEye.transform.position = transform.position + transform.TransformDirection(-S3DV.eyeDistance, 0, 0);
rightEye.transform.position = transform.position + transform.TransformDirection(S3DV.eyeDistance, 0, 0);
leftEye.transform.rotation = transform.rotation;
rightEye.transform.rotation = transform.rotation;
leftEye.camera.projectionMatrix = projectionMatrix(true);
rightEye.camera.projectionMatrix = projectionMatrix(false);
leftEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * S3DV.focalDistance));
rightEye.transform.LookAt (transform.position + (transform.TransformDirection (Vector3.forward) * S3DV.focalDistance));
leftEye.transform.parent = transform;
rightEye.transform.parent = transform;
}
function LateUpdate() {
UpdateView();
if (enableKeys) {
// o and p
var eyeDistanceAdjust : float = 0.01;
if (Input.GetKeyDown(upEyeDistance)) {
S3DV.eyeDistance += eyeDistanceAdjust;
} else if (Input.GetKeyDown(downEyeDistance)) {
S3DV.eyeDistance -= eyeDistanceAdjust;
}
// k and l
var focalDistanceAdjust : float = 0.5;
if (Input.GetKeyDown(upFocalDistance)) {
//Debug.Log("focal up");
S3DV.focalDistance += focalDistanceAdjust;
} else if (Input.GetKeyDown(downFocalDistance)) {
S3DV.focalDistance -= focalDistanceAdjust;
}
}
}
function OnRenderImage (source:RenderTexture, destination:RenderTexture) {
RenderTexture.active = destination;
GL.PushMatrix();
GL.LoadOrtho();
for(var i:int = 0; i < checkerMat.passCount; i++) {
checkerMat.SetPass(i);
DrawQuad();
}
GL.PopMatrix();
}
private function DrawQuad() {
GL.Begin (GL.QUADS);
GL.TexCoord2( 0.0, 0.0 ); GL.Vertex3( 0.0, 0.0, zvalue );
GL.TexCoord2( 1.0, 0.0 ); GL.Vertex3( 1.0, 0.0, zvalue );
GL.TexCoord2( 1.0, 1.0 ); GL.Vertex3( 1.0, 1.0, zvalue );
GL.TexCoord2( 0.0, 1.0 ); GL.Vertex3( 0.0, 1.0, zvalue );
GL.End();
}
function PerspectiveOffCenter(
left : float, right : float,
bottom : float, top : float,
near : float, far : float ) : Matrix4x4
{
var x = (2.0 * near) / (right - left);
var y = (2.0 * near) / (top - bottom);
var a = (right + left) / (right - left);
var b = (top + bottom) / (top - bottom);
var c = -(far + near) / (far - near);
var d = -(2.0 * far * near) / (far - near);
var e = -1.0;
var m : Matrix4x4;
m[0,0] = x; m[0,1] = 0; m[0,2] = a; m[0,3] = 0;
m[1,0] = 0; m[1,1] = y; m[1,2] = b; m[1,3] = 0;
m[2,0] = 0; m[2,1] = 0; m[2,2] = c; m[2,3] = d;
m[3,0] = 0; m[3,1] = 0; m[3,2] = e; m[3,3] = 0;
return m;
}
function projectionMatrix(isLeftEye : boolean) : Matrix4x4 {
var left : float;
var right : float;
var a : float;
var b : float;
var fov : float;
fov = camera.fieldOfView / 180.0 * Mathf.PI; // convert FOV to radians
var aspect : float = camera.aspect;
a = camera.nearClipPlane * Mathf.Tan(fov * 0.5);
b = camera.nearClipPlane / S3DV.focalDistance;
if (isLeftEye) // left camera
{
left = - aspect * a + (S3DV.eyeDistance) * b;
right = aspect * a + (S3DV.eyeDistance) * b;
}
else // right camera
{
left = - aspect * a - (S3DV.eyeDistance) * b;
right = aspect * a - (S3DV.eyeDistance) * b;
}
return PerspectiveOffCenter(left, right, -a, a, camera.nearClipPlane, camera.farClipPlane);
}
If someone could help get this script running, I believe it would be useful to many that go with the DLP 3d TV screen option. It’s fairly affordable $1000 for a 60" and shutter glasses and this system also works with the 3d ready games on the XBox 360 such as Avatar. Even though it’s not really mentioned on the box, there is a setup parameter that makes the game output in the various 3d formats including the checkerboard method.
Also, I second rahuxx request for native options for active stereoscopy in Unity 3.0. There’s a huge push towards stereoscopic displays. Some built in Stereoscopic camera scripts/shaders would be an awesome addition to Unity 3.0x.
Thanks again to all that have contributed their great work.
cheers
Stuart
