当前位置:   article > 正文

Cesium中开启等高线渲染_getaspectcontourmaterial

getaspectcontourmaterial

最近接到一个需求,需要在Cesium中基于实时地形开启等高线效果,让用户可以看到真实效果。在网上看了一些资料,许多都是需要付费的或者不提供源码,于是在cesium官网找到了示例代码,以下将具体介绍如何在cesium中进行集成。

第一步:设置viewer开启光照

viewer.scene.globe.enableLighting = true;

第二步:定义等高线初始值

  1. var minHeight = -414.0; // approximate dead sea elevation
  2. var maxHeight = 8777.0; // approximate everest elevation
  3. var contourColor = Cesium.Color.RED.clone();
  4. var contourUniforms = {};
  5. var shadingUniforms = {};

第三步:定义相关处理函数

  1. function getElevationContourMaterial() {
  2. // Creates a composite material with both elevation shading and contour lines
  3. return new Cesium.Material({
  4. fabric: {
  5. type: "ElevationColorContour",
  6. materials: {
  7. contourMaterial: {
  8. type: "ElevationContour",
  9. },
  10. elevationRampMaterial: {
  11. type: "ElevationRamp",
  12. },
  13. },
  14. components: {
  15. diffuse:
  16. "contourMaterial.alpha == 0.0 ? elevationRampMaterial.diffuse : contourMaterial.diffuse",
  17. alpha:
  18. "max(contourMaterial.alpha, elevationRampMaterial.alpha)",
  19. },
  20. },
  21. translucent: false,
  22. });
  23. }
  24. var elevationRamp = [0.0, 0.045, 0.1, 0.15, 0.37, 0.54, 1.0];
  25. var slopeRamp = [0.0, 0.29, 0.5, Math.sqrt(2) / 2, 0.87, 0.91, 1.0];
  26. var aspectRamp = [0.0, 0.2, 0.4, 0.6, 0.8, 0.9, 1.0];
  27. function getColorRamp(selectedShading) {
  28. var ramp = document.createElement("canvas");
  29. ramp.width = 100;
  30. ramp.height = 1;
  31. var ctx = ramp.getContext("2d");
  32. var values;
  33. if (selectedShading === "elevation") {
  34. values = elevationRamp;
  35. } else if (selectedShading === "slope") {
  36. values = slopeRamp;
  37. } else if (selectedShading === "aspect") {
  38. values = aspectRamp;
  39. }
  40. var grd = ctx.createLinearGradient(0, 0, 100, 0);
  41. grd.addColorStop(values[0], "#000000"); //black
  42. grd.addColorStop(values[1], "#2747E0"); //blue
  43. grd.addColorStop(values[2], "#D33B7D"); //pink
  44. grd.addColorStop(values[3], "#D33038"); //red
  45. grd.addColorStop(values[4], "#FF9742"); //orange
  46. grd.addColorStop(values[5], "#ffd700"); //yellow
  47. grd.addColorStop(values[6], "#ffffff"); //white
  48. ctx.fillStyle = grd;
  49. ctx.fillRect(0, 0, 100, 1);
  50. return ramp;
  51. }
  52. // The viewModel tracks the state of our mini application.
  53. var viewModel = {
  54. enableContour: false,
  55. contourSpacing: 50.0,
  56. contourWidth: 2.0,
  57. selectedShading: "none",
  58. changeColor: function () {
  59. contourUniforms.color = Cesium.Color.fromRandom(
  60. { alpha: 1.0 },
  61. contourColor
  62. );
  63. },
  64. };
  65. // Convert the viewModel members into knockout observables.
  66. Cesium.knockout.track(viewModel);
  67. // Bind the viewModel to the DOM elements of the UI that call for it.
  68. var toolbar = document.getElementById("toolbar");
  69. Cesium.knockout.applyBindings(viewModel, toolbar);
  70. function updateMaterial() {
  71. var hasContour = viewModel.enableContour;
  72. var selectedShading = viewModel.selectedShading;
  73. var globe = viewer.scene.globe;
  74. var material;
  75. if (hasContour) {
  76. if (selectedShading === "elevation") {
  77. material = getElevationContourMaterial();
  78. shadingUniforms = material.materials.elevationRampMaterial.uniforms;
  79. shadingUniforms.minimumHeight = minHeight;
  80. shadingUniforms.maximumHeight = maxHeight;
  81. contourUniforms = material.materials.contourMaterial.uniforms;
  82. }else {
  83. material = Cesium.Material.fromType("ElevationContour");
  84. contourUniforms = material.uniforms;
  85. }
  86. contourUniforms.width = viewModel.contourWidth;
  87. contourUniforms.spacing = viewModel.contourSpacing;
  88. contourUniforms.color = contourColor;
  89. } else if (selectedShading === "elevation") {
  90. material = Cesium.Material.fromType("ElevationRamp");
  91. shadingUniforms = material.uniforms;
  92. shadingUniforms.minimumHeight = minHeight;
  93. shadingUniforms.maximumHeight = maxHeight;
  94. }
  95. if (selectedShading !== "none") {
  96. shadingUniforms.image = getColorRamp(selectedShading);
  97. }
  98. globe.material = material;
  99. }
  100. updateMaterial();
  101. Cesium.knockout.getObservable(viewModel, "enableContour").subscribe(function (newValue) {
  102. updateMaterial();
  103. });
  104. Cesium.knockout.getObservable(viewModel, "contourWidth").subscribe(function (newValue) {
  105. contourUniforms.width = parseFloat(newValue);
  106. });
  107. Cesium.knockout.getObservable(viewModel, "contourSpacing").subscribe(function (newValue) {
  108. contourUniforms.spacing = parseFloat(newValue);
  109. });
  110. Cesium.knockout.getObservable(viewModel, "selectedShading").subscribe(function (value) {
  111. updateMaterial();
  112. });

第四步:绑定页面控件

  1. <div class="demo-container">
  2. <label><input type="radio" name="shadingMaterials"
  3. value="none" data-bind="checked: selectedShading">无渲染</label> <label><input
  4. type="radio" name="shadingMaterials" value="elevation"
  5. data-bind="checked: selectedShading">高程渲染</label>
  6. </div>
  7. <div class="demo-container">
  8. <div>
  9. <label><input type="checkbox"
  10. data-bind="checked: enableContour">等高线</label>
  11. </div>
  12. <div>
  13. 高程 <input style="width: 136px; float: left; width: 100px;"
  14. type="range" min="1.0" max="500.0" step="1.0"
  15. data-bind="value: contourSpacing, valueUpdate: 'input', enable: enableContour">
  16. <span data-bind="text: contourSpacing"></span>m
  17. </div>
  18. <div>
  19. 线宽 <input style="width: 125px; float: left; width: 100px;"
  20. type="range" min="1.0" max="10.0" step="1.0"
  21. data-bind="value: contourWidth, valueUpdate: 'input', enable: enableContour">
  22. <span data-bind="text: contourWidth"></span>px
  23. </div>
  24. <div>
  25. <button type="button"
  26. data-bind="click: changeColor, enable: enableContour">颜色</button>
  27. </div>
  28. </div>

完成上述代码后,运行页面即可看到如下效果:

图片

点击等高线,即可看到效果

图片

还可以开启高程渲染效果:

图片

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/456507
推荐阅读
相关标签
  

闽ICP备14008679号