Cesium 视锥体绘制,并动态更新朝向

方法封装:

复制代码

/**
 * 绘制视锥
 * 苹果园dog
 */
export default class {
    constructor(viewer, options) {
        this.viewer = viewer;
        this._position = options.position;
        this._orientation = options.orientation;
        this._fov = options.fov || 35.0;
        this._near = options.near||0.1;
        this._far = options.far||20;
        this._fill = options.fill || false;
        this._closed = options.closed || false;
        this._color = options.color || new Cesium.Color(1.0, 0.0, 0.0, 0.2);
        this._outlineColor=options.outlineColor||new Cesium.Color(0.0, 0.0, 0.0, 0.5);
        this._flat = options.flat || true;
        this.update(this._position,this._orientation);
    }

    update(position, orientation) {
        this._position = position;
        this._orientation = orientation;
        this._add();
    }

    _add() {
        this._clear();
        this._addFrustum();
        this._addOutline();
    }

    _clear() {
        this._clearFrustum();
        this._clearOutline();
    }

    _addFrustum() {
        if (!Cesium.defined(this._position)) {
            return;
        }
        if (!Cesium.defined(this.viewer)) {
            return;
        }
        var scene = this.viewer.scene;
        var frustum = new Cesium.PerspectiveFrustum({
            fov: Cesium.Math.toRadians(this._fov),
            aspectRatio: scene.canvas.clientWidth / scene.canvas.clientHeight,
            near: this._near,
            far: this._far,
        });
        this._frustum=frustum;

        var frustumGeometry = new Cesium.FrustumGeometry({
            frustum: frustum,
            origin: this._position,
            orientation: this._orientation,
            vertexFormat: Cesium.VertexFormat.POSITION_ONLY,
        });

        var frustumGeometryInstance = new Cesium.GeometryInstance({
            geometry: frustumGeometry,
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                    this._color
                ),
            },
            id: "frustum",
        });

        this._frustumPrimitive = scene.primitives.add(
            new Cesium.Primitive({
                geometryInstances: frustumGeometryInstance,
                appearance: new Cesium.PerInstanceColorAppearance({
                    closed: this._closed,
                    flat: this._flat,
                }),
                asynchronous:false
            })
        );
    }

复制代码

调用:(代码有的在其他js模块,比如matrixUtil.js,就不贴了)

复制代码

      //视锥绘制开始
      var mCameraHeightThanBottom = modelm.boundingSphere.radius / 1.7;
      let mCameraLoc = matrixUtil.localToWorldCartesian3(
        worldCoorOrigin,
        x - localconfig.x,
        y - localconfig.y,
        z + mCameraHeightThanBottom - localconfig.z
      );
      let m2 = Cesium.Transforms.eastNorthUpToFixedFrame(mLoc);
      var rotation222 = Cesium.Matrix4.getMatrix3(m2, new Cesium.Matrix3());
      Cesium.Matrix3.multiply(
        rotation222,
        Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(angle + 90)),
        rotation222
      );
      Cesium.Matrix3.multiply(
        rotation222,
        Cesium.Matrix3.fromRotationX(Cesium.Math.PI_OVER_TWO * 0.91),
        rotation222
      );
      var orientation222 = Cesium.Quaternion.fromRotationMatrix(rotation222);
      this.drawViewFrustum(mCameraLoc, orientation222);//画
      //视锥绘制结束
      this.angleHistory = angle; //保存当前角度
      modelm.modelMatrix = m;//新模型矩阵,更新模型位置和角度

复制代码

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
Cesium绘制锥体的方法比较复杂,需要使用Cesium的Primitive类型来实现。下面是一个简单的示例,展示如何使用Cesium绘制锥体并连接中心点: ```javascript var viewer = new Cesium.Viewer('cesiumContainer'); // 定义锥体的顶点 var positions = [ Cesium.Cartesian3.fromDegrees(0, 0, 0), Cesium.Cartesian3.fromDegrees(0, 10, 0), Cesium.Cartesian3.fromDegrees(10, 10, 0), Cesium.Cartesian3.fromDegrees(10, 0, 0), Cesium.Cartesian3.fromDegrees(5, 5, 10) ]; // 定义锥体的颜色 var color = new Cesium.Color(1.0, 1.0, 0.0, 0.5); // 创建锥体的Primitive var cone = viewer.scene.primitives.add(Cesium.Primitive.createCone({ vertexFormat: Cesium.VertexFormat.POSITION_AND_COLOR, length: 10.0, bottomRadius: 5.0, topRadius: 0.0, slices: 64, numberOfVerticalLines: 0, capMaterial: Cesium.Material.fromType('Color', { color: color }), bodyMaterial: Cesium.Material.fromType('Color', { color: color }), offsetAttribute: 0, positions: positions })); // 连接中心点 var center = Cesium.BoundingSphere.fromPoints(positions).center; var centerLine = viewer.entities.add({ polyline: { positions: [center, positions[4]], width: 5, material: color } }); viewer.zoomTo(cone); ``` 上面的代码中,我们首先定义锥体的顶点和颜色,然后使用`Cesium.Primitive.createCone`方法创建了一个Cone Primitive。这个方法接受许多参数,用于指定锥体的各种属性,包括高度、半径、切片数量、材质等等。我们在这里指定了顶点、颜色和一些默认值。 接下来,我们通过`Cesium.BoundingSphere.fromPoints`方法来计算锥体的中心点,然后使用`viewer.entities.add`方法创建了一个实体,用于连接中心点和锥体的顶点。最后,我们使用`viewer.zoomTo`方法将图缩放到锥体的范围内。 这只是一个简单的示例,实际上还有很多细节需要处理,比如如何处理锥体底面的填充、如何处理锥体的投影等等。但是这个示例可以让你了解如何使用Cesium绘制锥体,并连接中心点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苹果园dog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值