Search Unity

Question URP 7.5.3 Camera RenderToCubemap wrong result

Discussion in 'VR' started by willygaelhypnovr, Mar 1, 2021.

Thread Status:
Not open for further replies.
  1. willygaelhypnovr

    willygaelhypnovr

    Joined:
    Oct 25, 2018
    Posts:
    5
    Hello,

    I'm facing an issue with URP 7.5.3 on VR devices (Oculus and Pico G2 4K)

    In fact, I am trying to render a 360 stereo image during the runtime process. Therefore I used the “Camera.RenderToCubemap” function provided, for each eye, and then convert it to Equirect using the “Camera.ConvertToEquirect” to get the full texture.
    Code (CSharp):
    1.  
    2.             private RenderTexture leftEye;
    3.             private RenderTexture rightEye;
    4.             private RenderTexture equirect;
    5.  
    6.             public Camera LeftCameraEye;
    7.             public Camera RightCameraEye;
    8.  
    9.     public bool shouldRenderCubemap;
    10.  
    11.             private void Start()
    12.             {
    13.              
    14.                             leftEye = RenderTexture.GetTemporary(ResolutionWidth, ResolutionHeight, 24, RenderTextureFormat.ARGB32);
    15.                             rightEye = RenderTexture.GetTemporary(ResolutionWidth, ResolutionHeight, 24, RenderTextureFormat.ARGB32);
    16.                             leftEye.dimension = UnityEngine.Rendering.TextureDimension.Cube;
    17.                             rightEye.dimension = UnityEngine.Rendering.TextureDimension.Cube;
    18.  
    19.                             equirect = RenderTexture.GetTemporary(ResolutionWidth, 2 * ResolutionHeight, 24, RenderTextureFormat.ARGB32);
    20.             }
    21.  
    22.  
    23.             private void LateUpdate()
    24.             {
    25.                 if (shouldRenderCubemap)
    26.                 {
    27.                     CaptureEye(LeftCameraEye, Camera.MonoOrStereoscopicEye.Left, leftEye);
    28.                         CaptureEye(RightCameraEye, Camera.MonoOrStereoscopicEye.Right, rightEye);
    29.                     shouldRenderCubemap = false;
    30.     SaveEquirectCapture();
    31.                 }
    32.             }
    33.  
    34.             private void CaptureEye(Camera eyeCamera, Camera.MonoOrStereoscopicEye eyeSide, RenderTexture target)
    35.             {
    36.                 var rect = new Rect(0, 0, 1, 1);
    37.                 var r = eyeCamera.rect;
    38.                 eyeCamera.rect = rect;
    39.                 eyeCamera.RenderToCubemap(target, 63, eyeSide);
    40.                 eyeCamera.rect = r;
    41.  
    42.                 target.ConvertToEquirect(equirect, eyeSide);
    43.             }
    44.  
    45.             private void OnDestroy()
    46.             {
    47.                 RenderTexture.ReleaseTemporary(leftEye);
    48.                     RenderTexture.ReleaseTemporary(rightEye);
    49.                     RenderTexture.ReleaseTemporary(equirect);
    50.             }
    51.          
    52.  
    53.             private void SaveEquirectCapture()
    54.             {
    55.                 var rt = RenderTexture.active;
    56.                 RenderTexture.active = equirect;
    57.                 var stereoTex = new Texture2D(equirect.width, equirect.height, TextureFormat.ARGB32, false);
    58.                 stereoTex.ReadPixels(new Rect(0, 0, equirect.width, equirect.height), 0, 0);
    59.  
    60.                 RenderTexture.active = rt;
    61.  
    62.                 var bytes = stereoTex.EncodeToPNG();
    63.                 System.IO.File.WriteAllBytes(System.IO.Path.Combine(Application.persistentDataPath, "Capture_" + DateTime.Now.ToString("dd-MM-yy_H-mm-ss") + ".png"), bytes);
    64.                 DestroyImmediate(stereoTex);
    65.             }
    66.    
    However, the result that I get from the Editor is different from the one that I get in the Headset.

    Here is the result from the Editor:
    upload_2021-3-1_14-13-8.png

    Here is the result from the Pico G2 Headset (the result is the same on the Oculus Quest 1):
    upload_2021-3-1_14-13-32.png

    As you can see in the images, by using the same approach from the Editor and from the Headset, the image got from the headset is weird, whereas the one from the editor is what I wanted to obtain from the headset.

    I'm using:

    - Unity 2019.4.21ff (I got the same result with Unity 2019.4.20f1)
    - URP 7.5.3
    - Oculus Quest 1
    - Pico G2 4K



    Does someone have any idea of what is happening and how to resolve this?

    Thanks in advance for your reply.
     
Thread Status:
Not open for further replies.