0.2.0-dev Download ZIP
100 lines ·2.3 KB Raw
| 1 | /* clang-format off */ |
| 2 | [vertex] |
| 3 | |
| 4 | #ifdef USE_GLES_OVER_GL |
| 5 | #define lowp |
| 6 | #define mediump |
| 7 | #define highp |
| 8 | #else |
| 9 | precision mediump float; |
| 10 | precision mediump int; |
| 11 | #endif |
| 12 | |
| 13 | layout(location = 0) in highp vec4 vertex_attrib; |
| 14 | /* clang-format on */ |
| 15 | layout(location = 4) in vec2 uv_in; |
| 16 | |
| 17 | out vec2 uv_interp; |
| 18 | |
| 19 | void main() { |
| 20 | uv_interp = uv_in; |
| 21 | gl_Position = vertex_attrib; |
| 22 | } |
| 23 | |
| 24 | /* clang-format off */ |
| 25 | [fragment] |
| 26 | |
| 27 | #ifdef USE_GLES_OVER_GL |
| 28 | #define lowp |
| 29 | #define mediump |
| 30 | #define highp |
| 31 | #else |
| 32 | #if defined(USE_HIGHP_PRECISION) |
| 33 | precision highp float; |
| 34 | precision highp int; |
| 35 | #else |
| 36 | precision mediump float; |
| 37 | precision mediump int; |
| 38 | #endif |
| 39 | #endif |
| 40 | |
| 41 | uniform highp samplerCube source_cube; //texunit:0 |
| 42 | /* clang-format on */ |
| 43 | in vec2 uv_interp; |
| 44 | |
| 45 | uniform bool z_flip; |
| 46 | uniform highp float z_far; |
| 47 | uniform highp float z_near; |
| 48 | uniform highp float bias; |
| 49 | |
| 50 | void main() { |
| 51 | highp vec3 normal = vec3(uv_interp * 2.0 - 1.0, 0.0); |
| 52 | /* |
| 53 | if (z_flip) { |
| 54 | normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); |
| 55 | } else { |
| 56 | normal.z = -0.5 + 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); |
| 57 | } |
| 58 | */ |
| 59 | |
| 60 | //normal.z = sqrt(1.0 - dot(normal.xy, normal.xy)); |
| 61 | //normal.xy *= 1.0 + normal.z; |
| 62 | |
| 63 | normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); |
| 64 | normal = normalize(normal); |
| 65 | /* |
| 66 | normal.z = 0.5; |
| 67 | normal = normalize(normal); |
| 68 | */ |
| 69 | |
| 70 | if (!z_flip) { |
| 71 | normal.z = -normal.z; |
| 72 | } |
| 73 | |
| 74 | //normal = normalize(vec3( uv_interp * 2.0 - 1.0, 1.0 )); |
| 75 | float depth = textureCube(source_cube, normal).r; |
| 76 | |
| 77 | // absolute values for direction cosines, bigger value equals closer to basis axis |
| 78 | vec3 unorm = abs(normal); |
| 79 | |
| 80 | if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) { |
| 81 | // x code |
| 82 | unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0); |
| 83 | } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) { |
| 84 | // y code |
| 85 | unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0); |
| 86 | } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) { |
| 87 | // z code |
| 88 | unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0); |
| 89 | } else { |
| 90 | // oh-no we messed up code |
| 91 | // has to be |
| 92 | unorm = vec3(1.0, 0.0, 0.0); |
| 93 | } |
| 94 | |
| 95 | float depth_fix = 1.0 / dot(normal, unorm); |
| 96 | |
| 97 | depth = 2.0 * depth - 1.0; |
| 98 | float linear_depth = 2.0 * z_near * z_far / (z_far + z_near + depth * (z_far - z_near)); |
| 99 | gl_FragDepth = (z_far - (linear_depth * depth_fix + bias)) / z_far; |
| 100 | } |