当前位置:首页 > 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 提供了多种方式实现动画特效,主要通过内置组件和第三方库结合 CSS 或 JavaScript 完成。以下是常见的实现方法: 使用 Vue 内置过渡组件 Vue 的…

vue有没有实现特效

vue有没有实现特效

Vue 可以通过多种方式实现特效,包括内置过渡动画、第三方动画库、CSS 动画和 JavaScript 动画库。以下是一些常见方法: 使用 Vue 内置过渡动画 Vue 提供了 <transi…

vue登录功能实现特效

vue登录功能实现特效

Vue登录功能实现特效的方法 在Vue中实现登录功能的特效可以增强用户体验。以下是几种常见的特效实现方式: 背景粒子动画 使用particles.js或vue-particles库创建动态背景粒子效…

react实现特效

react实现特效

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

js实现图片特效

js实现图片特效

使用Canvas实现图片滤镜效果 Canvas API提供了强大的像素操作能力,可以轻松实现各种图片滤镜效果。通过获取图片的像素数据,对每个像素的RGB值进行数学运算。 const canvas =…

js 实现简单特效

js 实现简单特效

实现鼠标悬停按钮变色 通过监听鼠标事件改变按钮背景色,适合增强交互体验。以下代码使用纯JavaScript实现: const button = document.getElementById('ho…