createFrustumOutlineGeometry.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defaultValue-94c3e563', './Transforms-a076dbe6', './Matrix2-fc7e9822', './RuntimeError-c581ca93', './ComponentDatatype-4a60b8d6', './FrustumGeometry-aa31d188', './GeometryAttribute-2ecf73f6', './GeometryAttributes-7df9bef6', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f', './WebGLConstants-7dccdc96', './Plane-e20fba8c', './VertexFormat-e46f29d6'], (function (defaultValue, Transforms, Matrix2, RuntimeError, ComponentDatatype, FrustumGeometry, GeometryAttribute, GeometryAttributes, _commonjsHelpers3aae1032, combine, WebGLConstants, Plane, VertexFormat) { 'use strict';
  3. const PERSPECTIVE = 0;
  4. const ORTHOGRAPHIC = 1;
  5. /**
  6. * A description of the outline of a frustum with the given the origin and orientation.
  7. *
  8. * @alias FrustumOutlineGeometry
  9. * @constructor
  10. *
  11. * @param {Object} options Object with the following properties:
  12. * @param {PerspectiveFrustum|OrthographicFrustum} options.frustum The frustum.
  13. * @param {Cartesian3} options.origin The origin of the frustum.
  14. * @param {Quaternion} options.orientation The orientation of the frustum.
  15. */
  16. function FrustumOutlineGeometry(options) {
  17. //>>includeStart('debug', pragmas.debug);
  18. RuntimeError.Check.typeOf.object("options", options);
  19. RuntimeError.Check.typeOf.object("options.frustum", options.frustum);
  20. RuntimeError.Check.typeOf.object("options.origin", options.origin);
  21. RuntimeError.Check.typeOf.object("options.orientation", options.orientation);
  22. //>>includeEnd('debug');
  23. const frustum = options.frustum;
  24. const orientation = options.orientation;
  25. const origin = options.origin;
  26. // This is private because it is used by DebugCameraPrimitive to draw a multi-frustum by
  27. // creating multiple FrustumOutlineGeometrys. This way the near plane of one frustum doesn't overlap
  28. // the far plane of another.
  29. const drawNearPlane = defaultValue.defaultValue(options._drawNearPlane, true);
  30. let frustumType;
  31. let frustumPackedLength;
  32. if (frustum instanceof FrustumGeometry.PerspectiveFrustum) {
  33. frustumType = PERSPECTIVE;
  34. frustumPackedLength = FrustumGeometry.PerspectiveFrustum.packedLength;
  35. } else if (frustum instanceof FrustumGeometry.OrthographicFrustum) {
  36. frustumType = ORTHOGRAPHIC;
  37. frustumPackedLength = FrustumGeometry.OrthographicFrustum.packedLength;
  38. }
  39. this._frustumType = frustumType;
  40. this._frustum = frustum.clone();
  41. this._origin = Matrix2.Cartesian3.clone(origin);
  42. this._orientation = Transforms.Quaternion.clone(orientation);
  43. this._drawNearPlane = drawNearPlane;
  44. this._workerName = "createFrustumOutlineGeometry";
  45. /**
  46. * The number of elements used to pack the object into an array.
  47. * @type {Number}
  48. */
  49. this.packedLength =
  50. 2 + frustumPackedLength + Matrix2.Cartesian3.packedLength + Transforms.Quaternion.packedLength;
  51. }
  52. /**
  53. * Stores the provided instance into the provided array.
  54. *
  55. * @param {FrustumOutlineGeometry} value The value to pack.
  56. * @param {Number[]} array The array to pack into.
  57. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  58. *
  59. * @returns {Number[]} The array that was packed into
  60. */
  61. FrustumOutlineGeometry.pack = function (value, array, startingIndex) {
  62. //>>includeStart('debug', pragmas.debug);
  63. RuntimeError.Check.typeOf.object("value", value);
  64. RuntimeError.Check.defined("array", array);
  65. //>>includeEnd('debug');
  66. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  67. const frustumType = value._frustumType;
  68. const frustum = value._frustum;
  69. array[startingIndex++] = frustumType;
  70. if (frustumType === PERSPECTIVE) {
  71. FrustumGeometry.PerspectiveFrustum.pack(frustum, array, startingIndex);
  72. startingIndex += FrustumGeometry.PerspectiveFrustum.packedLength;
  73. } else {
  74. FrustumGeometry.OrthographicFrustum.pack(frustum, array, startingIndex);
  75. startingIndex += FrustumGeometry.OrthographicFrustum.packedLength;
  76. }
  77. Matrix2.Cartesian3.pack(value._origin, array, startingIndex);
  78. startingIndex += Matrix2.Cartesian3.packedLength;
  79. Transforms.Quaternion.pack(value._orientation, array, startingIndex);
  80. startingIndex += Transforms.Quaternion.packedLength;
  81. array[startingIndex] = value._drawNearPlane ? 1.0 : 0.0;
  82. return array;
  83. };
  84. const scratchPackPerspective = new FrustumGeometry.PerspectiveFrustum();
  85. const scratchPackOrthographic = new FrustumGeometry.OrthographicFrustum();
  86. const scratchPackQuaternion = new Transforms.Quaternion();
  87. const scratchPackorigin = new Matrix2.Cartesian3();
  88. /**
  89. * Retrieves an instance from a packed array.
  90. *
  91. * @param {Number[]} array The packed array.
  92. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  93. * @param {FrustumOutlineGeometry} [result] The object into which to store the result.
  94. */
  95. FrustumOutlineGeometry.unpack = function (array, startingIndex, result) {
  96. //>>includeStart('debug', pragmas.debug);
  97. RuntimeError.Check.defined("array", array);
  98. //>>includeEnd('debug');
  99. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  100. const frustumType = array[startingIndex++];
  101. let frustum;
  102. if (frustumType === PERSPECTIVE) {
  103. frustum = FrustumGeometry.PerspectiveFrustum.unpack(
  104. array,
  105. startingIndex,
  106. scratchPackPerspective
  107. );
  108. startingIndex += FrustumGeometry.PerspectiveFrustum.packedLength;
  109. } else {
  110. frustum = FrustumGeometry.OrthographicFrustum.unpack(
  111. array,
  112. startingIndex,
  113. scratchPackOrthographic
  114. );
  115. startingIndex += FrustumGeometry.OrthographicFrustum.packedLength;
  116. }
  117. const origin = Matrix2.Cartesian3.unpack(array, startingIndex, scratchPackorigin);
  118. startingIndex += Matrix2.Cartesian3.packedLength;
  119. const orientation = Transforms.Quaternion.unpack(
  120. array,
  121. startingIndex,
  122. scratchPackQuaternion
  123. );
  124. startingIndex += Transforms.Quaternion.packedLength;
  125. const drawNearPlane = array[startingIndex] === 1.0;
  126. if (!defaultValue.defined(result)) {
  127. return new FrustumOutlineGeometry({
  128. frustum: frustum,
  129. origin: origin,
  130. orientation: orientation,
  131. _drawNearPlane: drawNearPlane,
  132. });
  133. }
  134. const frustumResult =
  135. frustumType === result._frustumType ? result._frustum : undefined;
  136. result._frustum = frustum.clone(frustumResult);
  137. result._frustumType = frustumType;
  138. result._origin = Matrix2.Cartesian3.clone(origin, result._origin);
  139. result._orientation = Transforms.Quaternion.clone(orientation, result._orientation);
  140. result._drawNearPlane = drawNearPlane;
  141. return result;
  142. };
  143. /**
  144. * Computes the geometric representation of a frustum outline, including its vertices, indices, and a bounding sphere.
  145. *
  146. * @param {FrustumOutlineGeometry} frustumGeometry A description of the frustum.
  147. * @returns {Geometry|undefined} The computed vertices and indices.
  148. */
  149. FrustumOutlineGeometry.createGeometry = function (frustumGeometry) {
  150. const frustumType = frustumGeometry._frustumType;
  151. const frustum = frustumGeometry._frustum;
  152. const origin = frustumGeometry._origin;
  153. const orientation = frustumGeometry._orientation;
  154. const drawNearPlane = frustumGeometry._drawNearPlane;
  155. const positions = new Float64Array(3 * 4 * 2);
  156. FrustumGeometry.FrustumGeometry._computeNearFarPlanes(
  157. origin,
  158. orientation,
  159. frustumType,
  160. frustum,
  161. positions
  162. );
  163. const attributes = new GeometryAttributes.GeometryAttributes({
  164. position: new GeometryAttribute.GeometryAttribute({
  165. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  166. componentsPerAttribute: 3,
  167. values: positions,
  168. }),
  169. });
  170. let offset;
  171. let index;
  172. const numberOfPlanes = drawNearPlane ? 2 : 1;
  173. const indices = new Uint16Array(8 * (numberOfPlanes + 1));
  174. // Build the near/far planes
  175. let i = drawNearPlane ? 0 : 1;
  176. for (; i < 2; ++i) {
  177. offset = drawNearPlane ? i * 8 : 0;
  178. index = i * 4;
  179. indices[offset] = index;
  180. indices[offset + 1] = index + 1;
  181. indices[offset + 2] = index + 1;
  182. indices[offset + 3] = index + 2;
  183. indices[offset + 4] = index + 2;
  184. indices[offset + 5] = index + 3;
  185. indices[offset + 6] = index + 3;
  186. indices[offset + 7] = index;
  187. }
  188. // Build the sides of the frustums
  189. for (i = 0; i < 2; ++i) {
  190. offset = (numberOfPlanes + i) * 8;
  191. index = i * 4;
  192. indices[offset] = index;
  193. indices[offset + 1] = index + 4;
  194. indices[offset + 2] = index + 1;
  195. indices[offset + 3] = index + 5;
  196. indices[offset + 4] = index + 2;
  197. indices[offset + 5] = index + 6;
  198. indices[offset + 6] = index + 3;
  199. indices[offset + 7] = index + 7;
  200. }
  201. return new GeometryAttribute.Geometry({
  202. attributes: attributes,
  203. indices: indices,
  204. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  205. boundingSphere: Transforms.BoundingSphere.fromVertices(positions),
  206. });
  207. };
  208. function createFrustumOutlineGeometry(frustumGeometry, offset) {
  209. if (defaultValue.defined(offset)) {
  210. frustumGeometry = FrustumOutlineGeometry.unpack(frustumGeometry, offset);
  211. }
  212. return FrustumOutlineGeometry.createGeometry(frustumGeometry);
  213. }
  214. return createFrustumOutlineGeometry;
  215. }));