createFrustumOutlineGeometry.js 12 KB

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