createFrustumOutlineGeometry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defined-26bd4a03', './Check-da037458', './freezeObject-2d83f591', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './defineProperties-6f7a50f2', './Transforms-65aba0a4', './RuntimeError-ad75c885', './WebGLConstants-497deb20', './ComponentDatatype-69643096', './GeometryAttribute-ed359d71', './when-ee12a2cb', './GeometryAttributes-eecc9f43', './Plane-a1a3fd52', './VertexFormat-fbb91dc7', './FrustumGeometry-93211505'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, when, GeometryAttributes, Plane, VertexFormat, FrustumGeometry) { 'use strict';
  3. var PERSPECTIVE = 0;
  4. var 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. Check.Check.typeOf.object('options', options);
  19. Check.Check.typeOf.object('options.frustum', options.frustum);
  20. Check.Check.typeOf.object('options.origin', options.origin);
  21. Check.Check.typeOf.object('options.orientation', options.orientation);
  22. //>>includeEnd('debug');
  23. var frustum = options.frustum;
  24. var orientation = options.orientation;
  25. var 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. var drawNearPlane = defaultValue.defaultValue(options._drawNearPlane, true);
  30. var frustumType;
  31. var 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 = Cartesian2.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 = 2 + frustumPackedLength + Cartesian2.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. var frustumType = value._frustumType;
  67. var 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. Cartesian2.Cartesian3.pack(value._origin, array, startingIndex);
  77. startingIndex += Cartesian2.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. var scratchPackPerspective = new FrustumGeometry.PerspectiveFrustum();
  84. var scratchPackOrthographic = new FrustumGeometry.OrthographicFrustum();
  85. var scratchPackQuaternion = new Transforms.Quaternion();
  86. var scratchPackorigin = new Cartesian2.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. var frustumType = array[startingIndex++];
  100. var frustum;
  101. if (frustumType === PERSPECTIVE) {
  102. frustum = FrustumGeometry.PerspectiveFrustum.unpack(array, startingIndex, scratchPackPerspective);
  103. startingIndex += FrustumGeometry.PerspectiveFrustum.packedLength;
  104. } else {
  105. frustum = FrustumGeometry.OrthographicFrustum.unpack(array, startingIndex, scratchPackOrthographic);
  106. startingIndex += FrustumGeometry.OrthographicFrustum.packedLength;
  107. }
  108. var origin = Cartesian2.Cartesian3.unpack(array, startingIndex, scratchPackorigin);
  109. startingIndex += Cartesian2.Cartesian3.packedLength;
  110. var orientation = Transforms.Quaternion.unpack(array, startingIndex, scratchPackQuaternion);
  111. startingIndex += Transforms.Quaternion.packedLength;
  112. var drawNearPlane = array[startingIndex] === 1.0;
  113. if (!defined.defined(result)) {
  114. return new FrustumOutlineGeometry({
  115. frustum : frustum,
  116. origin : origin,
  117. orientation : orientation,
  118. _drawNearPlane : drawNearPlane
  119. });
  120. }
  121. var frustumResult = frustumType === result._frustumType ? result._frustum : undefined;
  122. result._frustum = frustum.clone(frustumResult);
  123. result._frustumType = frustumType;
  124. result._origin = Cartesian2.Cartesian3.clone(origin, result._origin);
  125. result._orientation = Transforms.Quaternion.clone(orientation, result._orientation);
  126. result._drawNearPlane = drawNearPlane;
  127. return result;
  128. };
  129. /**
  130. * Computes the geometric representation of a frustum outline, including its vertices, indices, and a bounding sphere.
  131. *
  132. * @param {FrustumOutlineGeometry} frustumGeometry A description of the frustum.
  133. * @returns {Geometry|undefined} The computed vertices and indices.
  134. */
  135. FrustumOutlineGeometry.createGeometry = function(frustumGeometry) {
  136. var frustumType = frustumGeometry._frustumType;
  137. var frustum = frustumGeometry._frustum;
  138. var origin = frustumGeometry._origin;
  139. var orientation = frustumGeometry._orientation;
  140. var drawNearPlane = frustumGeometry._drawNearPlane;
  141. var positions = new Float64Array(3 * 4 * 2);
  142. FrustumGeometry.FrustumGeometry._computeNearFarPlanes(origin, orientation, frustumType, frustum, positions);
  143. var attributes = new GeometryAttributes.GeometryAttributes({
  144. position : new GeometryAttribute.GeometryAttribute({
  145. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  146. componentsPerAttribute : 3,
  147. values : positions
  148. })
  149. });
  150. var offset;
  151. var index;
  152. var numberOfPlanes = drawNearPlane ? 2 : 1;
  153. var indices = new Uint16Array(8 * (numberOfPlanes + 1));
  154. // Build the near/far planes
  155. var i = drawNearPlane ? 0 : 1;
  156. for (; i < 2; ++i) {
  157. offset = drawNearPlane ? i * 8 : 0;
  158. index = i * 4;
  159. indices[offset] = index;
  160. indices[offset + 1] = index + 1;
  161. indices[offset + 2] = index + 1;
  162. indices[offset + 3] = index + 2;
  163. indices[offset + 4] = index + 2;
  164. indices[offset + 5] = index + 3;
  165. indices[offset + 6] = index + 3;
  166. indices[offset + 7] = index;
  167. }
  168. // Build the sides of the frustums
  169. for (i = 0; i < 2; ++i) {
  170. offset = (numberOfPlanes + i) * 8;
  171. index = i * 4;
  172. indices[offset] = index;
  173. indices[offset + 1] = index + 4;
  174. indices[offset + 2] = index + 1;
  175. indices[offset + 3] = index + 5;
  176. indices[offset + 4] = index + 2;
  177. indices[offset + 5] = index + 6;
  178. indices[offset + 6] = index + 3;
  179. indices[offset + 7] = index + 7;
  180. }
  181. return new GeometryAttribute.Geometry({
  182. attributes : attributes,
  183. indices : indices,
  184. primitiveType : GeometryAttribute.PrimitiveType.LINES,
  185. boundingSphere : Transforms.BoundingSphere.fromVertices(positions)
  186. });
  187. };
  188. function createFrustumOutlineGeometry(frustumGeometry, offset) {
  189. if (defined.defined(offset)) {
  190. frustumGeometry = FrustumOutlineGeometry.unpack(frustumGeometry, offset);
  191. }
  192. return FrustumOutlineGeometry.createGeometry(frustumGeometry);
  193. }
  194. return createFrustumOutlineGeometry;
  195. });