当前位置:   article > 正文

Cesium流动线纹理_cesium 1.68

cesium 1.68

1.扩展类PolylineTrailMaterialProperty (Cesium  1.67-1.68)

创建PolylineTrailMaterialProperty .js文件

  1. import * as Cesium from "cesium";
  2. export class PolylineTrailMaterialProperty {
  3. constructor(options) {
  4. options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
  5. this._definitionChanged = new Cesium.Event();
  6. this._color = undefined;
  7. this._colorSubscription = undefined;
  8. this.color = options.color;
  9. this.duration = options.duration;
  10. this.trailImage = options.trailImage;
  11. this._time = performance.now();
  12. }
  13. }
  14. Object.defineProperties(PolylineTrailMaterialProperty.prototype, {
  15. isConstant: {
  16. get: function () {
  17. return false;
  18. },
  19. },
  20. definitionChanged: {
  21. get: function () {
  22. return this._definitionChanged;
  23. },
  24. },
  25. color: Cesium.createPropertyDescriptor("color"),
  26. });
  27. PolylineTrailMaterialProperty.prototype.getType = function (time) {
  28. return "PolylineTrail";
  29. };
  30. PolylineTrailMaterialProperty.prototype.getValue = function (time, result) {
  31. if (!Cesium.defined(result)) {
  32. result = {};
  33. }
  34. result.color = Cesium.Property.getValueOrClonedDefault(
  35. this._color,
  36. time,
  37. Cesium.Color.WHITE,
  38. result.color
  39. ); //result.image = Cesium.Material.PolylineTrailImage;
  40. result.image = this.trailImage;
  41. result.time =
  42. ((performance.now() - this._time) % this.duration) / this.duration;
  43. return result;
  44. };
  45. PolylineTrailMaterialProperty.prototype.equals = function (other) {
  46. return (
  47. this === other ||
  48. (other instanceof PolylineTrailMaterialProperty &&
  49. Cesium.Property.equals(this._color, other._color))
  50. );
  51. };
  52. Cesium.Material.PolylineTrailType = "PolylineTrail";
  53. Cesium.Material.PolylineTrailImage = "/images/fire.png";
  54. // Cesium.Material.PolylineTrailSource =
  55. // "czm_material czm_getMaterial(czm_materialInput materialInput)\n" +
  56. // "{\n" +
  57. // "czm_material material = czm_getDefaultMaterial(materialInput);\n" +
  58. // "vec2 st = materialInput.st;\n" +
  59. // "vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n" +
  60. // "material.alpha = colorImage.a * color.a;\n" +
  61. // "material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n" +
  62. // "return material;\n" +
  63. // "}";
  64. Cesium.Material.PolylineTrailSource =
  65. "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  66. {\n\
  67. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  68. vec2 st = materialInput.st;\n\
  69. vec4 colorImage = texture(image, vec2(fract(st.t - time), st.t));\n\
  70. vec4 fragColor;\n\
  71. fragColor.rgb = color.rgb / 1.0;\n\
  72. fragColor = czm_gammaCorrect(fragColor);\n\
  73. material.alpha = colorImage.a * color.a;\n\
  74. material.diffuse = color.rgb;\n\
  75. material.emission = fragColor.rgb;\n\
  76. return material;\n\
  77. }";
  78. Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailType, {
  79. fabric: {
  80. type: Cesium.Material.PolylineTrailType,
  81. uniforms: {
  82. color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
  83. image: Cesium.Material.PolylineTrailImage,
  84. time: 0,
  85. },
  86. source: Cesium.Material.PolylineTrailSource,
  87. },
  88. translucent: function (material) {
  89. return true;
  90. },
  91. });
  92. Cesium.PolylineTrailMaterialProperty = PolylineTrailMaterialProperty;

2:在vue中引用

import PolylineTrailMaterialProperty from "@/utils/PolylineTrailLinkMaterialProperty";

3:调用

  1. const createFlyLines = (data, viewer) => {
  2. const center = data.center;
  3. const cities = data.points;
  4. const startPoint = Cesium.Cartesian3.fromDegrees(
  5. center.lon,
  6. center.lat,
  7. 0
  8. );
  9. //中心点
  10. viewer.entities.add({
  11. position: startPoint,
  12. point: {
  13. pixelSize: center.size,
  14. color: Cesium.Color.YELLOWGREEN
  15. }
  16. });
  17. //大批量操作时,临时禁用事件可以提高性能
  18. // viewer.entities.suspendEvents();
  19. //散点
  20. cities.map(city => {
  21. let material = new Cesium.PolylineTrailMaterialProperty({ //创建材质1
  22. color: Cesium.Color.GREEN.withAlpha(0.5),
  23. duration: 3000,
  24. trailImage: '/images/fire.png'
  25. });
  26. const endPoint = Cesium.Cartesian3.fromDegrees(city.lon, city.lat, 0);
  27. viewer.entities.add({
  28. position: endPoint,
  29. point: {
  30. pixelSize: city.size - 10,
  31. color: city.color
  32. }
  33. });
  34. viewer.entities.add({
  35. polyline: {
  36. positions: generateCurve(startPoint, endPoint), // 格式为世界坐标的线位置数组
  37. width: 2, // 线的宽度
  38. material: material, // 线的颜色
  39. clampToGround: true, // 线是否固定在地面
  40. }
  41. });
  42. });
  43. };
  44. /**
  45. * 生成流动曲线
  46. * @param startPoint 起点
  47. * @param endPoint 终点
  48. * @returns {Array}
  49. */
  50. const generateCurve = (startPoint, endPoint) => {
  51. let addPointCartesian = new Cesium.Cartesian3();
  52. Cesium.Cartesian3.add(startPoint, endPoint, addPointCartesian);
  53. let midPointCartesian = new Cesium.Cartesian3();
  54. Cesium.Cartesian3.divideByScalar(addPointCartesian, 2, midPointCartesian);
  55. let midPointCartographic = Cesium.Cartographic.fromCartesian(
  56. midPointCartesian
  57. );
  58. midPointCartographic.height =
  59. Cesium.Cartesian3.distance(startPoint, endPoint) / 5;
  60. let midPoint = new Cesium.Cartesian3();
  61. Cesium.Ellipsoid.WGS84.cartographicToCartesian(
  62. midPointCartographic,
  63. midPoint
  64. );
  65. let spline = new Cesium.CatmullRomSpline({
  66. times: [0.0, 0.5, 1.0],
  67. points: [startPoint, midPoint, endPoint]
  68. });
  69. let curvePoints = [];
  70. for (let i = 0, len = 200; i < len; i++) {
  71. curvePoints.push(spline.evaluate(i / len));
  72. }
  73. return curvePoints;
  74. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号