在Three.js v0.142.0版本中,Three.Geometry已经在v0.125.0版本中被废弃了,需要使用BufferGeometry代替。另外,Three.PointsMaterial也已经被废弃,需要使用ShaderMaterial或者PointsMaterial的后继者PointsMaterialParameters来取代。

下面是修改后的代码:

const particleCount = 1000;
const particles = new Three.BufferGeometry();
const particlePositions = new Float32Array(particleCount * 3);

for (let p = 0; p < particleCount; p++) {
  const x = Math.random() * 50 - 25;
  const y = Math.random() * 50 - 25;
  const z = Math.random() * 50 - 25;
  particlePositions[p * 3] = x;
  particlePositions[p * 3 + 1] = y;
  particlePositions[p * 3 + 2] = z;
}

particles.setAttribute('position', new Three.BufferAttribute(particlePositions, 3));

const particleMaterial = new Three.PointsMaterial({
  color: 0xff3300,
  size: 1,
  blending: Three.AdditiveBlending,
  transparent: true,
  opacity: 0.8
});

const particleSystem = new Three.Points(particles, particleMaterial);
const velocity = new Three.Vector3(0, 0.5, 0);
scene.add(particleSystem);

function animateFire() {
  const positions = particleSystem.geometry.attributes.position.array;
  for (let i = 0; i < particleCount; i++) {
    const x = positions[i * 3];
    const y = positions[i * 3 + 1];
    const z = positions[i * 3 + 2];
    positions[i * 3] = x + velocity.x;
    positions[i * 3 + 1] = y + velocity.y;
    positions[i * 3 + 2] = z + velocity.z;
    if (positions[i * 3 + 1] > 25) {
      positions[i * 3 + 1] = Math.random() * -10;
      positions[i * 3] = Math.random() * 50 - 25;
      positions[i * 3 + 2] = Math.random() * 50 - 25;
    }
  }
  particleSystem.geometry.attributes.position.needsUpdate = true;
}
Three.js v0.142.0版本中使用BufferGeometry和PointsMaterialParameters代替Geometry和PointsMaterial

原文地址: https://www.cveoy.top/t/topic/mLsH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录