<template>
  <div class="particle-container">
    <canvas ref="canvasRef"></canvas>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import * as THREE from 'three';

// 参数配置
const pointCount = 800; // 粒子数量
const particleSize = 5; // 粒子大小
const maxRange = 1000; // 粒子生成范围
const rotationSpeed = 0.001; // 旋转速度

const canvasRef = ref<HTMLCanvasElement | null>(null);
let renderer: THREE.WebGLRenderer;
let scene: THREE.Scene;
let camera: THREE.PerspectiveCamera;
let animationId: number;

let xBgPoints: THREE.Points, yBgPoints: THREE.Points, zBgPoints: THREE.Points;

// 初始化场景
function initScene() {
  const canvas = canvasRef.value;
  if (!canvas) return;

  const width = window.innerWidth;
  const height = window.innerHeight;

  // 创建场景
  scene = new THREE.Scene();
  scene.background = new THREE.Color(0x000000);

  // 创建相机
  camera = new THREE.PerspectiveCamera(40, width / height, 1, 3000);
  camera.position.z = 1000;

  // 创建渲染器
  renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true });
  renderer.setSize(width, height);
  renderer.setPixelRatio(window.devicePixelRatio);

  // 创建粒子背景
  initBackgroundPoints();
}

// 随机生成坐标
function randomPosition(min: number, max: number) {
  return Math.random() * (max - min) + min;
}

// 初始化背景粒子
function initBackgroundPoints() {
  const geometry = new THREE.BufferGeometry();
  const positions = [];
  for (let i = 0; i < pointCount; i++) {
    positions.push(
      randomPosition(-maxRange, maxRange),
      randomPosition(-maxRange, maxRange),
      randomPosition(-maxRange, maxRange),
    );
  }
  geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));

  const material = new THREE.PointsMaterial({
    size: particleSize,
    color: new THREE.Color(0x00aaff), // 纯色粒子
    transparent: true,
    opacity: 0.8,
    blending: THREE.AdditiveBlending,
  });

  // 创建三层粒子系统
  xBgPoints = new THREE.Points(geometry, material);
  xBgPoints.position.z = -0.5 * maxRange;

  yBgPoints = new THREE.Points(geometry, material);
  yBgPoints.position.z = -0.6 * maxRange;

  zBgPoints = new THREE.Points(geometry, material);
  zBgPoints.position.z = -0.7 * maxRange;

  scene.add(xBgPoints);
  scene.add(yBgPoints);
  scene.add(zBgPoints);
}

// 动画循环
function animate() {
  animationId = requestAnimationFrame(animate);

  // 添加旋转效果
  if (xBgPoints && yBgPoints && zBgPoints) {
    xBgPoints.rotation.y += rotationSpeed;
    yBgPoints.rotation.y += -rotationSpeed;
    zBgPoints.rotation.z += rotationSpeed / 2;
  }

  renderer.render(scene, camera);
}

// 窗口大小自适应
function onResize() {
  const width = window.innerWidth;
  const height = window.innerHeight;
  camera.aspect = width / height;
  camera.updateProjectionMatrix();
  renderer.setSize(width, height);
}

// 生命周期钩子
onMounted(() => {
  initScene();
  animate();
  window.addEventListener('resize', onResize);
});

onUnmounted(() => {
  cancelAnimationFrame(animationId);
  window.removeEventListener('resize', onResize);
});
</script>

<style scoped>
.particle-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #000;
}
</style>
