StripeMaterialProperty.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import Color from "../Core/Color.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import Event from "../Core/Event.js";
  5. import createPropertyDescriptor from "./createPropertyDescriptor.js";
  6. import Property from "./Property.js";
  7. import StripeOrientation from "./StripeOrientation.js";
  8. const defaultOrientation = StripeOrientation.HORIZONTAL;
  9. const defaultEvenColor = Color.WHITE;
  10. const defaultOddColor = Color.BLACK;
  11. const defaultOffset = 0;
  12. const defaultRepeat = 1;
  13. /**
  14. * A {@link MaterialProperty} that maps to stripe {@link Material} uniforms.
  15. * @alias StripeMaterialProperty
  16. * @constructor
  17. *
  18. * @param {object} [options] Object with the following properties:
  19. * @param {Property|StripeOrientation} [options.orientation=StripeOrientation.HORIZONTAL] A Property specifying the {@link StripeOrientation}.
  20. * @param {Property|Color} [options.evenColor=Color.WHITE] A Property specifying the first {@link Color}.
  21. * @param {Property|Color} [options.oddColor=Color.BLACK] A Property specifying the second {@link Color}.
  22. * @param {Property|number} [options.offset=0] A numeric Property specifying how far into the pattern to start the material.
  23. * @param {Property|number} [options.repeat=1] A numeric Property specifying how many times the stripes repeat.
  24. */
  25. function StripeMaterialProperty(options) {
  26. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  27. this._definitionChanged = new Event();
  28. this._orientation = undefined;
  29. this._orientationSubscription = undefined;
  30. this._evenColor = undefined;
  31. this._evenColorSubscription = undefined;
  32. this._oddColor = undefined;
  33. this._oddColorSubscription = undefined;
  34. this._offset = undefined;
  35. this._offsetSubscription = undefined;
  36. this._repeat = undefined;
  37. this._repeatSubscription = undefined;
  38. this.orientation = options.orientation;
  39. this.evenColor = options.evenColor;
  40. this.oddColor = options.oddColor;
  41. this.offset = options.offset;
  42. this.repeat = options.repeat;
  43. }
  44. Object.defineProperties(StripeMaterialProperty.prototype, {
  45. /**
  46. * Gets a value indicating if this property is constant. A property is considered
  47. * constant if getValue always returns the same result for the current definition.
  48. * @memberof StripeMaterialProperty.prototype
  49. *
  50. * @type {boolean}
  51. * @readonly
  52. */
  53. isConstant: {
  54. get: function () {
  55. return (
  56. Property.isConstant(this._orientation) && //
  57. Property.isConstant(this._evenColor) && //
  58. Property.isConstant(this._oddColor) && //
  59. Property.isConstant(this._offset) && //
  60. Property.isConstant(this._repeat)
  61. );
  62. },
  63. },
  64. /**
  65. * Gets the event that is raised whenever the definition of this property changes.
  66. * The definition is considered to have changed if a call to getValue would return
  67. * a different result for the same time.
  68. * @memberof StripeMaterialProperty.prototype
  69. *
  70. * @type {Event}
  71. * @readonly
  72. */
  73. definitionChanged: {
  74. get: function () {
  75. return this._definitionChanged;
  76. },
  77. },
  78. /**
  79. * Gets or sets the Property specifying the {@link StripeOrientation}/
  80. * @memberof StripeMaterialProperty.prototype
  81. * @type {Property|undefined}
  82. * @default StripeOrientation.HORIZONTAL
  83. */
  84. orientation: createPropertyDescriptor("orientation"),
  85. /**
  86. * Gets or sets the Property specifying the first {@link Color}.
  87. * @memberof StripeMaterialProperty.prototype
  88. * @type {Property|undefined}
  89. * @default Color.WHITE
  90. */
  91. evenColor: createPropertyDescriptor("evenColor"),
  92. /**
  93. * Gets or sets the Property specifying the second {@link Color}.
  94. * @memberof StripeMaterialProperty.prototype
  95. * @type {Property|undefined}
  96. * @default Color.BLACK
  97. */
  98. oddColor: createPropertyDescriptor("oddColor"),
  99. /**
  100. * Gets or sets the numeric Property specifying the point into the pattern
  101. * to begin drawing; with 0.0 being the beginning of the even color, 1.0 the beginning
  102. * of the odd color, 2.0 being the even color again, and any multiple or fractional values
  103. * being in between.
  104. * @memberof StripeMaterialProperty.prototype
  105. * @type {Property|undefined}
  106. * @default 0.0
  107. */
  108. offset: createPropertyDescriptor("offset"),
  109. /**
  110. * Gets or sets the numeric Property specifying how many times the stripes repeat.
  111. * @memberof StripeMaterialProperty.prototype
  112. * @type {Property|undefined}
  113. * @default 1.0
  114. */
  115. repeat: createPropertyDescriptor("repeat"),
  116. });
  117. /**
  118. * Gets the {@link Material} type at the provided time.
  119. *
  120. * @param {JulianDate} time The time for which to retrieve the type.
  121. * @returns {string} The type of material.
  122. */
  123. StripeMaterialProperty.prototype.getType = function (time) {
  124. return "Stripe";
  125. };
  126. /**
  127. * Gets the value of the property at the provided time.
  128. *
  129. * @param {JulianDate} time The time for which to retrieve the value.
  130. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  131. * @returns {object} The modified result parameter or a new instance if the result parameter was not supplied.
  132. */
  133. StripeMaterialProperty.prototype.getValue = function (time, result) {
  134. if (!defined(result)) {
  135. result = {};
  136. }
  137. result.horizontal =
  138. Property.getValueOrDefault(this._orientation, time, defaultOrientation) ===
  139. StripeOrientation.HORIZONTAL;
  140. result.evenColor = Property.getValueOrClonedDefault(
  141. this._evenColor,
  142. time,
  143. defaultEvenColor,
  144. result.evenColor
  145. );
  146. result.oddColor = Property.getValueOrClonedDefault(
  147. this._oddColor,
  148. time,
  149. defaultOddColor,
  150. result.oddColor
  151. );
  152. result.offset = Property.getValueOrDefault(this._offset, time, defaultOffset);
  153. result.repeat = Property.getValueOrDefault(this._repeat, time, defaultRepeat);
  154. return result;
  155. };
  156. /**
  157. * Compares this property to the provided property and returns
  158. * <code>true</code> if they are equal, <code>false</code> otherwise.
  159. *
  160. * @param {Property} [other] The other property.
  161. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  162. */
  163. StripeMaterialProperty.prototype.equals = function (other) {
  164. return (
  165. this === other || //
  166. (other instanceof StripeMaterialProperty && //
  167. Property.equals(this._orientation, other._orientation) && //
  168. Property.equals(this._evenColor, other._evenColor) && //
  169. Property.equals(this._oddColor, other._oddColor) && //
  170. Property.equals(this._offset, other._offset) && //
  171. Property.equals(this._repeat, other._repeat))
  172. );
  173. };
  174. export default StripeMaterialProperty;