Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Rendering culling results from another camera's view produces unexpected lighting

Discussion in 'Universal Render Pipeline' started by DeificGames, Dec 30, 2021.

  1. DeificGames

    DeificGames

    Joined:
    Dec 12, 2015
    Posts:
    25
    I am attempting to use a render feature to render objects from another camera's view. I'm getting the culling results from the second camera's view matrix, and rendering that to the main camera. Essentially rendering as if there were multiple cameras but without actually needing to render a second camera. This mostly works fine, except for one thing:

    The problem: As you can see in the screenshot below, the lighting isn't right. The lighting and skybox reflection seems to be using the main camera's transformation matrix and the object's matrix.

    Any ideas on how to resolve this?

    Note: In the screenshot below, sphere A is rendered normally by the main camera, while sphere B is rendered from the adjusted view matrix.

    Here's the code from the render pass:

    Code (CSharp):
    1.  
    2. //Get view matrix
    3.             Matrix4x4 projectionMatrix = Matrix4x4.Perspective(mainCam.fieldOfView, mainCam.aspect, mainCam.nearClipPlane, mainCam.farClipPlane);
    4.             Matrix4x4 worldToProjectionMatrix = projectionMatrix * virtualCameraTRS;
    5.             Matrix4x4 viewMatrix = virtualCameraTRS.inverse;
    6.             if (SystemInfo.usesReversedZBuffer) {
    7.                 viewMatrix.m20 = -viewMatrix.m20;
    8.                 viewMatrix.m21 = -viewMatrix.m21;
    9.                 viewMatrix.m22 = -viewMatrix.m22;
    10.                 viewMatrix.m23 = -viewMatrix.m23;
    11.             };
    12.  
    13.             Matrix4x4 gpuProjectionMatrix = GL.GetGPUProjectionMatrix(projectionMatrix, renderingData.cameraData.IsCameraProjectionMatrixFlipped());
    14.  
    15.             CommandBuffer cmd = CommandBufferPool.Get();
    16.             RenderingUtils.SetViewAndProjectionMatrices(cmd, viewMatrix, gpuProjectionMatrix, renderingData.cameraData.IsCameraProjectionMatrixFlipped());      
    17.             context.ExecuteCommandBuffer(cmd);
    18.             cmd.Release();
    19.  
    20.             //Get culling result
    21.             renderingData.cameraData.camera.TryGetCullingParameters(out ScriptableCullingParameters cullingParameters);
    22.             cullingParameters.cullingMatrix = projectionMatrix * viewMatrix;                  
    23.             Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(cullingParameters.cullingMatrix);
    24.             for (int i = 0; i < 6; i++) {
    25.                 cullingParameters.SetCullingPlane(i, frustumPlanes[i]);
    26.             }
    27.             CullingResults cullingResults = context.Cull(ref cullingParameters);
    28.  
    29.             shaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit"));
    30.             shaderTagIdList.Add(new ShaderTagId("UniversalForward"));
    31.             shaderTagIdList.Add(new ShaderTagId("UniversalForwardOnly"));
    32.             SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
    33.             DrawingSettings drawingSettings = CreateDrawingSettings(shaderTagIdList, ref renderingData, sortingCriteria);                  
    34.             RenderQueueRange renderQueueRange = RenderQueueRange.all;
    35.             FilteringSettings filteringSettings = new FilteringSettings(renderQueueRange);
    36.             RenderStateBlock renderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
    37.  
    38.             context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings, ref renderStateBlock);
    39.  



     
    Last edited: Dec 30, 2021
  2. DeificGames

    DeificGames

    Joined:
    Dec 12, 2015
    Posts:
    25
    Solved, but the solution is a bit convoluted and this is such a niche issue, so I won't be posting the entire solution here.
     
  3. ZenTeapot

    ZenTeapot

    Joined:
    Oct 19, 2014
    Posts:
    65
    Any hints as to how this is done? Thanks