FrustumGeometry.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. const PERSPECTIVE = 0;
  19. const 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. const frustum = options.frustum;
  40. const orientation = options.orientation;
  41. const origin = options.origin;
  42. const 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. const drawNearPlane = defaultValue(options._drawNearPlane, true);
  47. let frustumType;
  48. let 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 =
  68. 2 +
  69. frustumPackedLength +
  70. Cartesian3.packedLength +
  71. Quaternion.packedLength +
  72. VertexFormat.packedLength;
  73. }
  74. /**
  75. * Stores the provided instance into the provided array.
  76. *
  77. * @param {FrustumGeometry} value The value to pack.
  78. * @param {Number[]} array The array to pack into.
  79. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  80. *
  81. * @returns {Number[]} The array that was packed into
  82. */
  83. FrustumGeometry.pack = function (value, array, startingIndex) {
  84. //>>includeStart('debug', pragmas.debug);
  85. Check.typeOf.object("value", value);
  86. Check.defined("array", array);
  87. //>>includeEnd('debug');
  88. startingIndex = defaultValue(startingIndex, 0);
  89. const frustumType = value._frustumType;
  90. const frustum = value._frustum;
  91. array[startingIndex++] = frustumType;
  92. if (frustumType === PERSPECTIVE) {
  93. PerspectiveFrustum.pack(frustum, array, startingIndex);
  94. startingIndex += PerspectiveFrustum.packedLength;
  95. } else {
  96. OrthographicFrustum.pack(frustum, array, startingIndex);
  97. startingIndex += OrthographicFrustum.packedLength;
  98. }
  99. Cartesian3.pack(value._origin, array, startingIndex);
  100. startingIndex += Cartesian3.packedLength;
  101. Quaternion.pack(value._orientation, array, startingIndex);
  102. startingIndex += Quaternion.packedLength;
  103. VertexFormat.pack(value._vertexFormat, array, startingIndex);
  104. startingIndex += VertexFormat.packedLength;
  105. array[startingIndex] = value._drawNearPlane ? 1.0 : 0.0;
  106. return array;
  107. };
  108. const scratchPackPerspective = new PerspectiveFrustum();
  109. const scratchPackOrthographic = new OrthographicFrustum();
  110. const scratchPackQuaternion = new Quaternion();
  111. const scratchPackorigin = new Cartesian3();
  112. const scratchVertexFormat = new VertexFormat();
  113. /**
  114. * Retrieves an instance from a packed array.
  115. *
  116. * @param {Number[]} array The packed array.
  117. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  118. * @param {FrustumGeometry} [result] The object into which to store the result.
  119. */
  120. FrustumGeometry.unpack = function (array, startingIndex, result) {
  121. //>>includeStart('debug', pragmas.debug);
  122. Check.defined("array", array);
  123. //>>includeEnd('debug');
  124. startingIndex = defaultValue(startingIndex, 0);
  125. const frustumType = array[startingIndex++];
  126. let frustum;
  127. if (frustumType === PERSPECTIVE) {
  128. frustum = PerspectiveFrustum.unpack(
  129. array,
  130. startingIndex,
  131. scratchPackPerspective
  132. );
  133. startingIndex += PerspectiveFrustum.packedLength;
  134. } else {
  135. frustum = OrthographicFrustum.unpack(
  136. array,
  137. startingIndex,
  138. scratchPackOrthographic
  139. );
  140. startingIndex += OrthographicFrustum.packedLength;
  141. }
  142. const origin = Cartesian3.unpack(array, startingIndex, scratchPackorigin);
  143. startingIndex += Cartesian3.packedLength;
  144. const orientation = Quaternion.unpack(
  145. array,
  146. startingIndex,
  147. scratchPackQuaternion
  148. );
  149. startingIndex += Quaternion.packedLength;
  150. const vertexFormat = VertexFormat.unpack(
  151. array,
  152. startingIndex,
  153. scratchVertexFormat
  154. );
  155. startingIndex += VertexFormat.packedLength;
  156. const drawNearPlane = array[startingIndex] === 1.0;
  157. if (!defined(result)) {
  158. return new FrustumGeometry({
  159. frustum: frustum,
  160. origin: origin,
  161. orientation: orientation,
  162. vertexFormat: vertexFormat,
  163. _drawNearPlane: drawNearPlane,
  164. });
  165. }
  166. const frustumResult =
  167. frustumType === result._frustumType ? result._frustum : undefined;
  168. result._frustum = frustum.clone(frustumResult);
  169. result._frustumType = frustumType;
  170. result._origin = Cartesian3.clone(origin, result._origin);
  171. result._orientation = Quaternion.clone(orientation, result._orientation);
  172. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  173. result._drawNearPlane = drawNearPlane;
  174. return result;
  175. };
  176. function getAttributes(
  177. offset,
  178. normals,
  179. tangents,
  180. bitangents,
  181. st,
  182. normal,
  183. tangent,
  184. bitangent
  185. ) {
  186. const stOffset = (offset / 3) * 2;
  187. for (let i = 0; i < 4; ++i) {
  188. if (defined(normals)) {
  189. normals[offset] = normal.x;
  190. normals[offset + 1] = normal.y;
  191. normals[offset + 2] = normal.z;
  192. }
  193. if (defined(tangents)) {
  194. tangents[offset] = tangent.x;
  195. tangents[offset + 1] = tangent.y;
  196. tangents[offset + 2] = tangent.z;
  197. }
  198. if (defined(bitangents)) {
  199. bitangents[offset] = bitangent.x;
  200. bitangents[offset + 1] = bitangent.y;
  201. bitangents[offset + 2] = bitangent.z;
  202. }
  203. offset += 3;
  204. }
  205. st[stOffset] = 0.0;
  206. st[stOffset + 1] = 0.0;
  207. st[stOffset + 2] = 1.0;
  208. st[stOffset + 3] = 0.0;
  209. st[stOffset + 4] = 1.0;
  210. st[stOffset + 5] = 1.0;
  211. st[stOffset + 6] = 0.0;
  212. st[stOffset + 7] = 1.0;
  213. }
  214. const scratchRotationMatrix = new Matrix3();
  215. const scratchViewMatrix = new Matrix4();
  216. const scratchInverseMatrix = new Matrix4();
  217. const scratchXDirection = new Cartesian3();
  218. const scratchYDirection = new Cartesian3();
  219. const scratchZDirection = new Cartesian3();
  220. const scratchNegativeX = new Cartesian3();
  221. const scratchNegativeY = new Cartesian3();
  222. const scratchNegativeZ = new Cartesian3();
  223. const frustumSplits = new Array(3);
  224. const frustumCornersNDC = new Array(4);
  225. frustumCornersNDC[0] = new Cartesian4(-1.0, -1.0, 1.0, 1.0);
  226. frustumCornersNDC[1] = new Cartesian4(1.0, -1.0, 1.0, 1.0);
  227. frustumCornersNDC[2] = new Cartesian4(1.0, 1.0, 1.0, 1.0);
  228. frustumCornersNDC[3] = new Cartesian4(-1.0, 1.0, 1.0, 1.0);
  229. const scratchFrustumCorners = new Array(4);
  230. for (let i = 0; i < 4; ++i) {
  231. scratchFrustumCorners[i] = new Cartesian4();
  232. }
  233. FrustumGeometry._computeNearFarPlanes = function (
  234. origin,
  235. orientation,
  236. frustumType,
  237. frustum,
  238. positions,
  239. xDirection,
  240. yDirection,
  241. zDirection
  242. ) {
  243. const rotationMatrix = Matrix3.fromQuaternion(
  244. orientation,
  245. scratchRotationMatrix
  246. );
  247. let x = defaultValue(xDirection, scratchXDirection);
  248. let y = defaultValue(yDirection, scratchYDirection);
  249. let z = defaultValue(zDirection, scratchZDirection);
  250. x = Matrix3.getColumn(rotationMatrix, 0, x);
  251. y = Matrix3.getColumn(rotationMatrix, 1, y);
  252. z = Matrix3.getColumn(rotationMatrix, 2, z);
  253. Cartesian3.normalize(x, x);
  254. Cartesian3.normalize(y, y);
  255. Cartesian3.normalize(z, z);
  256. Cartesian3.negate(x, x);
  257. const view = Matrix4.computeView(origin, z, y, x, scratchViewMatrix);
  258. let inverseView;
  259. let inverseViewProjection;
  260. if (frustumType === PERSPECTIVE) {
  261. const projection = frustum.projectionMatrix;
  262. const viewProjection = Matrix4.multiply(
  263. projection,
  264. view,
  265. scratchInverseMatrix
  266. );
  267. inverseViewProjection = Matrix4.inverse(
  268. viewProjection,
  269. scratchInverseMatrix
  270. );
  271. } else {
  272. inverseView = Matrix4.inverseTransformation(view, scratchInverseMatrix);
  273. }
  274. if (defined(inverseViewProjection)) {
  275. frustumSplits[0] = frustum.near;
  276. frustumSplits[1] = frustum.far;
  277. } else {
  278. frustumSplits[0] = 0.0;
  279. frustumSplits[1] = frustum.near;
  280. frustumSplits[2] = frustum.far;
  281. }
  282. for (let i = 0; i < 2; ++i) {
  283. for (let j = 0; j < 4; ++j) {
  284. let corner = Cartesian4.clone(
  285. frustumCornersNDC[j],
  286. scratchFrustumCorners[j]
  287. );
  288. if (!defined(inverseViewProjection)) {
  289. if (defined(frustum._offCenterFrustum)) {
  290. frustum = frustum._offCenterFrustum;
  291. }
  292. const near = frustumSplits[i];
  293. const far = frustumSplits[i + 1];
  294. corner.x =
  295. (corner.x * (frustum.right - frustum.left) +
  296. frustum.left +
  297. frustum.right) *
  298. 0.5;
  299. corner.y =
  300. (corner.y * (frustum.top - frustum.bottom) +
  301. frustum.bottom +
  302. frustum.top) *
  303. 0.5;
  304. corner.z = (corner.z * (near - far) - near - far) * 0.5;
  305. corner.w = 1.0;
  306. Matrix4.multiplyByVector(inverseView, corner, corner);
  307. } else {
  308. corner = Matrix4.multiplyByVector(
  309. inverseViewProjection,
  310. corner,
  311. corner
  312. );
  313. // Reverse perspective divide
  314. const w = 1.0 / corner.w;
  315. Cartesian3.multiplyByScalar(corner, w, corner);
  316. Cartesian3.subtract(corner, origin, corner);
  317. Cartesian3.normalize(corner, corner);
  318. const fac = Cartesian3.dot(z, corner);
  319. Cartesian3.multiplyByScalar(corner, frustumSplits[i] / fac, corner);
  320. Cartesian3.add(corner, origin, corner);
  321. }
  322. positions[12 * i + j * 3] = corner.x;
  323. positions[12 * i + j * 3 + 1] = corner.y;
  324. positions[12 * i + j * 3 + 2] = corner.z;
  325. }
  326. }
  327. };
  328. /**
  329. * Computes the geometric representation of a frustum, including its vertices, indices, and a bounding sphere.
  330. *
  331. * @param {FrustumGeometry} frustumGeometry A description of the frustum.
  332. * @returns {Geometry|undefined} The computed vertices and indices.
  333. */
  334. FrustumGeometry.createGeometry = function (frustumGeometry) {
  335. const frustumType = frustumGeometry._frustumType;
  336. const frustum = frustumGeometry._frustum;
  337. const origin = frustumGeometry._origin;
  338. const orientation = frustumGeometry._orientation;
  339. const drawNearPlane = frustumGeometry._drawNearPlane;
  340. const vertexFormat = frustumGeometry._vertexFormat;
  341. const numberOfPlanes = drawNearPlane ? 6 : 5;
  342. let positions = new Float64Array(3 * 4 * 6);
  343. FrustumGeometry._computeNearFarPlanes(
  344. origin,
  345. orientation,
  346. frustumType,
  347. frustum,
  348. positions
  349. );
  350. // -x plane
  351. let offset = 3 * 4 * 2;
  352. positions[offset] = positions[3 * 4];
  353. positions[offset + 1] = positions[3 * 4 + 1];
  354. positions[offset + 2] = positions[3 * 4 + 2];
  355. positions[offset + 3] = positions[0];
  356. positions[offset + 4] = positions[1];
  357. positions[offset + 5] = positions[2];
  358. positions[offset + 6] = positions[3 * 3];
  359. positions[offset + 7] = positions[3 * 3 + 1];
  360. positions[offset + 8] = positions[3 * 3 + 2];
  361. positions[offset + 9] = positions[3 * 7];
  362. positions[offset + 10] = positions[3 * 7 + 1];
  363. positions[offset + 11] = positions[3 * 7 + 2];
  364. // -y plane
  365. offset += 3 * 4;
  366. positions[offset] = positions[3 * 5];
  367. positions[offset + 1] = positions[3 * 5 + 1];
  368. positions[offset + 2] = positions[3 * 5 + 2];
  369. positions[offset + 3] = positions[3];
  370. positions[offset + 4] = positions[3 + 1];
  371. positions[offset + 5] = positions[3 + 2];
  372. positions[offset + 6] = positions[0];
  373. positions[offset + 7] = positions[1];
  374. positions[offset + 8] = positions[2];
  375. positions[offset + 9] = positions[3 * 4];
  376. positions[offset + 10] = positions[3 * 4 + 1];
  377. positions[offset + 11] = positions[3 * 4 + 2];
  378. // +x plane
  379. offset += 3 * 4;
  380. positions[offset] = positions[3];
  381. positions[offset + 1] = positions[3 + 1];
  382. positions[offset + 2] = positions[3 + 2];
  383. positions[offset + 3] = positions[3 * 5];
  384. positions[offset + 4] = positions[3 * 5 + 1];
  385. positions[offset + 5] = positions[3 * 5 + 2];
  386. positions[offset + 6] = positions[3 * 6];
  387. positions[offset + 7] = positions[3 * 6 + 1];
  388. positions[offset + 8] = positions[3 * 6 + 2];
  389. positions[offset + 9] = positions[3 * 2];
  390. positions[offset + 10] = positions[3 * 2 + 1];
  391. positions[offset + 11] = positions[3 * 2 + 2];
  392. // +y plane
  393. offset += 3 * 4;
  394. positions[offset] = positions[3 * 2];
  395. positions[offset + 1] = positions[3 * 2 + 1];
  396. positions[offset + 2] = positions[3 * 2 + 2];
  397. positions[offset + 3] = positions[3 * 6];
  398. positions[offset + 4] = positions[3 * 6 + 1];
  399. positions[offset + 5] = positions[3 * 6 + 2];
  400. positions[offset + 6] = positions[3 * 7];
  401. positions[offset + 7] = positions[3 * 7 + 1];
  402. positions[offset + 8] = positions[3 * 7 + 2];
  403. positions[offset + 9] = positions[3 * 3];
  404. positions[offset + 10] = positions[3 * 3 + 1];
  405. positions[offset + 11] = positions[3 * 3 + 2];
  406. if (!drawNearPlane) {
  407. positions = positions.subarray(3 * 4);
  408. }
  409. const attributes = new GeometryAttributes({
  410. position: new GeometryAttribute({
  411. componentDatatype: ComponentDatatype.DOUBLE,
  412. componentsPerAttribute: 3,
  413. values: positions,
  414. }),
  415. });
  416. if (
  417. defined(vertexFormat.normal) ||
  418. defined(vertexFormat.tangent) ||
  419. defined(vertexFormat.bitangent) ||
  420. defined(vertexFormat.st)
  421. ) {
  422. const normals = defined(vertexFormat.normal)
  423. ? new Float32Array(3 * 4 * numberOfPlanes)
  424. : undefined;
  425. const tangents = defined(vertexFormat.tangent)
  426. ? new Float32Array(3 * 4 * numberOfPlanes)
  427. : undefined;
  428. const bitangents = defined(vertexFormat.bitangent)
  429. ? new Float32Array(3 * 4 * numberOfPlanes)
  430. : undefined;
  431. const st = defined(vertexFormat.st)
  432. ? new Float32Array(2 * 4 * numberOfPlanes)
  433. : undefined;
  434. const x = scratchXDirection;
  435. const y = scratchYDirection;
  436. const z = scratchZDirection;
  437. const negativeX = Cartesian3.negate(x, scratchNegativeX);
  438. const negativeY = Cartesian3.negate(y, scratchNegativeY);
  439. const negativeZ = Cartesian3.negate(z, scratchNegativeZ);
  440. offset = 0;
  441. if (drawNearPlane) {
  442. getAttributes(offset, normals, tangents, bitangents, st, negativeZ, x, y); // near
  443. offset += 3 * 4;
  444. }
  445. getAttributes(offset, normals, tangents, bitangents, st, z, negativeX, y); // far
  446. offset += 3 * 4;
  447. getAttributes(
  448. offset,
  449. normals,
  450. tangents,
  451. bitangents,
  452. st,
  453. negativeX,
  454. negativeZ,
  455. y
  456. ); // -x
  457. offset += 3 * 4;
  458. getAttributes(
  459. offset,
  460. normals,
  461. tangents,
  462. bitangents,
  463. st,
  464. negativeY,
  465. negativeZ,
  466. negativeX
  467. ); // -y
  468. offset += 3 * 4;
  469. getAttributes(offset, normals, tangents, bitangents, st, x, z, y); // +x
  470. offset += 3 * 4;
  471. getAttributes(offset, normals, tangents, bitangents, st, y, z, negativeX); // +y
  472. if (defined(normals)) {
  473. attributes.normal = new GeometryAttribute({
  474. componentDatatype: ComponentDatatype.FLOAT,
  475. componentsPerAttribute: 3,
  476. values: normals,
  477. });
  478. }
  479. if (defined(tangents)) {
  480. attributes.tangent = new GeometryAttribute({
  481. componentDatatype: ComponentDatatype.FLOAT,
  482. componentsPerAttribute: 3,
  483. values: tangents,
  484. });
  485. }
  486. if (defined(bitangents)) {
  487. attributes.bitangent = new GeometryAttribute({
  488. componentDatatype: ComponentDatatype.FLOAT,
  489. componentsPerAttribute: 3,
  490. values: bitangents,
  491. });
  492. }
  493. if (defined(st)) {
  494. attributes.st = new GeometryAttribute({
  495. componentDatatype: ComponentDatatype.FLOAT,
  496. componentsPerAttribute: 2,
  497. values: st,
  498. });
  499. }
  500. }
  501. const indices = new Uint16Array(6 * numberOfPlanes);
  502. for (let i = 0; i < numberOfPlanes; ++i) {
  503. const indexOffset = i * 6;
  504. const index = i * 4;
  505. indices[indexOffset] = index;
  506. indices[indexOffset + 1] = index + 1;
  507. indices[indexOffset + 2] = index + 2;
  508. indices[indexOffset + 3] = index;
  509. indices[indexOffset + 4] = index + 2;
  510. indices[indexOffset + 5] = index + 3;
  511. }
  512. return new Geometry({
  513. attributes: attributes,
  514. indices: indices,
  515. primitiveType: PrimitiveType.TRIANGLES,
  516. boundingSphere: BoundingSphere.fromVertices(positions),
  517. });
  518. };
  519. export default FrustumGeometry;