当前位置:首页 > 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);

烟花粒子系统实现

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

js实现3D烟花特效

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;
}

主渲染循环

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

js实现3D烟花特效

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()

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

相关文章

react如何实现特效

react如何实现特效

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

js实现3D烟花特效

js实现3D烟花特效

使用Three.js实现3D烟花特效 Three.js是一个强大的JavaScript 3D库,适合创建烟花特效。以下是一个基础实现示例: 安装Three.js: npm install thre…

jquery图片特效

jquery图片特效

以下是几种常见的jQuery图片特效实现方法,结合网络资源整理而成: 图片轮播(幻灯片)效果 使用jQuery的animate()或结合插件如slick实现轮播效果。基础代码结构: $('.sli…

uniapp滑动特效

uniapp滑动特效

uniapp 滑动特效实现方法 使用swiper组件实现基础滑动 swiper是uniapp内置的滑块视图容器,常用于轮播图或页面切换。通过设置autoplay、interval和circular等参…

vue实现动画特效

vue实现动画特效

Vue 实现动画特效的方法 Vue 提供了多种方式实现动画特效,包括内置的过渡和动画系统,以及结合第三方动画库使用。以下是几种常见的方法: 使用 Vue 的过渡系统 Vue 提供了 <tran…

vue实现滚轮特效

vue实现滚轮特效

vue实现滚轮特效的方法 使用原生滚动事件监听 在Vue组件中,可以通过@wheel事件监听鼠标滚轮动作。结合CSS transform属性实现缩放或位移效果。 <template>…