思路:
思路一:将模型中心点坐标先依据模型的包围盒半径,同时在X、Y、Z轴各平移半径长度,然后以这个点位参照,求得各轴的终点位置坐标,最后添加各轴。
思路二:依次在X、Y、Z轴各移动包围盒半径长度,以移动后的位置做坐标轴的终点位置,最后添加各轴。
1、思路一结果(红点为各轴均移动半径长度的位置点):
代码实现如下
this._length = (this._tiles._root._boundingVolume._boundingSphere.radius) / 0.8
let translateCartesian = new Cesium.Cartesian3(this._length, this._length, this._length) //单位为米
let originPos = this.returnTilesCentor(this._tiles)
let targetPos = this.getTransPostion_v1(originPos, translateCartesian) //思路一:求到对角的坐标
initLineArrow_v1(originPos,targetPos)
//获取变换后的对角坐标
getTransPostion_v1(originPos, translateCartesian){
const frompoint_to_world_matrix = Cesium.Transforms.eastNorthUpToFixedFrame(originPos);
const result = new Cesium.Cartesian3(0,0,0);
Cesium.Matrix4.multiplyByPoint(frompoint_to_world_matrix, translateCartesian, result)
return result;
}
//根据对角坐标分别求算出轴末尾点的坐标
initLineArrow_v1(originPOS, targetPos, length) {
const arrows = new Cesium.PolylineCollection()
const xPos = [new Cesium.Cartesian3(originPOS.x,originPOS.y,originPOS.z),
new Cesium.Cartesian3(targetPos.x,originPOS.y,originPOS.z)]
const xArrow = this.darwArrow(
arrows,
'model_edit_xArrow',
xPos,
Cesium.Color.GREEN
)
const yPos = [new Cesium.Cartesian3(originPOS.x,originPOS.y,originPOS.z),
new Cesium.Cartesian3(originPOS.x,targetPos.y,originPOS.z)]
const yArrow = this.darwArrow(
arrows,
'model_edit_yArrow',
yPos,
Cesium.Color.BLUE
)
const zPos = [new Cesium.Cartesian3(originPOS.x,originPOS.y,originPOS.z),
new Cesium.Cartesian3(originPOS.x,originPOS.y,targetPos.z)]
const zArrow = this.darwArrow(
arrows,
'model_edit_zArrow',
zPos,
Cesium.Color.RED
)
this._coordArrows = this._viewer.scene.primitives.add(arrows)
this._coordArrows._name = 'CoordAxis'
}
//添加轴线到线集合
darwArrow(arrows, name, positions, color) {
const arrow = arrows.add({
positions: positions,
width: 25,
material: Cesium.Material.fromType(Cesium.Material.PolylineArrowType, {
color: color
})
})
arrow._name = name
}
2、思路二结果(红点为各轴均移动半径长度的位置点):
this._length = (this._tiles._root._boundingVolume._boundingSphere.radius) / 0.8
let originPos = this.returnTilesCentor(this._tiles)
let targetPos = this.getTransPostion_v2(originPos, this._length) //思路二:每条轴单独移动
initLineArrow_v2(originPos,targetPos)
//获取变换后各轴的位置
getTransPostion_v2(originPos, length){
const frompoint_to_world_matrix = Cesium.Transforms.eastNorthUpToFixedFrame(originPos);
const local_translation_x = new Cesium.Cartesian3(length, 0, 0);
const local_translation_y = new Cesium.Cartesian3(0, length, 0);
const local_translation_z = new Cesium.Cartesian3(0, 0, length);
const result_x = new Cesium.Cartesian3(0,0,0);
const result_y = new Cesium.Cartesian3(0,0,0);
const result_z = new Cesium.Cartesian3(0,0,0);
Cesium.Matrix4.multiplyByPoint(frompoint_to_world_matrix, local_translation_x, result_x); // 转换矩阵左乘局部平移向量,结果存储在 result 中,结果是世界坐标下的平移终点向量
Cesium.Matrix4.multiplyByPoint(frompoint_to_world_matrix, local_translation_y, result_y);
Cesium.Matrix4.multiplyByPoint(frompoint_to_world_matrix, local_translation_z, result_z);
return {targetX:result_x,targetY:result_y,targetZ:result_z};
}
//根据对角坐标分别求算出轴末尾点的坐标
initLineArrow_v2(originPOS, targetPos, length) {
const arrows = new Cesium.PolylineCollection()
const xPos = [new Cesium.Cartesian3(originPOS.x,originPOS.y,originPOS.z),
new Cesium.Cartesian3(targetPos.targetX.x,targetPos.targetX.y,targetPos.targetX.z)]
const xArrow = this.darwArrow(
arrows,
'model_edit_xArrow',
xPos,
Cesium.Color.GREEN
)
const yPos = [new Cesium.Cartesian3(originPOS.x,originPOS.y,originPOS.z),
new Cesium.Cartesian3(targetPos.targetY.x,targetPos.targetY.y,targetPos.targetY.z)]
const yArrow = this.darwArrow(
arrows,
'model_edit_yArrow',
yPos,
Cesium.Color.BLUE
)
const zPos = [new Cesium.Cartesian3(originPOS.x,originPOS.y,originPOS.z),
new Cesium.Cartesian3(targetPos.targetZ.x,targetPos.targetZ.y,targetPos.targetZ.z)]
const zArrow = this.darwArrow(
arrows,
'model_edit_zArrow',
zPos,
Cesium.Color.RED
)
this._coordArrows = this._viewer.scene.primitives.add(arrows)
this._coordArrows._name = 'CoordAxis'
}
//添加轴线到线集合
darwArrow(arrows, name, positions, color) {
const arrow = arrows.add({
positions: positions,
width: 25,
material: Cesium.Material.fromType(Cesium.Material.PolylineArrowType, {
color: color
})
})
arrow._name = name
}