FrustumOutlineGeometry.js 9.0 KB

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