Skip to content

Commit

Permalink
Particle system uses uint16 indices instead of uint32 (#6661)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Jun 5, 2024
1 parent 6eb228d commit 8c1846a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/src/examples/graphics/particles-mesh.example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ assetListLoader.load(() => {

// when texture is loaded add particlesystem component to entity
entity.addComponent('particlesystem', {
numParticles: 150,
numParticles: 25,
lifetime: 1,
rate: 0.01,
scaleGraph: new pc.Curve([0, 0.2, 1, 0.7]),
Expand Down
20 changes: 14 additions & 6 deletions src/scene/particle-system/particle-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
BUFFER_DYNAMIC,
CULLFACE_NONE,
FILTER_LINEAR, FILTER_NEAREST,
INDEXFORMAT_UINT32,
INDEXFORMAT_UINT16,
PIXELFORMAT_RGBA8, PIXELFORMAT_RGBA32F,
PRIMITIVE_TRIANGLES,
SEMANTIC_ATTR0, SEMANTIC_ATTR1, SEMANTIC_ATTR2, SEMANTIC_ATTR3, SEMANTIC_ATTR4, SEMANTIC_TEXCOORD0,
Expand Down Expand Up @@ -576,7 +576,15 @@ class ParticleEmitter {

particleTexHeight = (this.useCpu || this.pack8) ? 4 : 2;

this.useMesh = !!this.mesh;
this.useMesh = false;
if (this.mesh) {
const totalVertCount = this.numParticles * this.mesh.vertexBuffer.numVertices;
if (totalVertCount > 65535) {
Debug.warn('WARNING: particle system can\'t render mesh particles because numParticles * numVertices is more than 65k. Reverting to quad particles.');
} else {
this.useMesh = true;
}
}

this.numParticlesPot = math.nextPowerOfTwo(this.numParticles);
this.rebuildGraphs();
Expand Down Expand Up @@ -982,7 +990,7 @@ class ParticleEmitter {
this.vertexBuffer = new VertexBuffer(this.graphicsDevice, particleFormat, psysVertCount, {
usage: BUFFER_DYNAMIC
});
this.indexBuffer = new IndexBuffer(this.graphicsDevice, INDEXFORMAT_UINT32, psysIndexCount);
this.indexBuffer = new IndexBuffer(this.graphicsDevice, INDEXFORMAT_UINT16, psysIndexCount);
} else {
const elements = [{
semantic: SEMANTIC_ATTR0,
Expand Down Expand Up @@ -1010,7 +1018,7 @@ class ParticleEmitter {
this.vertexBuffer = new VertexBuffer(this.graphicsDevice, particleFormat, psysVertCount, {
usage: BUFFER_DYNAMIC
});
this.indexBuffer = new IndexBuffer(this.graphicsDevice, INDEXFORMAT_UINT32, psysIndexCount);
this.indexBuffer = new IndexBuffer(this.graphicsDevice, INDEXFORMAT_UINT16, psysIndexCount);
}

// Fill the vertex buffer
Expand Down Expand Up @@ -1057,8 +1065,8 @@ class ParticleEmitter {

// Fill the index buffer
let dst = 0;
const indices = new Uint32Array(this.indexBuffer.lock());
if (this.useMesh) meshData = new Uint32Array(this.mesh.indexBuffer[0].lock());
const indices = new Uint16Array(this.indexBuffer.lock());
if (this.useMesh) meshData = new Uint16Array(this.mesh.indexBuffer[0].lock());
for (let i = 0; i < numParticles; i++) {
if (!this.useMesh) {
const baseIndex = i * 4;
Expand Down

0 comments on commit 8c1846a

Please sign in to comment.