WallMaterialProperty.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. class WallMaterialProperty {
  2. /**
  3. * 构造方法
  4. * @ignore 无需公开
  5. * @param {Cesium.Viewer} viewer 着色器运行所需的视图
  6. * @param {JSON} options 配置项
  7. * @param {Cesium.Color} options.color [墙的颜色,默认蓝色] 可选
  8. * @param {Number} options.duration [循环时间 默认1000] 可选
  9. * @param {String} options.trailImage 墙的贴图
  10. * @param {JSON} options.param 着色器参数
  11. * @param {Number} options.param.count [数量 可选 默认为1]
  12. * @param {String} options.param.direction [方向 可选 默认竖直 ] 取值vertical/horizontal
  13. * @param {String} options.param.order [顺序 可选 上下/下上/顺时针/逆时针 与方向配合使用 ] 取值+/-
  14. */
  15. constructor(viewer, options) {
  16. /* 着色器运行依赖的视图 */
  17. this._viewer = viewer;
  18. options = options || {};
  19. /* 变更事件 */
  20. this._definitionChanged = new Cesium.Event();
  21. this._color = undefined;
  22. /* 墙的颜色 */
  23. this.color = options.color || Cesium.Color.BLUE;
  24. /* 动态循环周期 */
  25. this.duration = options.duration || 1000;
  26. this.count = options.duration || 1;
  27. this.direction = options.duration || 'vertical';
  28. this.order = options.duration || '-';
  29. /* 墙的贴图 */
  30. this.trailImage = Cesium.defaultValue(options.trailImage, 'jt3dSDK/imgs/wallmaterial/wl.png');
  31. /* 默认时间 */
  32. this._time = (new Date()).getTime();
  33. /* 材质类型名称 */
  34. this._materialTypeName = 'WallMaterial' + this._guid();
  35. /* 存储相关参数的属性 以便后期进行追踪修改 */
  36. this._param = {
  37. color: this.color._value.toCssColorString(),
  38. image: this.trailImage,
  39. duration: this.duration,
  40. count: this.count,
  41. direction: this.direction,
  42. order: this.order,
  43. }
  44. /* 将材质加入缓存 以便重复利用 */
  45. Cesium.Material._materialCache.addMaterial(this._materialTypeName, {
  46. fabric: {
  47. type: this._materialTypeName,
  48. uniforms: {
  49. time: -20,
  50. color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
  51. image: options.trailImage,
  52. },
  53. source: this._getDirectionWallShader(options.param)
  54. },
  55. translucent: function(material) {
  56. /* 材质是否半透明 */
  57. return true;
  58. }
  59. });
  60. }
  61. /**
  62. * 生成GUID随机数
  63. * @ignore 无需公开
  64. */
  65. _guid() {
  66. function S4() {
  67. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  68. }
  69. return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  70. }
  71. /**
  72. * 重新获取类型方法
  73. * @ignore 无需公开
  74. * @param {Cesium.JulianDate} time 时间
  75. */
  76. getType(time) {
  77. return this._materialTypeName;
  78. }
  79. /**
  80. * 重写获取值方法
  81. * @ignore 无需公开
  82. * @param {Cesium.JulianDate} time
  83. * @param {JSON} result
  84. */
  85. getValue(time, result) {
  86. if (!Cesium.defined(result)) {
  87. result = {};
  88. }
  89. result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.BLUE, result.color);
  90. result.image = this.trailImage;
  91. if (this.duration) {
  92. result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
  93. }
  94. this._viewer.scene.requestRender();
  95. return result;
  96. }
  97. /**
  98. * 重写对比函数
  99. * @ignore 无需公开
  100. * @param {Object} other 传入对比对象
  101. */
  102. equals(other) {
  103. return (this === other || (other instanceof WallMaterialProperty && Cesium.Property.equals(this
  104. ._color, other._color) && other._param.order === this._param.order && other._param.count ===
  105. this._param.count && other._param.direction === this._param.direction && other.duration === this
  106. .duration));
  107. }
  108. /**
  109. * 创建着色器资源
  110. * @ignore 无需公开
  111. * @param {JSON} options 配置项
  112. * @param {Number} options.count [数量 可选 默认为1]
  113. * @param {String} options.direction [方向 可选 默认竖直 ] 取值vertical/horizontal
  114. * @param {String} options.order [顺序 可选 上下/下上/顺时针/逆时针 与方向配合使用 ] 取值+/-
  115. */
  116. _getDirectionWallShader(options) {
  117. let op = Cesium.defaultValue(options, {});
  118. // console.log('>>>op===', op);
  119. let count = op.count !== undefined && typeof op.count === 'number' && op.count > 0 ? op.count : 1;
  120. let direction = op.direction === 'horizontal' ? 'horizontal' : 'vertical';
  121. let order = op.order === '+' ? '+' : '-';
  122. this._param.count = count;
  123. this._param.direction = direction;
  124. this._param.order = order;
  125. let materail = '';
  126. /* 补充参数 */
  127. // console.log('_param', this._param);
  128. materail += 'czm_material czm_getMaterial(czm_materialInput materialInput){\n' +
  129. ' czm_material material = czm_getDefaultMaterial(materialInput);\n' +
  130. ' vec2 st = materialInput.st;\n';
  131. if (direction === 'vertical') {
  132. materail += ' vec4 colorImage = texture2D(image,vec2(st.s,fract(float(' + count + ')*st.t ' + order + ' time)));\n';
  133. } else if (direction === 'horizontal') {
  134. materail += ' vec4 colorImage = texture2D(image, vec2(fract(float(' + count + ')*st.s ' + order + ' time), st.t));\n'
  135. }
  136. materail += ' vec4 fragColor;\n' +
  137. ' fragColor.rgb = color.rgb / 1.0;\n' +
  138. ' fragColor = czm_gammaCorrect(fragColor);\n' +
  139. ' material.alpha = colorImage.a * color.a;\n' +
  140. ' material.diffuse = color.rgb;\n' +
  141. ' material.emission = fragColor.rgb;\n' +
  142. ' return material;\n' +
  143. '}';
  144. return materail;
  145. }
  146. }
  147. /**
  148. * 增加默认属性
  149. */
  150. Object.defineProperties(WallMaterialProperty.prototype, {
  151. /**
  152. * @ignore 无需公开
  153. * 判断是否相等,返回false表示属性一直在变化中
  154. */
  155. isConstant: {
  156. get: function() {
  157. return false;
  158. }
  159. },
  160. /**
  161. * @ignore 无需公开
  162. * 事件变更
  163. */
  164. definitionChanged: {
  165. get: function() {
  166. return this._definitionChanged;
  167. }
  168. },
  169. /* 颜色属性 */
  170. color: Cesium.createPropertyDescriptor('color')
  171. })
  172. export default WallMaterialProperty;