From 07a4e344c18b799483a25ac3e1af2186ded30f73 Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Sun, 31 Mar 2024 10:35:48 -0700 Subject: [PATCH] update primitives example to use some faceted examples --- examples/primitives.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/examples/primitives.js b/examples/primitives.js index 1954a7f..ecacfda 100644 --- a/examples/primitives.js +++ b/examples/primitives.js @@ -64,16 +64,24 @@ async function main() { } `; + function facet(arrays) { + const newArrays = wgh.primitives.deindex(arrays); + newArrays.normal = wgh.primitives.generateTriangleNormals(wgh.makeTypedArrayFromArrayUnion(newArrays.position, 'position')); + return newArrays; + } + const numInstances = 1000; const geometries = [ wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createSphereVertices()), + wgh.createBuffersAndAttributesFromArrays(device, facet(wgh.primitives.createSphereVertices({subdivisionsAxis: 6, subdivisionsHeight: 5}))), wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createTorusVertices()), + wgh.createBuffersAndAttributesFromArrays(device, facet(wgh.primitives.createTorusVertices({thickness: 0.5, radialSubdivisions: 8, bodySubdivisions: 8}))), wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createCubeVertices()), wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createCylinderVertices()), - ///wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createPlaneVertices()), - ///wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createDiscVertices()), + wgh.createBuffersAndAttributesFromArrays(device, facet(wgh.primitives.createCylinderVertices({radialSubdivisions: 7}))), + /////wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createPlaneVertices()), + /////wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createDiscVertices()), wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createTruncatedConeVertices()), - //wgh.createBuffersAndAttributesFromArrays(device, wgh.primitives.createCrescentVertices()), ]; function r(min, max) { @@ -236,8 +244,12 @@ async function main() { passEncoder.setBindGroup(0, bindGroup); passEncoder.setVertexBuffer(0, geometry.buffers[0]); - passEncoder.setIndexBuffer(geometry.indexBuffer, geometry.indexFormat); - passEncoder.drawIndexed(geometry.numElements); + if (geometry.indexBuffer) { + passEncoder.setIndexBuffer(geometry.indexBuffer, geometry.indexFormat); + passEncoder.drawIndexed(geometry.numElements); + } else { + passEncoder.draw(geometry.numElements); + } }); passEncoder.end(); device.queue.submit([commandEncoder.finish()]);