1. 首先需要拥有shp格式的数据。可以通过水经微图下载,注意此软件是付费的
    2. 将shp格式的数据处理为切片数据,可以使用cesiumlab
    3. 处理完成得到json数据就可以在mars3d中加载了

      1. function init() {
      2. // 判断webgl支持
      3. if (!mars3d.Util.webglreport()) {
      4. mars3d.Util.webglerror();
      5. }
      6. let configUrl = "./config/config.json";
      7. // 读取 config.json 配置文件
      8. mars3d.Resource.fetchJson({ url: configUrl })
      9. .then(function (json) {
      10. console.log("读取 config.json 配置文件完成", json); // 打印测试信息
      11. //合并属性参数,可覆盖config.json中的对应配置
      12. let mapOptions = mars3d.Util.merge(json.map3d, {
      13. scene: {
      14. center: {
      15. lat: 33.81253,
      16. lng: 115.768825,
      17. alt: 1816,
      18. heading: 15,
      19. pitch: -34,
      20. },
      21. },
      22. });
      23. //创建三维地球场景
      24. const map = new mars3d.Map("mars3dContainer", mapOptions);
      25. map.on(mars3d.EventType.cameraMoveEnd, function (event) {
      26. map.scene.light.direction = map.scene.camera.direction;
      27. });
      28. const bloomEffect = new mars3d.effect.BloomEffect({
      29. enabled: false,
      30. });
      31. map.addEffect(bloomEffect);
      32. // 自定义特效 Shader
      33. const fragmentShader = `
      34. // 注意shader中写浮点数是,一定要带小数点,否则会报错,比如0需要写成0.0,1要写成1.0
      35. float _baseHeight = 0.0; // 物体的基础高度,需要修改成一个合适的建筑基础高度
      36. float _heightRange = 80.0; // 高亮的范围(_baseHeight ~ _baseHeight + _heightRange)
      37. float _glowRange = 100.0; // 光环的移动范围(高度)
      38. // 建筑基础色
      39. float mars_height = marsJzwHeight - _baseHeight;
      40. float mars_a11 = fract(czm_frameNumber / 120.0) * 3.14159265 * 2.0;
      41. float mars_a12 = mars_height / _heightRange + sin(mars_a11) * 0.1;
      42. gl_FragColor *= vec4(vec3(mars_a12), 1.0);// 渐变
      43. // 动态光环
      44. float time = fract(czm_frameNumber / 360.0);
      45. time = abs(time - 0.5) * 2.0;
      46. float mars_h = clamp(mars_height / _glowRange, 0.0, 1.0);
      47. float mars_diff = step(0.005, abs(mars_h - time));
      48. gl_FragColor.rgb += gl_FragColor.rgb * (1.0 - mars_diff);
      49. `;
      50. const tiles3dLayer = new mars3d.layer.TilesetLayer({
      51. name: "合肥市建筑物",
      52. url: "qcq/tileset.json",
      53. maximumScreenSpaceError: 1,
      54. maximumMemoryUsage: 1024,
      55. // marsJzwStyle: true, //打开建筑物特效(内置Shader代码)
      56. marsJzwStyle: fragmentShader, // 字符串值时,表示使用该字符串定义的自定义Shader
      57. popup: [
      58. { field: "objectid", name: "编号" },
      59. { field: "name", name: "名称" },
      60. { field: "height", name: "楼高", unit: "米" },
      61. ],
      62. });
      63. map.addLayer(tiles3dLayer);
      64. const brightnessEffect = new mars3d.effect.BrightnessEffect({
      65. enabled: true,
      66. brightness: brightness,
      67. });
      68. map.addEffect(brightnessEffect);
      69. })
      70. .catch(function (error) {
      71. console.log("加载JSON出错", error);
      72. });
      73. }