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.
59 lines (47 loc) • 2.52 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-point-shadow-depth-vertex-shader-code.ts
/**
* Get {@link core/lights/PointLight.PointLight | PointLight} shadow map pass vertex shader.
* @param lightIndex - Index of the {@link core/lights/PointLight.PointLight | PointLight} for which to render the depth pass.
* @param parameters - {@link VertexShaderInputParams} used to compute the output `worldPosition` and `normal` vectors.
*/
const getDefaultPointShadowDepthVs = (lightIndex = 0, { bindings = [], geometry }) => `
struct PointShadowVSOutput {
position: vec4f,
worldPosition: vec3f,
}
fn main(
attributes: Attributes,
) -> PointShadowVSOutput {
var pointShadowVSOutput: PointShadowVSOutput;
let pointShadow: PointShadowsElement = pointShadows.pointShadowsElements[${lightIndex}];
${declareAttributesVars({ geometry })}
${getVertexTransformedPositionNormal({
bindings,
geometry
})}
let worldPos = worldPosition.xyz / worldPosition.w;
// TODO accessing viewMatrices from our pointShadow reference makes Firefox bug?!
// let viewMatrix: mat4x4f = pointShadow.viewMatrices[cubeFace.face];
// we need to access it directly instead!
let viewMatrix: mat4x4f = pointShadows.pointShadowsElements[${lightIndex}].viewMatrices[cubeFace.face];
// shadows calculations in view space instead of world space
// prevents world-space scaling issues for normal bias
var shadowViewPos: vec3f = (viewMatrix * worldPosition).xyz;
let lightViewPos: vec3f = (viewMatrix * vec4(pointShadow.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 = pointShadow.normalBias * sinNdotL;
// Apply bias in shadow view space
shadowViewPos -= shadowNormal * normalBias;
pointShadowVSOutput.position = pointShadow.projectionMatrix * vec4(shadowViewPos, 1.0);
pointShadowVSOutput.worldPosition = worldPos;
return pointShadowVSOutput;
}`;
//#endregion
export { getDefaultPointShadowDepthVs };