Cesium glb 模型运动、姿态变换

import { toCartesian, toDegrees } from "./tool";

/**
 * 计算实体的位置矩阵
 * @param entity
 * @param time
 * @returns {*}
 */
export const computeModelMatrix = ({ Cesium, viewer }, entity, time) => {
  let position = Cesium.Property.getValueOrUndefined(
    entity.position,
    time,
    new Cesium.Cartesian3()
  );
  if (!Cesium.defined(position)) {
    return undefined;
  }
  let orientation = Cesium.Property.getValueOrUndefined(
    entity.orientation,
    time,
    new Cesium.Quaternion()
  );
  let modelMatrix;
  if (!Cesium.defined(orientation)) {
    modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(
      position,
      undefined,
      new Cesium.Matrix4()
    );
  } else {
    modelMatrix = Cesium.Matrix4.fromRotationTranslation(
      Cesium.Matrix3.fromQuaternion(orientation, new Cesium.Matrix3()),
      position,
      new Cesium.Matrix4()
    );
  }
  return modelMatrix;
};

/**
 * 计算实体的当前旋转状态
 * @param entity
 */
export const getEntityHPR = ({ Cesium, viewer }, entity) => {
  return Cesium.Transforms.fixedFrameToHeadingPitchRoll(
    computeModelMatrix(
      { Cesium, viewer },
      entity,
      viewer.clock.currentTime || Cesium.JulianDate.now()
    )
  );
};

/**
 *
 * @param {*} param0
 * @param {*} model 实体模型
 * @param {*} angle hpr
 * @param {*} seconds 秒数
 */
export const model2HPR = ({ Cesium, viewer }, model, angle, seconds) => {
  let modelHPR = Cesium.Transforms.fixedFrameToHeadingPitchRoll(
    computeModelMatrix(
      { Cesium, viewer },
      model,
      viewer.clock.currentTime || Cesium.JulianDate.now()
    )
  );
  let startHPR = {
      heading: Cesium.Math.toDegrees(modelHPR.heading),
      pitch: Cesium.Math.toDegrees(modelHPR.pitch),
      roll: Cesium.Math.toDegrees(modelHPR.roll),
    },
    endHPR = {
      heading: Cesium.Math.toDegrees(
        modelHPR.heading + Cesium.Math.toRadians(angle.heading)
      ),
      pitch: Cesium.Math.toDegrees(
        modelHPR.pitch + Cesium.Math.toRadians(angle.pitch)
      ),
      roll: Cesium.Math.toDegrees(
        modelHPR.roll + Cesium.Math.toRadians(angle.roll)
      ),
    };
  modelRotate({ Cesium, viewer }, model, startHPR, endHPR, seconds);
};

/**
 * 模型旋转
 * @param model
 * @param startHpr
 * @param endHpr
 * @param seconds
 * @param startPoint
 * @param endPoint
 */
export const modelRotate = (
  { Cesium, viewer },
  model,
  startHpr,
  endHpr,
  seconds,
  startPoint,
  endPoint
) => {
  let startQ = Cesium.Transforms.headingPitchRollQuaternion(
    startPoint || getEntityPosition({ Cesium, viewer }, model),
    new Cesium.HeadingPitchRoll(
      Cesium.Math.toRadians(startHpr.heading),
      Cesium.Math.toRadians(startHpr.pitch),
      Cesium.Math.toRadians(startHpr.roll)
    )
  );
  let stopQ = Cesium.Transforms.headingPitchRollQuaternion(
    endPoint || getEntityPosition({ Cesium, viewer }, model),
    new Cesium.HeadingPitchRoll(
      Cesium.Math.toRadians(endHpr.heading),
      Cesium.Math.toRadians(endHpr.pitch),
      Cesium.Math.toRadians(endHpr.roll)
    )
  );
  let start = viewer.clock.currentTime || Cesium.JulianDate.now();
  let stop = Cesium.JulianDate.addSeconds(
    start,
    seconds,
    new Cesium.JulianDate()
  );
  let stop_ = Cesium.JulianDate.addSeconds(
    start,
    seconds + 99999999,
    new Cesium.JulianDate()
  );
  let sampled = new Cesium.SampledProperty(Cesium.Quaternion);
  sampled.addSample(start, startQ);
  sampled.addSample(stop, stopQ);
  sampled.addSample(stop_, stopQ);
  model.orientation = sampled;
};

/**
 * 获取当前实体的位置
 * @param entity
 * @returns {*}
 */
export const getEntityPosition = ({ Cesium, viewer }, entity) => {
  return (
    entity.position._value ||
    Cesium.Property.getValueOrUndefined(
      entity.position,
      viewer.clock.currentTime || Cesium.JulianDate.now(),
      new Cesium.Cartesian3()
    )
  );
};

/**
 * 模型移动函数 米
 * @param model
 * @param meters
 * @param seconds
 */
export const modelMove2Position = (
  { Cesium, viewer },
  model,
  position,
  seconds
) => {
  if (!model || !position || !seconds || !position.lng) {
    return;
  }
  let modelNowPosition = toDegrees(
    { Cesium, viewer },
    getEntityPosition({ Cesium, viewer }, model)
  );
  if (modelNowPosition && modelNowPosition.lng) {
    modelMove({ Cesium, viewer }, model, modelNowPosition, position, seconds);
  }
};

/**
 * 模型移动函数
 * @param model
 * @param startPosition 起点位置
 * @param endPosition 终点位置
 * @param seconds 秒数
 */
export const modelMove = (
  { Cesium, viewer },
  model,
  startPosition,
  endPosition,
  seconds
) => {
  if (model) {
    let p1 = {
      lng: parseFloat(startPosition.lng),
      lat: parseFloat(startPosition.lat),
      alt: parseFloat(startPosition.alt),
    };
    let p2 = {
      lng: parseFloat(endPosition.lng),
      lat: parseFloat(endPosition.lat),
      alt: parseFloat(endPosition.alt),
    };
    let start = viewer.clock.currentTime || Cesium.JulianDate.now();
    let stop = Cesium.JulianDate.addSeconds(
      start,
      seconds,
      new Cesium.JulianDate()
    );
    let stop_ = Cesium.JulianDate.addSeconds(
      start,
      seconds + 99999999,
      new Cesium.JulianDate()
    );
    if (
      typeof model.position !== "undefined" &&
      typeof model.position.addSample !== "undefined"
    ) {
      model.position.removeSamples(
        new Cesium.TimeInterval({
          start: start,
          stop: stop_,
        })
      );
      model.position.addSample(start, toCartesian({ Cesium, viewer }, p1));
      model.position.addSample(stop, toCartesian({ Cesium, viewer }, p2));
      model.position.addSample(stop_, toCartesian({ Cesium, viewer }, p2));
    } else {
      let property = new Cesium.SampledPositionProperty();
      property.addSample(start, toCartesian({ Cesium, viewer }, p1));
      property.addSample(stop, toCartesian({ Cesium, viewer }, p2));
      property.addSample(stop_, toCartesian({ Cesium, viewer }, p2));
      model.position = property;
    }
  }
};

你可能感兴趣的:(Cesium glb 模型运动、姿态变换)