NearFarScalar.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import defaultValue from "./defaultValue.js";
  2. import defined from "./defined.js";
  3. import DeveloperError from "./DeveloperError.js";
  4. /**
  5. * Represents a scalar value's lower and upper bound at a near distance and far distance in eye space.
  6. * @alias NearFarScalar
  7. * @constructor
  8. *
  9. * @param {Number} [near=0.0] The lower bound of the camera range.
  10. * @param {Number} [nearValue=0.0] The value at the lower bound of the camera range.
  11. * @param {Number} [far=1.0] The upper bound of the camera range.
  12. * @param {Number} [farValue=0.0] The value at the upper bound of the camera range.
  13. *
  14. * @see Packable
  15. */
  16. function NearFarScalar(near, nearValue, far, farValue) {
  17. /**
  18. * The lower bound of the camera range.
  19. * @type {Number}
  20. * @default 0.0
  21. */
  22. this.near = defaultValue(near, 0.0);
  23. /**
  24. * The value at the lower bound of the camera range.
  25. * @type {Number}
  26. * @default 0.0
  27. */
  28. this.nearValue = defaultValue(nearValue, 0.0);
  29. /**
  30. * The upper bound of the camera range.
  31. * @type {Number}
  32. * @default 1.0
  33. */
  34. this.far = defaultValue(far, 1.0);
  35. /**
  36. * The value at the upper bound of the camera range.
  37. * @type {Number}
  38. * @default 0.0
  39. */
  40. this.farValue = defaultValue(farValue, 0.0);
  41. }
  42. /**
  43. * Duplicates a NearFarScalar instance.
  44. *
  45. * @param {NearFarScalar} nearFarScalar The NearFarScalar to duplicate.
  46. * @param {NearFarScalar} [result] The object onto which to store the result.
  47. * @returns {NearFarScalar} The modified result parameter or a new NearFarScalar instance if one was not provided. (Returns undefined if nearFarScalar is undefined)
  48. */
  49. NearFarScalar.clone = function (nearFarScalar, result) {
  50. if (!defined(nearFarScalar)) {
  51. return undefined;
  52. }
  53. if (!defined(result)) {
  54. return new NearFarScalar(
  55. nearFarScalar.near,
  56. nearFarScalar.nearValue,
  57. nearFarScalar.far,
  58. nearFarScalar.farValue
  59. );
  60. }
  61. result.near = nearFarScalar.near;
  62. result.nearValue = nearFarScalar.nearValue;
  63. result.far = nearFarScalar.far;
  64. result.farValue = nearFarScalar.farValue;
  65. return result;
  66. };
  67. /**
  68. * The number of elements used to pack the object into an array.
  69. * @type {Number}
  70. */
  71. NearFarScalar.packedLength = 4;
  72. /**
  73. * Stores the provided instance into the provided array.
  74. *
  75. * @param {NearFarScalar} value The value to pack.
  76. * @param {Number[]} array The array to pack into.
  77. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  78. *
  79. * @returns {Number[]} The array that was packed into
  80. */
  81. NearFarScalar.pack = function (value, array, startingIndex) {
  82. //>>includeStart('debug', pragmas.debug);
  83. if (!defined(value)) {
  84. throw new DeveloperError("value is required");
  85. }
  86. if (!defined(array)) {
  87. throw new DeveloperError("array is required");
  88. }
  89. //>>includeEnd('debug');
  90. startingIndex = defaultValue(startingIndex, 0);
  91. array[startingIndex++] = value.near;
  92. array[startingIndex++] = value.nearValue;
  93. array[startingIndex++] = value.far;
  94. array[startingIndex] = value.farValue;
  95. return array;
  96. };
  97. /**
  98. * Retrieves an instance from a packed array.
  99. *
  100. * @param {Number[]} array The packed array.
  101. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  102. * @param {NearFarScalar} [result] The object into which to store the result.
  103. * @returns {NearFarScalar} The modified result parameter or a new NearFarScalar instance if one was not provided.
  104. */
  105. NearFarScalar.unpack = function (array, startingIndex, result) {
  106. //>>includeStart('debug', pragmas.debug);
  107. if (!defined(array)) {
  108. throw new DeveloperError("array is required");
  109. }
  110. //>>includeEnd('debug');
  111. startingIndex = defaultValue(startingIndex, 0);
  112. if (!defined(result)) {
  113. result = new NearFarScalar();
  114. }
  115. result.near = array[startingIndex++];
  116. result.nearValue = array[startingIndex++];
  117. result.far = array[startingIndex++];
  118. result.farValue = array[startingIndex];
  119. return result;
  120. };
  121. /**
  122. * Compares the provided NearFarScalar and returns <code>true</code> if they are equal,
  123. * <code>false</code> otherwise.
  124. *
  125. * @param {NearFarScalar} [left] The first NearFarScalar.
  126. * @param {NearFarScalar} [right] The second NearFarScalar.
  127. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  128. */
  129. NearFarScalar.equals = function (left, right) {
  130. return (
  131. left === right ||
  132. (defined(left) &&
  133. defined(right) &&
  134. left.near === right.near &&
  135. left.nearValue === right.nearValue &&
  136. left.far === right.far &&
  137. left.farValue === right.farValue)
  138. );
  139. };
  140. /**
  141. * Duplicates this instance.
  142. *
  143. * @param {NearFarScalar} [result] The object onto which to store the result.
  144. * @returns {NearFarScalar} The modified result parameter or a new NearFarScalar instance if one was not provided.
  145. */
  146. NearFarScalar.prototype.clone = function (result) {
  147. return NearFarScalar.clone(this, result);
  148. };
  149. /**
  150. * Compares this instance to the provided NearFarScalar and returns <code>true</code> if they are equal,
  151. * <code>false</code> otherwise.
  152. *
  153. * @param {NearFarScalar} [right] The right hand side NearFarScalar.
  154. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  155. */
  156. NearFarScalar.prototype.equals = function (right) {
  157. return NearFarScalar.equals(this, right);
  158. };
  159. export default NearFarScalar;