UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

4 lines (2 loc) 1.54 kB
var reflectionEnvPS = "\n#ifndef ENV_ATLAS\n#define ENV_ATLAS\nuniform sampler2D texture_envAtlas;\n#endif\nuniform float material_reflectivity;\nfloat shinyMipLevel(vec2 uv) {\n vec2 dx = dFdx(uv);\n vec2 dy = dFdy(uv);\n vec2 uv2 = vec2(fract(uv.x + 0.5), uv.y);\n vec2 dx2 = dFdx(uv2);\n vec2 dy2 = dFdy(uv2);\n float maxd = min(max(dot(dx, dx), dot(dy, dy)), max(dot(dx2, dx2), dot(dy2, dy2)));\n return clamp(0.5 * log2(maxd) - 1.0 + textureBias, 0.0, 5.0);\n}\nvec3 calcReflection(vec3 reflDir, float gloss) {\n vec3 dir = cubeMapProject(reflDir) * vec3(-1.0, 1.0, 1.0);\n vec2 uv = toSphericalUv(dir);\n float level = saturate(1.0 - gloss) * 5.0;\n float ilevel = floor(level);\n float level2 = shinyMipLevel(uv * atlasSize);\n float ilevel2 = floor(level2);\n vec2 uv0, uv1;\n float weight;\n if (ilevel == 0.0) {\n uv0 = mapShinyUv(uv, ilevel2);\n uv1 = mapShinyUv(uv, ilevel2 + 1.0);\n weight = level2 - ilevel2;\n } else {\n uv0 = uv1 = mapRoughnessUv(uv, ilevel);\n weight = 0.0;\n }\n vec3 linearA = {reflectionDecode}(texture2D(texture_envAtlas, uv0));\n vec3 linearB = {reflectionDecode}(texture2D(texture_envAtlas, uv1));\n vec3 linear0 = mix(linearA, linearB, weight);\n vec3 linear1 = {reflectionDecode}(texture2D(texture_envAtlas, mapRoughnessUv(uv, ilevel + 1.0)));\n return processEnvironment(mix(linear0, linear1, level - ilevel));\n}\nvoid addReflection(vec3 reflDir, float gloss) { \n dReflection += vec4(calcReflection(reflDir, gloss), material_reflectivity);\n}\n"; export { reflectionEnvPS as default };