From c91f6d64f59303dc7a4017b8c45629aa23c0a504 Mon Sep 17 00:00:00 2001 From: Martin Valigursky Date: Wed, 5 Jun 2024 10:49:06 +0100 Subject: [PATCH] Particle system uses uint16 indices instead of uint32 --- .../graphics/particles-mesh.example.mjs | 2 +- src/scene/particle-system/particle-emitter.js | 20 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/examples/src/examples/graphics/particles-mesh.example.mjs b/examples/src/examples/graphics/particles-mesh.example.mjs index 78417bf2b25..eae2fdba9db 100644 --- a/examples/src/examples/graphics/particles-mesh.example.mjs +++ b/examples/src/examples/graphics/particles-mesh.example.mjs @@ -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]), diff --git a/src/scene/particle-system/particle-emitter.js b/src/scene/particle-system/particle-emitter.js index a5115f6a4f8..94635615899 100644 --- a/src/scene/particle-system/particle-emitter.js +++ b/src/scene/particle-system/particle-emitter.js @@ -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, @@ -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(); @@ -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, @@ -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 @@ -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;