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.
68 lines (53 loc) • 2.33 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";
//#region src/core/shaders/chunks/fragment/body/get-lambert-shading.ts
/**
* Set the `outgoingLight` (`vec3f`) using Lambert shading.
* @param parameters - Parameters to use to apply Lambert shading.
* @param parameters.receiveShadows - Whether the shading function should account for current shadows. Default to `false`.
* @returns - A string with Lambert shading applied to `outgoingLight`.
*/
const getLambertShading = ({ receiveShadows = false } = {}) => {
return `
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 = getAmbientLightIrradiance();
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;`;
};
//#endregion
export { getLambertShading };