美文网首页开源
Cesium Cesium3DTileset customSha

Cesium Cesium3DTileset customSha

作者: 宿州刘德华 | 来源:发表于2024-01-21 17:14 被阅读0次

    通过传入范围,在顶点着色器函数内判断渲染的顶点是否在范围内,如果在将顶点坐标0。如果不在则正常显示。反之也可以将范围外的顶点坐标0。

    /**
     * @class
     * @description 3dtiles 模型按照(经纬度二维)范围裁剪
     */
    class Clipping {
    
      constructor(Cesium, tileset, opt) {
    
        if (!tileset || !Cesium) {
          throw new Error("参数错误!")
        }
    
        if (opt.polygons && opt.polygons instanceof Array && opt.polygons.length > 0) {
          for (let polygon of opt.polygons) {
            if (!(polygon instanceof Array && polygon.length >= 3)) {
              throw new Error("参数错误!")
            }
          }
        } else {
          throw new Error("参数错误!")
        }
    
        this.Cesium = Cesium;
        this.tileset = tileset;
        if (typeof opt.excavate === "boolean") {
          this.excavate = opt.excavate
        } else {
          this.excavate = false;
        }
        this.polygons = opt.polygons;
    
        this.center = tileset.boundingSphere.center.clone();
        this.matrix = Cesium.Transforms.eastNorthUpToFixedFrame(this.center.clone());
        this.localMatrix = Cesium.Matrix4.inverse(this.matrix, new Cesium.Matrix4());
    
        this.localPositionsArr = [];
        for (let pp = 0; pp < this.polygons.length; pp++) {
          let positions = this.polygons[pp];
          let localCoor = this.cartesiansToLocal(positions);
          this.localPositionsArr.push(localCoor);
        }
    
        this.initialize();
    
      }
    
    
      initialize() {
    
        let out_v_str = ``;
        let in_v_str = ``;
        let temp_str = ``;
        for (let m = 0; m < this.localPositionsArr.length; m++) {
    
          let polygon = this.localPositionsArr[m];
          out_v_str +=
            `vec2 points_${m}[${polygon.length}];
                  bool isPointInPolygon_${m}(vec2 point){
                    int nCross = 0; // 交点数
                    const int n = ${polygon.length};
                    for(int i = 0; i < n; i++){
                        vec2 p1 = points_${m}[i];
                        vec2 p2 = points_${m}[int(mod(float(i+1),float(n)))];
                        if(p1[1] == p2[1]){
                            continue;
                        }
                        if(point[1] < min(p1[1], p2[1])){
                            continue;
                        }
                        if(point[1] >= max(p1[1], p2[1])){
                            continue;
                        }
                        float x = p1[0] + ((point[1] - p1[1]) * (p2[0] - p1[0])) / (p2[1] - p1[1]);
                        if(x > point[0]){
                        nCross++;
                        }
                    }
                    return int(mod(float(nCross), float(2))) == 1;
                  }\n
            `
    
          for (let n = 0; n < polygon.length; n++) {
            in_v_str += `points_${m}[${n}] = vec2(${polygon[n][0]}, ${polygon[n][1]});\n`;
          }
    
    
    
          temp_str += `isPointInPolygon_${m}(position2D)||`
        }
    
        temp_str = temp_str.slice(0, -2);
    
    
        if (this.excavate == false) {
          in_v_str +=
            `
            if(${temp_str}){
            }else{
              vsOutput.positionMC *= 0.0;
            }
            `
        } else if (this.excavate == true) {
          in_v_str +=
            `
            if(${temp_str}){
              vsOutput.positionMC *= 0.0;
            }
            `
        }
    
        this.updateShader(out_v_str, in_v_str);
      }
    
    
      /**
       * 销毁
       */
      destroy() {
        this.tileset.customShader = undefined;
      }
    
    
      updateShader(vtx1, vtx2) {
    
        let v_text = `
        // 所有isPointInPolygon函数
        ${vtx1}
        void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput){
            vec3 modelMC = vsInput.attributes.positionMC;
            vec4 model_local_position = vec4(modelMC.x, modelMC.y, modelMC.z, 1.0);
            vec4 tileset_local_position = u_tileset_worldToLocalMatrix * czm_model * model_local_position;
            vec2 position2D = vec2(tileset_local_position.x,tileset_local_position.y);
            // 多个多边形区域
            ${vtx2}
        }`;
    
        console.log(v_text)
        let flatCustomShader = new Cesium.CustomShader({
          uniforms: {
            u_tileset_localToWorldMatrix: {
              type: Cesium.UniformType.MAT4,
              value: this.matrix,
            },
            u_tileset_worldToLocalMatrix: {
              type: Cesium.UniformType.MAT4,
              value: this.localMatrix,
            },
            u_flatHeight: {
              type: Cesium.UniformType.FLOAT,
              value: this.flatHeight,
            },
          },
          vertexShaderText: v_text,
        });
        this.tileset.customShader = flatCustomShader;
      }
    
    
    
    
      // 世界坐标转数组局部坐标
      cartesiansToLocal(positions) {
        let arr = [];
        for (let i = 0; i < positions.length; i++) {
          let position = positions[i];
          let localp = Cesium.Matrix4.multiplyByPoint(
            this.localMatrix,
            position.clone(),
            new Cesium.Cartesian3()
          )
          arr.push([localp.x, localp.y]);
        }
        return arr;
      }
    
    
    }
    
    
    

    例子


    1705914838292.jpg

    相关文章

      网友评论

        本文标题:Cesium Cesium3DTileset customSha

        本文链接:https://www.haomeiwen.com/subject/sbmeodtx.html