createFrustumOutlineGeometry.js 9.2 KB

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