CheckerboardMaterialProperty.js 5.0 KB

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