createFrustumOutlineGeometry.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['./defaultValue-a6eb9f34', './Transforms-c78c4637', './Matrix2-ab676047', './RuntimeError-1088cc64', './ComponentDatatype-e06f4e16', './FrustumGeometry-947bc56f', './GeometryAttribute-4f02e2ad', './GeometryAttributes-aff51037', './_commonjsHelpers-89c9b271', './combine-7cf28d88', './WebGLConstants-d81b330d', './Plane-c985a1d2', './VertexFormat-65fd4be5'], (function (defaultValue, Transforms, Matrix2, RuntimeError, ComponentDatatype, FrustumGeometry, GeometryAttribute, GeometryAttributes, _commonjsHelpers, combine, WebGLConstants, Plane, VertexFormat) { 'use strict';
  26. const PERSPECTIVE = 0;
  27. const ORTHOGRAPHIC = 1;
  28. /**
  29. * A description of the outline of a frustum with the given the origin and orientation.
  30. *
  31. * @alias FrustumOutlineGeometry
  32. * @constructor
  33. *
  34. * @param {Object} options Object with the following properties:
  35. * @param {PerspectiveFrustum|OrthographicFrustum} options.frustum The frustum.
  36. * @param {Cartesian3} options.origin The origin of the frustum.
  37. * @param {Quaternion} options.orientation The orientation of the frustum.
  38. */
  39. function FrustumOutlineGeometry(options) {
  40. //>>includeStart('debug', pragmas.debug);
  41. RuntimeError.Check.typeOf.object("options", options);
  42. RuntimeError.Check.typeOf.object("options.frustum", options.frustum);
  43. RuntimeError.Check.typeOf.object("options.origin", options.origin);
  44. RuntimeError.Check.typeOf.object("options.orientation", options.orientation);
  45. //>>includeEnd('debug');
  46. const frustum = options.frustum;
  47. const orientation = options.orientation;
  48. const origin = options.origin;
  49. // This is private because it is used by DebugCameraPrimitive to draw a multi-frustum by
  50. // creating multiple FrustumOutlineGeometrys. This way the near plane of one frustum doesn't overlap
  51. // the far plane of another.
  52. const drawNearPlane = defaultValue.defaultValue(options._drawNearPlane, true);
  53. let frustumType;
  54. let frustumPackedLength;
  55. if (frustum instanceof FrustumGeometry.PerspectiveFrustum) {
  56. frustumType = PERSPECTIVE;
  57. frustumPackedLength = FrustumGeometry.PerspectiveFrustum.packedLength;
  58. } else if (frustum instanceof FrustumGeometry.OrthographicFrustum) {
  59. frustumType = ORTHOGRAPHIC;
  60. frustumPackedLength = FrustumGeometry.OrthographicFrustum.packedLength;
  61. }
  62. this._frustumType = frustumType;
  63. this._frustum = frustum.clone();
  64. this._origin = Matrix2.Cartesian3.clone(origin);
  65. this._orientation = Transforms.Quaternion.clone(orientation);
  66. this._drawNearPlane = drawNearPlane;
  67. this._workerName = "createFrustumOutlineGeometry";
  68. /**
  69. * The number of elements used to pack the object into an array.
  70. * @type {Number}
  71. */
  72. this.packedLength =
  73. 2 + frustumPackedLength + Matrix2.Cartesian3.packedLength + Transforms.Quaternion.packedLength;
  74. }
  75. /**
  76. * Stores the provided instance into the provided array.
  77. *
  78. * @param {FrustumOutlineGeometry} value The value to pack.
  79. * @param {Number[]} array The array to pack into.
  80. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  81. *
  82. * @returns {Number[]} The array that was packed into
  83. */
  84. FrustumOutlineGeometry.pack = function (value, array, startingIndex) {
  85. //>>includeStart('debug', pragmas.debug);
  86. RuntimeError.Check.typeOf.object("value", value);
  87. RuntimeError.Check.defined("array", array);
  88. //>>includeEnd('debug');
  89. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  90. const frustumType = value._frustumType;
  91. const frustum = value._frustum;
  92. array[startingIndex++] = frustumType;
  93. if (frustumType === PERSPECTIVE) {
  94. FrustumGeometry.PerspectiveFrustum.pack(frustum, array, startingIndex);
  95. startingIndex += FrustumGeometry.PerspectiveFrustum.packedLength;
  96. } else {
  97. FrustumGeometry.OrthographicFrustum.pack(frustum, array, startingIndex);
  98. startingIndex += FrustumGeometry.OrthographicFrustum.packedLength;
  99. }
  100. Matrix2.Cartesian3.pack(value._origin, array, startingIndex);
  101. startingIndex += Matrix2.Cartesian3.packedLength;
  102. Transforms.Quaternion.pack(value._orientation, array, startingIndex);
  103. startingIndex += Transforms.Quaternion.packedLength;
  104. array[startingIndex] = value._drawNearPlane ? 1.0 : 0.0;
  105. return array;
  106. };
  107. const scratchPackPerspective = new FrustumGeometry.PerspectiveFrustum();
  108. const scratchPackOrthographic = new FrustumGeometry.OrthographicFrustum();
  109. const scratchPackQuaternion = new Transforms.Quaternion();
  110. const scratchPackorigin = new Matrix2.Cartesian3();
  111. /**
  112. * Retrieves an instance from a packed array.
  113. *
  114. * @param {Number[]} array The packed array.
  115. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  116. * @param {FrustumOutlineGeometry} [result] The object into which to store the result.
  117. */
  118. FrustumOutlineGeometry.unpack = function (array, startingIndex, result) {
  119. //>>includeStart('debug', pragmas.debug);
  120. RuntimeError.Check.defined("array", array);
  121. //>>includeEnd('debug');
  122. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  123. const frustumType = array[startingIndex++];
  124. let frustum;
  125. if (frustumType === PERSPECTIVE) {
  126. frustum = FrustumGeometry.PerspectiveFrustum.unpack(
  127. array,
  128. startingIndex,
  129. scratchPackPerspective
  130. );
  131. startingIndex += FrustumGeometry.PerspectiveFrustum.packedLength;
  132. } else {
  133. frustum = FrustumGeometry.OrthographicFrustum.unpack(
  134. array,
  135. startingIndex,
  136. scratchPackOrthographic
  137. );
  138. startingIndex += FrustumGeometry.OrthographicFrustum.packedLength;
  139. }
  140. const origin = Matrix2.Cartesian3.unpack(array, startingIndex, scratchPackorigin);
  141. startingIndex += Matrix2.Cartesian3.packedLength;
  142. const orientation = Transforms.Quaternion.unpack(
  143. array,
  144. startingIndex,
  145. scratchPackQuaternion
  146. );
  147. startingIndex += Transforms.Quaternion.packedLength;
  148. const drawNearPlane = array[startingIndex] === 1.0;
  149. if (!defaultValue.defined(result)) {
  150. return new FrustumOutlineGeometry({
  151. frustum: frustum,
  152. origin: origin,
  153. orientation: orientation,
  154. _drawNearPlane: drawNearPlane,
  155. });
  156. }
  157. const frustumResult =
  158. frustumType === result._frustumType ? result._frustum : undefined;
  159. result._frustum = frustum.clone(frustumResult);
  160. result._frustumType = frustumType;
  161. result._origin = Matrix2.Cartesian3.clone(origin, result._origin);
  162. result._orientation = Transforms.Quaternion.clone(orientation, result._orientation);
  163. result._drawNearPlane = drawNearPlane;
  164. return result;
  165. };
  166. /**
  167. * Computes the geometric representation of a frustum outline, including its vertices, indices, and a bounding sphere.
  168. *
  169. * @param {FrustumOutlineGeometry} frustumGeometry A description of the frustum.
  170. * @returns {Geometry|undefined} The computed vertices and indices.
  171. */
  172. FrustumOutlineGeometry.createGeometry = function (frustumGeometry) {
  173. const frustumType = frustumGeometry._frustumType;
  174. const frustum = frustumGeometry._frustum;
  175. const origin = frustumGeometry._origin;
  176. const orientation = frustumGeometry._orientation;
  177. const drawNearPlane = frustumGeometry._drawNearPlane;
  178. const positions = new Float64Array(3 * 4 * 2);
  179. FrustumGeometry.FrustumGeometry._computeNearFarPlanes(
  180. origin,
  181. orientation,
  182. frustumType,
  183. frustum,
  184. positions
  185. );
  186. const attributes = new GeometryAttributes.GeometryAttributes({
  187. position: new GeometryAttribute.GeometryAttribute({
  188. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  189. componentsPerAttribute: 3,
  190. values: positions,
  191. }),
  192. });
  193. let offset;
  194. let index;
  195. const numberOfPlanes = drawNearPlane ? 2 : 1;
  196. const indices = new Uint16Array(8 * (numberOfPlanes + 1));
  197. // Build the near/far planes
  198. let i = drawNearPlane ? 0 : 1;
  199. for (; i < 2; ++i) {
  200. offset = drawNearPlane ? i * 8 : 0;
  201. index = i * 4;
  202. indices[offset] = index;
  203. indices[offset + 1] = index + 1;
  204. indices[offset + 2] = index + 1;
  205. indices[offset + 3] = index + 2;
  206. indices[offset + 4] = index + 2;
  207. indices[offset + 5] = index + 3;
  208. indices[offset + 6] = index + 3;
  209. indices[offset + 7] = index;
  210. }
  211. // Build the sides of the frustums
  212. for (i = 0; i < 2; ++i) {
  213. offset = (numberOfPlanes + i) * 8;
  214. index = i * 4;
  215. indices[offset] = index;
  216. indices[offset + 1] = index + 4;
  217. indices[offset + 2] = index + 1;
  218. indices[offset + 3] = index + 5;
  219. indices[offset + 4] = index + 2;
  220. indices[offset + 5] = index + 6;
  221. indices[offset + 6] = index + 3;
  222. indices[offset + 7] = index + 7;
  223. }
  224. return new GeometryAttribute.Geometry({
  225. attributes: attributes,
  226. indices: indices,
  227. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  228. boundingSphere: Transforms.BoundingSphere.fromVertices(positions),
  229. });
  230. };
  231. function createFrustumOutlineGeometry(frustumGeometry, offset) {
  232. if (defaultValue.defined(offset)) {
  233. frustumGeometry = FrustumOutlineGeometry.unpack(frustumGeometry, offset);
  234. }
  235. return FrustumOutlineGeometry.createGeometry(frustumGeometry);
  236. }
  237. return createFrustumOutlineGeometry;
  238. }));