playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
73 lines (72 loc) • 2.54 kB
JavaScript
var refractionDynamic_default = `
uniform float material_invAttenuationDistance;
uniform vec3 material_attenuation;
vec3 evalRefractionColor(vec3 refractionVector, float gloss, float refractionIndex) {
vec4 pointOfRefraction = vec4(vPositionW + refractionVector, 1.0);
vec4 projectionPoint = matrix_viewProjection * pointOfRefraction;
vec2 uv = getGrabScreenPos(projectionPoint);
float iorToRoughness = (1.0 - gloss) * clamp((1.0 / refractionIndex) * 2.0 - 2.0, 0.0, 1.0);
float refractionLod = log2(uScreenSize.x) * iorToRoughness;
vec3 refraction = texture2DLod(uSceneColorMap, uv, refractionLod).rgb;
refraction = decodeGamma(refraction);
return refraction;
}
void addRefraction(
vec3 worldNormal,
vec3 viewDir,
float thickness,
float gloss,
vec3 specularity,
vec3 albedo,
float transmission,
float refractionIndex,
float dispersion
#if defined(LIT_IRIDESCENCE)
, vec3 iridescenceFresnel,
float iridescenceIntensity
#endif
) {
vec3 modelScale;
modelScale.x = length(vec3(matrix_model[0].xyz));
modelScale.y = length(vec3(matrix_model[1].xyz));
modelScale.z = length(vec3(matrix_model[2].xyz));
vec3 scale = thickness * modelScale;
vec3 refractionVector = normalize(refract(-viewDir, worldNormal, refractionIndex)) * scale;
vec3 refraction = evalRefractionColor(refractionVector, gloss, refractionIndex);
float halfSpread = (1.0 / refractionIndex - 1.0) * 0.025 * dispersion;
float refractionIndexR = refractionIndex - halfSpread;
refractionVector = normalize(refract(-viewDir, worldNormal, refractionIndexR)) * scale;
refraction.r = evalRefractionColor(refractionVector, gloss, refractionIndexR).r;
float refractionIndexB = refractionIndex + halfSpread;
refractionVector = normalize(refract(-viewDir, worldNormal, refractionIndexB)) * scale;
refraction.b = evalRefractionColor(refractionVector, gloss, refractionIndexB).b;
vec3 transmittance;
if (material_invAttenuationDistance != 0.0)
{
vec3 attenuation = -log(material_attenuation) * material_invAttenuationDistance;
transmittance = exp(-attenuation * length(refractionVector));
}
else
{
transmittance = vec3(1.0);
}
vec3 fresnel = vec3(1.0) -
getFresnel(
dot(viewDir, worldNormal),
gloss,
specularity
, iridescenceFresnel,
iridescenceIntensity
);
dDiffuseLight = mix(dDiffuseLight, refraction * transmittance * fresnel, transmission);
}
`;
export {
refractionDynamic_default as default
};