Skip to content

Commit 03ca8bc

Browse files
authored
Update depth-texture.md
1 parent 00bdb3f commit 03ca8bc

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

depth-texture.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ Depth based effects will show up incorrectly in VRChat mirrors without special h
7373

7474
https://github.com/lukis101/VRCUnityStuffs/blob/master/Shaders/DJL/Overlays/WorldPosOblique.shader
7575

76-
## Normals from depth
77-
Once you have the world space position of each fragment, you can use pixel derivatives `ddx` and `ddy` to get an approximation of the normal vector of each fragment:
76+
## A note on normals
77+
The simplest way to calculate normals from depth is by taking the gradient of the reconstructed world space position using `ddx(...)` and `ddy(...)`. Like so:
7878
```glsl
7979
...
8080
float3 wpos = homWorldPos.xyz / homWorldPos.w; // world space fragment position
@@ -83,6 +83,10 @@ float3 wposy = ddy(wpos);
8383
float3 wnormal = normalize(cross(wposy,wposx)); // world space fragment normal
8484
```
8585

86+
This works, but results in very jagged looking normals at the edges of objects. There are better, slightly more expensive ways of calculating normals, some of which are described here (praise bgolus):
87+
- https://twitter.com/bgolus/status/1365449067770220551
88+
- https://gist.github.com/bgolus/a07ed65602c009d5e2f753826e8078a0
89+
8690
## Making shaders show up in the depth texture
8791
For a shader to appear in the depth texture, a couple of properties must be satisfied:
8892
- The shader/material must have `ZWrite On`.
@@ -142,11 +146,6 @@ if (!dot(unity_LightShadowBias, 1))
142146
```
143147
Thanks, Silent!
144148

145-
## A note on normals
146-
The simplest way to calculate normals from depth is by taking the gradient of the reconstructed world space position using `ddx(...)` and `ddy(...)`. This works, but results in very jagged looking normals at the edges of objects. There are better, slightly more expensive ways of calculating normals, some of which are described here (praise bgolus):
147-
- https://twitter.com/bgolus/status/1365449067770220551
148-
- https://gist.github.com/bgolus/a07ed65602c009d5e2f753826e8078a0
149-
150149
## Depth buffer is reversed on Oculus Quest
151150
When writing shaders for the quest, make sure to use the `UNITY_REVERSED_Z` macro.
152151
For example:

0 commit comments

Comments
 (0)