FrustumGeometry.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. const projection = frustum.projectionMatrix;
  261. if (frustumType === PERSPECTIVE) {
  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. const offCenterFrustum = frustum.offCenterFrustum;
  290. if (defined(offCenterFrustum)) {
  291. frustum = offCenterFrustum;
  292. }
  293. const near = frustumSplits[i];
  294. const far = frustumSplits[i + 1];
  295. corner.x =
  296. (corner.x * (frustum.right - frustum.left) +
  297. frustum.left +
  298. frustum.right) *
  299. 0.5;
  300. corner.y =
  301. (corner.y * (frustum.top - frustum.bottom) +
  302. frustum.bottom +
  303. frustum.top) *
  304. 0.5;
  305. corner.z = (corner.z * (near - far) - near - far) * 0.5;
  306. corner.w = 1.0;
  307. Matrix4.multiplyByVector(inverseView, corner, corner);
  308. } else {
  309. corner = Matrix4.multiplyByVector(
  310. inverseViewProjection,
  311. corner,
  312. corner
  313. );
  314. // Reverse perspective divide
  315. const w = 1.0 / corner.w;
  316. Cartesian3.multiplyByScalar(corner, w, corner);
  317. Cartesian3.subtract(corner, origin, corner);
  318. Cartesian3.normalize(corner, corner);
  319. const fac = Cartesian3.dot(z, corner);
  320. Cartesian3.multiplyByScalar(corner, frustumSplits[i] / fac, corner);
  321. Cartesian3.add(corner, origin, corner);
  322. }
  323. positions[12 * i + j * 3] = corner.x;
  324. positions[12 * i + j * 3 + 1] = corner.y;
  325. positions[12 * i + j * 3 + 2] = corner.z;
  326. }
  327. }
  328. };
  329. /**
  330. * Computes the geometric representation of a frustum, including its vertices, indices, and a bounding sphere.
  331. *
  332. * @param {FrustumGeometry} frustumGeometry A description of the frustum.
  333. * @returns {Geometry|undefined} The computed vertices and indices.
  334. */
  335. FrustumGeometry.createGeometry = function (frustumGeometry) {
  336. const frustumType = frustumGeometry._frustumType;
  337. const frustum = frustumGeometry._frustum;
  338. const origin = frustumGeometry._origin;
  339. const orientation = frustumGeometry._orientation;
  340. const drawNearPlane = frustumGeometry._drawNearPlane;
  341. const vertexFormat = frustumGeometry._vertexFormat;
  342. const numberOfPlanes = drawNearPlane ? 6 : 5;
  343. let positions = new Float64Array(3 * 4 * 6);
  344. FrustumGeometry._computeNearFarPlanes(
  345. origin,
  346. orientation,
  347. frustumType,
  348. frustum,
  349. positions
  350. );
  351. // -x plane
  352. let offset = 3 * 4 * 2;
  353. positions[offset] = positions[3 * 4];
  354. positions[offset + 1] = positions[3 * 4 + 1];
  355. positions[offset + 2] = positions[3 * 4 + 2];
  356. positions[offset + 3] = positions[0];
  357. positions[offset + 4] = positions[1];
  358. positions[offset + 5] = positions[2];
  359. positions[offset + 6] = positions[3 * 3];
  360. positions[offset + 7] = positions[3 * 3 + 1];
  361. positions[offset + 8] = positions[3 * 3 + 2];
  362. positions[offset + 9] = positions[3 * 7];
  363. positions[offset + 10] = positions[3 * 7 + 1];
  364. positions[offset + 11] = positions[3 * 7 + 2];
  365. // -y plane
  366. offset += 3 * 4;
  367. positions[offset] = positions[3 * 5];
  368. positions[offset + 1] = positions[3 * 5 + 1];
  369. positions[offset + 2] = positions[3 * 5 + 2];
  370. positions[offset + 3] = positions[3];
  371. positions[offset + 4] = positions[3 + 1];
  372. positions[offset + 5] = positions[3 + 2];
  373. positions[offset + 6] = positions[0];
  374. positions[offset + 7] = positions[1];
  375. positions[offset + 8] = positions[2];
  376. positions[offset + 9] = positions[3 * 4];
  377. positions[offset + 10] = positions[3 * 4 + 1];
  378. positions[offset + 11] = positions[3 * 4 + 2];
  379. // +x plane
  380. offset += 3 * 4;
  381. positions[offset] = positions[3];
  382. positions[offset + 1] = positions[3 + 1];
  383. positions[offset + 2] = positions[3 + 2];
  384. positions[offset + 3] = positions[3 * 5];
  385. positions[offset + 4] = positions[3 * 5 + 1];
  386. positions[offset + 5] = positions[3 * 5 + 2];
  387. positions[offset + 6] = positions[3 * 6];
  388. positions[offset + 7] = positions[3 * 6 + 1];
  389. positions[offset + 8] = positions[3 * 6 + 2];
  390. positions[offset + 9] = positions[3 * 2];
  391. positions[offset + 10] = positions[3 * 2 + 1];
  392. positions[offset + 11] = positions[3 * 2 + 2];
  393. // +y plane
  394. offset += 3 * 4;
  395. positions[offset] = positions[3 * 2];
  396. positions[offset + 1] = positions[3 * 2 + 1];
  397. positions[offset + 2] = positions[3 * 2 + 2];
  398. positions[offset + 3] = positions[3 * 6];
  399. positions[offset + 4] = positions[3 * 6 + 1];
  400. positions[offset + 5] = positions[3 * 6 + 2];
  401. positions[offset + 6] = positions[3 * 7];
  402. positions[offset + 7] = positions[3 * 7 + 1];
  403. positions[offset + 8] = positions[3 * 7 + 2];
  404. positions[offset + 9] = positions[3 * 3];
  405. positions[offset + 10] = positions[3 * 3 + 1];
  406. positions[offset + 11] = positions[3 * 3 + 2];
  407. if (!drawNearPlane) {
  408. positions = positions.subarray(3 * 4);
  409. }
  410. const attributes = new GeometryAttributes({
  411. position: new GeometryAttribute({
  412. componentDatatype: ComponentDatatype.DOUBLE,
  413. componentsPerAttribute: 3,
  414. values: positions,
  415. }),
  416. });
  417. if (
  418. defined(vertexFormat.normal) ||
  419. defined(vertexFormat.tangent) ||
  420. defined(vertexFormat.bitangent) ||
  421. defined(vertexFormat.st)
  422. ) {
  423. const normals = defined(vertexFormat.normal)
  424. ? new Float32Array(3 * 4 * numberOfPlanes)
  425. : undefined;
  426. const tangents = defined(vertexFormat.tangent)
  427. ? new Float32Array(3 * 4 * numberOfPlanes)
  428. : undefined;
  429. const bitangents = defined(vertexFormat.bitangent)
  430. ? new Float32Array(3 * 4 * numberOfPlanes)
  431. : undefined;
  432. const st = defined(vertexFormat.st)
  433. ? new Float32Array(2 * 4 * numberOfPlanes)
  434. : undefined;
  435. const x = scratchXDirection;
  436. const y = scratchYDirection;
  437. const z = scratchZDirection;
  438. const negativeX = Cartesian3.negate(x, scratchNegativeX);
  439. const negativeY = Cartesian3.negate(y, scratchNegativeY);
  440. const negativeZ = Cartesian3.negate(z, scratchNegativeZ);
  441. offset = 0;
  442. if (drawNearPlane) {
  443. getAttributes(offset, normals, tangents, bitangents, st, negativeZ, x, y); // near
  444. offset += 3 * 4;
  445. }
  446. getAttributes(offset, normals, tangents, bitangents, st, z, negativeX, y); // far
  447. offset += 3 * 4;
  448. getAttributes(
  449. offset,
  450. normals,
  451. tangents,
  452. bitangents,
  453. st,
  454. negativeX,
  455. negativeZ,
  456. y
  457. ); // -x
  458. offset += 3 * 4;
  459. getAttributes(
  460. offset,
  461. normals,
  462. tangents,
  463. bitangents,
  464. st,
  465. negativeY,
  466. negativeZ,
  467. negativeX
  468. ); // -y
  469. offset += 3 * 4;
  470. getAttributes(offset, normals, tangents, bitangents, st, x, z, y); // +x
  471. offset += 3 * 4;
  472. getAttributes(offset, normals, tangents, bitangents, st, y, z, negativeX); // +y
  473. if (defined(normals)) {
  474. attributes.normal = new GeometryAttribute({
  475. componentDatatype: ComponentDatatype.FLOAT,
  476. componentsPerAttribute: 3,
  477. values: normals,
  478. });
  479. }
  480. if (defined(tangents)) {
  481. attributes.tangent = new GeometryAttribute({
  482. componentDatatype: ComponentDatatype.FLOAT,
  483. componentsPerAttribute: 3,
  484. values: tangents,
  485. });
  486. }
  487. if (defined(bitangents)) {
  488. attributes.bitangent = new GeometryAttribute({
  489. componentDatatype: ComponentDatatype.FLOAT,
  490. componentsPerAttribute: 3,
  491. values: bitangents,
  492. });
  493. }
  494. if (defined(st)) {
  495. attributes.st = new GeometryAttribute({
  496. componentDatatype: ComponentDatatype.FLOAT,
  497. componentsPerAttribute: 2,
  498. values: st,
  499. });
  500. }
  501. }
  502. const indices = new Uint16Array(6 * numberOfPlanes);
  503. for (let i = 0; i < numberOfPlanes; ++i) {
  504. const indexOffset = i * 6;
  505. const index = i * 4;
  506. indices[indexOffset] = index;
  507. indices[indexOffset + 1] = index + 1;
  508. indices[indexOffset + 2] = index + 2;
  509. indices[indexOffset + 3] = index;
  510. indices[indexOffset + 4] = index + 2;
  511. indices[indexOffset + 5] = index + 3;
  512. }
  513. return new Geometry({
  514. attributes: attributes,
  515. indices: indices,
  516. primitiveType: PrimitiveType.TRIANGLES,
  517. boundingSphere: BoundingSphere.fromVertices(positions),
  518. });
  519. };
  520. export default FrustumGeometry;