Cesium中DrawCommand使用

Cesium中DrawCommand使用

viewer.scene.primitives.add(object)方法在添加自定义的物体时,首先会加载object里的update方法,若没有则会报如下错误在这里插入图片描述
代码如下:

//定义一个Trangles类
      function Trangles(options) {
        this._viewer = options.viewer
        this._modelMatrix = options.modelMatrix
        this.arr = options.arr
      }
      //用prototype给定方法和属性
      Trangles.prototype.getCommand = function(context) {
        //顶点着色器
        let v = `
        attribute vec3 position3DHigh;
        attribute vec3 position3DLow;
        void main()
        {
          vec4 p = czm_translateRelativeToEye(position3DHigh, position3DLow);
          p = czm_modelViewProjectionRelativeToEye * p;
          gl_Position = p;
        }
      `
        //片元着色器
        let f = `void main()
        {
            gl_FragColor = vec4(0,1,0,1);
        }
        `
        //shaderProgram将两个着色器合并
        var shaderProgram = Cesium.ShaderProgram.fromCache({
          context: context,
          vertexShaderSource: v,
          fragmentShaderSource: f
        })
        //渲染状态
        var renderState = Cesium.RenderState.fromCache({
          depthTest: {
            enabled: false
          },
          depthMask: false,
          blending: Cesium.BlendingState.ALPHA_BLEND
        })
        //索引数组Buffer
        var indexBuffer = Cesium.Buffer.createIndexBuffer({
          context: context,
          typedArray: new Uint32Array([0, 1, 2]), //索引顺序
          usage: Cesium.BufferUsage.STATIC_DRAW,
          indexDatatype: Cesium.IndexDatatype.UNSIGNED_INT
        })
        //顶点数组Buffer
        var vertexBuffer = Cesium.Buffer.createVertexBuffer({
          context: context,
          typedArray: Cesium.ComponentDatatype.createTypedArray(
            Cesium.ComponentDatatype.FLOAT,
            this.arr //顶点位置数组
          ),
          usage: Cesium.BufferUsage.STATIC_DRAW
        })
        //用来表示逐个顶点的信息
        var attributes = []
        attributes.push({
          index: 0,
          vertexBuffer: vertexBuffer,
          componentDatatype: Cesium.ComponentDatatype.FLOAT,
          componentsPerAttribute: 3,
          normalize: false
        })
        //顶点数组(设置顶点属性和索引Buffer)
        var vertexArray = new Cesium.VertexArray({
          context: context,
          attributes: attributes,
          indexBuffer: indexBuffer
        })

        //新建一个DrawCommand
        this.pointCommand = new Cesium.DrawCommand({
          primitiveType: Cesium.PrimitiveType.TRIANGLES,
          shaderProgram: shaderProgram,
          renderState: renderState,
          vertexArray: vertexArray,
          pass: Cesium.Pass.OPAQUE,
          modelMatrix: this._modelMatrix
        })
      }
      Trangles.prototype.destroy = function() {
        //this.arr = [];
      }
      Trangles.prototype.update = function(frameState) {
        if (this.pointCommand) {
          var commandList = frameState.commandList
          commandList.push(this.pointCommand)
          this._viewer.scene.requestRender()
        } else {
          this.getCommand(this._viewer.scene.context)
        }
      }
      //模型矩阵
      var position = Cesium.Cartesian3.fromDegrees(119, 40, 0)
      var heading = Cesium.Math.toRadians(0)
      var pitch = Cesium.Math.toRadians(0)
      var roll = Cesium.Math.toRadians(0)
      var headingPitchRoll = new Cesium.HeadingPitchRoll(heading, pitch, roll)

      var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(position, headingPitchRoll, Cesium.Ellipsoid.WGS84, Cesium.Transforms.eastNorthUpToFixedFrame, new Cesium.Matrix4())
      //顶点数组
      var arr = [0, 0, 0, 0, 100000, 0, 100000, 100000, 0]

      var temp = new Trangles({ viewer, modelMatrix, arr })
      viewer.scene.primitives.add(temp)
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cesium 1.7版本开始,Cesium的WebGL绘制部分进行了大规模的重构,DrawCommand方法被移除了,取而代之的是新的Primitive API。 Primitive API 是Cesium在1.7版本引入的一种新的绘制模型,它提供了更高效和更灵活的绘制方式,可以更好地支持大规模的数据集和更复杂的场景。Primitive API使用WebGL底层API直接进行绘制,因此效率更高,同时也支持更多的绘制方式和参数设置。 如果你想在Cesium使用Primitive API,可以参考Cesium官方文档的相关说明和示例,如下所示: ```javascript // 创建Primitive var primitive = new Cesium.Primitive({ geometryInstances: new Cesium.GeometryInstance({ geometry: new Cesium.BoxGeometry({ minimum: new Cesium.Cartesian3(-250000.0, -250000.0, -250000.0), maximum: new Cesium.Cartesian3(250000.0, 250000.0, 250000.0) }), attributes: { color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.RED) } }), appearance: new Cesium.PerInstanceColorAppearance({ flat: true, translucent: false }), modelMatrix: Cesium.Matrix4.IDENTITY }); // 将Primitive添加到场景 viewer.scene.primitives.add(primitive); ``` 在这个示例,我们首先创建了一个BoxGeometry实例,并设置了其最小和最大坐标。然后我们使用ColorGeometryInstanceAttribute创建了一个颜色属性,指定了BoxGeometry实例的颜色。接着,我们使用这个BoxGeometry实例和颜色属性创建了一个GeometryInstance实例。最后,我们将这个GeometryInstance实例和其他参数,比如appearance和modelMatrix,传给了Primitive构造函数,创建了一个新的Primitive实例。最终,我们将这个Primitive添加到了Cesium场景。 这是Primitive API的一个简单示例,如果你想了解更多关于Primitive API的内容,建议你查看Cesium官方文档的相关内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值