| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <!--
- Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
- All rights reserved.
- Code licensed under the BSD License.
- http://www.anki3d.org/LICENSE
- -->
- <shaderProgram>
- <shaders>
- <shader type="vert">
- <source><![CDATA[
- #include "shaders/QuadVert.glsl"
- ]]></source>
- </shader>
- <shader type="frag">
- <inputs>
- <input name="ENV_TEX_TILE_SIZE" type="uint" const="1"/>
- <input name="ENV_TEX_MIP" type="float" const="1"/>
- </inputs>
- <source><![CDATA[
- // Compute the irradiance given an environment map
- // It's almost the same as http://www.codinglabs.net/article_physically_based_rendering.aspx
- // It's more bright though
- #include "shaders/Functions.glsl"
- layout(location = 0) in vec2 in_uv;
- layout(location = 0) out vec3 out_color;
- layout(ANKI_TEX_BINDING(0, 0)) uniform samplerCubeArray u_envTex;
- layout(ANKI_UBO_BINDING(0, 0)) uniform u0_
- {
- // x: The face index to render to
- // y: The array index to read from the u_envTex
- uvec4 u_faceIdxArrayIdxPad2;
- };
- float areaElement(float x, float y)
- {
- return atan(x * y, sqrt(x * x + y * y + 1.0));
- }
- float cubeCoordSolidAngle(vec2 norm)
- {
- float invSize = 1.0 / float(ENV_TEX_TILE_SIZE);
- vec2 v0 = norm - vec2(invSize);
- vec2 v1 = norm + vec2(invSize);
- return areaElement(v0.x, v0.y) - areaElement(v0.x, v1.y) - areaElement(v1.x, v0.y) + areaElement(v1.x, v1.y);
- }
- // Integrate the environment map to compute the irradiance for a single fragment
- void main()
- {
- uint face = u_faceIdxArrayIdxPad2.x;
- float texArrIdx = float(u_faceIdxArrayIdxPad2.y);
- // Get the r coordinate of the current face and fragment
- vec3 ri = getCubemapDirection(UV_TO_NDC(in_uv), face);
- vec3 outCol = vec3(0.0);
- float weight = EPSILON;
- // For all the faces and texels of the environment map calculate a color sum
- for(uint f = 0u; f < 6u; ++f)
- {
- for(uint i = 0u; i < ENV_TEX_TILE_SIZE; ++i)
- {
- for(uint j = 0u; j < ENV_TEX_TILE_SIZE; ++j)
- {
- vec2 uv = vec2(j, i) / float(ENV_TEX_TILE_SIZE);
- vec3 r = getCubemapDirection(UV_TO_NDC(uv), f);
- float lambert = dot(r, ri);
- if(lambert > 0.0)
- {
- vec3 col = textureLod(u_envTex, vec4(r, texArrIdx), ENV_TEX_MIP).rgb;
- outCol += col * lambert * cubeCoordSolidAngle(UV_TO_NDC(uv));
- weight += lambert;
- }
- }
- }
- }
- //out_color = outCol / weight * PI;
- out_color = outCol / PI;
- }
- ]]></source>
- </shader>
- </shaders>
- </shaderProgram>
|