function createModel() {
var params = {
tx: 94.94663004042711, //模型中心X轴坐标(经度,单位:十进制度)
ty: 29.74529725388329,//模型中心Y轴坐标(纬度,单位:十进制度)
tz: 3400.0, //模型中心Z轴坐标(高程,单位:米)
rx: 0, //X轴(经度)方向旋转角度(单位:度)
ry: 0, //Y轴(纬度)方向旋转角度(单位:度)
rz: -90 //Z轴(高程)方向旋转角度(单位:度)
};
var point = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(params.tx, params.ty, 30),
point: {
pixelSize: 8,
color: Cesium.Color.TRANSPARENT,
outlineColor: Cesium.Color.RED,
outlineWidth: 3,
},
});
var model = viewer.scene.primitives.add(
Cesium.Model.fromGltf({
url: "./static/ceshi/hp3/滑坡原始位置/ys.gltf", //如果为bgltf则为.bgltf static\ceshi\滑坡原始位置(1)\滑坡原始位置\ys.gltf
show: true, // default
scale: 3.0,
maximumScale: 1
})
);
model.type = "model";
model.tx = params.tx;
model.ty = params.ty;
model.tz = params.tz;
model.rx = 0;
model.ry = 0;
model.rz = 0;
update3dtilesMaxtrix(model, params)
model.readyPromise.then(function (model) {
viewer.camera.setView({
destination: Cesium.Cartesian3.fromDegrees(params.tx,
params.ty,
60000.0),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-90),
roll: Cesium.Math.toRadians(0)
}
});
}).otherwise(function (error) {
window.alert(error);
});
let leftDownFlag = false;
var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
// 旋转的关键代码
handler.setInputAction(function (movement) {
pickedObject = viewer.scene.pick(movement.position);
if (Cesium.defined(pickedObject)) {
if (pickedObject.primitive.isCesium3DTileset == undefined) {
leftDownFlag = true;
document.body.style.cursor = 'pointer';
viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
pickedObject.primitive.color = new Cesium.Color(0, 1, 0, 1); //选中模型后高亮
pickedObject.primitive.silhouetteSize = 3.0;//选中模型后高亮
}
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
handler.setInputAction(function () {
if(Cesium.defined(pickedObject)){
pickedObject.primitive.color = new Cesium.Color(1, 1, 1, 1);
pickedObject.primitive.silhouetteSize = 0;
leftDownFlag = false;
pickedObject = null;
viewer.scene.screenSpaceCameraController.enableRotate = true;//解除锁定相机
document.body.style.cursor = 'default';
}
}, Cesium.ScreenSpaceEventType.LEFT_UP);
handler.setInputAction((movement) => {
if (leftDownFlag && pickedObject.primitive.modelMatrix) {
// ************g关键代码:cartesian的值************
// let ray = viewer.camera.getPickRay(movement.endPosition);
// let cartesian = viewer.scene.globe.pick(ray, viewer.scene);
// 屏幕转世界坐标
let cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(movement.endPosition),viewer.scene);
// 世界坐标转经纬度
let ellipsoid = viewer.scene.globe.ellipsoid;
let cartesian3=new Cesium.Cartesian3(cartesian.x,cartesian.y,cartesian.z);
let cartographic=ellipsoid.cartesianToCartographic(cartesian3);
let lat=Cesium.Math.toDegrees(cartographic.latitude);
let lng=Cesium.Math.toDegrees(cartographic.longitude);
params.tx = lng
params.ty = lat
console.log(lat,lng)
update3dtilesMaxtrix(pickedObject,params)
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
}
function update3dtilesMaxtrix(model, params) {
//旋转
let mx = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(params.rx));
let my = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(params.ry));
let mz = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(params.rz));
let rotationX = Cesium.Matrix4.fromRotationTranslation(mx);
let rotationY = Cesium.Matrix4.fromRotationTranslation(my);
let rotationZ = Cesium.Matrix4.fromRotationTranslation(mz);
//平移
let position = Cesium.Cartesian3.fromDegrees(params.tx, params.ty, params.tz);
let m = Cesium.Transforms.eastNorthUpToFixedFrame(position);
//旋转、平移矩阵相乘
Cesium.Matrix4.multiply(m, rotationX, m);
Cesium.Matrix4.multiply(m, rotationY, m);
Cesium.Matrix4.multiply(m, rotationZ, m);
//赋值给model
if (Cesium.defined(model.primitive)) {
model.primitive.modelMatrix = m;
}
model.modelMatrix = m;
model.color = Cesium.Color.WHITE.withAlpha(1);
}