ShaderVariablesFunctions.hlsl 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED
  2. #define UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
  4. // Note: '_WorldSpaceCameraPos' is set by the legacy Unity code.
  5. float3 GetPrimaryCameraPosition()
  6. {
  7. #if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
  8. return float3(0, 0, 0);
  9. #else
  10. return _WorldSpaceCameraPos;
  11. #endif
  12. }
  13. // Could be e.g. the position of a primary camera or a shadow-casting light.
  14. float3 GetCurrentViewPosition()
  15. {
  16. #if defined(SHADERPASS) && (SHADERPASS != SHADERPASS_SHADOWS)
  17. return GetPrimaryCameraPosition();
  18. #else
  19. // This is a generic solution.
  20. // However, for the primary camera, using '_WorldSpaceCameraPos' is better for cache locality,
  21. // and in case we enable camera-relative rendering, we can statically set the position is 0.
  22. return UNITY_MATRIX_I_V._14_24_34;
  23. #endif
  24. }
  25. // Returns the forward (central) direction of the current view in the world space.
  26. float3 GetViewForwardDir()
  27. {
  28. float4x4 viewMat = GetWorldToViewMatrix();
  29. return -viewMat[2].xyz;
  30. }
  31. // Returns 'true' if the current view performs a perspective projection.
  32. bool IsPerspectiveProjection()
  33. {
  34. #if defined(SHADERPASS) && (SHADERPASS != SHADERPASS_SHADOWS)
  35. return (unity_OrthoParams.w == 0);
  36. #else
  37. // TODO: set 'unity_OrthoParams' during the shadow pass.
  38. return UNITY_MATRIX_P[3][3] == 0;
  39. #endif
  40. }
  41. // Computes the world space view direction (pointing towards the viewer).
  42. float3 GetWorldSpaceNormalizeViewDir(float3 positionWS)
  43. {
  44. if (IsPerspectiveProjection())
  45. {
  46. // Perspective
  47. float3 V = GetCurrentViewPosition() - positionWS;
  48. return normalize(V);
  49. }
  50. else
  51. {
  52. // Orthographic
  53. return -GetViewForwardDir();
  54. }
  55. }
  56. // UNITY_MATRIX_V defines a right-handed view space with the Z axis pointing towards the viewer.
  57. // This function reverses the direction of the Z axis (so that it points forward),
  58. // making the view space coordinate system left-handed.
  59. void GetLeftHandedViewSpaceMatrices(out float4x4 viewMatrix, out float4x4 projMatrix)
  60. {
  61. viewMatrix = UNITY_MATRIX_V;
  62. viewMatrix._31_32_33_34 = -viewMatrix._31_32_33_34;
  63. projMatrix = UNITY_MATRIX_P;
  64. projMatrix._13_23_33_43 = -projMatrix._13_23_33_43;
  65. }
  66. #if UNITY_REVERSED_Z
  67. #if SHADER_API_OPENGL || SHADER_API_GLES || SHADER_API_GLES3
  68. //GL with reversed z => z clip range is [near, -far] -> should remap in theory but dont do it in practice to save some perf (range is close enough)
  69. #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max(-(coord), 0)
  70. #else
  71. //D3d with reversed Z => z clip range is [near, 0] -> remapping to [0, far]
  72. //max is required to protect ourselves from near plane not being correct/meaningfull in case of oblique matrices.
  73. #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max(((1.0-(coord)/_ProjectionParams.y)*_ProjectionParams.z),0)
  74. #endif
  75. #elif UNITY_UV_STARTS_AT_TOP
  76. //D3d without reversed z => z clip range is [0, far] -> nothing to do
  77. #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) (coord)
  78. #else
  79. //Opengl => z clip range is [-near, far] -> should remap in theory but dont do it in practice to save some perf (range is close enough)
  80. #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) (coord)
  81. #endif
  82. #endif // UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED