-
Notifications
You must be signed in to change notification settings - Fork 2
/
pom.glsl
34 lines (25 loc) · 1 KB
/
pom.glsl
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
vec2 ParallaxOcclusionMapping( sampler2D depthMap, vec2 uv, vec2 displacement, float pivot ) {
const float layerDepth = 1.0 / float( layers );
float currentLayerDepth = 0.0;
vec2 deltaUv = displacement / float( layers );
vec2 currentUv = uv + pivot * displacement;
float currentDepth = texture2D( depthMap, currentUv ).r;
for( int i = 0; i < layers; i++ ) {
if( currentLayerDepth > currentDepth )
break;
currentUv -= deltaUv;
currentDepth = texture2D( depthMap, currentUv ).r;
currentLayerDepth += layerDepth;
}
vec2 prevUv = currentUv + deltaUv;
float endDepth = currentDepth - currentLayerDepth;
float startDepth =
texture2D( depthMap, prevUv ).r - currentLayerDepth + layerDepth;
float w = endDepth / ( endDepth - startDepth );
return mix( currentUv, prevUv, w );
}
vec2 ParallaxOcclusionMapping( sampler2D depthMap, vec2 uv, vec2 displacement ) {
return ParallaxOcclusionMapping( depthMap, uv, displacement, 0.0 );
}
// clang-format off
#pragma glslify: export(ParallaxOcclusionMapping)