Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Particle system uses uint16 indices instead of uint32 #6661

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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