Hello,
I am trying to use compute shaders to compute positions of a large particle system. I have read the boids webgpu example.
In the code below, I am trying to update the mesh vertices positions using a VertexBuffer
with setVerticesBuffer, but it doesn’t work. When doing so, the mesh just disappear;
See the mesh.setVerticesBuffer(positionsVertexBuffer, false);
commented code line.
import * as BABYLON from 'babylonjs';
export async function SimulateBabylon() {
const canvas = document.createElement('canvas');
document.body.style.margin = '0';
document.body.style.overflow = 'hidden'; // Prevents scrollbars
document.documentElement.style.margin = '0';
canvas.style.width = '100vw';
canvas.style.height = '100vh';
canvas.style.display = 'block'; // Ensures the canvas fills the width/height without margins
document.body.appendChild(canvas);
const engine = new BABYLON.WebGPUEngine(canvas, { adaptToDeviceRatio: true, antialias: true });
await engine.initAsync();
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.ArcRotateCamera("camera", Math.PI / 2, Math.PI / 2, 10, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, true);
new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene);
const mesh = new BABYLON.Mesh("custom", scene);
const vertexData = new BABYLON.VertexData();
const vertexCount = 4;
const positions = [0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0]; // 4 vertices with x, y, z
const indices = [0, 1, 2, 1, 3, 2]; // 2 triangles with 3 vertices each, in a square shape
vertexData.positions = positions;
vertexData.indices = indices;
vertexData.applyToMesh(mesh);
const material = new BABYLON.StandardMaterial("material", scene);
material.diffuseColor = new BABYLON.Color3(1, 0, 0); // RGB for red
material.wireframe = false;
material.backFaceCulling = false;
mesh.material = material;
mesh.sideOrientation = BABYLON.Mesh.DOUBLESIDE;
engine.runRenderLoop(() => {
scene.render();
});
window.addEventListener('resize', () => {
engine.resize();
});
// set up webgpu buffer for storing positions
const positionsStorageBuffer = new BABYLON.StorageBuffer(engine, vertexCount * 4 * 3, BABYLON.Constants.BUFFER_CREATIONFLAG_VERTEX | BABYLON.Constants.BUFFER_CREATIONFLAG_WRITE);
const positionsVertexBuffer = new BABYLON.VertexBuffer(engine, positionsStorageBuffer.getBuffer(), BABYLON.VertexBuffer.PositionKind, false, false, 3, true, 0, 3);
positionsStorageBuffer.update(new Float32Array(positions));
// index buffer
const indexBuffer = new BABYLON.StorageBuffer(engine, indices.length * 4, BABYLON.Constants.BUFFER_CREATIONFLAG_INDEX | BABYLON.Constants.BUFFER_CREATIONFLAG_WRITE);
indexBuffer.update(new Uint32Array(indices));
async function animate() {
engine.runRenderLoop(async () => {
// mesh.setVerticesBuffer(positionsVertexBuffer, false); // uncomment this line to try to update the mesh with the new positions. However, this doesn't work, the mesh just disappears
// mesh.setIndexBuffer(indexBuffer.getBuffer(), vertexCount, indices.length); // this one too
scene.render();
});
}
await animate();
}
Do I miss something?