UNPKG

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 (66 loc) 1.95 kB
const declareAttributesVars = ({ geometry, additionalVaryings = [] }) => { let attributeVars = ( /* wgsl */ ` let fragmentPosition: vec4f = fsInput.position; let frontFacing: bool = fsInput.frontFacing; ` ); const normalAttribute = geometry && geometry.getAttributeByName("normal"); const tangentAttribute = geometry && geometry.getAttributeByName("tangent"); const disabledAttributes = ["position", "normal", "tangent", "color", "joints", "weights"]; const attributes = []; if (geometry && geometry.vertexBuffers && geometry.vertexBuffers.length) { geometry.vertexBuffers.forEach((vertexBuffer) => { vertexBuffer.attributes.forEach((attribute) => { if (!disabledAttributes.some((attr) => attribute.name.includes(attr))) { attributes.push(attribute); } }); }); } attributeVars += attributes.map((attribute) => { return ` var ${attribute.name}: ${attribute.type} = fsInput.${attribute.name};`; }).join(""); if (normalAttribute) { attributeVars += /* wgsl */ ` var normal: vec3f = normalize(fsInput.normal); `; } else { attributeVars += /* wgsl */ ` // silly default normal var normal: vec3f = vec3(0.0, 0.0, 1.0); `; } if (tangentAttribute) { attributeVars += /* wgsl */ ` var tangent: vec3f = normalize(fsInput.tangent.xyz); var bitangent: vec3f = normalize(fsInput.bitangent); `; } else { attributeVars += /* wgsl */ ` var tangent: vec3f; var bitangent: vec3f; `; } attributeVars += /* wgsl */ ` let worldPosition: vec3f = fsInput.worldPosition; let viewDirection: vec3f = normalize(fsInput.viewDirection); let modelScale: vec3f = fsInput.modelScale; `; attributeVars += additionalVaryings.map((attribute) => { return ` var ${attribute.name}: ${attribute.type} = fsInput.${attribute.name};`; }).join(""); return attributeVars; }; export { declareAttributesVars };