GridMaterialProperty.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 defaultColor = Color.WHITE;
  9. const defaultCellAlpha = 0.1;
  10. const defaultLineCount = new Cartesian2(8, 8);
  11. const defaultLineOffset = new Cartesian2(0, 0);
  12. const defaultLineThickness = new Cartesian2(1, 1);
  13. /**
  14. * A {@link MaterialProperty} that maps to grid {@link Material} uniforms.
  15. * @alias GridMaterialProperty
  16. *
  17. * @param {object} [options] Object with the following properties:
  18. * @param {Property|Color} [options.color=Color.WHITE] A Property specifying the grid {@link Color}.
  19. * @param {Property|number} [options.cellAlpha=0.1] A numeric Property specifying cell alpha values.
  20. * @param {Property|Cartesian2} [options.lineCount=new Cartesian2(8, 8)] A {@link Cartesian2} Property specifying the number of grid lines along each axis.
  21. * @param {Property|Cartesian2} [options.lineThickness=new Cartesian2(1.0, 1.0)] A {@link Cartesian2} Property specifying the thickness of grid lines along each axis.
  22. * @param {Property|Cartesian2} [options.lineOffset=new Cartesian2(0.0, 0.0)] A {@link Cartesian2} Property specifying starting offset of grid lines along each axis.
  23. *
  24. * @constructor
  25. */
  26. function GridMaterialProperty(options) {
  27. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  28. this._definitionChanged = new Event();
  29. this._color = undefined;
  30. this._colorSubscription = undefined;
  31. this._cellAlpha = undefined;
  32. this._cellAlphaSubscription = undefined;
  33. this._lineCount = undefined;
  34. this._lineCountSubscription = undefined;
  35. this._lineThickness = undefined;
  36. this._lineThicknessSubscription = undefined;
  37. this._lineOffset = undefined;
  38. this._lineOffsetSubscription = undefined;
  39. this.color = options.color;
  40. this.cellAlpha = options.cellAlpha;
  41. this.lineCount = options.lineCount;
  42. this.lineThickness = options.lineThickness;
  43. this.lineOffset = options.lineOffset;
  44. }
  45. Object.defineProperties(GridMaterialProperty.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 GridMaterialProperty.prototype
  50. *
  51. * @type {boolean}
  52. * @readonly
  53. */
  54. isConstant: {
  55. get: function () {
  56. return (
  57. Property.isConstant(this._color) &&
  58. Property.isConstant(this._cellAlpha) &&
  59. Property.isConstant(this._lineCount) &&
  60. Property.isConstant(this._lineThickness) &&
  61. Property.isConstant(this._lineOffset)
  62. );
  63. },
  64. },
  65. /**
  66. * Gets the event that is raised whenever the definition of this property changes.
  67. * The definition is considered to have changed if a call to getValue would return
  68. * a different result for the same time.
  69. * @memberof GridMaterialProperty.prototype
  70. *
  71. * @type {Event}
  72. * @readonly
  73. */
  74. definitionChanged: {
  75. get: function () {
  76. return this._definitionChanged;
  77. },
  78. },
  79. /**
  80. * Gets or sets the Property specifying the grid {@link Color}.
  81. * @memberof GridMaterialProperty.prototype
  82. * @type {Property|undefined}
  83. * @default Color.WHITE
  84. */
  85. color: createPropertyDescriptor("color"),
  86. /**
  87. * Gets or sets the numeric Property specifying cell alpha values.
  88. * @memberof GridMaterialProperty.prototype
  89. * @type {Property|undefined}
  90. * @default 0.1
  91. */
  92. cellAlpha: createPropertyDescriptor("cellAlpha"),
  93. /**
  94. * Gets or sets the {@link Cartesian2} Property specifying the number of grid lines along each axis.
  95. * @memberof GridMaterialProperty.prototype
  96. * @type {Property|undefined}
  97. * @default new Cartesian2(8.0, 8.0)
  98. */
  99. lineCount: createPropertyDescriptor("lineCount"),
  100. /**
  101. * Gets or sets the {@link Cartesian2} Property specifying the thickness of grid lines along each axis.
  102. * @memberof GridMaterialProperty.prototype
  103. * @type {Property|undefined}
  104. * @default new Cartesian2(1.0, 1.0)
  105. */
  106. lineThickness: createPropertyDescriptor("lineThickness"),
  107. /**
  108. * Gets or sets the {@link Cartesian2} Property specifying the starting offset of grid lines along each axis.
  109. * @memberof GridMaterialProperty.prototype
  110. * @type {Property|undefined}
  111. * @default new Cartesian2(0.0, 0.0)
  112. */
  113. lineOffset: createPropertyDescriptor("lineOffset"),
  114. });
  115. /**
  116. * Gets the {@link Material} type at the provided time.
  117. *
  118. * @param {JulianDate} time The time for which to retrieve the type.
  119. * @returns {string} The type of material.
  120. */
  121. GridMaterialProperty.prototype.getType = function (time) {
  122. return "Grid";
  123. };
  124. /**
  125. * Gets the value of the property at the provided time.
  126. *
  127. * @param {JulianDate} time The time for which to retrieve the value.
  128. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  129. * @returns {object} The modified result parameter or a new instance if the result parameter was not supplied.
  130. */
  131. GridMaterialProperty.prototype.getValue = function (time, result) {
  132. if (!defined(result)) {
  133. result = {};
  134. }
  135. result.color = Property.getValueOrClonedDefault(
  136. this._color,
  137. time,
  138. defaultColor,
  139. result.color
  140. );
  141. result.cellAlpha = Property.getValueOrDefault(
  142. this._cellAlpha,
  143. time,
  144. defaultCellAlpha
  145. );
  146. result.lineCount = Property.getValueOrClonedDefault(
  147. this._lineCount,
  148. time,
  149. defaultLineCount,
  150. result.lineCount
  151. );
  152. result.lineThickness = Property.getValueOrClonedDefault(
  153. this._lineThickness,
  154. time,
  155. defaultLineThickness,
  156. result.lineThickness
  157. );
  158. result.lineOffset = Property.getValueOrClonedDefault(
  159. this._lineOffset,
  160. time,
  161. defaultLineOffset,
  162. result.lineOffset
  163. );
  164. return result;
  165. };
  166. /**
  167. * Compares this property to the provided property and returns
  168. * <code>true</code> if they are equal, <code>false</code> otherwise.
  169. *
  170. * @param {Property} [other] The other property.
  171. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  172. */
  173. GridMaterialProperty.prototype.equals = function (other) {
  174. return (
  175. this === other || //
  176. (other instanceof GridMaterialProperty && //
  177. Property.equals(this._color, other._color) && //
  178. Property.equals(this._cellAlpha, other._cellAlpha) && //
  179. Property.equals(this._lineCount, other._lineCount) && //
  180. Property.equals(this._lineThickness, other._lineThickness) && //
  181. Property.equals(this._lineOffset, other._lineOffset))
  182. );
  183. };
  184. export default GridMaterialProperty;