| 1 |
material { |
| 2 |
name : equirectToCube, |
| 3 |
parameters : [ |
| 4 |
{ |
| 5 |
type : sampler2d, |
| 6 |
name : equirect, |
| 7 |
precision: medium |
| 8 |
}, |
| 9 |
{ |
| 10 |
type : float, |
| 11 |
name : side, |
| 12 |
precision: medium |
| 13 |
} |
| 14 |
], |
| 15 |
outputs : [ |
| 16 |
{ |
| 17 |
name : outx, |
| 18 |
target : color, |
| 19 |
type : float3 |
| 20 |
}, |
| 21 |
{ |
| 22 |
name : outy, |
| 23 |
target : color, |
| 24 |
type : float3 |
| 25 |
}, |
| 26 |
{ |
| 27 |
name : outz, |
| 28 |
target : color, |
| 29 |
type : float3 |
| 30 |
} |
| 31 |
], |
| 32 |
variables : [ |
| 33 |
vertex |
| 34 |
], |
| 35 |
domain : postprocess, |
| 36 |
depthWrite : false, |
| 37 |
depthCulling : false |
| 38 |
} |
| 39 |
|
| 40 |
vertex { |
| 41 |
void postProcessVertex(inout PostProcessVertexInputs postProcess) { |
| 42 |
postProcess.vertex.xy = postProcess.normalizedUV; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
fragment { |
| 47 |
|
| 48 |
void dummy() {} |
| 49 |
|
| 50 |
highp vec2 toEquirect(const highp vec3 s) { |
| 51 |
highp float xf = atan2(s.x, s.z) * (1.0 / PI); // range [-1.0, 1.0] |
| 52 |
highp float yf = asin(s.y) * (2.0 / PI); // range [-1.0, 1.0] |
| 53 |
xf = (xf + 1.0) * 0.5; // range [0, 1.0] |
| 54 |
yf = (1.0 - yf) * 0.5; // range [0, 1.0] |
| 55 |
return vec2(xf, yf); |
| 56 |
} |
| 57 |
|
| 58 |
mediump vec3 sampleEquirect(mediump sampler2D equirect, const highp vec3 r) { |
| 59 |
mediump vec3 c = texture(equirect, toEquirect(r)).rgb; |
| 60 |
return clamp(c, 0.0, 65536.0); // clamp to ger rid of possible +inf |
| 61 |
} |
| 62 |
|
| 63 |
void postProcess(inout PostProcessInputs postProcess) { |
| 64 |
highp vec2 uv = variable_vertex.xy; // interpolated at pixel's center |
| 65 |
highp vec2 p = uv * 2.0 - 1.0; |
| 66 |
float side = materialParams.side; |
| 67 |
|
| 68 |
// compute the view (and normal, since v = n) direction for each face |
| 69 |
highp vec3 rx = normalize(vec3( side, -p.y, side * -p.x)); |
| 70 |
highp vec3 ry = normalize(vec3( p.x, side, side * p.y)); |
| 71 |
highp vec3 rz = normalize(vec3(side * p.x, -p.y, side)); |
| 72 |
|
| 73 |
postProcess.outx = sampleEquirect(materialParams_equirect, rx); |
| 74 |
postProcess.outy = sampleEquirect(materialParams_equirect, ry); |
| 75 |
postProcess.outz = sampleEquirect(materialParams_equirect, rz); |
| 76 |
} |
| 77 |
|
| 78 |
} |