Three.js流星划过效果[粒子系统]
// 引入Three.js库
import * as THREE from 'three';
// 定义场景、摄像机和渲染器
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建流星划过的粒子系统
const maxParticles = 200; // 最大粒子数量
const particleGeometry = new THREE.Geometry(); // 粒子几何体
const particleMaterial = new THREE.PointsMaterial({
color: 0xFFFFFF,
size: 1,
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false
});
// 初始化粒子位置和速度
for (let i = 0; i < maxParticles; i++) {
const particle = new THREE.Vector3(
Math.random() * 2 - 1,
Math.random() * 2 - 1,
Math.random() * 2 - 1
);
particle.speed = Math.random() * 0.01 + 0.005;
particleGeometry.vertices.push(particle);
}
const particleSystem = new THREE.Points(particleGeometry, particleMaterial);
scene.add(particleSystem);
// 渲染循环
function animate() {
requestAnimationFrame(animate);
// 更新粒子位置
for (let i = 0; i < maxParticles; i++) {
const particle = particleGeometry.vertices[i];
particle.multiplyScalar(1 - particle.speed);
particle.z += particle.speed;
if (particle.z > 1) {
particle.z = -1;
particle.x = Math.random() * 2 - 1;
particle.y = Math.random() * 2 - 1;
}
}
particleGeometry.verticesNeedUpdate = true;
renderer.render(scene, camera);
}
animate();
这段代码创建了一个简单的粒子系统,用于模拟流星划过的效果。它定义了最大粒子数量、粒子材质和几何体,并在渲染循环中更新了粒子的位置和速度,实现了流星划过的视觉效果。
评论已关闭