DynamicWallMaterialProperty.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. 动态墙材质
  3. color 颜色
  4. duration 持续时间 毫秒
  5. trailImage 贴图地址
  6. */
  7. function DynamicWallMaterialProperty(options) {
  8. this._definitionChanged = new Cesium.Event();
  9. this._color = undefined;
  10. this._colorSubscription = undefined;
  11. this.color = options.color || Color.BLUE;
  12. this.duration = options.duration || 1000;
  13. this.trailImage = options.trailImage;
  14. this._time = (new Date()).getTime();
  15. }
  16. /**
  17. * 带方向的墙体
  18. * @param {*} options.get:true/false
  19. * @param {*} options.count:数量
  20. * @param {*} options.direction:vertical/standard
  21. * @param {*} options.order:+/-
  22. */
  23. function _getDirectionWallShader(options) {
  24. if (options && options.get) {
  25. var materail = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  26. {\n\
  27. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  28. vec2 st = materialInput.st;";
  29. if (options.direction == "vertical") { //(由下到上)
  30. materail += "vec4 colorImage = texture2D(image, vec2(fract(st.s), fract(float(" + options.count + ")*st.t" + options.order + " time)));\n\ ";
  31. } else { //(逆时针)
  32. materail += "vec4 colorImage = texture2D(image, vec2(fract(float(" + options.count + ")*st.s " + options.order + " time), fract(st.t)));\n\ ";
  33. }
  34. //泛光
  35. materail += "vec4 fragColor;\n\
  36. fragColor.rgb = (colorImage.rgb+color.rgb) / 1.0;\n\
  37. fragColor = czm_gammaCorrect(fragColor);\n\
  38. material.diffuse = colorImage.rgb;\n\
  39. material.alpha = colorImage.a;\n\
  40. material.emission = fragColor.rgb;\n\
  41. return material;\n\
  42. }";
  43. return materail
  44. }
  45. }
  46. Object.defineProperties(DynamicWallMaterialProperty.prototype, {
  47. isConstant: {
  48. get: function() {
  49. return false;
  50. }
  51. },
  52. definitionChanged: {
  53. get: function() {
  54. return this._definitionChanged;
  55. }
  56. },
  57. color: Cesium.createPropertyDescriptor('color')
  58. });
  59. var MaterialType = 'wallType' + parseInt(Math.random() * 1000);
  60. DynamicWallMaterialProperty.prototype.getType = function(time) {
  61. return MaterialType;
  62. };
  63. DynamicWallMaterialProperty.prototype.getValue = function(time, result) {
  64. if (!Cesium.defined(result)) {
  65. result = {};
  66. }
  67. result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
  68. result.image = this.trailImage;
  69. if (this.duration) {
  70. result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
  71. }
  72. viewer.scene.requestRender();
  73. return result;
  74. };
  75. DynamicWallMaterialProperty.prototype.equals = function(other) {
  76. return this === other ||
  77. (other instanceof DynamicWallMaterialProperty &&
  78. Cesium.Property.equals(this._color, other._color))
  79. };
  80. Cesium.Material._materialCache.addMaterial(MaterialType, {
  81. fabric: {
  82. type: MaterialType,
  83. uniforms: {
  84. color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
  85. image: Cesium.Material.DefaultImageId,
  86. time: -20
  87. },
  88. source: _getDirectionWallShader({
  89. get: true,
  90. count: 3.0,
  91. direction: 'vertical',
  92. order: '-'
  93. })
  94. },
  95. translucent: function(material) {
  96. return true;
  97. }
  98. });
  99. Cesium.DynamicWallMaterialProperty = DynamicWallMaterialProperty;
  100. export default DynamicWallMaterialProperty;