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