智慧水务管理系统 - 精河县供水工程综合管理平台

ImageMaterialProperty.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Color from "../Core/Color.js";
  3. import Frozen from "../Core/Frozen.js";
  4. import defined from "../Core/defined.js";
  5. import Event from "../Core/Event.js";
  6. import JulianDate from "../Core/JulianDate.js";
  7. import createPropertyDescriptor from "./createPropertyDescriptor.js";
  8. import Property from "./Property.js";
  9. const defaultRepeat = new Cartesian2(1, 1);
  10. const defaultTransparent = false;
  11. const defaultColor = Color.WHITE;
  12. /**
  13. * A {@link MaterialProperty} that maps to image {@link Material} uniforms.
  14. * @alias ImageMaterialProperty
  15. * @constructor
  16. *
  17. * @param {object} [options] Object with the following properties:
  18. * @param {Property|string|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement} [options.image] A Property specifying the Image, URL, Canvas, or Video.
  19. * @param {Property|Cartesian2} [options.repeat=new Cartesian2(1.0, 1.0)] A {@link Cartesian2} Property specifying the number of times the image repeats in each direction.
  20. * @param {Property|Color} [options.color=Color.WHITE] The color applied to the image
  21. * @param {Property|boolean} [options.transparent=false] Set to true when the image has transparency (for example, when a png has transparent sections)
  22. */
  23. function ImageMaterialProperty(options) {
  24. options = options ?? Frozen.EMPTY_OBJECT;
  25. this._definitionChanged = new Event();
  26. this._image = undefined;
  27. this._imageSubscription = undefined;
  28. this._repeat = undefined;
  29. this._repeatSubscription = undefined;
  30. this._color = undefined;
  31. this._colorSubscription = undefined;
  32. this._transparent = undefined;
  33. this._transparentSubscription = undefined;
  34. this.image = options.image;
  35. this.repeat = options.repeat;
  36. this.color = options.color;
  37. this.transparent = options.transparent;
  38. }
  39. Object.defineProperties(ImageMaterialProperty.prototype, {
  40. /**
  41. * Gets a value indicating if this property is constant. A property is considered
  42. * constant if getValue always returns the same result for the current definition.
  43. * @memberof ImageMaterialProperty.prototype
  44. *
  45. * @type {boolean}
  46. * @readonly
  47. */
  48. isConstant: {
  49. get: function () {
  50. return (
  51. Property.isConstant(this._image) && Property.isConstant(this._repeat)
  52. );
  53. },
  54. },
  55. /**
  56. * Gets the event that is raised whenever the definition of this property changes.
  57. * The definition is considered to have changed if a call to getValue would return
  58. * a different result for the same time.
  59. * @memberof ImageMaterialProperty.prototype
  60. *
  61. * @type {Event}
  62. * @readonly
  63. */
  64. definitionChanged: {
  65. get: function () {
  66. return this._definitionChanged;
  67. },
  68. },
  69. /**
  70. * Gets or sets the Property specifying Image, URL, Canvas, or Video to use.
  71. * @memberof ImageMaterialProperty.prototype
  72. * @type {Property|undefined}
  73. */
  74. image: createPropertyDescriptor("image"),
  75. /**
  76. * Gets or sets the {@link Cartesian2} Property specifying the number of times the image repeats in each direction.
  77. * @memberof ImageMaterialProperty.prototype
  78. * @type {Property|undefined}
  79. * @default new Cartesian2(1, 1)
  80. */
  81. repeat: createPropertyDescriptor("repeat"),
  82. /**
  83. * Gets or sets the Color Property specifying the desired color applied to the image.
  84. * @memberof ImageMaterialProperty.prototype
  85. * @type {Property|undefined}
  86. * @default 1.0
  87. */
  88. color: createPropertyDescriptor("color"),
  89. /**
  90. * Gets or sets the Boolean Property specifying whether the image has transparency
  91. * @memberof ImageMaterialProperty.prototype
  92. * @type {Property|undefined}
  93. * @default 1.0
  94. */
  95. transparent: createPropertyDescriptor("transparent"),
  96. });
  97. /**
  98. * Gets the {@link Material} type at the provided time.
  99. *
  100. * @param {JulianDate} time The time for which to retrieve the type.
  101. * @returns {string} The type of material.
  102. */
  103. ImageMaterialProperty.prototype.getType = function (time) {
  104. return "Image";
  105. };
  106. const timeScratch = new JulianDate();
  107. /**
  108. * Gets the value of the property at the provided time.
  109. *
  110. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  111. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  112. * @returns {object} The modified result parameter or a new instance if the result parameter was not supplied.
  113. */
  114. ImageMaterialProperty.prototype.getValue = function (time, result) {
  115. if (!defined(time)) {
  116. time = JulianDate.now(timeScratch);
  117. }
  118. if (!defined(result)) {
  119. result = {};
  120. }
  121. result.image = Property.getValueOrUndefined(this._image, time);
  122. result.repeat = Property.getValueOrClonedDefault(
  123. this._repeat,
  124. time,
  125. defaultRepeat,
  126. result.repeat,
  127. );
  128. result.color = Property.getValueOrClonedDefault(
  129. this._color,
  130. time,
  131. defaultColor,
  132. result.color,
  133. );
  134. if (Property.getValueOrDefault(this._transparent, time, defaultTransparent)) {
  135. result.color.alpha = Math.min(0.99, result.color.alpha);
  136. }
  137. return result;
  138. };
  139. /**
  140. * Compares this property to the provided property and returns
  141. * <code>true</code> if they are equal, <code>false</code> otherwise.
  142. *
  143. * @param {Property} [other] The other property.
  144. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  145. */
  146. ImageMaterialProperty.prototype.equals = function (other) {
  147. return (
  148. this === other ||
  149. (other instanceof ImageMaterialProperty &&
  150. Property.equals(this._image, other._image) &&
  151. Property.equals(this._repeat, other._repeat) &&
  152. Property.equals(this._color, other._color) &&
  153. Property.equals(this._transparent, other._transparent))
  154. );
  155. };
  156. export default ImageMaterialProperty;