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 模型运动、姿态变换
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 之前整理过:《透析矩阵,由浅入深娓娓道来—高数-线性代数-矩阵》、《三维旋转笔记:欧拉角/四元数/旋转矩阵/轴角-...
- Cesium中常用的坐标 1.屏幕坐标(像素) 即二维笛卡尔平面坐标,我们通过鼠标点击直接获取的坐标就是屏幕坐标了...
- glTF小模型只是Primitive 或Entity的其中一种数据类型; Cesium支持包含关键帧(key-fr...