cesium着色器学习系列3-着色器方式绘制图元

通过前两篇文章的研究,那么就可以基于着色器写自己的图元实现逻辑了


    let viewer = new Cesium.Viewer('cesiumContainer', {
        imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
            url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer',
            enablePickFeatures: false
        }),


        //terrainProvider: Cesium.createWorldTerrain(),


    })
    let v=`
         attribute vec3 position3DHigh;
         attribute vec3 position3DLow;
         // attribute vec3 position2DHigh;
         // attribute vec3 position2DLow;
         attribute vec4 color;
         varying vec4 v_color;
         attribute float batchId;
         void main()
         {
           //vec4 p = czm_translateRelativeToEye(position3DHigh, position3DLow);
            vec4 p = czm_computePosition();  //The position relative to eye
            v_color =color;
            p = czm_modelViewProjectionRelativeToEye * p;
            gl_Position=p;
           // gl_Position =  czm_modelViewProjectionRelativeToEye * p;
            gl_PointSize=20.0;
         }`
    let f=`  varying vec4 v_color;
             void main()
             {
                   float d = distance(gl_PointCoord, vec2(0.5,0.5));
                  if(d < 0.5){
                     gl_FragColor = v_color;
                  }else{
                     discard;
                  }
             }`
    var appearance = new Cesium.Appearance({
        renderState: {
            blending: Cesium.BlendingState.PRE_MULTIPLIED_ALPHA_BLEND,  //混合
            depthTest: { enabled: true }, //深度测试
            depthMask: true
        },
        fragmentShaderSource: f,
        vertexShaderSource: v
    });

    viewer.camera.setView( {
        destination  : Cesium.Cartesian3.fromDegrees( 110.20, 34.55, 3000000 )
    } );
    var p1 = Cesium.Cartesian3.fromDegrees(120.6822, 50.9247, 10);
    var p2 = Cesium.Cartesian3.fromDegrees(120.6822, 38.9247, 10);
    var p3 = Cesium.Cartesian3.fromDegrees(133.6822, 38.9247, 10);
    var p4 = Cesium.Cartesian3.fromDegrees(133.6822, 50.9247, 10);
    var positions = new Float64Array([
        p1.x, p1.y, p1.z,
        p2.x, p2.y, p2.z,
        p3.x, p3.y, p3.z,
        p4.x, p4.y, p4.z
    ]);
    var colors = new Float32Array([
        1.0, 0.0, 0.0, 1.0,
        0.0, 1.0, 0.0, 1.0,
        1.0, 1.0, 0.0, 1.0,
        1.0, 1.0, 1.0, 1.0
    ]);
    var geometry = new Cesium.Geometry({
        attributes: {
            position: new Cesium.GeometryAttribute({
                componentDatatype: Cesium.ComponentDatatype.DOUBLE,
                componentsPerAttribute: 3,
                values: positions
            }),
            color: new Cesium.GeometryAttribute({
                componentDatatype: Cesium.ComponentDatatype.FLOAT,
                componentsPerAttribute: 4,
                values: colors
            })
        },
        //索引
        indices: new Uint16Array([
            0, 1, 2,  //第一个三角形用的坐标点
            1, 2, 3  //第二个三角形用的坐标点
        ]),
        //绘制类型
        primitiveType: Cesium.PrimitiveType.TRIANGLE_FAN ,
        boundingSphere: Cesium.BoundingSphere.fromVertices(positions)
    });


    viewer.scene.primitives.add(new Cesium.Primitive({
        geometryInstances: new Cesium.GeometryInstance({
            geometry: geometry
        }),
        appearance: appearance,
        asynchronous: false
    }));

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值