StripeMaterialProperty.js 7.2 KB

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