3dtiles平移空间坐标系添加

文章讲述了两种在Cesium中为3D模型创建坐标轴的方法。思路一是基于模型的包围盒半径,计算对角线上的终点坐标;思路二是分别在X、Y、Z轴上移动半径来确定轴的终点。这两种方法都被用于生成并显示模型的坐标轴辅助线。
摘要由CSDN通过智能技术生成
  • 思路:

思路一:将模型中心点坐标先依据模型的包围盒半径,同时在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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值