Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 JeezumsDev · Dec 27, 2021 at 06:07 PM · camerarenderingoculusportal

Can Someone Help Me Figure Out a VR Problem?

Maybe someone can help me as I've searched the internet for many hours and have yet to find any clear method for this that works.

I am trying to make portal rendering work in my current VR project. The portals work similarly to those in the Portal games. When I am playing the game without the VR set on the portals work fine as seen in the image below.

alt text

When I try to use VR with the same methods, the rendering ends up looking like the image below (seen from the left eye).

alt text

On top of this, the portal renders as though the player is cross-eyed when they are looking at it.

I have tried placing overlapping portals and using a culling mask on the camera to make each eye only see one set of portals, but culling mask seems to not work in OVR cameras for Unity.

Here is the script I am using for placing and rendering the camera:

 using UnityEngine;
 using UnityEngine.Rendering;
 using UnityEngine.Rendering.Universal;
 using RenderPipeline = UnityEngine.Rendering.RenderPipelineManager;
  
 public class PortalCamera : MonoBehaviour
 {
     [SerializeField]
     private Portal[] portals = new Portal[2];
  
     [SerializeField]
     private Camera portalCamera;
  
     [SerializeField]
     private int iterations = 7;
  
     private RenderTexture tempTexture1;
     private RenderTexture tempTexture2;
  
     public Camera mainCamera;
  
     private void Awake()
     {
         tempTexture1 = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
         tempTexture2 = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
     }
  
     private void Start()
     {
         portals[0].Renderer.material.mainTexture = tempTexture1;
         portals[1].Renderer.material.mainTexture = tempTexture2;
     }
  
     private void OnEnable()
     {
         RenderPipeline.beginCameraRendering += UpdateCamera;
     }
  
     private void OnDisable()
     {
         RenderPipeline.beginCameraRendering -= UpdateCamera;
     }
  
     void UpdateCamera(ScriptableRenderContext SRC, Camera camera)
     {
         if (!portals[0].IsPlaced || !portals[1].IsPlaced)
         {
             return;
         }
  
         if (portals[0].Renderer.isVisible)
         {
             portalCamera.targetTexture = tempTexture1;
             for (int i = iterations - 1; i >= 0; --i)
             {
                 RenderCamera(portals[0], portals[1], i, SRC);
             }
         }
  
         if(portals[1].Renderer.isVisible)
         {
             portalCamera.targetTexture = tempTexture2;
             for (int i = iterations - 1; i >= 0; --i)
             {
                 RenderCamera(portals[1], portals[0], i, SRC);
             }
         }
     }
  
     private void RenderCamera(Portal inPortal, Portal outPortal, int iterationID, ScriptableRenderContext SRC)
     {
         Transform inTransform = inPortal.transform;
         Transform outTransform = outPortal.transform;
  
         Transform cameraTransform = portalCamera.transform;
         cameraTransform.position = transform.position;
         cameraTransform.rotation = transform.rotation;
  
         for(int i = 0; i <= iterationID; ++i)
         {
             // Position the camera behind the other portal.
             Vector3 relativePos = inTransform.InverseTransformPoint(cameraTransform.position);
             relativePos = Quaternion.Euler(0.0f, 180.0f, 0.0f) * relativePos;
             cameraTransform.position = outTransform.TransformPoint(relativePos);
  
             // Rotate the camera to look through the other portal.
             Quaternion relativeRot = Quaternion.Inverse(inTransform.rotation) * cameraTransform.rotation;
             relativeRot = Quaternion.Euler(0.0f, 180.0f, 0.0f) * relativeRot;
             cameraTransform.rotation = outTransform.rotation * relativeRot;
         }
  
         // Set the camera's oblique view frustum.
         Plane p = new Plane(-outTransform.forward, outTransform.position);
         Vector4 clipPlaneWorldSpace = new Vector4(p.normal.x, p.normal.y, p.normal.z, p.distance);
         Vector4 clipPlaneCameraSpace =
             Matrix4x4.Transpose(Matrix4x4.Inverse(portalCamera.worldToCameraMatrix)) * clipPlaneWorldSpace;
  
         var newMatrix = mainCamera.CalculateObliqueMatrix(clipPlaneCameraSpace);
         portalCamera.projectionMatrix = newMatrix;
  
         // Render the camera to its render target.
         UniversalRenderPipeline.RenderSingleCamera(SRC, portalCamera);
     }
 }


Here is the custom shader I am using:

 Shader "Portals/PortalMask"
 {
     Properties
     {
         _MainTex("Main Texture", 2D) = "white" {}
     }
     SubShader
     {
         Tags 
         { 
             "RenderType" = "Opaque"
             "Queue" = "Geometry"
             "RenderPipeline" = "UniversalPipeline"
         }
 
         HLSLINCLUDE
             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
         ENDHLSL
 
         Pass
         {
             Name "Mask"
 
             Stencil
             {
                 Ref 1
                 Pass replace
             }
 
             HLSLPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
 
                 struct appdata
                 {
                     float4 vertex : POSITION;
                 };
 
                 struct v2f
                 {
                     float4 vertex : SV_POSITION;
                     float4 screenPos : TEXCOORD0;
                 };
 
                 v2f vert(appdata v)
                 {
                     v2f o;
                     o.vertex = TransformObjectToHClip(v.vertex.xyz);
                     o.screenPos = ComputeScreenPos(o.vertex);
                     return o;
                 }
 
                 uniform sampler2D _MainTex;
 
                 float4 frag(v2f i) : SV_Target
                 {
                     float2 uv = i.screenPos.xy / i.screenPos.w;
                     float4 col = tex2D(_MainTex, uv);
                     return col;
                 }
             ENDHLSL
         }
     }
 }


If anyone can help me, it would be greatly appreciated. Thanks.

portal-non-vr-screenshot-2.png (499.0 kB)
portal-vr-screenshot-2.png (465.2 kB)
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

1 Reply

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

Answer by JeezumsDev · Jan 07 at 01:00 AM

For anyone that might have this problem in the future, I ended up fixing this by changing the script I used for rendering the portals to one similar to the one Sebastian Lague made on YouTube and I went from there. There was another problem I encountered which I discovered was cause by URP. I removed URP from my project and the portals seemed to work fine visually in VR as well as not in VR. I also changed the way I was doing VR by removing the OVR prefabs from the project entirely and using the XR default one.

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.

Follow this Question

Answers Answers and Comments

213 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

VR mirror view custom size and FoV 0 Answers

Need some help with portal rendering 2 Answers

Does the projection matrix calculation affect the shader? 1 Answer

Object seems to be moving while player (3D, 1st person) moving around it. 1 Answer

Oculus Quest 2 can't display skybox 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