PolylineDirectionMaterialProperty.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. /**
  4. * 流动纹理线
  5. * @ignore
  6. */
  7. class PolylineDirectionMaterialProperty {
  8. /**
  9. * 流动纹理线 初始化
  10. * @author joy/创睿
  11. * @param {Object} options 具有以下属性:
  12. * @param {String} options.color 颜色
  13. * @param {Number} options.duration 持续时间 毫秒,越小越快
  14. * @param {String} options.imgUrl 材质图片
  15. * @param {Number} [options.count] 重复次数
  16. * @param {String} [options.direction] 方向 vertical纵,垂直方向,horizontal横,水平方向
  17. * @param {String} [options.order] 方向正负
  18. * 纵:'-'(由下到上) , '+"(由上到下)
  19. * 横:'-'(顺时针) , '+'(逆时针)
  20. */
  21. constructor(options) {
  22. this.defaultColor = new Cesium.Color(255/255, 0, 0, 0);
  23. options = options || {};
  24. options.isImageAlpha = Cesium.defaultValue(options.isImageAlpha, true);
  25. options.imgUrl = Cesium.defaultValue(options.imgUrl, 'jt3dSDK/imgs/polylinematerial/spriteline1.png');
  26. options.duration = Cesium.defaultValue(options.duration, 3000);
  27. options.count = Cesium.defaultValue(options.count, 1);
  28. options.direction = Cesium.defaultValue(options.direction, "horizontal");
  29. options.order = Cesium.defaultValue(options.order, "-");
  30. if (options.isImageAlpha) {
  31. options.color = this.defaultColor;
  32. } else {
  33. if (options.color instanceof Array) {
  34. options.color = new Cesium.Color(options.color[0]/255, options.color[1]/255, options.color[2]/255, options.color[3]);
  35. } else if (typeof options.color === 'string') {
  36. options.color = new Cesium.Color.fromCssColorString(options.color);
  37. } else {
  38. options.color = this.defaultColor;
  39. }
  40. }
  41. /* 变更事件 */
  42. this._definitionChanged = new Cesium.Event();
  43. this._color = undefined;
  44. this._image = undefined;
  45. this.color = options.color; //颜色
  46. this.image = options.imgUrl; //材质图片
  47. this._isImageAlpha = options.isImageAlpha;
  48. this._duration = options.duration; //动态循环周期
  49. this._count = options.count; //重复次数
  50. this._direction = options.direction; //方向
  51. this._order = options.order; //方向正负
  52. this._time = performance.now();
  53. this.addMaterial()
  54. }
  55. addMaterial() {
  56. Cesium.Material.PolylineTrailType = 'PolylineTrail';
  57. Cesium.Material.Polylineimage = "images/colors.png";
  58. if (this._direction === "vertical") { //垂直方向
  59. if (this._isImageAlpha) {
  60. Cesium.Material.PolylineTrailSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  61. {\n\
  62. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  63. vec2 st = repeat * materialInput.st;\n\
  64. vec4 colorImage = texture2D(image, vec2(fract(st.t " + this._order + " time), st.s));\n\
  65. material.alpha = colorImage.a;\n\
  66. material.diffuse = colorImage.rgb* 1.5 ;\n\
  67. return material;\n\
  68. }";
  69. } else {
  70. Cesium.Material.PolylineTrailSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  71. {\n\
  72. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  73. vec2 st = repeat * materialInput.st;\n\
  74. vec4 colorImage = texture2D(image, vec2(fract(st.t " + this._order + " time), st.s));\n\
  75. material.alpha = colorImage.a * color.a;\n\
  76. material.diffuse = max(color.rgb * material.alpha * 3.0, color.rgb);\n\
  77. return material;\n\
  78. }";
  79. }
  80. } else if (this._direction === "horizontal") { //水平方向
  81. if (this._isImageAlpha) {
  82. Cesium.Material.PolylineTrailSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  83. {\n\
  84. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  85. vec2 st = repeat * materialInput.st;\n\
  86. vec4 colorImage = texture2D(image, vec2(fract(st.s " + this._order + " time), st.t));\n\
  87. material.alpha = colorImage.a;\n\
  88. material.diffuse = colorImage.rgb * 1.5 ;\n\
  89. return material;\n\
  90. }";
  91. } else {
  92. Cesium.Material.PolylineTrailSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  93. {\n\
  94. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  95. vec2 st = repeat * materialInput.st;\n\
  96. vec4 colorImage = texture2D(image, vec2(fract(st.s " + this._order + " time), st.t));\n\
  97. material.alpha = colorImage.a * color.a;\n\
  98. material.diffuse = max(color.rgb * material.alpha * 3.0, color.rgb);\n\
  99. return material;\n\
  100. }";
  101. }
  102. }
  103. /* 将材质加入缓存 以便重复利用 */
  104. Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailType, {
  105. fabric: {
  106. type: Cesium.Material.PolylineTrailType,
  107. uniforms: {
  108. color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
  109. image: Cesium.Material.Polylineimage,
  110. time: 0,
  111. repeat: new Cesium.Cartesian2(5.0, 1.0), // 横纵方向重复的次数
  112. order: '-',
  113. },
  114. source: Cesium.Material.PolylineTrailSource
  115. },
  116. translucent: function(material) {
  117. /* 材质是否半透明 */
  118. return true;
  119. }
  120. });
  121. }
  122. }
  123. // 材质类型
  124. PolylineDirectionMaterialProperty.prototype.getType = function(time) {
  125. return 'PolylineTrail';
  126. }
  127. // 这个方法在每次渲染时被调用,result的参数会传入glsl中。
  128. PolylineDirectionMaterialProperty.prototype.getValue = function(time, result) {
  129. if (!Cesium.defined(result)) {
  130. result = {};
  131. }
  132. result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, this.defaultColor, result.color);
  133. result.image = Cesium.Property.getValueOrUndefined(this._image, time);
  134. result.time = ((performance.now() - this._time) % this._duration) / this._duration;
  135. result.repeat = new Cesium.Cartesian2(this._count, 1.0);
  136. result.order = this._order;
  137. return result;
  138. }
  139. PolylineDirectionMaterialProperty.prototype.equals = function(other) {
  140. return this === other ||
  141. (other instanceof PolylineDirectionMaterialProperty &&
  142. Cesium.Property.equals(this._color, other._color))
  143. }
  144. Object.defineProperties(PolylineDirectionMaterialProperty.prototype, {
  145. isConstant: {
  146. get: function() {
  147. return false;
  148. }
  149. },
  150. definitionChanged: {
  151. get: function() {
  152. return this._definitionChanged;
  153. }
  154. },
  155. color: Cesium.createPropertyDescriptor('color'),
  156. image: Cesium.createPropertyDescriptor('image')
  157. });
  158. export default PolylineDirectionMaterialProperty;