Skip to main content

Variable: PBR_ENVIRONMENT_FRAGMENT_GLSL

const PBR_ENVIRONMENT_FRAGMENT_GLSL: "#version 300 es\nprecision highp float;\nprecision highp int;\n\nin vec2 uv;\nout vec4 fragmentColor;\n\nconst float ENVIRONMENT_PI = 3.141592653589793;\n\nvec2 getEnvironmentHammersley(uint index, uint sampleCount)\n{\n uint reversedBits = (index << 16u) | (index >> 16u);\n reversedBits = ((reversedBits & 0x55555555u) << 1u) |\n ((reversedBits & 0xAAAAAAAAu) >> 1u);\n reversedBits = ((reversedBits & 0x33333333u) << 2u) |\n ((reversedBits & 0xCCCCCCCCu) >> 2u);\n reversedBits = ((reversedBits & 0x0F0F0F0Fu) << 4u) |\n ((reversedBits & 0xF0F0F0F0u) >> 4u);\n reversedBits = ((reversedBits & 0x00FF00FFu) << 8u) |\n ((reversedBits & 0xFF00FF00u) >> 8u);\n return vec2(\n float(index) / float(sampleCount),\n float(reversedBits) * 2.3283064365386963e-10\n );\n}\n\nvec3 getEnvironmentFaceDirection(int face, vec2 textureCoordinate)\n{\n vec2 coordinate = textureCoordinate * 2.0 - 1.0;\n if (face == 0) return normalize(vec3(1.0, -coordinate.y, -coordinate.x));\n if (face == 1) return normalize(vec3(-1.0, -coordinate.y, coordinate.x));\n if (face == 2) return normalize(vec3(coordinate.x, 1.0, coordinate.y));\n if (face == 3) return normalize(vec3(coordinate.x, -1.0, -coordinate.y));\n if (face == 4) return normalize(vec3(coordinate.x, -coordinate.y, 1.0));\n return normalize(vec3(-coordinate.x, -coordinate.y, -1.0));\n}\n\nmat3 getEnvironmentTangentBasis(vec3 normal)\n{\n vec3 up = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);\n vec3 tangent = normalize(cross(up, normal));\n return mat3(tangent, cross(normal, tangent), normal);\n}\n\nvec3 sampleEnvironmentDirection(vec3 direction)\n{\n vec3 normalizedDirection = normalize(direction);\n vec2 textureCoordinate = vec2(\n atan(normalizedDirection.z, normalizedDirection.x) / (2.0 * ENVIRONMENT_PI) + 0.5,\n acos(clamp(normalizedDirection.y, -1.0, 1.0)) / ENVIRONMENT_PI\n );\n vec3 color = texture(pbrEnvironmentSource, textureCoordinate).rgb;\n if (pbrEnvironmentFilter.sourceEncoding == 1) {\n vec3 lower = color / 12.92;\n vec3 upper = pow((color + 0.055) / 1.055, vec3(2.4));\n color = mix(lower, upper, step(vec3(0.04045), color));\n }\n return color;\n}\n\nvec3 importanceSampleEnvironment(vec2 sequence, vec3 normal, float roughness)\n{\n float alpha = max(roughness * roughness, 0.0001);\n float azimuth = 2.0 * ENVIRONMENT_PI * sequence.x;\n float cosine = sqrt(\n (1.0 - sequence.y) / max(1.0 + (alpha * alpha - 1.0) * sequence.y, 0.0001)\n );\n float sine = sqrt(max(1.0 - cosine * cosine, 0.0));\n vec3 halfVector = vec3(cos(azimuth) * sine, sin(azimuth) * sine, cosine);\n return normalize(getEnvironmentTangentBasis(normal) * halfVector);\n}\n\nfloat getEnvironmentGeometry(float normalDotDirection, float roughness)\n{\n float alpha = roughness * roughness;\n float factor = alpha * 0.5;\n return normalDotDirection / max(normalDotDirection * (1.0 - factor) + factor, 0.0001);\n}\n\nvec2 integrateEnvironmentBRDF(vec2 textureCoordinate, int sampleCount)\n{\n float normalDotView = max(textureCoordinate.x, 0.001);\n float roughness = 1.0 - textureCoordinate.y;\n vec3 viewDirection = vec3(sqrt(max(1.0 - normalDotView * normalDotView, 0.0)), 0.0, normalDotView);\n vec2 integration = vec2(0.0);\n\n for (int sampleIndex = 0; sampleIndex < 1024; sampleIndex++) {\n if (sampleIndex >= sampleCount) break;\n vec3 halfVector = importanceSampleEnvironment(\n getEnvironmentHammersley(uint(sampleIndex), uint(sampleCount)),\n vec3(0.0, 0.0, 1.0),\n roughness\n );\n vec3 lightDirection = normalize(2.0 * dot(viewDirection, halfVector) * halfVector - viewDirection);\n float normalDotLight = max(lightDirection.z, 0.0);\n float normalDotHalf = max(halfVector.z, 0.0);\n float viewDotHalf = max(dot(viewDirection, halfVector), 0.0);\n if (normalDotLight > 0.0) {\n float visibility = getEnvironmentGeometry(normalDotView, roughness) *\n getEnvironmentGeometry(normalDotLight, roughness) * viewDotHalf /\n max(normalDotHalf * normalDotView, 0.0001);\n float fresnel = pow(1.0 - viewDotHalf, 5.0);\n integration += vec2(1.0 - fresnel, fresnel) * visibility;\n }\n }\n return integration / float(sampleCount);\n}\n\nvec3 integrateEnvironmentDirection(vec3 normal, int sampleCount)\n{\n vec3 accumulatedColor = vec3(0.0);\n float accumulatedWeight = 0.0;\n\n for (int sampleIndex = 0; sampleIndex < 1024; sampleIndex++) {\n if (sampleIndex >= sampleCount) break;\n vec2 sequence = getEnvironmentHammersley(uint(sampleIndex), uint(sampleCount));\n vec3 direction;\n float sampleWeight;\n\n if (pbrEnvironmentFilter.mode == 1) {\n float azimuth = 2.0 * ENVIRONMENT_PI * sequence.x;\n float cosine = sqrt(1.0 - sequence.y);\n float sine = sqrt(sequence.y);\n direction = normalize(\n getEnvironmentTangentBasis(normal) *\n vec3(cos(azimuth) * sine, sin(azimuth) * sine, cosine)\n );\n sampleWeight = 1.0;\n } else {\n vec3 halfVector = importanceSampleEnvironment(sequence, normal, pbrEnvironmentFilter.roughness);\n direction = normalize(2.0 * dot(normal, halfVector) * halfVector - normal);\n sampleWeight = max(dot(normal, direction), 0.0);\n }\n\n accumulatedColor += sampleEnvironmentDirection(direction) * sampleWeight;\n accumulatedWeight += sampleWeight;\n }\n\n return accumulatedColor / max(accumulatedWeight, 0.0001);\n}\n\nvoid main(void)\n{\n int sampleCount = max(pbrEnvironmentFilter.sampleCount, 1);\n if (pbrEnvironmentFilter.mode == 2) {\n fragmentColor = vec4(integrateEnvironmentBRDF(uv, sampleCount), 0.0, 1.0);\n return;\n }\n\n vec3 direction = getEnvironmentFaceDirection(pbrEnvironmentFilter.face, uv);\n fragmentColor = vec4(integrateEnvironmentDirection(direction, sampleCount), 1.0);\n}\n"

Defined in: modules/lighting/prefiltered-environment/prefiltered-environment.ts:81