cesium 3dtils 房屋打组 实现单个房屋选中效果

发布时间 2023-10-18 12:43:38作者: 1156740846

效果

 scenetree.json记录的是模型的数据,这种element的节点上记录的 name 就是建模时给建筑物打组时起的名字,后面cesium创建模型时,建筑物上的每个feature都会添加这个name参数,根据这个name参数可以找到这个模型中的所有feature

 

 

    handler.setInputAction((event) => {
        saveView(viewer)
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

  handler.setInputAction(function (movement) {
    const feature = viewer.scene.pick(movement.position);
    if (!Cesium.defined(feature)) {
      return;
    }
    window.pick = feature
    if (!feature.myId) {
      window.lcxFun()
    }

    if (window.oldFeatureId) {
      let arr = nodeMap.get(window.oldFeatureId)
      for (let fff of arr) {
        fff.color = new Cesium.Color(1, 1, 1, 1)
      }
    }

    window.oldFeatureId = feature.getProperty('id')
    let arr = nodeMap.get(feature.getProperty('id'))
    console.log(feature, feature.getProperty('id'), feature.getProperty('name'), feature.myId, arr);
    
    for (let fff of arr) {
      fff.color = Cesium.Color.fromCssColorString('#39A2FD')
    }
  }, Cesium.ScreenSpaceEventType.MIDDLE_CLICK);
if (!window.lcx) {
    window.lcx = new Map()
  }

  model.tileLoad.addEventListener(function(tile: { content: any }) {
    let content = tile.content;
    let featuresLength = content.featuresLength;
    
    if (featuresLength <= 0) {
      for (let child of content._contents) {
        console.log("child要素数量为:", child.featuresLength, "child", child);
        window.lcx.set(window.lcx.size + "-child", child)
      }
    } else {
      console.log("要素数量为:", featuresLength, "content", content);
      window.lcx.set(window.lcx.size + "-root", content)
    }
  })

  window.lcxFun = function() {
    window.nodeMap = new Map()
    for (let key of window.lcx.keys()) {
      let content = window.lcx.get(key)
      let featuresLength = content.featuresLength
      for (let i = 0; i < featuresLength; i++) {
        let feature = content.getFeature(i)
        feature.myId = key + "-" + i
        let arr = window.nodeMap.get(feature.getProperty('id')) || []
        arr.push(feature)
        window.nodeMap.set(feature.getProperty('id'), arr)
      }
    }    
  }