// 引入Three.js相关库
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { TWEEN } from 'three/examples/jsm/libs/tween.module.min.js';
// 设置场景、相机和渲染器
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);
// 创建补间动画库TWEEN的全局计时器
TWEEN.init();
// 创建星空背景
const starsGeometry = new THREE.SphereGeometry(10000, 200, 200);
const starsMaterial = new THREE.MeshBasicMaterial({ color: 0x000000 });
const stars = new THREE.Mesh(starsGeometry, starsMaterial);
scene.add(stars);
// 创建补间动画 - 让星空旋转
new TWEEN.Tween(stars.rotation).to({
y: (Math.PI * 2)
}, 10000).easing(TWEEN.Easing.Linear).repeat(Infinity).start();
// 创建粒子系统
const particleCount = 1000;
const particleGeometry = new THREE.BufferGeometry();
const positions = new Float32Array(particleCount * 3);
particleGeometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
const particleMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 1 });
const particles = new THREE.Points(particleGeometry, particleMaterial);
scene.add(particles);
// 初始化粒子位置
function initParticles() {
for (let i = 0; i < particleCount; i++) {
positions[i * 3 + 0] = Math.random() * 2 - 1;
positions[i * 3 + 1] = Math.random() * 2 - 1;
positions[i * 3 + 2] = Math.random() * 2 - 1;
}
particleGeometry.attributes.position.needsUpdate = true;
}
initParticles();
// 创建补间动画 - 让粒子随机飞舞
new TWEEN.Tween(this).to({}, 10000).onUpdate(function() {
for (let i = 0; i < particleCount; i++) {
const particle = {
x: positions[i * 3 + 0],
y: positions[i * 3 + 1],
z: positions[i * 3 + 2]
};
particle.y += Math.random() * 0.01;
particle.x += Math.random() * 0.001;
particle.z += Math.random() * 0.001;
positions[i * 3 + 0] = particle.x;
positions[i * 3 + 1] = particle.y;
posit
评论已关闭