NormalReconstruction.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. namespace UnityEngine.Rendering.Universal.Internal
  2. {
  3. /// <summary>
  4. /// Util class for normal reconstruction.
  5. /// </summary>
  6. public static class NormalReconstruction
  7. {
  8. private static readonly int s_NormalReconstructionMatrixID = Shader.PropertyToID("_NormalReconstructionMatrix");
  9. private static Matrix4x4[] s_NormalReconstructionMatrix = new Matrix4x4[2];
  10. /// <summary>
  11. /// Setup properties needed for normal reconstruction from depth using shader functions in NormalReconstruction.hlsl
  12. /// </summary>
  13. /// <param name="cmd">Command Buffer used for properties setup.</param>
  14. /// <param name="cameraData">CameraData containing camera matrices information.</param>
  15. public static void SetupProperties(CommandBuffer cmd, in CameraData cameraData)
  16. {
  17. #if ENABLE_VR && ENABLE_XR_MODULE
  18. int eyeCount = cameraData.xr.enabled && cameraData.xr.singlePassEnabled ? 2 : 1;
  19. #else
  20. int eyeCount = 1;
  21. #endif
  22. for (int eyeIndex = 0; eyeIndex < eyeCount; eyeIndex++)
  23. {
  24. Matrix4x4 view = cameraData.GetViewMatrix(eyeIndex);
  25. Matrix4x4 proj = cameraData.GetProjectionMatrix(eyeIndex);
  26. s_NormalReconstructionMatrix[eyeIndex] = proj * view;
  27. // camera view space without translation, used by SSAO.hlsl ReconstructViewPos() to calculate view vector.
  28. Matrix4x4 cview = view;
  29. cview.SetColumn(3, new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  30. Matrix4x4 cviewProj = proj * cview;
  31. Matrix4x4 cviewProjInv = cviewProj.inverse;
  32. s_NormalReconstructionMatrix[eyeIndex] = cviewProjInv;
  33. }
  34. cmd.SetGlobalMatrixArray(s_NormalReconstructionMatrixID, s_NormalReconstructionMatrix);
  35. }
  36. }
  37. }