1#define SCALE 0.12
2#define BIAS -0.04
3#define BIN_ITER 5
4
5#ifndef BUMP_HQ
6    #define LIN_ITER 5
7#endif
8
9vec2 Bump_DoOcclusionParallax(in sampler2D heightMap, in vec2 texCoord, in vec3 tanViewDir){
10    float size = 1.0 / float(BIN_ITER);
11
12     // depth
13    float d = 1.0;
14    // best depth
15    float bd = 0.0;
16
17    #ifdef BUMP_HQ
18        const int N = 8;
19        int LIN_ITER = mix(2 * N, N, tanViewDir.z);
20    #endif
21
22    // search from front to back
23    for (int i = 0; i < LIN_ITER; i++){
24        d -= dstep;
25        float h = texture2D(heightMap, dp + ds * (1.0 - d)).a;
26        if (bd < 0.005) // if no depth found yet
27        if (d <= h) bd = depth; // best depth
28    }
29
30    for (int i = 0; i < BIN_ITER; i++) {
31        size *= 0.5;
32        float t = texture2D(heightMap, dp + ds * (1.0 - d)).a;
33        if (d <= t) {
34            bd = depth;
35            d += 2 * size;
36        }
37        d -= size;
38    }
39}
40
41vec2 Bump_DoParallax(in sampler2D heightMap, in vec2 texCoord, in vec3 tanViewDir){
42    float h = texture2D(heightMap, texCoord).a * SCALE + BIAS;
43    return texCoord + h * tanViewDir.xy;
44}