粒子特效
console.clear()
function particleSpread(entity, colorList, scale = 1) {
? ? entity.particleRate = 550//粒子生成速率
? ? entity.particleLifetime = 2//粒子存活時間為2秒
? ? entity.particleColor = colorList//粒子的顏色列表
? ? entity.particleSize = [15 * scale, 10 * scale, 5 * scale, 2 * scale, scale]//粒子在5個階段的大小
? ? entity.particleSizeSpread = 2 * scale//粒子的大小的隨機變化幅度
? ? entity.particleVelocitySpread = [20 * scale, 20 * scale, 20 * scale]//粒子XYZ方向速度的隨機變化幅度
? ? entity.particleAcceleration = [0, -10 * scale, 0]//粒子往下飄落模擬重力效果
}
const YELLOW = new Box3RGBColor(10, 10, 2)//黃
const CYAN = new Box3RGBColor(2, 10, 10)//青
const MAGENTA = new Box3RGBColor(10, 2, 10)//品紅
const RED = new Box3RGBColor(10, 2, 2)//紅
const GREEN = new Box3RGBColor(2, 10, 2)//綠
const BLUE = new Box3RGBColor(2, 2, 10)//藍(lán)
//各個顏色5個粒子階段的顏色列表
const ColorList = [
? ? [YELLOW, YELLOW, YELLOW, YELLOW, YELLOW],
? ? [CYAN, CYAN, CYAN, CYAN, CYAN],
? ? [MAGENTA, MAGENTA, MAGENTA, MAGENTA, MAGENTA],
? ? [RED, RED, RED, RED, RED],
? ? [GREEN, GREEN, GREEN, GREEN, GREEN],
? ? [BLUE, BLUE, BLUE, BLUE, BLUE],
]
function randomColor() {
? ? return ColorList[Math.floor(ColorList.length * Math.random())] //隨機選取一個顏色列表
}
const MeshScale = [1 / 16, 1 / 16, 1 / 16] //默認(rèn)的模型縮放系數(shù)
async function particleShoot(entity) {
? ? const fireball = world.createEntity({
? ? ? ? bounds: [0, 0, 0], // 隱形實體默認(rèn)大小是1x1x1的方塊, 現(xiàn)在把它從方塊縮成大小為0x0x0的1個點
? ? ? ? collides: false, // 開啟碰撞
? ? ? ? gravity: false, // 不受重力影響
? ? ? ? position: entity.position,
? ? })
? ? const color = randomColor()//隨機顏色
? ? particleSpread(fireball, color, 0.3) //粒子大小系數(shù)縮到0.3, 形成小火球
? ? await sleep(100) // 等待0.1秒后升空
? ? fireball.velocity.set(0, 1, 0) // 火球速度為每秒向上1格
? ? await sleep(2000) //升空2秒后爆炸
? ? fireball.velocity.set(0, 0, 0) // 火球停止運動
? ? particleSpread(fireball, color, 1) // 系數(shù)增大到1, 讓火球變大
? ? await sleep(1500) //大火球等待1.5秒后消失
? ? fireball.destroy()
}
const firework = world.querySelector('#新年煙花-1')
firework.enableInteract = true
firework.interactRadius = 3
firework.onInteract(async () => {
? ? var n = 3 //每次
? ? while (n-- > 0) {
? ? ? ? particleShoot(firework)//在"新年煙花-1"實體的位置發(fā)射煙花
? ? ? ? await sleep(2000)//每隔2秒射1發(fā)
? ? }
})