I want to implement a particle, Below is my code
const geometry = new THREE.BufferGeometry();
const material = new THREE.PointsMaterial({
size: 2,
transparent: true,
depthWrite: false,
vertexColors: true,
blending: THREE.AdditiveBlending,
map: new THREE.TextureLoader().load(getImageFile('dot2.png')),
});
const system = new Particles();
const { position, color } = system.getInfo();
geometry.attributes.position = position;
geometry.attributes.color = color;
const particles = new THREE.Points(geometry, material);
const animate = (timestamp: number) => {
const { position, color, opacity } = system.animateAndInfo(timestamp);
geometry.attributes.position = position;
geometry.attributes.color = color;
geometry.attributes.opacity = opacity;
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
scene.add(particles);
export default class Particles {
...
animate(timestamp: number) {
if (this.state === 'SUSPEND') return;
if (!this.timestamp) this.timestamp = timestamp;
const deltaTime = timestamp - this.lastTimestamp;
this.lastTimestamp = timestamp;
this.pointList.forEach(point => point.update(timestamp, deltaTime));
this.clearPoint();
this.addFloatUpPoint(timestamp);
}
getInfo() {
const position = this.pointList.reduce((p, c) => [...p, ...c.getPosition()], []);
const color = this.pointList.reduce((p, c) => {
const color = new THREE.Color(c.color);
return [...p, ...[color.r, color.g, color.b]];
}, [])
const opacity = this.pointList.length < 1 ? [] : [1, ...new Array(this.pointList.length - 1).fill(0)];
return {
position: new THREE.Float32BufferAttribute(position, 3),
color: new THREE.Float32BufferAttribute(color, 3),
opacity: new THREE.Float32BufferAttribute(new Float32Array(opacity), 1),
}
}
animateAndInfo(timestamp: number) {
this.animate(timestamp);
return this.getInfo();
}
}
When I try to control the transparency of some points, it doesn’t work.
How to change part of point’s opactity in three.js BufferGeometry? Or is there any other way to achieve it?