跳到主要内容

扫光效果

类型

默认用法

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

export default function App() {
const sweepLightRef = useRef<SweepLight | null>(null)

const handleCreated = async (scene: THREE.Scene) => {
const model = await GLTFLoaderAsync('/models/perseverance-draco.glb', true)
model.scale.set(0.8, 0.8, 0.8)
scene.add(model)

sweepLightRef.current = new SweepLight(model)
}

// 卸载时清理
useEffect(() => {
return () => {
sweepLightRef.current?.dispose()
sweepLightRef.current = null
}
}, [])

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

配置项

属性类型默认值描述
colornumber0x00ffff扫光颜色(默认青色)
speednumber1.0扫光动画速度
widthnumber0.3扫光带宽度(0-1)
intensitynumber1.5光晕强度
directionnumber0扫光方向轴:0=X, 1=Y, 2=Z
loopLoopOnce | LoopRepeat | LoopPingPongLoopRepeat动画循环类型

方法

名称参数描述
constructor(model: THREE.Object3D, options?: SweepLightOptions)在模型上创建扫光效果
play() => void播放或继续动画。如果处于暂停状态,则从暂停位置继续;如果已停止,则从头开始播放。
pause() => void暂停在当前帧。调用 play() 可继续播放。
stop() => void停止动画并重置到初始状态。调用 play() 可从头开始播放。
dispose() => void销毁扫光效果并从场景中移除。应在组件卸载时调用以防止内存泄漏。