• 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
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

People who like this

0 Show 0
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
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

People who like this

0 Show 0 · 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

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

People who like this

0 Show 0 · 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Can Someone Help Me Figure Out a VR Problem? 1 Answer

How would you render a seemlessly wrapping scene? 1 Answer

Main Camera Stops working or just freezes? 0 Answers

VideoPlayer component using near plane with LRP 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges