当前位置:首页 > JavaScript

js实现3D烟花特效

2026-03-02 02:28:46JavaScript

实现3D烟花特效的方法

使用Three.js库可以高效实现3D烟花效果。Three.js是一个基于WebGL的JavaScript 3D图形库,适合创建复杂的3D动画效果。

基础环境搭建

引入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);

烟花粒子系统实现

创建烟花爆炸时的粒子系统:

function createFirework(position) {
  const particles = 1000;
  const geometry = new THREE.BufferGeometry();
  const positions = new Float32Array(particles * 3);

  for (let i = 0; i < particles; i++) {
    const i3 = i * 3;
    positions[i3] = position.x;
    positions[i3 + 1] = position.y;
    positions[i3 + 2] = position.z;
  }

  geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
  const material = new THREE.PointsMaterial({
    color: new THREE.Color(Math.random(), Math.random(), Math.random()),
    size: 0.2,
    blending: THREE.AdditiveBlending,
    transparent: true
  });

  const particleSystem = new THREE.Points(geometry, material);
  scene.add(particleSystem);

  return { particleSystem, positions };
}

动画更新逻辑

实现粒子爆炸和下落动画:

function animateFirework(firework) {
  const { particleSystem, positions } = firework;
  const geometry = particleSystem.geometry;
  const positionAttribute = geometry.attributes.position;

  for (let i = 0; i < positionAttribute.count; i++) {
    const i3 = i * 3;
    positionAttribute.array[i3] += (Math.random() - 0.5) * 0.5;
    positionAttribute.array[i3 + 1] += (Math.random() - 0.5) * 0.5;
    positionAttribute.array[i3 + 2] += (Math.random() - 0.5) * 0.5;

    // 模拟重力
    positionAttribute.array[i3 + 1] -= 0.01;
  }

  positionAttribute.needsUpdate = true;

  // 淡出效果
  particleSystem.material.opacity -= 0.005;

  if (particleSystem.material.opacity <= 0) {
    scene.remove(particleSystem);
    return false;
  }

  return true;
}

主渲染循环

整合所有元素到主动画循环:

const fireworks = [];
let lastFireworkTime = 0;

function animate() {
  requestAnimationFrame(animate);

  const currentTime = Date.now();
  if (currentTime - lastFireworkTime > 1000) {
    const position = new THREE.Vector3(
      (Math.random() - 0.5) * 10,
      0,
      (Math.random() - 0.5) * 10
    );
    fireworks.push(createFirework(position));
    lastFireworkTime = currentTime;
  }

  for (let i = fireworks.length - 1; i >= 0; i--) {
    if (!animateFirework(fireworks[i])) {
      fireworks.splice(i, 1);
    }
  }

  renderer.render(scene, camera);
}

animate();

性能优化技巧

使用InstancedMesh代替Points可以提升性能:

const particleCount = 10000;
const instancedMesh = new THREE.InstancedMesh(
  new THREE.SphereGeometry(0.1, 8, 8),
  new THREE.MeshBasicMaterial({ color: 0xffffff }),
  particleCount
);

const dummy = new THREE.Object3D();
for (let i = 0; i < particleCount; i++) {
  dummy.position.set(Math.random() * 10 - 5, Math.random() * 10 - 5, Math.random() * 10 - 5);
  dummy.updateMatrix();
  instancedMesh.setMatrixAt(i, dummy.matrix);
}
scene.add(instancedMesh);

视觉效果增强

添加辉光效果提升视觉冲击力:

import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass.js';

const bloomPass = new UnrealBloomPass(
  new THREE.Vector2(window.innerWidth, window.innerHeight),
  1.5, 0.4, 0.85
);
bloomPass.threshold = 0;
bloomPass.strength = 1.5;
bloomPass.radius = 0.5;

const composer = new EffectComposer(renderer);
composer.addPass(new RenderPass(scene, camera));
composer.addPass(bloomPass);

// 在animate函数中使用composer.render()代替renderer.render()

js实现3D烟花特效

标签: 烟花特效
分享给朋友:

相关文章

vue实现烟花效果

vue实现烟花效果

Vue 实现烟花效果的方法 在Vue中实现烟花效果可以通过结合Canvas绘图和动画技术来完成。以下是一种常见的实现方式: 创建Vue组件 新建一个名为Fireworks.vue的组件,核心逻辑如下…

vue实现边框特效

vue实现边框特效

Vue 实现边框特效的方法 边框特效可以通过 CSS 动画、伪元素、SVG 或 JavaScript 动态计算来实现。以下是几种常见的实现方式: 使用 CSS 动画实现动态边框 在 Vue 组件的…

react如何实现特效

react如何实现特效

React 实现特效的方法 在 React 中实现特效可以通过多种方式,包括 CSS 动画、JavaScript 动画库以及 React 特定的动画解决方案。以下是几种常见的实现方法: 使用 CS…

react如何实现导航特效

react如何实现导航特效

实现导航特效的常见方法 在React中实现导航特效可以通过多种方式,以下是一些常见的实现方案: 使用CSS过渡和动画 通过CSS的transition或animation属性为导航元素添加平滑的过渡…

react实现特效

react实现特效

React 实现特效的常见方法 使用 CSS 动画和过渡 通过 CSS 的 @keyframes 和 transition 属性实现基础动画效果,结合 React 的 className 或 styl…

特效react实现点击变大

特效react实现点击变大

实现点击元素放大效果的React方法 使用CSS过渡和React状态结合的方式可以轻松实现点击放大效果。以下是一个完整示例: import React, { useState } from…