CheckerboardMaterialProperty.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 defineProperties from '../Core/defineProperties.js';
  6. import Event from '../Core/Event.js';
  7. import createPropertyDescriptor from './createPropertyDescriptor.js';
  8. import Property from './Property.js';
  9. var defaultEvenColor = Color.WHITE;
  10. var defaultOddColor = Color.BLACK;
  11. var defaultRepeat = new Cartesian2(2.0, 2.0);
  12. /**
  13. * A {@link MaterialProperty} that maps to checkerboard {@link Material} uniforms.
  14. * @alias CheckerboardMaterialProperty
  15. * @constructor
  16. *
  17. * @param {Object} [options] Object with the following properties:
  18. * @param {Property} [options.evenColor=Color.WHITE] A Property specifying the first {@link Color}.
  19. * @param {Property} [options.oddColor=Color.BLACK] A Property specifying the second {@link Color}.
  20. * @param {Property} [options.repeat=new Cartesian2(2.0, 2.0)] A {@link Cartesian2} Property specifying how many times the tiles repeat in each direction.
  21. */
  22. function CheckerboardMaterialProperty(options) {
  23. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  24. this._definitionChanged = new Event();
  25. this._evenColor = undefined;
  26. this._evenColorSubscription = undefined;
  27. this._oddColor = undefined;
  28. this._oddColorSubscription = undefined;
  29. this._repeat = undefined;
  30. this._repeatSubscription = undefined;
  31. this.evenColor = options.evenColor;
  32. this.oddColor = options.oddColor;
  33. this.repeat = options.repeat;
  34. }
  35. defineProperties(CheckerboardMaterialProperty.prototype, {
  36. /**
  37. * Gets a value indicating if this property is constant. A property is considered
  38. * constant if getValue always returns the same result for the current definition.
  39. * @memberof CheckerboardMaterialProperty.prototype
  40. *
  41. * @type {Boolean}
  42. * @readonly
  43. */
  44. isConstant : {
  45. get : function() {
  46. return Property.isConstant(this._evenColor) && //
  47. Property.isConstant(this._oddColor) && //
  48. Property.isConstant(this._repeat);
  49. }
  50. },
  51. /**
  52. * Gets the event that is raised whenever the definition of this property changes.
  53. * The definition is considered to have changed if a call to getValue would return
  54. * a different result for the same time.
  55. * @memberof CheckerboardMaterialProperty.prototype
  56. *
  57. * @type {Event}
  58. * @readonly
  59. */
  60. definitionChanged : {
  61. get : function() {
  62. return this._definitionChanged;
  63. }
  64. },
  65. /**
  66. * Gets or sets the Property specifying the first {@link Color}.
  67. * @memberof CheckerboardMaterialProperty.prototype
  68. * @type {Property}
  69. * @default Color.WHITE
  70. */
  71. evenColor : createPropertyDescriptor('evenColor'),
  72. /**
  73. * Gets or sets the Property specifying the second {@link Color}.
  74. * @memberof CheckerboardMaterialProperty.prototype
  75. * @type {Property}
  76. * @default Color.BLACK
  77. */
  78. oddColor : createPropertyDescriptor('oddColor'),
  79. /**
  80. * Gets or sets the {@link Cartesian2} Property specifying how many times the tiles repeat in each direction.
  81. * @memberof CheckerboardMaterialProperty.prototype
  82. * @type {Property}
  83. * @default new Cartesian2(2.0, 2.0)
  84. */
  85. repeat : createPropertyDescriptor('repeat')
  86. });
  87. /**
  88. * Gets the {@link Material} type at the provided time.
  89. *
  90. * @param {JulianDate} time The time for which to retrieve the type.
  91. * @returns {String} The type of material.
  92. */
  93. CheckerboardMaterialProperty.prototype.getType = function(time) {
  94. return 'Checkerboard';
  95. };
  96. /**
  97. * Gets the value of the property at the provided time.
  98. *
  99. * @param {JulianDate} time The time for which to retrieve the value.
  100. * @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  101. * @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied.
  102. */
  103. CheckerboardMaterialProperty.prototype.getValue = function(time, result) {
  104. if (!defined(result)) {
  105. result = {};
  106. }
  107. result.lightColor = Property.getValueOrClonedDefault(this._evenColor, time, defaultEvenColor, result.lightColor);
  108. result.darkColor = Property.getValueOrClonedDefault(this._oddColor, time, defaultOddColor, result.darkColor);
  109. result.repeat = Property.getValueOrDefault(this._repeat, time, defaultRepeat);
  110. return result;
  111. };
  112. /**
  113. * Compares this property to the provided property and returns
  114. * <code>true</code> if they are equal, <code>false</code> otherwise.
  115. *
  116. * @param {Property} [other] The other property.
  117. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  118. */
  119. CheckerboardMaterialProperty.prototype.equals = function(other) {
  120. return this === other || //
  121. (other instanceof CheckerboardMaterialProperty && //
  122. Property.equals(this._evenColor, other._evenColor) && //
  123. Property.equals(this._oddColor, other._oddColor) && //
  124. Property.equals(this._repeat, other._repeat));
  125. };
  126. export default CheckerboardMaterialProperty;