ImageMaterialProperty.js 5.4 KB

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