| 1 |
material { |
| 2 |
name : iblprefilter, |
| 3 |
parameters : [ |
| 4 |
{ |
| 5 |
type : samplerCubemap, |
| 6 |
name : environment, |
| 7 |
precision: medium |
| 8 |
}, |
| 9 |
{ |
| 10 |
type : sampler2d, |
| 11 |
name : kernel, |
| 12 |
precision: high |
| 13 |
}, |
| 14 |
{ |
| 15 |
type : float2, |
| 16 |
name : compress, |
| 17 |
precision: medium |
| 18 |
}, |
| 19 |
{ |
| 20 |
type : float, |
| 21 |
name : side, |
| 22 |
precision: medium |
| 23 |
}, |
| 24 |
{ |
| 25 |
type : float, |
| 26 |
name : lodOffset, |
| 27 |
precision: medium |
| 28 |
}, |
| 29 |
{ |
| 30 |
type : uint, |
| 31 |
name : sampleCount, |
| 32 |
precision: medium |
| 33 |
}, |
| 34 |
{ |
| 35 |
type : uint, |
| 36 |
name : attachmentLevel, |
| 37 |
precision: medium |
| 38 |
} |
| 39 |
], |
| 40 |
outputs : [ |
| 41 |
{ |
| 42 |
name : outx, |
| 43 |
target : color, |
| 44 |
type : float3 |
| 45 |
}, |
| 46 |
{ |
| 47 |
name : outy, |
| 48 |
target : color, |
| 49 |
type : float3 |
| 50 |
}, |
| 51 |
{ |
| 52 |
name : outz, |
| 53 |
target : color, |
| 54 |
type : float3 |
| 55 |
} |
| 56 |
], |
| 57 |
variables : [ |
| 58 |
vertex |
| 59 |
], |
| 60 |
domain : postprocess, |
| 61 |
depthWrite : false, |
| 62 |
depthCulling : false |
| 63 |
} |
| 64 |
|
| 65 |
vertex { |
| 66 |
void postProcessVertex(inout PostProcessVertexInputs postProcess) { |
| 67 |
postProcess.vertex.xy = postProcess.normalizedUV; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
fragment { |
| 72 |
|
| 73 |
void dummy() {} |
| 74 |
|
| 75 |
precision highp float; |
| 76 |
|
| 77 |
mat3 tangentSpace(const vec3 N) { |
| 78 |
const vec3 up = vec3(0, 0, 1); |
| 79 |
mat3 R; |
| 80 |
R[0] = normalize(cross(up, N)); |
| 81 |
R[1] = cross(N, R[0]); |
| 82 |
R[2] = N; |
| 83 |
return R; |
| 84 |
} |
| 85 |
|
| 86 |
float random(const highp vec2 w) { |
| 87 |
const vec3 m = vec3(0.06711056, 0.00583715, 52.9829189); |
| 88 |
return fract(m.z * fract(dot(w, m.xy))); |
| 89 |
} |
| 90 |
|
| 91 |
// See: http://graphicrants.blogspot.com/2013/12/tone-mapping.html, By Brian Karis |
| 92 |
vec3 compress(const vec3 color, const float linear, const float compressed) { |
| 93 |
const mediump vec3 rec709 = vec3(0.2126, 0.7152, 0.0722); |
| 94 |
float luma = dot(color, rec709); // REC 709 |
| 95 |
float s = 1.0; |
| 96 |
if (luma > linear) { |
| 97 |
s = ((linear * linear - compressed * luma) / ((2.0 * linear - compressed - luma) * luma)); |
| 98 |
} |
| 99 |
return color * s; |
| 100 |
} |
| 101 |
|
| 102 |
vec3 sampleEnvironment(mediump samplerCube environment, highp vec3 r, mediump float lod) { |
| 103 |
mediump float linear = materialParams.compress.x; |
| 104 |
mediump float compressed = materialParams.compress.y; |
| 105 |
mediump vec3 c = textureLod(environment, r, lod).rgb; |
| 106 |
return compress(c, linear, compressed); |
| 107 |
// return c * (compressed / (compressed + luma)); // cheaper |
| 108 |
// return clamp(c, 0.0, compressed); // cheapest |
| 109 |
} |
| 110 |
|
| 111 |
void postProcess(inout PostProcessInputs postProcess) { |
| 112 |
vec2 uv = variable_vertex.xy; // interpolated at pixel's center |
| 113 |
vec2 p = uv * 2.0 - 1.0; |
| 114 |
float side = materialParams.side; |
| 115 |
|
| 116 |
// compute the view (and normal, since v = n) direction for each face |
| 117 |
vec3 rx = normalize(vec3( side, -p.y, side * -p.x)); |
| 118 |
vec3 ry = normalize(vec3( p.x, side, side * p.y)); |
| 119 |
vec3 rz = normalize(vec3(side * p.x, -p.y, side)); |
| 120 |
|
| 121 |
// random rotation around r |
| 122 |
mediump float a = 2.0 * PI * random(gl_FragCoord.xy); |
| 123 |
mediump float c = cos(a); |
| 124 |
mediump float s = sin(a); |
| 125 |
mat3 R; |
| 126 |
R[0] = vec3( c, s, 0); |
| 127 |
R[1] = vec3(-s, c, 0); |
| 128 |
R[2] = vec3( 0, 0, 1); |
| 129 |
|
| 130 |
// compute the rotation by which to transform our sample locations for each face |
| 131 |
mat3 Tx = tangentSpace(rx) * R; |
| 132 |
mat3 Ty = tangentSpace(ry) * R; |
| 133 |
mat3 Tz = tangentSpace(rz) * R; |
| 134 |
|
| 135 |
// accumulated environment light for each face |
| 136 |
vec3 Lx = vec3(0); |
| 137 |
vec3 Ly = vec3(0); |
| 138 |
vec3 Lz = vec3(0); |
| 139 |
|
| 140 |
float kernelWeight = 0.0; |
| 141 |
for (uint i = 0u ; i < materialParams.sampleCount ; i++) { |
| 142 |
// { L, lod }, with L.z == NoL |
| 143 |
mediump vec4 entry = texelFetch(materialParams_kernel, ivec2(materialParams.attachmentLevel, i), 0); |
| 144 |
if (entry.z > 0.0) { |
| 145 |
float l = entry.w + materialParams.lodOffset;// we don't need to clamp, the h/w does it for us |
| 146 |
Lx += sampleEnvironment(materialParams_environment, Tx * entry.xyz, l) * entry.z; |
| 147 |
Ly += sampleEnvironment(materialParams_environment, Ty * entry.xyz, l) * entry.z; |
| 148 |
Lz += sampleEnvironment(materialParams_environment, Tz * entry.xyz, l) * entry.z; |
| 149 |
kernelWeight += entry.z; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
float invKernelWeight = 1.0 / kernelWeight; |
| 154 |
Lx *= invKernelWeight; |
| 155 |
Ly *= invKernelWeight; |
| 156 |
Lz *= invKernelWeight; |
| 157 |
|
| 158 |
postProcess.outx = Lx; |
| 159 |
postProcess.outy = Ly; |
| 160 |
postProcess.outz = Lz; |
| 161 |
} |
| 162 |
|
| 163 |
} |