1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #ifndef UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED
- #define UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED
- #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
- // Note: '_WorldSpaceCameraPos' is set by the legacy Unity code.
- float3 GetPrimaryCameraPosition()
- {
- #if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
- return float3(0, 0, 0);
- #else
- return _WorldSpaceCameraPos;
- #endif
- }
- // Could be e.g. the position of a primary camera or a shadow-casting light.
- float3 GetCurrentViewPosition()
- {
- #if defined(SHADERPASS) && (SHADERPASS != SHADERPASS_SHADOWS)
- return GetPrimaryCameraPosition();
- #else
- // This is a generic solution.
- // However, for the primary camera, using '_WorldSpaceCameraPos' is better for cache locality,
- // and in case we enable camera-relative rendering, we can statically set the position is 0.
- return UNITY_MATRIX_I_V._14_24_34;
- #endif
- }
- // Returns the forward (central) direction of the current view in the world space.
- float3 GetViewForwardDir()
- {
- float4x4 viewMat = GetWorldToViewMatrix();
- return -viewMat[2].xyz;
- }
- // Returns 'true' if the current view performs a perspective projection.
- bool IsPerspectiveProjection()
- {
- #if defined(SHADERPASS) && (SHADERPASS != SHADERPASS_SHADOWS)
- return (unity_OrthoParams.w == 0);
- #else
- // TODO: set 'unity_OrthoParams' during the shadow pass.
- return UNITY_MATRIX_P[3][3] == 0;
- #endif
- }
- // Computes the world space view direction (pointing towards the viewer).
- float3 GetWorldSpaceNormalizeViewDir(float3 positionWS)
- {
- if (IsPerspectiveProjection())
- {
- // Perspective
- float3 V = GetCurrentViewPosition() - positionWS;
- return normalize(V);
- }
- else
- {
- // Orthographic
- return -GetViewForwardDir();
- }
- }
- // UNITY_MATRIX_V defines a right-handed view space with the Z axis pointing towards the viewer.
- // This function reverses the direction of the Z axis (so that it points forward),
- // making the view space coordinate system left-handed.
- void GetLeftHandedViewSpaceMatrices(out float4x4 viewMatrix, out float4x4 projMatrix)
- {
- viewMatrix = UNITY_MATRIX_V;
- viewMatrix._31_32_33_34 = -viewMatrix._31_32_33_34;
- projMatrix = UNITY_MATRIX_P;
- projMatrix._13_23_33_43 = -projMatrix._13_23_33_43;
- }
- #if UNITY_REVERSED_Z
- #if SHADER_API_OPENGL || SHADER_API_GLES || SHADER_API_GLES3
- //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)
- #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max(-(coord), 0)
- #else
- //D3d with reversed Z => z clip range is [near, 0] -> remapping to [0, far]
- //max is required to protect ourselves from near plane not being correct/meaningfull in case of oblique matrices.
- #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) max(((1.0-(coord)/_ProjectionParams.y)*_ProjectionParams.z),0)
- #endif
- #elif UNITY_UV_STARTS_AT_TOP
- //D3d without reversed z => z clip range is [0, far] -> nothing to do
- #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) (coord)
- #else
- //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)
- #define UNITY_Z_0_FAR_FROM_CLIPSPACE(coord) (coord)
- #endif
- #endif // UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED
|