/ Assets / shaders / Dump / NMGGeometryHelpers.hlsl
NMGGeometryHelpers.hlsl
 1  // MIT License
 2  
 3  // Copyright (c) 2020 NedMakesGames
 4  
 5  // Permission is hereby granted, free of charge, to any person obtaining a copy
 6  // of this software and associated documentation files(the "Software"), to deal
 7  // in the Software without restriction, including without limitation the rights
 8  // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
 9  // copies of the Software, and to permit persons to whom the Software is
10  // furnished to do so, subject to the following conditions :
11  
12  // The above copyright notice and this permission notice shall be included in all
13  // copies or substantial portions of the Software.
14  
15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  // SOFTWARE.
22  
23  // Make sure this file is not included twice
24  #ifndef NMG_HELPERS_INCLUDED
25  #define NMG_HELPERS_INCLUDED
26  
27  // Math
28  
29  // Returns the normal of a plane containing the triangle defined by the three arguments
30  float3 GetNormalFromTriangle(float3 a, float3 b, float3 c) {
31      return normalize(cross(b - a, c - a));
32  }
33  
34  // Returns the center point of a triangle defined by the three arguments
35  float3 GetTriangleCenter(float3 a, float3 b, float3 c) {
36      return (a + b + c) / 3.0;
37  }
38  float2 GetTriangleCenter(float2 a, float2 b, float2 c) {
39      return (a + b + c) / 3.0;
40  }
41  
42  // Returns the view direction in world space
43  float3 GetViewDirectionFromPosition(float3 positionWS) {
44      return normalize(GetCameraPositionWS() - positionWS);
45  }
46  
47  // If this is the shadow caster pass, we also need this variable, which URP sets
48  #ifdef SHADOW_CASTER_PASS
49  float3 _LightDirection;
50  #endif
51  
52  // Calculates the position in clip space, taking into account various strategies
53  // to improve shadow quality in the shadow caster pass
54  float4 CalculatePositionCSWithShadowCasterLogic(float3 positionWS, float3 normalWS) {
55      float4 positionCS;
56  
57  #ifdef SHADOW_CASTER_PASS
58      // From URP's ShadowCasterPass.hlsl
59      // If this is the shadow caster pass, we need to adjust the clip space position to account
60      // for shadow bias and offset (this helps reduce shadow artifacts)
61      positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
62  #if UNITY_REVERSED_Z
63      positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
64  #else
65      positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
66  #endif
67  #else
68      // This built in function transforms from world space to clip space
69      positionCS = TransformWorldToHClip(positionWS);
70  #endif
71  
72      return positionCS;
73  }
74  
75  // Calculates the shadow texture coordinate for lighting calculations
76  float4 CalculateShadowCoord(float3 positionWS, float4 positionCS) {
77      // Calculate the shadow coordinate depending on the type of shadows currently in use
78  #if SHADOWS_SCREEN
79      return ComputeScreenPos(positionCS);
80  #else
81      return TransformWorldToShadowCoord(positionWS);
82  #endif
83  }
84  
85  #endif