当前位置:首页 > JavaScript

js实现3D烟花特效

2026-04-05 03:19:59JavaScript

实现3D烟花特效的方法

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

安装Three.js:

npm install three

基础场景搭建

创建场景、相机和渲染器:

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 particleCount = 1000;
const particles = new THREE.BufferGeometry();
const positions = new Float32Array(particleCount * 3);
const colors = new Float32Array(particleCount * 3);

for (let i = 0; i < particleCount; i++) {
    positions[i*3] = (Math.random() - 0.5) * 2;
    positions[i*3+1] = Math.random() * 5;
    positions[i*3+2] = (Math.random() - 0.5) * 2;

    colors[i*3] = Math.random();
    colors[i*3+1] = Math.random();
    colors[i*3+2] = Math.random();
}

particles.setAttribute('position', new THREE.BufferAttribute(positions, 3));
particles.setAttribute('color', new THREE.BufferAttribute(colors, 3));

const particleMaterial = new THREE.PointsMaterial({
    size: 0.1,
    vertexColors: true
});

const particleSystem = new THREE.Points(particles, particleMaterial);
scene.add(particleSystem);

动画与物理效果

实现粒子运动轨迹和重力效果:

function animate() {
    requestAnimationFrame(animate);

    const positions = particles.attributes.position.array;
    for (let i = 0; i < particleCount; i++) {
        positions[i*3+1] -= 0.01; // 重力效果
        positions[i*3] += (Math.random() - 0.5) * 0.02; // 随机漂移
        positions[i*3+2] += (Math.random() - 0.5) * 0.02;
    }

    particles.attributes.position.needsUpdate = true;
    renderer.render(scene, camera);
}

animate();

交互触发烟花

添加点击事件触发新烟花:

window.addEventListener('click', () => {
    const explosionPosition = new THREE.Vector3(
        (Math.random() - 0.5) * 10,
        0,
        (Math.random() - 0.5) * 10
    );

    createExplosion(explosionPosition);
});

function createExplosion(position) {
    const explosionParticles = new THREE.BufferGeometry();
    // 类似上面的粒子创建代码,但初始位置设为爆炸点
    // 添加爆炸后的扩散效果
}

性能优化建议

使用InstancedMesh提高大量粒子性能:

const instancedMesh = new THREE.InstancedMesh(geometry, material, count);
// 为每个实例设置不同位置和颜色

考虑使用GPU加速计算:

// 通过ShaderMaterial编写自定义着色器
const shaderMaterial = new THREE.ShaderMaterial({
    uniforms: {...},
    vertexShader: `...`,
    fragmentShader: `...`
});

视觉效果增强

添加后期处理效果:

import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer';
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass';
import { GlitchPass } from 'three/examples/jsm/postprocessing/GlitchPass';

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

实现拖尾效果:

js实现3D烟花特效

// 使用自定义着色器或在每帧更新时保留粒子历史位置

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

相关文章

vue实现各种特效

vue实现各种特效

Vue实现动画效果 使用Vue的过渡系统实现元素进入/离开的动画效果。通过<transition>组件包裹需要动画的元素,并定义CSS过渡类名或JavaScript钩子。 <tra…

vue跳页面实现特效

vue跳页面实现特效

Vue 页面跳转实现特效的方法 在 Vue 中实现页面跳转特效可以通过多种方式,以下是几种常见的方法: 使用 Vue Router 的过渡动画 Vue Router 与 Vue 的过渡系统结合,可以…

react如何实现特效

react如何实现特效

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

react如何实现导航特效

react如何实现导航特效

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

css 制作特效

css 制作特效

使用 CSS 制作特效的方法 动画效果(@keyframes) 通过 @keyframes 定义动画序列,结合 animation 属性实现动态效果。 示例代码: @keyframes fade…

css文字制作特效

css文字制作特效

文字阴影效果 使用 text-shadow 属性为文字添加阴影,支持多阴影叠加。参数依次为水平偏移、垂直偏移、模糊半径和颜色。 .text-shadow { text-shadow: 2px…