Adding shadows to custom mesh

Hey it’s me again.

I’ve gotten my code up and running and working using my custom shader, but I was just wondering if it would be possible to add shadows using shadowGenerator or if I’d have to implement my own shadow mapping.

I’m currently passing the vertices in through a storageBuffer and then re-using them with transforms in my vertex shader with vertexID, this seems to give about double the performance than using regular instancing.

boidMesh = new Mesh("custom", scene);
boidMesh.setVerticesData(VertexBuffer.PositionKind, [0]);
boidMesh.isUnIndexed = true;
const numVerts = pyramidMesh.vertices.length / 4;
boidMesh.subMeshes[0].verticesCount = numBoids * numVerts;
boidMat.setUInt("numVertices", numVerts);

boidVerticesBuffer = new StorageBuffer(engine, pyramidMesh.vertices.byteLength);
boidVerticesBuffer.update(pyramidMesh.vertices);
boidMat.setStorageBuffer("boidVertices", boidVerticesBuffer);
boidNormalsBuffer = new StorageBuffer(engine, pyramidMesh.normals.byteLength);
boidNormalsBuffer.update(pyramidMesh.normals);
boidMat.setStorageBuffer("boidNormals", boidNormalsBuffer);

boidMesh.material = boidMat;

The issue I’m running into is when I try to add shadows to this mesh, because it’s trying to use the vertexBuffer which technically has a length of 1.

Vertex range (first: 0, count: 576) requires a larger buffer (6912) than the bound buffer size (4) of the vertex buffer at slot 0 with stride 12.

Is there a way to set the shadowGenerator to work with my custom mesh, or will have to generate my own shadowMap and use that?

Source code is here if that helps: GitHub - jtsorlinis/BoidsWebGPU

You will need to create your own depth shader material, as your use case is quite uncommon and WebGPU specific. You can then instruct the shadow generator to use this material when rendering the mesh to the shadow texture by calling shadowGenerator.getShadowMap().setMaterialForRendering(boidMesh, depthMaterial).

1 Like

Okay thanks for that. Will look into doing that :blush: