跳到主要内容

烟花效果

类型​

类

完全由着色器实现的烟花弹。一条发射拖尾从原点升起,随后炸裂成球状喷洒的、受重力弯曲的丝带轨迹:头部发光,尾部通过数值噪声溶解消散。一个 Fireworks 实例就是一发烟花 —— 添加多个不同颜色、高度与相位的实例即可编排一整场表演。

默认用法​

下面的每个示例都可以用 WebGPU(默认)或 WebGL 渲染器查看 —— 切换标签页进行对比。

import { useRef, useEffect } from 'react'
import * as THREE from 'three'
import { Scene, Fireworks } from 'react-three-lite'
import type { SceneComponents } from 'react-three-lite'

export default function App() {
const fireworksRef = useRef<Fireworks[]>([])

const handleCreated = (scene: THREE.Scene, components: SceneComponents) => {
const { camera } = components
if (!camera) return

camera.position.set(0, 6, 17)
camera.lookAt(0, 6, 0)

const shells = [
{ color: 0xffd4a0, height: 6.6, scale: 3.6, offset: [-3.6, 0, -1.5], launchDuration: 1.25, burstDuration: 1.9 },
{ color: 0x7ad7ff, height: 5.9, scale: 3.9, offset: [-1.4, 0, 1.6], launchDuration: 1.7, burstDuration: 1.6 },
{ color: 0xff6b6b, height: 7.4, scale: 3.2, offset: [3.2, 0, -0.8], launchDuration: 1.5, burstDuration: 2.2 }
]

shells.forEach((shell) => {
const fireworks = new Fireworks({
color: shell.color,
burstHeight: shell.height,
burstScale: shell.scale,
burstPositionOffset: new THREE.Vector3(...shell.offset),
launchDuration: shell.launchDuration,
burstDuration: shell.burstDuration,
loop: true
})
scene.add(fireworks)
fireworksRef.current.push(fireworks)
})
}

// Cleanup on unmount
useEffect(() => {
return () => {
fireworksRef.current.forEach((fireworks) => fireworks.dispose())
fireworksRef.current = []
}
}, [])

return (
<Scene
bgColor="#1a1a2e"
onCreated={handleCreated}
style={{ marginTop: '10px', width: '100%', height: '300px' }}
/>
)
}

配置项​

属性类型默认值描述
radiusnumber1炸裂拖尾的目标长度半径,烘焙进几何体
detailnumber0二十面体细分级别,烘焙进几何体
segmentsnumber5每条拖尾上采样的分段数,烘焙进几何体
gravitynumber1使拖尾向下弯曲的重力抛物线系数,烘焙进几何体
widthnumber0.1丝带半宽
startTimenumber0动画起始时间(秒)
launchDurationnumber1.5发射阶段时长(秒)
delayDurationnumber-0.4发射与炸裂之间的延迟,可为负值以重叠
burstDurationnumber2炸裂阶段时长(秒)
burstHeightnumber6.5沿局部 Y 轴的炸裂高度
burstScalenumber3.5整体炸裂缩放
burstPositionOffsetTHREE.Vector3(0, 0, 0)炸裂原点偏移
loopbooleantrue一个周期结束后是否重新播放
colorTHREE.ColorRepresentation'#ffd4a0'丝带基础颜色
bloomIntensitynumber9头部光晕强度
bloomFalloffnumber18头部光晕长度指数,越大头部越收窄
noiseFalloffnumber3噪声溶解的时间指数
noiseScaleTHREE.Vector2(10, 200)噪声 UV 缩放(横向、纵向)

方法​

名称参数描述
constructor(options?: FireworksOptions)创建一发烟花。它继承自 THREE.Mesh,直接加入场景并通过 position 摆放
update(deltaSeconds: number) => void按秒推进动画时间
restart() => void将当前时间重置为 0,并从 startTime 重新播放
dispose() => void销毁烟花、停止动画并从场景中移除。组件卸载时应调用以避免内存泄漏