- Home /
Need some help with portal rendering
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];
}
Answer by Daelus · Feb 17, 2013 at 03:22 PM
Solved my own problem by simply doing the projection in a shader instead of in javascript. I also fixed a few things to correct for aspect ratio differences. For anyone who reads this, simply remove the section from the code above about texture projection, and use the following shader:
Shader "Camera-Based Projection" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
Category {
Fog { Color [_AddFog] }
SubShader {
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma exclude_renderers d3d11 xbox360
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : POSITION;
float4 uvproj;
};
float4 _MainTex_ST;
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uvproj.xy = TRANSFORM_TEX(o.pos, _MainTex);
o.uvproj.zw = o.pos.zw;
#if SHADER_API_D3D9
o.uvproj.y = -o.uvproj.y;
#endif
return o;
}
uniform sampler2D _MainTex;
float4 frag (v2f i) : COLOR
{
i.uvproj /= i.uvproj.w;
i.uvproj = (i.uvproj + 1) * 0.5;
half4 color = tex2D(_MainTex,i.uvproj.xy);
return color;
}
ENDCG
}
}
}
}
Answer by DanJC · Apr 25, 2013 at 08:32 AM
http://answers.unity3d.com/questions/421450/how-can-i-render-a-scene-to-a-surface.html#answer-444650
Uses a 2 line shader instead of RenderToTexture(). To make it one-way... hmm not sure.