I’m trying to create a one way visual portal (just like in portal). I’ve got the RenderToTexture working, the cameras are synced up, but then I need to project the RenderToTexture onto to the entrance portal in screen space, and I’m just kind of lost. My code was based heavily off of the MirrorReflection effect on the unity3d wiki. Anyway, any assistance would be greatly appreciated, been banging my head against this for a few days now.
Here’s the full code, left a comment where I think I’m going wrong.
#pragma strict
var PortalResolution : int = 512;
var Destination : Transform;
var CameraPrefab : Transform;
var ShaderToUse : String = "Mirror";
var m_ClipPlaneOffset : float = 0.07f;
private var PortalCamOutput : RenderTexture;
private var PortalCam : Transform;
private var PortalMat : Shader;
private var M_Camera : Camera;
private var M_Camera_Tr : Transform;
function Start()
{
M_Camera = Camera.mainCamera;
M_Camera_Tr = M_Camera.transform;
PortalCam = Instantiate(CameraPrefab, Destination.position, Destination.rotation);
PortalCamOutput = new RenderTexture(PortalResolution, PortalResolution, 16);
PortalCam.camera.targetTexture = PortalCamOutput;
PortalMat = Shader.Find(ShaderToUse);
renderer.material = new Material(PortalMat);
renderer.material.mainTexture = PortalCamOutput;
}
function Update()
{
var PosOffset : Vector3 = transform.position - M_Camera_Tr.position;
var pos : Vector3 = Destination.position;
var normal : Vector3 = Destination.position.up;
var dot : float = Vector3.Dot(normal, pos) - m_ClipPlaneOffset;
var clipPlane : Vector4 = CameraSpacePlane(PortalCam.camera, pos, normal, 1.0f);
var projection : Matrix4x4 = M_Camera.projectionMatrix;
CalculateObliqueMatrix (projection, clipPlane);
PortalCam.camera.projectionMatrix = projection;
var newpos : Vector3 = Destination.position - PosOffset;
PortalCam.position = newpos;
var euler : Vector3= M_Camera_Tr.eulerAngles;
PortalCam.eulerAngles = new Vector3(euler.x, euler.y, 0);
PortalCam.camera.Render();
//////////////////////////////////////
/*Help needed here (I think) */
/////////////////////////////////////
//calculate portal texture projection
var scaleOffset : Matrix4x4 = Matrix4x4.TRS(Vector3(0.5f,0.5f,0.5f), Quaternion.identity, Vector3(0.5f,0.5f,0.5f) );
var scale : Vector3= transform.lossyScale;
var mtx : Matrix4x4 = transform.localToWorldMatrix * Matrix4x4.Scale(Vector3(1.0f/scale.x, 1.0f/scale.y, 1.0f/scale.z) );
mtx = scaleOffset * M_Camera.projectionMatrix * M_Camera.worldToCameraMatrix * mtx;
renderer.material.SetMatrix( "_ProjMatrix", mtx);
}
// Given position/normal of the plane, calculates plane in camera space.
function CameraSpacePlane (cam : Camera, pos : Vector3, normal : Vector3, sideSign : float)
{
var offsetPos : Vector3 = pos + normal * m_ClipPlaneOffset;
var m : Matrix4x4= cam.worldToCameraMatrix;
var cpos : Vector3 = m.MultiplyPoint( offsetPos );
var cnormal : Vector3= m.MultiplyVector( normal ).normalized * sideSign;
return new Vector4 (cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos, cnormal) );
}
// Extended sign: returns -1, 0 or 1 based on sign of a
function sgn(a : float)
{
if (a > 0.0f) return 1.0f;
if (a < 0.0f) return -1.0f;
return 0.0f;
}
// Adjusts the given projection matrix so that near plane is the given clipPlane
// clipPlane is given in camera space.
function CalculateObliqueMatrix (projection : Matrix4x4, clipPlane : Vector4)
{
var q : Vector4 = projection.inverse * new Vector4(sgn(clipPlane.x), sgn(clipPlane.y), 1.0f, 1.0f);
var c : Vector4 = clipPlane * (2.0F / (Vector4.Dot (clipPlane, q)));
// third row = clip plane - fourth row
projection[2] = c.x - projection[3];
projection[6] = c.y - projection[7];
projection[10] = c.z - projection[11];
projection[14] = c.w - projection[15];
}