• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Daelus · Feb 16, 2013 at 02:49 PM · camerajavascriptrenderingportalrendertotexture

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];
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
             }
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Does the projection matrix calculation affect the shader? 1 Answer

How would you render a seemlessly wrapping scene? 1 Answer

Main Camera Stops working or just freezes? 0 Answers

Is it possible to view two camera at the same time? 2 Answers

Problem of using multiple scripts attached to camera containing OnPostRender() for VR projects 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges