This commit is contained in:
Andras Schmelczer 2023-04-29 21:18:40 +01:00
commit 99e1923066
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 47 additions and 33 deletions

View file

@ -34,24 +34,23 @@ export class AgentPipeline {
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const serializedAgents = new Float32Array(
new ArrayBuffer(agents.length * AGENT_SIZE_IN_BYTES)
);
agents.forEach((agent, i) => {
serializedAgents[(i * AGENT_SIZE_IN_BYTES) / Float32Array.BYTES_PER_ELEMENT + 0] =
agent.position[0];
serializedAgents[(i * AGENT_SIZE_IN_BYTES) / Float32Array.BYTES_PER_ELEMENT + 1] =
agent.position[1];
serializedAgents[(i * AGENT_SIZE_IN_BYTES) / Float32Array.BYTES_PER_ELEMENT + 2] =
agent.angle;
});
this.agentsBuffer = device.createBuffer({
size: agents.length * AGENT_SIZE_IN_BYTES,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
usage: GPUBufferUsage.STORAGE,
mappedAtCreation: true,
});
device.queue.writeBuffer(this.agentsBuffer, 0, serializedAgents.buffer);
new Float32Array(this.agentsBuffer.getMappedRange()).set(
agents.flatMap((agent) => [
agent.position[0],
agent.position[1],
agent.angle,
agent.species,
agent.timeToLive,
0, // padding
])
);
this.agentsBuffer.unmap();
}
public setParameters({