cesium扫描射线

先看图:

 本人也是小白,找了好久才找到这个效果,主要还是动态修改材质实现,废话不多说,直接上方法。
参考连接:[cesium] 卫星雷达传感器,雷达探测效果_cesium 雷达探测图-CSDN博客

 

function setProbingPrimitive(
  viewer: any,
  options: any = {
    color: new Cesium.Color(1.0, 0.0, 1.0, 0.8),
    repeat: 30.0,
    offset: 0.0,
    thickness: 0.3,
    center: Cesium.Cartesian3.fromDegrees(116.39, 39.9),
    length: 400000.0,
    bottom: 1000,
    top: 0.0
  }
) {
  function getModelMatrix() {
    return Cesium.Matrix4.multiplyByTranslation(
      //转换矩阵
      Cesium.Transforms.eastNorthUpToFixedFrame(options.center), //矩阵
      new Cesium.Cartesian3(0.0, 0.0, options.length * 0.5), //要转换的笛卡尔坐标
      new Cesium.Matrix4() //返回新的矩阵
    )
  }

  //材质
  function getMaterial() {
    var materialSource = `uniform vec4 color;
                uniform float repeat;
                uniform float offset;
                uniform float thickness;

                czm_material czm_getMaterial(czm_materialInput materialInput){
                    czm_material material = czm_getDefaultMaterial(materialInput);
                    float sp = 1.0/repeat;
                    vec2 st = materialInput.st;
                    float dis = distance(st, vec2(0.5));
                    float m = mod(dis + offset, sp);
                    float a = step(sp*(1.0-thickness), m);

                    material.diffuse = color.rgb;
                    material.alpha = a * color.a;
                    return material;
                }`

    return new Cesium.Material({
      fabric: {
        type: 'radarPrimitive',
        uniforms: {
          //动态传递参数
          color: options.color,
          repeat: options.repeat,
          offset: options.offset,
          thickness: options.thickness
        },
        source: materialSource
      },
      translucent: false
    })
  }

  let cylinderGeometry = new Cesium.CylinderGeometry({
    length: options.length,
    topRadius: options.top,
    bottomRadius: options.bottom,
    vertexFormat: Cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat
  })
  let redCone = new Cesium.GeometryInstance({
    geometry: cylinderGeometry,
    modelMatrix: getModelMatrix()
  })
  let appearance = new Cesium.MaterialAppearance({
    material: getMaterial(),
    faceForward: false,
    closed: true
  })

  const radar = viewer.scene.primitives.add(
    new Cesium.Primitive({
      geometryInstances: [redCone],
      appearance: appearance
    })
  )
  //监听渲染事件 动态修改雷达材质中的offset变量 从而实现动态效果
  viewer.scene.preUpdate.addEventListener(function () {
    var offset = radar.appearance.material.uniforms.offset
    offset -= 0.001
    if (offset > 1.0) {
      offset = 0.0
    }
    radar.appearance.material.uniforms.offset = offset
  })
}

要计算射线与模型的交点,可以使用Cesium的Ray类和IntersectionTests类,具体步骤如下: 1. 创建射线,可以使用Cesium的Ray类,代码如下: ```javascript const ray = new Cesium.Ray(origin, direction); ``` 其中,origin是射线的起点,direction是射线的方向向量。 2. 获取模型的包围盒,可以使用Cesium的BoundingSphere类,代码如下: ```javascript const modelMatrix = model.modelMatrix; const boundingSphere = model.boundingSphere.clone(); boundingSphere.transform(modelMatrix); ``` 其中,model是Cesium的Model类,boundingSphere是模型的包围球,modelMatrix是模型的变换矩阵。 3. 计算射线与包围球的交点,如果没有交点则直接返回,代码如下: ```javascript const intersection = Cesium.IntersectionTests.raySphere(ray, boundingSphere); if (!intersection) { return null; } ``` 4. 计算射线与模型的交点,可以使用Cesium的IntersectionTests类,代码如下: ```javascript const result = new Cesium.Cartesian3(); Cesium.IntersectionTests.raySphere(ray, boundingSphere, result); const pickResult = viewer.scene.pickFromRay(ray, [model]); ``` 其中,result是交点的输出参数,pickResult是拾取结果,包含了交点的位置和模型的信息。 5. 将交点的坐标转换到世界坐标系,代码如下: ```javascript const worldPosition = Cesium.Matrix4.multiplyByPoint(modelMatrix, result, new Cesium.Cartesian3()); ``` 这样就可以计算出射线与模型的交点了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值