UNPKG

molstar

Version:

A comprehensive macromolecular library.

117 lines (95 loc) 4.63 kB
"use strict"; /** * Copyright (c) 2017-2024 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> * @author Gianluca Tomasello <giagitom@gmail.com> * * adapted from three.js (https://github.com/mrdoob/three.js/) * which under the MIT License, Copyright © 2010-2021 three.js authors */ Object.defineProperty(exports, "__esModule", { value: true }); exports.apply_light_color = void 0; exports.apply_light_color = ` #if defined(dIgnoreLight) #ifdef bumpEnabled if (uBumpFrequency > 0.0 && uBumpAmplitude > 0.0 && bumpiness > 0.0) { material.rgb += fbm(vModelPosition * uBumpFrequency) * uBumpAmplitude * bumpiness; material.rgb -= 0.5 * uBumpAmplitude * bumpiness; } #endif #if defined(dRenderVariant_color) material.rgb += material.rgb * emissive; #endif gl_FragColor = material; #else #ifdef bumpEnabled if (uBumpFrequency > 0.0 && uBumpAmplitude > 0.0 && bumpiness > 0.0) { normal = perturbNormal(-vViewPosition, normal, fbm(vModelPosition * uBumpFrequency), (uBumpAmplitude * bumpiness) / uBumpFrequency); } #endif vec4 color = material; #if defined(dCelShaded) // clamp to avoid artifacts metalness = clamp(metalness, 0.0, 0.99); roughness = clamp(roughness, 0.05, 1.0); #endif GeometricContext geometry; geometry.position = -vViewPosition; geometry.normal = normal; geometry.viewDir = normalize(vViewPosition); PhysicalMaterial physicalMaterial; physicalMaterial.diffuseColor = color.rgb * (1.0 - metalness); #ifdef enabledFragDepth physicalMaterial.roughness = min(max(roughness, 0.0525), 1.0); #else vec3 dxy = max(abs(dFdx(normal)), abs(dFdy(normal))); float geometryRoughness = max(max(dxy.x, dxy.y), dxy.z); physicalMaterial.roughness = min(max(roughness, 0.0525) + geometryRoughness, 1.0); #endif physicalMaterial.specularColor = mix(vec3(0.04), color.rgb, metalness); physicalMaterial.specularF90 = 1.0; IncidentLight directLight; vec3 outgoingLight = vec3(0.0); #if defined(dCelShaded) float celDiffuse; float celSpecular; float celIntensity; #pragma unroll_loop_start for (int i = 0; i < dLightCount; ++i) { directLight.direction = uLightDirection[i]; directLight.color = uLightColor[i] * PI; // * PI for punctual light celDiffuse = RECIPROCAL_PI * max(dot(geometry.normal, directLight.direction), 0.0) * (1.0 - metalness); celSpecular = luminance(saturate(dot(geometry.normal, directLight.direction)) * BRDF_GGX(directLight.direction, geometry.viewDir, geometry.normal, physicalMaterial.specularColor, physicalMaterial.specularF90, roughness)); celIntensity = celDiffuse + celSpecular; celIntensity = ceil(celIntensity * uCelSteps) / uCelSteps; outgoingLight += color.rgb * directLight.color * celIntensity; } #pragma unroll_loop_end outgoingLight += physicalMaterial.diffuseColor * luminance(uAmbientColor); #else ReflectedLight reflectedLight = ReflectedLight(vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0)); #pragma unroll_loop_start for (int i = 0; i < dLightCount; ++i) { directLight.direction = uLightDirection[i]; directLight.color = uLightColor[i] * PI; // * PI for punctual light RE_Direct_Physical(directLight, geometry, physicalMaterial, reflectedLight); } #pragma unroll_loop_end vec3 irradiance = uAmbientColor * PI; // * PI for punctual light RE_IndirectDiffuse_Physical(irradiance, geometry, physicalMaterial, reflectedLight); // indirect specular only metals vec3 radiance = uAmbientColor * metalness; vec3 iblIrradiance = uAmbientColor * metalness; vec3 clearcoatRadiance = vec3(0.0); RE_IndirectSpecular_Physical(radiance, iblIrradiance, clearcoatRadiance, geometry, physicalMaterial, reflectedLight); outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular; #endif outgoingLight = clamp(outgoingLight, 0.01, 0.99); // prevents black artifacts on specular highlight with transparent background #if defined(dRenderVariant_color) outgoingLight += color.rgb * emissive; #endif gl_FragColor = vec4(outgoingLight, color.a); #endif gl_FragColor.rgb *= uExposure; `;