分享:Cesium添加水面效果

Cesium中内置了水面效果的shader,通过添加图元(Primitive),能够在Cesium的地图上实现水面流动的效果,借助互联网上搜索、并进行了修改。
下载了一块水系的geojson数据,因为按照代码,需要根据polygon的坐标进行勾画。
关键实现代码:

//_polygonArr 为polygon的坐标
         let waterPrimitive = new Cesium.Primitive({

                allowPicking: false,

                geometryInstances: new Cesium.GeometryInstance({

                    geometry: new Cesium.PolygonGeometry({

                        polygonHierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3

                            .fromDegreesArrayHeights(_polygonArr)),


                        vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT,


                    })

                }),

                // 使用内置的水面shader

                appearance: new Cesium.EllipsoidSurfaceAppearance({

                    aboveGround: true,

                    material: new Cesium.Material({

                        fabric: {

                            type: 'Water',

                            uniforms: { 


                                blendColor: new Cesium.Color(0.0, 0.0, 1.0, 0.3), 

                                //设置水面使用的图片,

                                //此图片在Cesium源码Source\Assets\Textures文件夹中


                                normalMap: 'images/waterNormalsSmall.jpg',

                                //频率速度设置

                                frequency: 200.0,

                                animationSpeed: 0.01,

                                amplitude: 10.0

                            }

                        }

                    })

             //frameShader代码也可以根据需要进行修改

                    //                     fragmentShaderSource: 'varying vec3 v_positionMC;\nvarying vec3 v_positionEC;\nvarying vec2 v_st;\nvoid main()\n{\nczm_materialInput materialInput;\nvec3 normalEC = normalize(czm_normal3D * czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0)));\n#ifdef FACE_FORWARD\nnormalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC);\n#endif\nmaterialInput.s = v_st.s;\nmaterialInput.st = v_st;\nmaterialInput.str = vec3(v_st, 0.0);\nmaterialInput.normalEC = normalEC;\nmaterialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(v_positionMC, materialInput.normalEC);\nvec3 positionToEyeEC = -v_positionEC;\nmaterialInput.positionToEyeEC = positionToEyeEC;\nczm_material material = czm_getMaterial(materialInput);\n#ifdef FLAT\ngl_FragColor = vec4(material.diffuse + material.emission, material.alpha);\n#else\ngl_FragColor = czm_phong(normalize(positionToEyeEC), material);\

                    // gl_FragColor.a=0.5;\n#endif\n}\n' //重写shader,修改水面的透明度

                })

            });
//添加水面数据到viewer中
  viewer.scene.primitives.add(waterPrimitive);

效果图:
在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是实现 cesium 添加水面特效倒影的代码: ```javascript // 设置地球表面水面效果 var waterShader = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\ {\n\ czm_material material = czm_getDefaultMaterial(materialInput);\n\ vec2 st = materialInput.st;\n\ st += viewerPosition.xz * 0.0001;\n\ material.alpha = 0.6;\n\ material.diffuse = 0.7 * vec3(0.3, 0.5, 0.7) + 0.3 * vec3(0.1, 0.1, 0.2);\n\ material.emission = 0.3 * vec3(0.2, 0.3, 0.4);\n\ material.normal = computeNormal(vec3(st, 0.0));\n\ material.normal = czm_normal3D * material.normal;\n\ return material;\n\ }"; Cesium.Material.addMaterialShaderSource(waterShader); // 添加水面效果 var water = viewer.scene.primitives.add(new Cesium.Primitive({ geometryInstances : new Cesium.GeometryInstance({ geometry : new Cesium.PlaneGeometry({ vertexFormat : Cesium.VertexFormat.POSITION_ONLY, minimum : new Cesium.Cartesian3(-1000000.0, -1000000.0, 0.0), maximum : new Cesium.Cartesian3(1000000.0, 1000000.0, 0.0) }), attributes : { color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.WHITE.withAlpha(0.0)) }, id : 'water' }), appearance : new Cesium.MaterialAppearance({ material : new Cesium.Material({ fabric : { type : 'Water', uniforms : { specularMap : 'Assets/Textures/waterSpecularMap.jpg', normalMap : 'Assets/Textures/waterNormals.jpg', frequency : 1000.0, animationSpeed : 0.01, amplitude : 10.0 } } }) }) })); // 添加水面倒影 var reflection = viewer.entities.add({ name : 'Reflection', position : new Cesium.Cartesian3.fromDegrees(-75.62898254394531, 40.02804946899414, 0.0), orientation : Cesium.Transforms.headingPitchRollQuaternion(new Cesium.Cartesian3.fromDegrees(-75.62898254394531, 40.02804946899414, 0.0), new Cesium.HeadingPitchRoll(0.0, -Math.PI / 2.0, 0.0)), model : { uri : 'Assets/Models/CesiumBalloon/CesiumBalloon.glb', minimumPixelSize : 128, maximumScale : 20000, scale : 500.0 }, reflection : true }); ``` 这段代码会在地球表面添加一个水面效果,并在指定位置添加一个模型的倒影。其中,添加水面效果使用了 Cesium 的材质系统,通过设置材质的属性来实现水面效果添加倒影使用了 Cesium 的实体系统,通过设置实体的属性来实现倒影效果
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值