summaryrefslogtreecommitdiffstats
path: root/cnlohr.cginc
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2024-10-07 23:33:35 -0700
committeryum <yum.food.vr@gmail.com>2024-10-07 23:33:35 -0700
commitbcadf1d091efe76e7a1b2393f87f7e24128b723b (patch)
treeb5a37c88686f15f08845d597f223473511917a34 /cnlohr.cginc
parente51760ab5e6d698b26b60e1811e7afce62be55d0 (diff)
Add fog gimmick
Diffstat (limited to 'cnlohr.cginc')
-rw-r--r--cnlohr.cginc35
1 files changed, 35 insertions, 0 deletions
diff --git a/cnlohr.cginc b/cnlohr.cginc
index bed117d..c3163d2 100644
--- a/cnlohr.cginc
+++ b/cnlohr.cginc
@@ -50,4 +50,39 @@ float3 getCenterCamPos() {
#endif
}
+float GetLinearZFromZDepth_WorksWithMirrors(float zDepthFromMap, float2 screenUV)
+{
+ #if defined(UNITY_REVERSED_Z)
+ zDepthFromMap = 1 - zDepthFromMap;
+
+ // When using a mirror, the far plane is whack. This just checks for it and aborts.
+ if( zDepthFromMap >= 1.0 ) return _ProjectionParams.z;
+ #endif
+
+ float4 clipPos = float4(screenUV.xy, zDepthFromMap, 1.0);
+ clipPos.xyz = 2.0f * clipPos.xyz - 1.0f;
+ float4 camPos = mul(unity_CameraInvProjection, clipPos);
+ return -camPos.z / camPos.w;
+}
+
+float GetDepthOfWorldPos(float3 worldPos)
+{
+ float3 full_vec_eye_to_geometry = worldPos - _WorldSpaceCameraPos;
+ float3 world_dir = normalize(worldPos - _WorldSpaceCameraPos);
+ float4 objPos = mul(unity_WorldToObject, float4(worldPos, 1));
+ float4 clipPos = UnityObjectToClipPos(objPos);
+
+ float2 suv = clipPos * float2(0.5, 0.5 * _ProjectionParams.x);
+ float2 screenPos = TransformStereoScreenSpaceTex(suv + 0.5 * clipPos.w, clipPos.w);
+
+ float perspective_divide = 1.0 / clipPos.w;
+ float perspective_factor = length(full_vec_eye_to_geometry * perspective_divide);
+ float2 screen_uv = screenPos.xy * perspective_divide;
+ float eye_depth_world =
+ GetLinearZFromZDepth_WorksWithMirrors(
+ SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screen_uv),
+ screen_uv) * perspective_factor;
+ return eye_depth_world;
+}
+
#endif // __CNLOHR_INC