forked from ColinLeung-NiloCat/UnityURPToonLitShaderExample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NiloZOffset.hlsl
38 lines (33 loc) · 1.54 KB
/
NiloZOffset.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// For more information, visit -> https://github.com/ColinLeung-NiloCat/UnityURPToonLitShaderExample
#ifndef Include_NiloZOffset
#define Include_NiloZOffset
// Push an imaginary vertex towards camera in view space (linear, view space unit),
// then only overwrite original positionCS.z using imaginary vertex's result positionCS.z value
// Will only affect ZTest ZWrite's depth value of vertex shader
// Useful for:
// -Hide ugly outline on face/eye
// -Make eyebrow render on top of hair
// -Solve ZFighting issue without moving geometry
float4 NiloGetNewClipPosWithZOffset(float4 originalPositionCS, float viewSpaceZOffsetAmount)
{
if(unity_OrthoParams.w == 0)
{
////////////////////////////////
//Perspective camera case
////////////////////////////////
float2 ProjM_ZRow_ZW = UNITY_MATRIX_P[2].zw;
float modifiedPositionVS_Z = -originalPositionCS.w + -viewSpaceZOffsetAmount; // push imaginary vertex
float modifiedPositionCS_Z = modifiedPositionVS_Z * ProjM_ZRow_ZW[0] + ProjM_ZRow_ZW[1];
originalPositionCS.z = modifiedPositionCS_Z * originalPositionCS.w / (-modifiedPositionVS_Z); // overwrite positionCS.z
return originalPositionCS;
}
else
{
////////////////////////////////
//Orthographic camera case
////////////////////////////////
originalPositionCS.z += -viewSpaceZOffsetAmount / _ProjectionParams.z; // push imaginary vertex and overwrite positionCS.z
return originalPositionCS;
}
}
#endif