//offsetopt.x,y不能多次更改
updateMatrix: function (offsetopt) {
/*此函数用于计算,同一个模型的偏移前后位置的偏移量。
* 并转换成Matrix4,保存到模型的modelMatrix属性下。
* 其中,offsetopt类似这样:"offset": {
"x": 117.75327858104695,
"y": 39.12059826155268,
"z": -11
},
*/
if (this.model == null) return;
console.log(" 模型修改后位置:" + JSON.stringify(offsetopt));
/*模型修改后位置:{"x":117.75327858104696,"y":39.12059826155268,"z":-11,"heading":0}*/
var boundingSphere = this.model.boundingSphere;
var catographic = Cesium.Cartographic.fromCartesian(boundingSphere.center);
var surface = Cesium.Cartesian3.fromRadians(catographic.longitude, catographic.latitude, 0.0);
var offset = Cesium.Cartesian3.fromDegrees(offsetopt.x, offsetopt.y, offsetopt.z);
var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
this.model.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
//this.model.update();
}
函数解读:
1.
Cesium.Cartesian3.subtract(left, right, result) → Cartesian3
计算两个 笛卡尔坐标的 差。可用于计算两个模型偏移前后位置的偏移量。
参数 类型 描述
left Cartesian3 The first Cartesian.
right Cartesian3 The second Cartesian.
result Cartesian3 The object onto which to store the result.
2.
Cesium.Matrix4.fromTranslation(translation, result) → Matrix4
从表示转换的cartesian 3创建一个矩阵4实例。
参数 类型 描述
translation Cartesian3 表示转换的矩阵的右上部分。
result Matrix4 optional The object in which the result will be stored,
if undefined a new instance will be created.
3.
Cesium.Cartesian3.fromDegrees(longitude, latitude, height, ellipsoid, result) → Cartesian3
返回一个Cartesian3的位置,从给定的经纬度(单位:度)。
参数 类型 默认值 描述
longitude Number The longitude, in degrees
latitude Number The latitude, in degrees
height Number 0.0 optional The height, in meters, above the ellipsoid.
ellipsoid Ellipsoid Ellipsoid.WGS84 optional The ellipsoid on which the position lies.
result Cartesian3 optional The object onto which to store the result.
Returns:
The position
Example:
var position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
4.
Cesium.Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result) → Cartesian3
返回一个Cartesian3的位置,从给定的经纬度(单位:弧度)。
参数 类型 默认值 描述
longitude Number The longitude, in radians
latitude Number The latitude, in radians
height Number 0.0 optional The height, in meters, above the ellipsoid.
ellipsoid Ellipsoid Ellipsoid.WGS84 optional The ellipsoid on which the position lies.
result Cartesian3 optional The object onto which to store the result.
Returns:
The position
Example:
var position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
5.
Cesium.Cartographic.fromCartesian(cartesian, ellipsoid, result) → Cartographic
从笛卡尔位置创建一个新的制图实例。所得到的对象中的值将以弧度表示。
Name Type Default Description
cartesian Cartesian3 笛卡尔位置, 要转换到cartographic形式.
ellipsoid Ellipsoid Ellipsoid.WGS84 optional The ellipsoid on which the position lies.
result Cartographic optional The object onto which to store the result.
Returns:
The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
节点定位函数及其解读:
locateNode: function(nodeId, nodesphere){
/*此函数用于,单击目录树的最后一级节点时,
* 在模型中飞到该特性,并高亮显示该特性。
* nodesphere, 类似这样: (4) [-2177708.5078214, 4388622.93689201, 4070262.1795747, 0.577990424188384]*/
// console.log("nodesphere:", nodesphere);
if (nodesphere[3] <= 0)
return;
// 获取实体中心位置。
var center = new Cesium.Cartesian3(nodesphere[0], nodesphere[1], nodesphere[2]);
// this.tileset.modelMatrix,是原始模型位置 和 偏移后的模型位置之间的差值的矩阵。
// 此处判断如果存在偏移,即配置文件的 "offset": {
// "x": 117.169182,
// "y": 39.073653,
// "z": 43
// }, 则更改模型的节点的位置,即进行同样的偏移。
// 模型的偏移不知在哪,估计源码已经实现。
// console.log("this.tileset.modelMatrix:", this.tileset.modelMatrix);
// this.tileset.modelMatrix: c {0: 1, 1: 0, 2: 0, 3: 0, 4: 0, 5: 1, 6: 0, 7: 0, 8: 0, 9: 0, 10: 1, 11: 0, 12: 0, 13: 0, 14: 0, 15: 1}
if(this.tileset.modelMatrix){
center = new Cesium.Cartesian3(nodesphere[0] + this.tileset.modelMatrix[12], nodesphere[1] + this.tileset.modelMatrix[13], nodesphere[2] + this.tileset.modelMatrix[14]);
}
// console.log("center:", center);
// center类似这样: a {x: -2177708.5078214, y: 4388622.93689201, z: 4070262.1795747}
// new Cesium.BoundingSphere(center, radius)
// 获取实体的包容球。
var sphere = new Cesium.BoundingSphere(center, nodesphere[3]);
// console.log("sphere:", sphere);
// sphere类似这样:sphere:
// p {center: a, radius: 0.577990424188384}
// center: a {x: -2177708.5078214, y: 4388622.93689201, z: 4070262.1795747}
// radius: 0.577990424188384
//飞行过去
this.viewer.camera.flyToBoundingSphere(sphere, {
duration: 0.5
});
// 设置tileset的样式
if (this.tileset) {
this.tileset.style = new Cesium.Cesium3DTileStyle({
color: {
conditions: [
// 当前节点的实体颜色。
// ["${name} ==='" + nodename + "'", "rgb(255, 25, 25)"],
["${id} ==='" + nodeId + "'", "rgb(255, 255, 255)"],
// 其他实体的颜色。
["true", "rgba(255, 200, 200, 0.1)"]
]
}
});
}
},
showCompleteModel: function() {
/*此函数用于,重置模型到原始样式。*/
if (this.tileset) {
this.tileset.style = undefined;
// 查看新添加的模型。
this.layerWork.centerAt(1);
}
},
cesium 模型偏移-定位节点2
于 2019-04-08 18:14:34 首次发布