UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

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