Skip to content

概述

粒子出生位置:由 发射器类型 + emitterModelMatrix 决定。

粒子运动方向:由 速度、重力、风力等 updateCallback 决定。

javascript
viewer = new Cesium.Viewer('cesiumContainer', {
  shouldAnimate: true,
})
const planeEntity = viewer.entities.add({
  id: "plane1",
  position: Cesium.Cartesian3.fromDegrees(121.47, 31.23, 500),
  model: {
    uri: '/models/plane.glb',
    scale: 1,
  }
})

const gravityVector = new Cesium.Cartesian3()
const gravity = -5
function applyGravity(p, dt) {
  const position = p.position
  Cesium.Cartesian3.normalize(position, gravityVector)
  Cesium.Cartesian3.multiplyByScalar(
    gravityVector,
    gravity * dt,
    gravityVector
  )
  p.velocity = Cesium.Cartesian3.add(p.velocity, gravityVector, p.velocity)
}

const emitterModelMatrix = new Cesium.Matrix4()
const translation = new Cesium.Cartesian3()
const rotation = new Cesium.Quaternion()
let hpr = new Cesium.HeadingPitchRoll()
const trs = new Cesium.TranslationRotationScale()
function computeEmitterModelMatrix() {
  hpr = Cesium.HeadingPitchRoll.fromDegrees(0.0, 90.0, 0.0, hpr)
  trs.translation = Cesium.Cartesian3.fromElements(-10.0, 0.0, 0.0, translation)
  trs.rotation = Cesium.Quaternion.fromHeadingPitchRoll(hpr, rotation)
  return Cesium.Matrix4.fromTranslationRotationScale(trs, emitterModelMatrix)
}

viewer.scene.primitives.add(
  new Cesium.ParticleSystem({
    image: "/smoke.png",
    emissionRate: 5.0,
    emitter: new Cesium.ConeEmitter(Cesium.Math.toRadians(30.0)),
    imageSize: new Cesium.Cartesian2(25.0, 25.0),
    modelMatrix: planeEntity.computeModelMatrix(
      viewer.clock.startTime,
      new Cesium.Matrix4()
    ),
    emitterModelMatrix: computeEmitterModelMatrix(),
    updateCallback: applyGravity,
    speed: 5.0,
  })
)

viewer.trackedEntity = planeEntity
/