@google/model-viewer
Version:
Easily display interactive 3D models on the web and in AR!
148 lines (82 loc) • 4.5 kB
JavaScript
export const envmapChunk = /* glsl */ `
vec3 getLightProbeIndirectIrradiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in int maxMIPLevel ) {
vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
// TODO: replace with properly filtered cubemaps and access the irradiance LOD level, be it the last LOD level
// of a specular cubemap, or just the default level of a specially created irradiance cubemap.
vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
// force the bias high to get the last LOD level as it is the most blurred.
vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
vec4 envMapColor = textureCubeUV( envMap, queryVec, 1.0 );
vec4 envMapColor = vec4( 0.0 );
return PI * envMapColor.rgb * envMapIntensity;
}
// (elalish) Changed from Blinn-Phong to Trowbridge-Reitz distribution and added anti-aliasing.
float getSpecularMIPLevel( const in float roughness, const in vec3 sampleVec, const in int maxMIPLevel ) {
float maxMIPLevelScalar = float( maxMIPLevel );
float sigma = PI * roughness * roughness / ( 1.0 + roughness );
// Add anti-aliasing mipmap contribution
vec3 dxy = max(abs(dFdx(sampleVec)), abs(dFdy(sampleVec)));
sigma += max(max(dxy.x, dxy.y), dxy.z);
float desiredMIPLevel = -log2( sigma );
desiredMIPLevel = clamp( maxMIPLevelScalar - desiredMIPLevel, 0.0, maxMIPLevelScalar );
return desiredMIPLevel;
}
// (elalish) Changed the input from blinnShininessExponent to roughness, so that we can use the Trowbridge-Reitz distribution instead.
vec3 getLightProbeIndirectRadiance( /*const in SpecularLightProbe specularLightProbe,*/ const in GeometricContext geometry, const in float roughness, const in int maxMIPLevel ) {
vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
// (elalish) Mixing the reflection with the normal is more accurate and keeps rough objects from gathering light from behind their tangent plane.
reflectVec = normalize( mix( reflectVec, geometry.normal, roughness * roughness) );
reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
float specularMIPLevel = getSpecularMIPLevel( roughness, reflectVec, maxMIPLevel );
vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
vec4 envMapColor = textureCubeUV( envMap, queryReflectVec, roughness );
vec2 sampleUV;
sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );
vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
return envMapColor.rgb * envMapIntensity;
}
`;
//# sourceMappingURL=envmap_physical_pars_fragment.glsl.js.map