跳到主要内容

光柱效果

类型​

类

由半透明外锥、更亮的内层光芯、顶部呼吸光晕以及两圈地面扩散脉冲环组成的锥形光柱。常用作数据标记:把归一化后的数值映射到 height 配置项,柱体越高代表数值越大。

默认用法​

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

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

export default function App() {
const pillarsRef = useRef<LightPillar[]>([])

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

camera.position.set(0, 2.4, 6)
camera.lookAt(0, 1, 0)

const data = [
{ x: -2.2, z: 0, value: 0.35, color: 0x00ffff },
{ x: 0, z: -0.6, value: 1, color: 0x36e0a0 },
{ x: 2.2, z: 0, value: 0.62, color: 0xffb347 }
]

data.forEach(({ x, z, value, color }) => {
const pillar = new LightPillar({
color,
height: 1 + value * 2.4,
radius: 0.22
})
pillar.position.set(x, 0, z)
scene.add(pillar)
pillarsRef.current.push(pillar)
})
}

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

return (
<Scene
bgColor="rgb(40, 42, 54)"
onCreated={handleCreated}
style={{ marginTop: '10px', width: '100%', height: '300px' }}
/>
)
}

配置项​

属性类型默认值描述
colornumber0x00ffff光柱颜色,默认青色
heightnumber3光柱沿 Y 轴的高度,底面位于 y = 0
radiusnumber0.3锥体底面半径
opacitynumber1全局不透明度系数(0-1)
speednumber1动画速度系数
segmentsnumber32锥体与圆环的径向分段数
glowbooleantrue是否渲染顶部呼吸光晕
ringbooleantrue是否渲染底部的两圈扩散脉冲环
ringRadiusnumberradius * 3脉冲环扩散到的外半径

方法​

名称参数描述
constructor(options?: LightPillarOptions)创建光柱。它继承自 THREE.Group,直接加入场景并通过 position 摆放
dispose() => void销毁光柱、停止动画并从场景中移除。组件卸载时应调用以避免内存泄漏