完善粒子

This commit is contained in:
unanmed 2023-02-15 21:15:33 +08:00
parent 6a667d4ff7
commit 9d995567bd
3 changed files with 38 additions and 17 deletions

View File

@ -49,6 +49,10 @@ export class Particle {
/** 需要渲染的粒子列表 */
list: ParticleOne[] = [];
/** 是否需要更新缓冲区数据 */
needUpdateBuffer: boolean = false;
/** 当前缓存信息 */
cache?: Float32Array;
/** 是否需要更新 */
private needUpdate: boolean = false;
@ -175,6 +179,27 @@ export class Particle {
this.list = particles;
}
/**
* Float32Array信息
*/
getArrayInfo() {
if (!this.cache || this.needUpdateBuffer) {
const array = this.list;
const particleArray = new Float32Array(
array
.map(v => {
const [r, g, b, a] = v.color;
return [v.x, v.y, v.z, r, g, b, a, v.r, 0];
})
.flat()
);
this.cache = particleArray;
return particleArray;
} else {
return this.cache;
}
}
/**
*
*/
@ -185,10 +210,12 @@ export class Particle {
// check number
if (this.list.length > this.density) {
this.list.splice(this.density);
this.needUpdateBuffer = true;
} else if (this.list.length < this.density) {
this.list.push(
...this.generateNewParticles(this.density - this.list.length)
);
this.needUpdateBuffer = true;
}
// check radius
@ -198,6 +225,7 @@ export class Particle {
this.list.forEach(v => {
v.r += delta;
});
this.needUpdateBuffer = true;
}
}
@ -214,6 +242,7 @@ export class Particle {
v.color[2] += b;
v.color[3] += a;
});
this.needUpdateBuffer = true;
}
}
@ -228,6 +257,7 @@ export class Particle {
v.y += y;
v.z += z;
});
this.needUpdateBuffer = true;
}
}
@ -266,6 +296,7 @@ export class Particle {
}) as ParticleColor;
});
}
this.needUpdateBuffer = true;
}
}
@ -298,6 +329,6 @@ export class Particle {
*
*/
private render() {
this.renderer?.render(this);
this.renderer?.render();
}
}

View File

@ -202,15 +202,8 @@ export class Renderer {
*
* @param array
*/
private updateOneParticleBufferData(array: ParticleOne[]) {
const particleArray = new Float32Array(
array
.map(v => {
const [r, g, b, a] = v.color;
return [v.x, v.y, v.z, r, g, b, a, v.r, 0];
})
.flat()
);
private updateOneParticleBufferData(particle: Particle) {
const particleArray = particle.getArrayInfo();
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
particleArray,
@ -240,7 +233,7 @@ export class Renderer {
* @param particle
*/
private renderOne(particle: Particle) {
const arr = this.updateOneParticleBufferData(particle.list);
const arr = this.updateOneParticleBufferData(particle);
const size = arr.BYTES_PER_ELEMENT;
const { position, color, radius } = this.attribLocation;
@ -307,11 +300,9 @@ window.addEventListener('load', async () => {
camera.lookAt([1, 1, 5], [0, 0, 0], [0, 1, 0]);
camera.setPerspective(20, 1, 1, 100);
console.log(camera.view, camera.projection);
particle.setColor([0.3, 0.6, 0.7, 0.7]);
particle.setRadius(3);
particle.setDensity(1000);
particle.setColor([0.3, 0.6, 0.7, 1.0]);
particle.setRadius(2);
particle.setDensity(5000);
particle.setThreshold({
posX: 0.2,
posY: 0.2,

View File

@ -1,4 +1,3 @@
import { cloneDeep } from 'lodash';
import { has } from '../utils';
export class Matrix extends Array<number[]> {