gpu-curtains
Version:
gpu-curtains is a 3D WebGPU rendering engine. It can be used as a standalone 3D engine, but also includes extra classes focused on mapping 3d objects to DOM elements; It allows users to synchronize values such as position, sizing, or scale between them.
65 lines (48 loc) • 1.93 kB
JavaScript
import { getPCFShadows } from './get-PCF-shadows.mjs';
import { applyDirectionalShadows } from './apply-directional-shadows.mjs';
import { applyPointShadows } from './apply-point-shadows.mjs';
import { applySpotShadows } from './apply-spot-shadows.mjs';
const getLambertShading = ({ receiveShadows = false } = {}) => {
return (
/* wgsl */
`
var directLight: DirectLight;
var reflectedLight: ReflectedLight;
${receiveShadows ? getPCFShadows : ""}
// point lights
for(var i = 0; i < pointLights.count; i++) {
getPointLightInfo(pointLights.elements[i], worldPosition, &directLight);
if(!directLight.visible) {
continue;
}
${receiveShadows ? applyPointShadows : ""}
getLambertDirect(normal, outputColor.rgb, directLight, &reflectedLight);
}
// spot lights
for(var i = 0; i < spotLights.count; i++) {
getSpotLightInfo(spotLights.elements[i], worldPosition, &directLight);
if(!directLight.visible) {
continue;
}
${receiveShadows ? applySpotShadows : ""}
getLambertDirect(normal, outputColor.rgb, directLight, &reflectedLight);
}
// directional lights
for(var i = 0; i < directionalLights.count; i++) {
getDirectionalLightInfo(directionalLights.elements[i], &directLight);
if(!directLight.visible) {
continue;
}
${receiveShadows ? applyDirectionalShadows : ""}
getLambertDirect(normal, outputColor.rgb, directLight, &reflectedLight);
}
// ambient lights
var irradiance: vec3f = vec3(0.0);
RE_IndirectDiffuse(irradiance, outputColor.rgb, &reflectedLight);
let totalDirect: vec3f = reflectedLight.directDiffuse + reflectedLight.directSpecular;
var totalIndirect: vec3f = reflectedLight.indirectDiffuse + reflectedLight.indirectSpecular;
totalIndirect *= occlusion;
var outgoingLight: vec3f = totalDirect + totalIndirect;`
);
};
export { getLambertShading };