FrustumGeometry.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. import BoundingSphere from './BoundingSphere.js';
  2. import Cartesian3 from './Cartesian3.js';
  3. import Cartesian4 from './Cartesian4.js';
  4. import Check from './Check.js';
  5. import ComponentDatatype from './ComponentDatatype.js';
  6. import defaultValue from './defaultValue.js';
  7. import defined from './defined.js';
  8. import Geometry from './Geometry.js';
  9. import GeometryAttribute from './GeometryAttribute.js';
  10. import GeometryAttributes from './GeometryAttributes.js';
  11. import Matrix3 from './Matrix3.js';
  12. import Matrix4 from './Matrix4.js';
  13. import OrthographicFrustum from './OrthographicFrustum.js';
  14. import PerspectiveFrustum from './PerspectiveFrustum.js';
  15. import PrimitiveType from './PrimitiveType.js';
  16. import Quaternion from './Quaternion.js';
  17. import VertexFormat from './VertexFormat.js';
  18. var PERSPECTIVE = 0;
  19. var ORTHOGRAPHIC = 1;
  20. /**
  21. * Describes a frustum at the given the origin and orientation.
  22. *
  23. * @alias FrustumGeometry
  24. * @constructor
  25. *
  26. * @param {Object} options Object with the following properties:
  27. * @param {PerspectiveFrustum|OrthographicFrustum} options.frustum The frustum.
  28. * @param {Cartesian3} options.origin The origin of the frustum.
  29. * @param {Quaternion} options.orientation The orientation of the frustum.
  30. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  31. */
  32. function FrustumGeometry(options) {
  33. //>>includeStart('debug', pragmas.debug);
  34. Check.typeOf.object('options', options);
  35. Check.typeOf.object('options.frustum', options.frustum);
  36. Check.typeOf.object('options.origin', options.origin);
  37. Check.typeOf.object('options.orientation', options.orientation);
  38. //>>includeEnd('debug');
  39. var frustum = options.frustum;
  40. var orientation = options.orientation;
  41. var origin = options.origin;
  42. var vertexFormat = defaultValue(options.vertexFormat, VertexFormat.DEFAULT);
  43. // This is private because it is used by DebugCameraPrimitive to draw a multi-frustum by
  44. // creating multiple FrustumGeometrys. This way the near plane of one frustum doesn't overlap
  45. // the far plane of another.
  46. var drawNearPlane = defaultValue(options._drawNearPlane, true);
  47. var frustumType;
  48. var frustumPackedLength;
  49. if (frustum instanceof PerspectiveFrustum) {
  50. frustumType = PERSPECTIVE;
  51. frustumPackedLength = PerspectiveFrustum.packedLength;
  52. } else if (frustum instanceof OrthographicFrustum) {
  53. frustumType = ORTHOGRAPHIC;
  54. frustumPackedLength = OrthographicFrustum.packedLength;
  55. }
  56. this._frustumType = frustumType;
  57. this._frustum = frustum.clone();
  58. this._origin = Cartesian3.clone(origin);
  59. this._orientation = Quaternion.clone(orientation);
  60. this._drawNearPlane = drawNearPlane;
  61. this._vertexFormat = vertexFormat;
  62. this._workerName = 'createFrustumGeometry';
  63. /**
  64. * The number of elements used to pack the object into an array.
  65. * @type {Number}
  66. */
  67. this.packedLength = 2 + frustumPackedLength + Cartesian3.packedLength + Quaternion.packedLength + VertexFormat.packedLength;
  68. }
  69. /**
  70. * Stores the provided instance into the provided array.
  71. *
  72. * @param {FrustumGeometry} value The value to pack.
  73. * @param {Number[]} array The array to pack into.
  74. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  75. *
  76. * @returns {Number[]} The array that was packed into
  77. */
  78. FrustumGeometry.pack = function(value, array, startingIndex) {
  79. //>>includeStart('debug', pragmas.debug);
  80. Check.typeOf.object('value', value);
  81. Check.defined('array', array);
  82. //>>includeEnd('debug');
  83. startingIndex = defaultValue(startingIndex, 0);
  84. var frustumType = value._frustumType;
  85. var frustum = value._frustum;
  86. array[startingIndex++] = frustumType;
  87. if (frustumType === PERSPECTIVE) {
  88. PerspectiveFrustum.pack(frustum, array, startingIndex);
  89. startingIndex += PerspectiveFrustum.packedLength;
  90. } else {
  91. OrthographicFrustum.pack(frustum, array, startingIndex);
  92. startingIndex += OrthographicFrustum.packedLength;
  93. }
  94. Cartesian3.pack(value._origin, array, startingIndex);
  95. startingIndex += Cartesian3.packedLength;
  96. Quaternion.pack(value._orientation, array, startingIndex);
  97. startingIndex += Quaternion.packedLength;
  98. VertexFormat.pack(value._vertexFormat, array, startingIndex);
  99. startingIndex += VertexFormat.packedLength;
  100. array[startingIndex] = value._drawNearPlane ? 1.0 : 0.0;
  101. return array;
  102. };
  103. var scratchPackPerspective = new PerspectiveFrustum();
  104. var scratchPackOrthographic = new OrthographicFrustum();
  105. var scratchPackQuaternion = new Quaternion();
  106. var scratchPackorigin = new Cartesian3();
  107. var scratchVertexFormat = new VertexFormat();
  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 {FrustumGeometry} [result] The object into which to store the result.
  114. */
  115. FrustumGeometry.unpack = function(array, startingIndex, result) {
  116. //>>includeStart('debug', pragmas.debug);
  117. Check.defined('array', array);
  118. //>>includeEnd('debug');
  119. startingIndex = defaultValue(startingIndex, 0);
  120. var frustumType = array[startingIndex++];
  121. var frustum;
  122. if (frustumType === PERSPECTIVE) {
  123. frustum = PerspectiveFrustum.unpack(array, startingIndex, scratchPackPerspective);
  124. startingIndex += PerspectiveFrustum.packedLength;
  125. } else {
  126. frustum = OrthographicFrustum.unpack(array, startingIndex, scratchPackOrthographic);
  127. startingIndex += OrthographicFrustum.packedLength;
  128. }
  129. var origin = Cartesian3.unpack(array, startingIndex, scratchPackorigin);
  130. startingIndex += Cartesian3.packedLength;
  131. var orientation = Quaternion.unpack(array, startingIndex, scratchPackQuaternion);
  132. startingIndex += Quaternion.packedLength;
  133. var vertexFormat = VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  134. startingIndex += VertexFormat.packedLength;
  135. var drawNearPlane = array[startingIndex] === 1.0;
  136. if (!defined(result)) {
  137. return new FrustumGeometry({
  138. frustum : frustum,
  139. origin : origin,
  140. orientation : orientation,
  141. vertexFormat : vertexFormat,
  142. _drawNearPlane : drawNearPlane
  143. });
  144. }
  145. var frustumResult = frustumType === result._frustumType ? result._frustum : undefined;
  146. result._frustum = frustum.clone(frustumResult);
  147. result._frustumType = frustumType;
  148. result._origin = Cartesian3.clone(origin, result._origin);
  149. result._orientation = Quaternion.clone(orientation, result._orientation);
  150. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  151. result._drawNearPlane = drawNearPlane;
  152. return result;
  153. };
  154. function getAttributes(offset, normals, tangents, bitangents, st, normal, tangent, bitangent) {
  155. var stOffset = offset / 3 * 2;
  156. for (var i = 0; i < 4; ++i) {
  157. if (defined(normals)) {
  158. normals[offset] = normal.x;
  159. normals[offset + 1] = normal.y;
  160. normals[offset + 2] = normal.z;
  161. }
  162. if (defined(tangents)) {
  163. tangents[offset] = tangent.x;
  164. tangents[offset + 1] = tangent.y;
  165. tangents[offset + 2] = tangent.z;
  166. }
  167. if (defined(bitangents)) {
  168. bitangents[offset] = bitangent.x;
  169. bitangents[offset + 1] = bitangent.y;
  170. bitangents[offset + 2] = bitangent.z;
  171. }
  172. offset += 3;
  173. }
  174. st[stOffset] = 0.0;
  175. st[stOffset + 1] = 0.0;
  176. st[stOffset + 2] = 1.0;
  177. st[stOffset + 3] = 0.0;
  178. st[stOffset + 4] = 1.0;
  179. st[stOffset + 5] = 1.0;
  180. st[stOffset + 6] = 0.0;
  181. st[stOffset + 7] = 1.0;
  182. }
  183. var scratchRotationMatrix = new Matrix3();
  184. var scratchViewMatrix = new Matrix4();
  185. var scratchInverseMatrix = new Matrix4();
  186. var scratchXDirection = new Cartesian3();
  187. var scratchYDirection = new Cartesian3();
  188. var scratchZDirection = new Cartesian3();
  189. var scratchNegativeX = new Cartesian3();
  190. var scratchNegativeY = new Cartesian3();
  191. var scratchNegativeZ = new Cartesian3();
  192. var frustumSplits = new Array(3);
  193. var frustumCornersNDC = new Array(4);
  194. frustumCornersNDC[0] = new Cartesian4(-1.0, -1.0, 1.0, 1.0);
  195. frustumCornersNDC[1] = new Cartesian4(1.0, -1.0, 1.0, 1.0);
  196. frustumCornersNDC[2] = new Cartesian4(1.0, 1.0, 1.0, 1.0);
  197. frustumCornersNDC[3] = new Cartesian4(-1.0, 1.0, 1.0, 1.0);
  198. var scratchFrustumCorners = new Array(4);
  199. for (var i = 0; i < 4; ++i) {
  200. scratchFrustumCorners[i] = new Cartesian4();
  201. }
  202. FrustumGeometry._computeNearFarPlanes = function(origin, orientation, frustumType, frustum, positions, xDirection, yDirection, zDirection) {
  203. var rotationMatrix = Matrix3.fromQuaternion(orientation, scratchRotationMatrix);
  204. var x = defaultValue(xDirection, scratchXDirection);
  205. var y = defaultValue(yDirection, scratchYDirection);
  206. var z = defaultValue(zDirection, scratchZDirection);
  207. x = Matrix3.getColumn(rotationMatrix, 0, x);
  208. y = Matrix3.getColumn(rotationMatrix, 1, y);
  209. z = Matrix3.getColumn(rotationMatrix, 2, z);
  210. Cartesian3.normalize(x, x);
  211. Cartesian3.normalize(y, y);
  212. Cartesian3.normalize(z, z);
  213. Cartesian3.negate(x, x);
  214. var view = Matrix4.computeView(origin, z, y, x, scratchViewMatrix);
  215. var inverseView;
  216. var inverseViewProjection;
  217. if (frustumType === PERSPECTIVE) {
  218. var projection = frustum.projectionMatrix;
  219. var viewProjection = Matrix4.multiply(projection, view, scratchInverseMatrix);
  220. inverseViewProjection = Matrix4.inverse(viewProjection, scratchInverseMatrix);
  221. } else {
  222. inverseView = Matrix4.inverseTransformation(view, scratchInverseMatrix);
  223. }
  224. if (defined(inverseViewProjection)) {
  225. frustumSplits[0] = frustum.near;
  226. frustumSplits[1] = frustum.far;
  227. } else {
  228. frustumSplits[0] = 0.0;
  229. frustumSplits[1] = frustum.near;
  230. frustumSplits[2] = frustum.far;
  231. }
  232. for (var i = 0; i < 2; ++i) {
  233. for (var j = 0; j < 4; ++j) {
  234. var corner = Cartesian4.clone(frustumCornersNDC[j], scratchFrustumCorners[j]);
  235. if (!defined(inverseViewProjection)) {
  236. if (defined(frustum._offCenterFrustum)) {
  237. frustum = frustum._offCenterFrustum;
  238. }
  239. var near = frustumSplits[i];
  240. var far = frustumSplits[i + 1];
  241. corner.x = (corner.x * (frustum.right - frustum.left) + frustum.left + frustum.right) * 0.5;
  242. corner.y = (corner.y * (frustum.top - frustum.bottom) + frustum.bottom + frustum.top) * 0.5;
  243. corner.z = (corner.z * (near - far) - near - far) * 0.5;
  244. corner.w = 1.0;
  245. Matrix4.multiplyByVector(inverseView, corner, corner);
  246. } else {
  247. corner = Matrix4.multiplyByVector(inverseViewProjection, corner, corner);
  248. // Reverse perspective divide
  249. var w = 1.0 / corner.w;
  250. Cartesian3.multiplyByScalar(corner, w, corner);
  251. Cartesian3.subtract(corner, origin, corner);
  252. Cartesian3.normalize(corner, corner);
  253. var fac = Cartesian3.dot(z, corner);
  254. Cartesian3.multiplyByScalar(corner, frustumSplits[i] / fac, corner);
  255. Cartesian3.add(corner, origin, corner);
  256. }
  257. positions[12 * i + j * 3] = corner.x;
  258. positions[12 * i + j * 3 + 1] = corner.y;
  259. positions[12 * i + j * 3 + 2] = corner.z;
  260. }
  261. }
  262. };
  263. /**
  264. * Computes the geometric representation of a frustum, including its vertices, indices, and a bounding sphere.
  265. *
  266. * @param {FrustumGeometry} frustumGeometry A description of the frustum.
  267. * @returns {Geometry|undefined} The computed vertices and indices.
  268. */
  269. FrustumGeometry.createGeometry = function(frustumGeometry) {
  270. var frustumType = frustumGeometry._frustumType;
  271. var frustum = frustumGeometry._frustum;
  272. var origin = frustumGeometry._origin;
  273. var orientation = frustumGeometry._orientation;
  274. var drawNearPlane = frustumGeometry._drawNearPlane;
  275. var vertexFormat = frustumGeometry._vertexFormat;
  276. var numberOfPlanes = drawNearPlane ? 6 : 5;
  277. var positions = new Float64Array(3 * 4 * 6);
  278. FrustumGeometry._computeNearFarPlanes(origin, orientation, frustumType, frustum, positions);
  279. // -x plane
  280. var offset = 3 * 4 * 2;
  281. positions[offset] = positions[3 * 4];
  282. positions[offset + 1] = positions[3 * 4 + 1];
  283. positions[offset + 2] = positions[3 * 4 + 2];
  284. positions[offset + 3] = positions[0];
  285. positions[offset + 4] = positions[1];
  286. positions[offset + 5] = positions[2];
  287. positions[offset + 6] = positions[3 * 3];
  288. positions[offset + 7] = positions[3 * 3 + 1];
  289. positions[offset + 8] = positions[3 * 3 + 2];
  290. positions[offset + 9] = positions[3 * 7];
  291. positions[offset + 10] = positions[3 * 7 + 1];
  292. positions[offset + 11] = positions[3 * 7 + 2];
  293. // -y plane
  294. offset += 3 * 4;
  295. positions[offset] = positions[3 * 5];
  296. positions[offset + 1] = positions[3 * 5 + 1];
  297. positions[offset + 2] = positions[3 * 5 + 2];
  298. positions[offset + 3] = positions[3];
  299. positions[offset + 4] = positions[3 + 1];
  300. positions[offset + 5] = positions[3 + 2];
  301. positions[offset + 6] = positions[0];
  302. positions[offset + 7] = positions[1];
  303. positions[offset + 8] = positions[2];
  304. positions[offset + 9] = positions[3 * 4];
  305. positions[offset + 10] = positions[3 * 4 + 1];
  306. positions[offset + 11] = positions[3 * 4 + 2];
  307. // +x plane
  308. offset += 3 * 4;
  309. positions[offset] = positions[3];
  310. positions[offset + 1] = positions[3 + 1];
  311. positions[offset + 2] = positions[3 + 2];
  312. positions[offset + 3] = positions[3 * 5];
  313. positions[offset + 4] = positions[3 * 5 + 1];
  314. positions[offset + 5] = positions[3 * 5 + 2];
  315. positions[offset + 6] = positions[3 * 6];
  316. positions[offset + 7] = positions[3 * 6 + 1];
  317. positions[offset + 8] = positions[3 * 6 + 2];
  318. positions[offset + 9] = positions[3 * 2];
  319. positions[offset + 10] = positions[3 * 2 + 1];
  320. positions[offset + 11] = positions[3 * 2 + 2];
  321. // +y plane
  322. offset += 3 * 4;
  323. positions[offset] = positions[3 * 2];
  324. positions[offset + 1] = positions[3 * 2 + 1];
  325. positions[offset + 2] = positions[3 * 2 + 2];
  326. positions[offset + 3] = positions[3 * 6];
  327. positions[offset + 4] = positions[3 * 6 + 1];
  328. positions[offset + 5] = positions[3 * 6 + 2];
  329. positions[offset + 6] = positions[3 * 7];
  330. positions[offset + 7] = positions[3 * 7 + 1];
  331. positions[offset + 8] = positions[3 * 7 + 2];
  332. positions[offset + 9] = positions[3 * 3];
  333. positions[offset + 10] = positions[3 * 3 + 1];
  334. positions[offset + 11] = positions[3 * 3 + 2];
  335. if (!drawNearPlane) {
  336. positions = positions.subarray(3 * 4);
  337. }
  338. var attributes = new GeometryAttributes({
  339. position : new GeometryAttribute({
  340. componentDatatype : ComponentDatatype.DOUBLE,
  341. componentsPerAttribute : 3,
  342. values : positions
  343. })
  344. });
  345. if (defined(vertexFormat.normal) || defined(vertexFormat.tangent) || defined(vertexFormat.bitangent) || defined(vertexFormat.st)) {
  346. var normals = defined(vertexFormat.normal) ? new Float32Array(3 * 4 * numberOfPlanes) : undefined;
  347. var tangents = defined(vertexFormat.tangent) ? new Float32Array(3 * 4 * numberOfPlanes) : undefined;
  348. var bitangents = defined(vertexFormat.bitangent) ? new Float32Array(3 * 4 * numberOfPlanes) : undefined;
  349. var st = defined(vertexFormat.st) ? new Float32Array(2 * 4 * numberOfPlanes) : undefined;
  350. var x = scratchXDirection;
  351. var y = scratchYDirection;
  352. var z = scratchZDirection;
  353. var negativeX = Cartesian3.negate(x, scratchNegativeX);
  354. var negativeY = Cartesian3.negate(y, scratchNegativeY);
  355. var negativeZ = Cartesian3.negate(z, scratchNegativeZ);
  356. offset = 0;
  357. if (drawNearPlane) {
  358. getAttributes(offset, normals, tangents, bitangents, st, negativeZ, x, y); // near
  359. offset += 3 * 4;
  360. }
  361. getAttributes(offset, normals, tangents, bitangents, st, z, negativeX, y); // far
  362. offset += 3 * 4;
  363. getAttributes(offset, normals, tangents, bitangents, st, negativeX, negativeZ, y); // -x
  364. offset += 3 * 4;
  365. getAttributes(offset, normals, tangents, bitangents, st, negativeY, negativeZ, negativeX); // -y
  366. offset += 3 * 4;
  367. getAttributes(offset, normals, tangents, bitangents, st, x, z, y); // +x
  368. offset += 3 * 4;
  369. getAttributes(offset, normals, tangents, bitangents, st, y, z, negativeX); // +y
  370. if (defined(normals)) {
  371. attributes.normal = new GeometryAttribute({
  372. componentDatatype : ComponentDatatype.FLOAT,
  373. componentsPerAttribute : 3,
  374. values : normals
  375. });
  376. }
  377. if (defined(tangents)) {
  378. attributes.tangent = new GeometryAttribute({
  379. componentDatatype : ComponentDatatype.FLOAT,
  380. componentsPerAttribute : 3,
  381. values : tangents
  382. });
  383. }
  384. if (defined(bitangents)) {
  385. attributes.bitangent = new GeometryAttribute({
  386. componentDatatype : ComponentDatatype.FLOAT,
  387. componentsPerAttribute : 3,
  388. values : bitangents
  389. });
  390. }
  391. if (defined(st)) {
  392. attributes.st = new GeometryAttribute({
  393. componentDatatype : ComponentDatatype.FLOAT,
  394. componentsPerAttribute : 2,
  395. values : st
  396. });
  397. }
  398. }
  399. var indices = new Uint16Array(6 * numberOfPlanes);
  400. for (var i = 0; i < numberOfPlanes; ++i) {
  401. var indexOffset = i * 6;
  402. var index = i * 4;
  403. indices[indexOffset] = index;
  404. indices[indexOffset + 1] = index + 1;
  405. indices[indexOffset + 2] = index + 2;
  406. indices[indexOffset + 3] = index;
  407. indices[indexOffset + 4] = index + 2;
  408. indices[indexOffset + 5] = index + 3;
  409. }
  410. return new Geometry({
  411. attributes : attributes,
  412. indices : indices,
  413. primitiveType : PrimitiveType.TRIANGLES,
  414. boundingSphere : BoundingSphere.fromVertices(positions)
  415. });
  416. };
  417. export default FrustumGeometry;