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.
50 lines (41 loc) • 2.1 kB
JavaScript
import { declareAttributesVars } from "../../chunks/vertex/body/declare-attributes-vars.mjs";
import { getVertexTransformedPositionNormal } from "../../chunks/vertex/body/get-vertex-transformed-position-normal.mjs";
//#region src/core/shaders/full/vertex/get-default-spot-shadow-depth-vertex-shader-code.ts
/**
* Get default ({@link core/lights/SpotLight.SpotLight | SpotLight}) shadow map pass vertex shader.
* @param lightIndex - Index of the {@link core/lights/SpotLight.SpotLight | SpotLight} for which to render the depth pass.
* @param parameters - {@link VertexShaderInputBaseParams} used to compute the output `worldPosition` and `normal` vectors.
*/
const getDefaultSpotShadowDepthVs = (lightIndex = 0, { bindings = [], geometry }) => `
struct SpotShadowVSOutput {
position: vec4f,
worldPosition: vec3f,
}
fn main(
attributes: Attributes,
) -> vec4f {
var spotShadowVSOutput: SpotShadowVSOutput;
let spotShadow: SpotShadowsElement = spotShadows.spotShadowsElements[${lightIndex}];
${declareAttributesVars({ geometry })}
${getVertexTransformedPositionNormal({
bindings,
geometry
})}
// shadows calculations in view space instead of world space
// prevents world-space scaling issues for normal bias
let viewMatrix: mat4x4f = spotShadow.viewMatrix;
var shadowViewPos: vec3f = (viewMatrix * worldPosition).xyz;
let lightViewPos: vec3f = (viewMatrix * vec4(spotShadow.position, 1.0)).xyz;
// Transform normal into shadow view space
let shadowNormal: vec3f = normalize((viewMatrix * vec4(normal, 0.0)).xyz);
// Compute light direction in shadow space
let lightDirection: vec3f = normalize(lightViewPos - shadowViewPos);
let NdotL: f32 = dot(shadowNormal, lightDirection);
let sinNdotL = sqrt(1.0 - NdotL * NdotL);
let normalBias: f32 = spotShadow.normalBias * sinNdotL;
// Apply bias in shadow view space
shadowViewPos -= shadowNormal * normalBias;
return spotShadow.projectionMatrix * vec4(shadowViewPos, 1.0);
}`;
//#endregion
export { getDefaultSpotShadowDepthVs };