BoxGeometry.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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 DeveloperError from "./DeveloperError.js";
  8. import Geometry from "./Geometry.js";
  9. import GeometryAttribute from "./GeometryAttribute.js";
  10. import GeometryAttributes from "./GeometryAttributes.js";
  11. import GeometryOffsetAttribute from "./GeometryOffsetAttribute.js";
  12. import PrimitiveType from "./PrimitiveType.js";
  13. import VertexFormat from "./VertexFormat.js";
  14. const diffScratch = new Cartesian3();
  15. /**
  16. * Describes a cube centered at the origin.
  17. *
  18. * @alias BoxGeometry
  19. * @constructor
  20. *
  21. * @param {object} options Object with the following properties:
  22. * @param {Cartesian3} options.minimum The minimum x, y, and z coordinates of the box.
  23. * @param {Cartesian3} options.maximum The maximum x, y, and z coordinates of the box.
  24. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  25. *
  26. * @see BoxGeometry.fromDimensions
  27. * @see BoxGeometry.createGeometry
  28. * @see Packable
  29. *
  30. * @demo {@link https://sandcastle.cesium.com/index.html?src=Box.html|Cesium Sandcastle Box Demo}
  31. *
  32. * @example
  33. * const box = new Cesium.BoxGeometry({
  34. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY,
  35. * maximum : new Cesium.Cartesian3(250000.0, 250000.0, 250000.0),
  36. * minimum : new Cesium.Cartesian3(-250000.0, -250000.0, -250000.0)
  37. * });
  38. * const geometry = Cesium.BoxGeometry.createGeometry(box);
  39. */
  40. function BoxGeometry(options) {
  41. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  42. const min = options.minimum;
  43. const max = options.maximum;
  44. //>>includeStart('debug', pragmas.debug);
  45. Check.typeOf.object("min", min);
  46. Check.typeOf.object("max", max);
  47. if (
  48. defined(options.offsetAttribute) &&
  49. options.offsetAttribute === GeometryOffsetAttribute.TOP
  50. ) {
  51. throw new DeveloperError(
  52. "GeometryOffsetAttribute.TOP is not a supported options.offsetAttribute for this geometry."
  53. );
  54. }
  55. //>>includeEnd('debug');
  56. const vertexFormat = defaultValue(options.vertexFormat, VertexFormat.DEFAULT);
  57. this._minimum = Cartesian3.clone(min);
  58. this._maximum = Cartesian3.clone(max);
  59. this._vertexFormat = vertexFormat;
  60. this._offsetAttribute = options.offsetAttribute;
  61. this._workerName = "createBoxGeometry";
  62. }
  63. /**
  64. * Creates a cube centered at the origin given its dimensions.
  65. *
  66. * @param {object} options Object with the following properties:
  67. * @param {Cartesian3} options.dimensions The width, depth, and height of the box stored in the x, y, and z coordinates of the <code>Cartesian3</code>, respectively.
  68. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  69. * @returns {BoxGeometry}
  70. *
  71. * @exception {DeveloperError} All dimensions components must be greater than or equal to zero.
  72. *
  73. *
  74. * @example
  75. * const box = Cesium.BoxGeometry.fromDimensions({
  76. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY,
  77. * dimensions : new Cesium.Cartesian3(500000.0, 500000.0, 500000.0)
  78. * });
  79. * const geometry = Cesium.BoxGeometry.createGeometry(box);
  80. *
  81. * @see BoxGeometry.createGeometry
  82. */
  83. BoxGeometry.fromDimensions = function (options) {
  84. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  85. const dimensions = options.dimensions;
  86. //>>includeStart('debug', pragmas.debug);
  87. Check.typeOf.object("dimensions", dimensions);
  88. Check.typeOf.number.greaterThanOrEquals("dimensions.x", dimensions.x, 0);
  89. Check.typeOf.number.greaterThanOrEquals("dimensions.y", dimensions.y, 0);
  90. Check.typeOf.number.greaterThanOrEquals("dimensions.z", dimensions.z, 0);
  91. //>>includeEnd('debug');
  92. const corner = Cartesian3.multiplyByScalar(dimensions, 0.5, new Cartesian3());
  93. return new BoxGeometry({
  94. minimum: Cartesian3.negate(corner, new Cartesian3()),
  95. maximum: corner,
  96. vertexFormat: options.vertexFormat,
  97. offsetAttribute: options.offsetAttribute,
  98. });
  99. };
  100. /**
  101. * Creates a cube from the dimensions of an AxisAlignedBoundingBox.
  102. *
  103. * @param {AxisAlignedBoundingBox} boundingBox A description of the AxisAlignedBoundingBox.
  104. * @returns {BoxGeometry}
  105. *
  106. *
  107. *
  108. * @example
  109. * const aabb = Cesium.AxisAlignedBoundingBox.fromPoints(Cesium.Cartesian3.fromDegreesArray([
  110. * -72.0, 40.0,
  111. * -70.0, 35.0,
  112. * -75.0, 30.0,
  113. * -70.0, 30.0,
  114. * -68.0, 40.0
  115. * ]));
  116. * const box = Cesium.BoxGeometry.fromAxisAlignedBoundingBox(aabb);
  117. *
  118. * @see BoxGeometry.createGeometry
  119. */
  120. BoxGeometry.fromAxisAlignedBoundingBox = function (boundingBox) {
  121. //>>includeStart('debug', pragmas.debug);
  122. Check.typeOf.object("boundingBox", boundingBox);
  123. //>>includeEnd('debug');
  124. return new BoxGeometry({
  125. minimum: boundingBox.minimum,
  126. maximum: boundingBox.maximum,
  127. });
  128. };
  129. /**
  130. * The number of elements used to pack the object into an array.
  131. * @type {number}
  132. */
  133. BoxGeometry.packedLength =
  134. 2 * Cartesian3.packedLength + VertexFormat.packedLength + 1;
  135. /**
  136. * Stores the provided instance into the provided array.
  137. *
  138. * @param {BoxGeometry} value The value to pack.
  139. * @param {number[]} array The array to pack into.
  140. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  141. *
  142. * @returns {number[]} The array that was packed into
  143. */
  144. BoxGeometry.pack = function (value, array, startingIndex) {
  145. //>>includeStart('debug', pragmas.debug);
  146. Check.typeOf.object("value", value);
  147. Check.defined("array", array);
  148. //>>includeEnd('debug');
  149. startingIndex = defaultValue(startingIndex, 0);
  150. Cartesian3.pack(value._minimum, array, startingIndex);
  151. Cartesian3.pack(
  152. value._maximum,
  153. array,
  154. startingIndex + Cartesian3.packedLength
  155. );
  156. VertexFormat.pack(
  157. value._vertexFormat,
  158. array,
  159. startingIndex + 2 * Cartesian3.packedLength
  160. );
  161. array[
  162. startingIndex + 2 * Cartesian3.packedLength + VertexFormat.packedLength
  163. ] = defaultValue(value._offsetAttribute, -1);
  164. return array;
  165. };
  166. const scratchMin = new Cartesian3();
  167. const scratchMax = new Cartesian3();
  168. const scratchVertexFormat = new VertexFormat();
  169. const scratchOptions = {
  170. minimum: scratchMin,
  171. maximum: scratchMax,
  172. vertexFormat: scratchVertexFormat,
  173. offsetAttribute: undefined,
  174. };
  175. /**
  176. * Retrieves an instance from a packed array.
  177. *
  178. * @param {number[]} array The packed array.
  179. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  180. * @param {BoxGeometry} [result] The object into which to store the result.
  181. * @returns {BoxGeometry} The modified result parameter or a new BoxGeometry instance if one was not provided.
  182. */
  183. BoxGeometry.unpack = function (array, startingIndex, result) {
  184. //>>includeStart('debug', pragmas.debug);
  185. Check.defined("array", array);
  186. //>>includeEnd('debug');
  187. startingIndex = defaultValue(startingIndex, 0);
  188. const min = Cartesian3.unpack(array, startingIndex, scratchMin);
  189. const max = Cartesian3.unpack(
  190. array,
  191. startingIndex + Cartesian3.packedLength,
  192. scratchMax
  193. );
  194. const vertexFormat = VertexFormat.unpack(
  195. array,
  196. startingIndex + 2 * Cartesian3.packedLength,
  197. scratchVertexFormat
  198. );
  199. const offsetAttribute =
  200. array[
  201. startingIndex + 2 * Cartesian3.packedLength + VertexFormat.packedLength
  202. ];
  203. if (!defined(result)) {
  204. scratchOptions.offsetAttribute =
  205. offsetAttribute === -1 ? undefined : offsetAttribute;
  206. return new BoxGeometry(scratchOptions);
  207. }
  208. result._minimum = Cartesian3.clone(min, result._minimum);
  209. result._maximum = Cartesian3.clone(max, result._maximum);
  210. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  211. result._offsetAttribute =
  212. offsetAttribute === -1 ? undefined : offsetAttribute;
  213. return result;
  214. };
  215. /**
  216. * Computes the geometric representation of a box, including its vertices, indices, and a bounding sphere.
  217. *
  218. * @param {BoxGeometry} boxGeometry A description of the box.
  219. * @returns {Geometry|undefined} The computed vertices and indices.
  220. */
  221. BoxGeometry.createGeometry = function (boxGeometry) {
  222. const min = boxGeometry._minimum;
  223. const max = boxGeometry._maximum;
  224. const vertexFormat = boxGeometry._vertexFormat;
  225. if (Cartesian3.equals(min, max)) {
  226. return;
  227. }
  228. const attributes = new GeometryAttributes();
  229. let indices;
  230. let positions;
  231. if (
  232. vertexFormat.position &&
  233. (vertexFormat.st ||
  234. vertexFormat.normal ||
  235. vertexFormat.tangent ||
  236. vertexFormat.bitangent)
  237. ) {
  238. if (vertexFormat.position) {
  239. // 8 corner points. Duplicated 3 times each for each incident edge/face.
  240. positions = new Float64Array(6 * 4 * 3);
  241. // +z face
  242. positions[0] = min.x;
  243. positions[1] = min.y;
  244. positions[2] = max.z;
  245. positions[3] = max.x;
  246. positions[4] = min.y;
  247. positions[5] = max.z;
  248. positions[6] = max.x;
  249. positions[7] = max.y;
  250. positions[8] = max.z;
  251. positions[9] = min.x;
  252. positions[10] = max.y;
  253. positions[11] = max.z;
  254. // -z face
  255. positions[12] = min.x;
  256. positions[13] = min.y;
  257. positions[14] = min.z;
  258. positions[15] = max.x;
  259. positions[16] = min.y;
  260. positions[17] = min.z;
  261. positions[18] = max.x;
  262. positions[19] = max.y;
  263. positions[20] = min.z;
  264. positions[21] = min.x;
  265. positions[22] = max.y;
  266. positions[23] = min.z;
  267. // +x face
  268. positions[24] = max.x;
  269. positions[25] = min.y;
  270. positions[26] = min.z;
  271. positions[27] = max.x;
  272. positions[28] = max.y;
  273. positions[29] = min.z;
  274. positions[30] = max.x;
  275. positions[31] = max.y;
  276. positions[32] = max.z;
  277. positions[33] = max.x;
  278. positions[34] = min.y;
  279. positions[35] = max.z;
  280. // -x face
  281. positions[36] = min.x;
  282. positions[37] = min.y;
  283. positions[38] = min.z;
  284. positions[39] = min.x;
  285. positions[40] = max.y;
  286. positions[41] = min.z;
  287. positions[42] = min.x;
  288. positions[43] = max.y;
  289. positions[44] = max.z;
  290. positions[45] = min.x;
  291. positions[46] = min.y;
  292. positions[47] = max.z;
  293. // +y face
  294. positions[48] = min.x;
  295. positions[49] = max.y;
  296. positions[50] = min.z;
  297. positions[51] = max.x;
  298. positions[52] = max.y;
  299. positions[53] = min.z;
  300. positions[54] = max.x;
  301. positions[55] = max.y;
  302. positions[56] = max.z;
  303. positions[57] = min.x;
  304. positions[58] = max.y;
  305. positions[59] = max.z;
  306. // -y face
  307. positions[60] = min.x;
  308. positions[61] = min.y;
  309. positions[62] = min.z;
  310. positions[63] = max.x;
  311. positions[64] = min.y;
  312. positions[65] = min.z;
  313. positions[66] = max.x;
  314. positions[67] = min.y;
  315. positions[68] = max.z;
  316. positions[69] = min.x;
  317. positions[70] = min.y;
  318. positions[71] = max.z;
  319. attributes.position = new GeometryAttribute({
  320. componentDatatype: ComponentDatatype.DOUBLE,
  321. componentsPerAttribute: 3,
  322. values: positions,
  323. });
  324. }
  325. if (vertexFormat.normal) {
  326. const normals = new Float32Array(6 * 4 * 3);
  327. // +z face
  328. normals[0] = 0.0;
  329. normals[1] = 0.0;
  330. normals[2] = 1.0;
  331. normals[3] = 0.0;
  332. normals[4] = 0.0;
  333. normals[5] = 1.0;
  334. normals[6] = 0.0;
  335. normals[7] = 0.0;
  336. normals[8] = 1.0;
  337. normals[9] = 0.0;
  338. normals[10] = 0.0;
  339. normals[11] = 1.0;
  340. // -z face
  341. normals[12] = 0.0;
  342. normals[13] = 0.0;
  343. normals[14] = -1.0;
  344. normals[15] = 0.0;
  345. normals[16] = 0.0;
  346. normals[17] = -1.0;
  347. normals[18] = 0.0;
  348. normals[19] = 0.0;
  349. normals[20] = -1.0;
  350. normals[21] = 0.0;
  351. normals[22] = 0.0;
  352. normals[23] = -1.0;
  353. // +x face
  354. normals[24] = 1.0;
  355. normals[25] = 0.0;
  356. normals[26] = 0.0;
  357. normals[27] = 1.0;
  358. normals[28] = 0.0;
  359. normals[29] = 0.0;
  360. normals[30] = 1.0;
  361. normals[31] = 0.0;
  362. normals[32] = 0.0;
  363. normals[33] = 1.0;
  364. normals[34] = 0.0;
  365. normals[35] = 0.0;
  366. // -x face
  367. normals[36] = -1.0;
  368. normals[37] = 0.0;
  369. normals[38] = 0.0;
  370. normals[39] = -1.0;
  371. normals[40] = 0.0;
  372. normals[41] = 0.0;
  373. normals[42] = -1.0;
  374. normals[43] = 0.0;
  375. normals[44] = 0.0;
  376. normals[45] = -1.0;
  377. normals[46] = 0.0;
  378. normals[47] = 0.0;
  379. // +y face
  380. normals[48] = 0.0;
  381. normals[49] = 1.0;
  382. normals[50] = 0.0;
  383. normals[51] = 0.0;
  384. normals[52] = 1.0;
  385. normals[53] = 0.0;
  386. normals[54] = 0.0;
  387. normals[55] = 1.0;
  388. normals[56] = 0.0;
  389. normals[57] = 0.0;
  390. normals[58] = 1.0;
  391. normals[59] = 0.0;
  392. // -y face
  393. normals[60] = 0.0;
  394. normals[61] = -1.0;
  395. normals[62] = 0.0;
  396. normals[63] = 0.0;
  397. normals[64] = -1.0;
  398. normals[65] = 0.0;
  399. normals[66] = 0.0;
  400. normals[67] = -1.0;
  401. normals[68] = 0.0;
  402. normals[69] = 0.0;
  403. normals[70] = -1.0;
  404. normals[71] = 0.0;
  405. attributes.normal = new GeometryAttribute({
  406. componentDatatype: ComponentDatatype.FLOAT,
  407. componentsPerAttribute: 3,
  408. values: normals,
  409. });
  410. }
  411. if (vertexFormat.st) {
  412. const texCoords = new Float32Array(6 * 4 * 2);
  413. // +z face
  414. texCoords[0] = 0.0;
  415. texCoords[1] = 0.0;
  416. texCoords[2] = 1.0;
  417. texCoords[3] = 0.0;
  418. texCoords[4] = 1.0;
  419. texCoords[5] = 1.0;
  420. texCoords[6] = 0.0;
  421. texCoords[7] = 1.0;
  422. // -z face
  423. texCoords[8] = 1.0;
  424. texCoords[9] = 0.0;
  425. texCoords[10] = 0.0;
  426. texCoords[11] = 0.0;
  427. texCoords[12] = 0.0;
  428. texCoords[13] = 1.0;
  429. texCoords[14] = 1.0;
  430. texCoords[15] = 1.0;
  431. //+x face
  432. texCoords[16] = 0.0;
  433. texCoords[17] = 0.0;
  434. texCoords[18] = 1.0;
  435. texCoords[19] = 0.0;
  436. texCoords[20] = 1.0;
  437. texCoords[21] = 1.0;
  438. texCoords[22] = 0.0;
  439. texCoords[23] = 1.0;
  440. // -x face
  441. texCoords[24] = 1.0;
  442. texCoords[25] = 0.0;
  443. texCoords[26] = 0.0;
  444. texCoords[27] = 0.0;
  445. texCoords[28] = 0.0;
  446. texCoords[29] = 1.0;
  447. texCoords[30] = 1.0;
  448. texCoords[31] = 1.0;
  449. // +y face
  450. texCoords[32] = 1.0;
  451. texCoords[33] = 0.0;
  452. texCoords[34] = 0.0;
  453. texCoords[35] = 0.0;
  454. texCoords[36] = 0.0;
  455. texCoords[37] = 1.0;
  456. texCoords[38] = 1.0;
  457. texCoords[39] = 1.0;
  458. // -y face
  459. texCoords[40] = 0.0;
  460. texCoords[41] = 0.0;
  461. texCoords[42] = 1.0;
  462. texCoords[43] = 0.0;
  463. texCoords[44] = 1.0;
  464. texCoords[45] = 1.0;
  465. texCoords[46] = 0.0;
  466. texCoords[47] = 1.0;
  467. attributes.st = new GeometryAttribute({
  468. componentDatatype: ComponentDatatype.FLOAT,
  469. componentsPerAttribute: 2,
  470. values: texCoords,
  471. });
  472. }
  473. if (vertexFormat.tangent) {
  474. const tangents = new Float32Array(6 * 4 * 3);
  475. // +z face
  476. tangents[0] = 1.0;
  477. tangents[1] = 0.0;
  478. tangents[2] = 0.0;
  479. tangents[3] = 1.0;
  480. tangents[4] = 0.0;
  481. tangents[5] = 0.0;
  482. tangents[6] = 1.0;
  483. tangents[7] = 0.0;
  484. tangents[8] = 0.0;
  485. tangents[9] = 1.0;
  486. tangents[10] = 0.0;
  487. tangents[11] = 0.0;
  488. // -z face
  489. tangents[12] = -1.0;
  490. tangents[13] = 0.0;
  491. tangents[14] = 0.0;
  492. tangents[15] = -1.0;
  493. tangents[16] = 0.0;
  494. tangents[17] = 0.0;
  495. tangents[18] = -1.0;
  496. tangents[19] = 0.0;
  497. tangents[20] = 0.0;
  498. tangents[21] = -1.0;
  499. tangents[22] = 0.0;
  500. tangents[23] = 0.0;
  501. // +x face
  502. tangents[24] = 0.0;
  503. tangents[25] = 1.0;
  504. tangents[26] = 0.0;
  505. tangents[27] = 0.0;
  506. tangents[28] = 1.0;
  507. tangents[29] = 0.0;
  508. tangents[30] = 0.0;
  509. tangents[31] = 1.0;
  510. tangents[32] = 0.0;
  511. tangents[33] = 0.0;
  512. tangents[34] = 1.0;
  513. tangents[35] = 0.0;
  514. // -x face
  515. tangents[36] = 0.0;
  516. tangents[37] = -1.0;
  517. tangents[38] = 0.0;
  518. tangents[39] = 0.0;
  519. tangents[40] = -1.0;
  520. tangents[41] = 0.0;
  521. tangents[42] = 0.0;
  522. tangents[43] = -1.0;
  523. tangents[44] = 0.0;
  524. tangents[45] = 0.0;
  525. tangents[46] = -1.0;
  526. tangents[47] = 0.0;
  527. // +y face
  528. tangents[48] = -1.0;
  529. tangents[49] = 0.0;
  530. tangents[50] = 0.0;
  531. tangents[51] = -1.0;
  532. tangents[52] = 0.0;
  533. tangents[53] = 0.0;
  534. tangents[54] = -1.0;
  535. tangents[55] = 0.0;
  536. tangents[56] = 0.0;
  537. tangents[57] = -1.0;
  538. tangents[58] = 0.0;
  539. tangents[59] = 0.0;
  540. // -y face
  541. tangents[60] = 1.0;
  542. tangents[61] = 0.0;
  543. tangents[62] = 0.0;
  544. tangents[63] = 1.0;
  545. tangents[64] = 0.0;
  546. tangents[65] = 0.0;
  547. tangents[66] = 1.0;
  548. tangents[67] = 0.0;
  549. tangents[68] = 0.0;
  550. tangents[69] = 1.0;
  551. tangents[70] = 0.0;
  552. tangents[71] = 0.0;
  553. attributes.tangent = new GeometryAttribute({
  554. componentDatatype: ComponentDatatype.FLOAT,
  555. componentsPerAttribute: 3,
  556. values: tangents,
  557. });
  558. }
  559. if (vertexFormat.bitangent) {
  560. const bitangents = new Float32Array(6 * 4 * 3);
  561. // +z face
  562. bitangents[0] = 0.0;
  563. bitangents[1] = 1.0;
  564. bitangents[2] = 0.0;
  565. bitangents[3] = 0.0;
  566. bitangents[4] = 1.0;
  567. bitangents[5] = 0.0;
  568. bitangents[6] = 0.0;
  569. bitangents[7] = 1.0;
  570. bitangents[8] = 0.0;
  571. bitangents[9] = 0.0;
  572. bitangents[10] = 1.0;
  573. bitangents[11] = 0.0;
  574. // -z face
  575. bitangents[12] = 0.0;
  576. bitangents[13] = 1.0;
  577. bitangents[14] = 0.0;
  578. bitangents[15] = 0.0;
  579. bitangents[16] = 1.0;
  580. bitangents[17] = 0.0;
  581. bitangents[18] = 0.0;
  582. bitangents[19] = 1.0;
  583. bitangents[20] = 0.0;
  584. bitangents[21] = 0.0;
  585. bitangents[22] = 1.0;
  586. bitangents[23] = 0.0;
  587. // +x face
  588. bitangents[24] = 0.0;
  589. bitangents[25] = 0.0;
  590. bitangents[26] = 1.0;
  591. bitangents[27] = 0.0;
  592. bitangents[28] = 0.0;
  593. bitangents[29] = 1.0;
  594. bitangents[30] = 0.0;
  595. bitangents[31] = 0.0;
  596. bitangents[32] = 1.0;
  597. bitangents[33] = 0.0;
  598. bitangents[34] = 0.0;
  599. bitangents[35] = 1.0;
  600. // -x face
  601. bitangents[36] = 0.0;
  602. bitangents[37] = 0.0;
  603. bitangents[38] = 1.0;
  604. bitangents[39] = 0.0;
  605. bitangents[40] = 0.0;
  606. bitangents[41] = 1.0;
  607. bitangents[42] = 0.0;
  608. bitangents[43] = 0.0;
  609. bitangents[44] = 1.0;
  610. bitangents[45] = 0.0;
  611. bitangents[46] = 0.0;
  612. bitangents[47] = 1.0;
  613. // +y face
  614. bitangents[48] = 0.0;
  615. bitangents[49] = 0.0;
  616. bitangents[50] = 1.0;
  617. bitangents[51] = 0.0;
  618. bitangents[52] = 0.0;
  619. bitangents[53] = 1.0;
  620. bitangents[54] = 0.0;
  621. bitangents[55] = 0.0;
  622. bitangents[56] = 1.0;
  623. bitangents[57] = 0.0;
  624. bitangents[58] = 0.0;
  625. bitangents[59] = 1.0;
  626. // -y face
  627. bitangents[60] = 0.0;
  628. bitangents[61] = 0.0;
  629. bitangents[62] = 1.0;
  630. bitangents[63] = 0.0;
  631. bitangents[64] = 0.0;
  632. bitangents[65] = 1.0;
  633. bitangents[66] = 0.0;
  634. bitangents[67] = 0.0;
  635. bitangents[68] = 1.0;
  636. bitangents[69] = 0.0;
  637. bitangents[70] = 0.0;
  638. bitangents[71] = 1.0;
  639. attributes.bitangent = new GeometryAttribute({
  640. componentDatatype: ComponentDatatype.FLOAT,
  641. componentsPerAttribute: 3,
  642. values: bitangents,
  643. });
  644. }
  645. // 12 triangles: 6 faces, 2 triangles each.
  646. indices = new Uint16Array(6 * 2 * 3);
  647. // +z face
  648. indices[0] = 0;
  649. indices[1] = 1;
  650. indices[2] = 2;
  651. indices[3] = 0;
  652. indices[4] = 2;
  653. indices[5] = 3;
  654. // -z face
  655. indices[6] = 4 + 2;
  656. indices[7] = 4 + 1;
  657. indices[8] = 4 + 0;
  658. indices[9] = 4 + 3;
  659. indices[10] = 4 + 2;
  660. indices[11] = 4 + 0;
  661. // +x face
  662. indices[12] = 8 + 0;
  663. indices[13] = 8 + 1;
  664. indices[14] = 8 + 2;
  665. indices[15] = 8 + 0;
  666. indices[16] = 8 + 2;
  667. indices[17] = 8 + 3;
  668. // -x face
  669. indices[18] = 12 + 2;
  670. indices[19] = 12 + 1;
  671. indices[20] = 12 + 0;
  672. indices[21] = 12 + 3;
  673. indices[22] = 12 + 2;
  674. indices[23] = 12 + 0;
  675. // +y face
  676. indices[24] = 16 + 2;
  677. indices[25] = 16 + 1;
  678. indices[26] = 16 + 0;
  679. indices[27] = 16 + 3;
  680. indices[28] = 16 + 2;
  681. indices[29] = 16 + 0;
  682. // -y face
  683. indices[30] = 20 + 0;
  684. indices[31] = 20 + 1;
  685. indices[32] = 20 + 2;
  686. indices[33] = 20 + 0;
  687. indices[34] = 20 + 2;
  688. indices[35] = 20 + 3;
  689. } else {
  690. // Positions only - no need to duplicate corner points
  691. positions = new Float64Array(8 * 3);
  692. positions[0] = min.x;
  693. positions[1] = min.y;
  694. positions[2] = min.z;
  695. positions[3] = max.x;
  696. positions[4] = min.y;
  697. positions[5] = min.z;
  698. positions[6] = max.x;
  699. positions[7] = max.y;
  700. positions[8] = min.z;
  701. positions[9] = min.x;
  702. positions[10] = max.y;
  703. positions[11] = min.z;
  704. positions[12] = min.x;
  705. positions[13] = min.y;
  706. positions[14] = max.z;
  707. positions[15] = max.x;
  708. positions[16] = min.y;
  709. positions[17] = max.z;
  710. positions[18] = max.x;
  711. positions[19] = max.y;
  712. positions[20] = max.z;
  713. positions[21] = min.x;
  714. positions[22] = max.y;
  715. positions[23] = max.z;
  716. attributes.position = new GeometryAttribute({
  717. componentDatatype: ComponentDatatype.DOUBLE,
  718. componentsPerAttribute: 3,
  719. values: positions,
  720. });
  721. // 12 triangles: 6 faces, 2 triangles each.
  722. indices = new Uint16Array(6 * 2 * 3);
  723. // plane z = corner.Z
  724. indices[0] = 4;
  725. indices[1] = 5;
  726. indices[2] = 6;
  727. indices[3] = 4;
  728. indices[4] = 6;
  729. indices[5] = 7;
  730. // plane z = -corner.Z
  731. indices[6] = 1;
  732. indices[7] = 0;
  733. indices[8] = 3;
  734. indices[9] = 1;
  735. indices[10] = 3;
  736. indices[11] = 2;
  737. // plane x = corner.X
  738. indices[12] = 1;
  739. indices[13] = 6;
  740. indices[14] = 5;
  741. indices[15] = 1;
  742. indices[16] = 2;
  743. indices[17] = 6;
  744. // plane y = corner.Y
  745. indices[18] = 2;
  746. indices[19] = 3;
  747. indices[20] = 7;
  748. indices[21] = 2;
  749. indices[22] = 7;
  750. indices[23] = 6;
  751. // plane x = -corner.X
  752. indices[24] = 3;
  753. indices[25] = 0;
  754. indices[26] = 4;
  755. indices[27] = 3;
  756. indices[28] = 4;
  757. indices[29] = 7;
  758. // plane y = -corner.Y
  759. indices[30] = 0;
  760. indices[31] = 1;
  761. indices[32] = 5;
  762. indices[33] = 0;
  763. indices[34] = 5;
  764. indices[35] = 4;
  765. }
  766. const diff = Cartesian3.subtract(max, min, diffScratch);
  767. const radius = Cartesian3.magnitude(diff) * 0.5;
  768. if (defined(boxGeometry._offsetAttribute)) {
  769. const length = positions.length;
  770. const offsetValue =
  771. boxGeometry._offsetAttribute === GeometryOffsetAttribute.NONE ? 0 : 1;
  772. const applyOffset = new Uint8Array(length / 3).fill(offsetValue);
  773. attributes.applyOffset = new GeometryAttribute({
  774. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  775. componentsPerAttribute: 1,
  776. values: applyOffset,
  777. });
  778. }
  779. return new Geometry({
  780. attributes: attributes,
  781. indices: indices,
  782. primitiveType: PrimitiveType.TRIANGLES,
  783. boundingSphere: new BoundingSphere(Cartesian3.ZERO, radius),
  784. offsetAttribute: boxGeometry._offsetAttribute,
  785. });
  786. };
  787. let unitBoxGeometry;
  788. /**
  789. * Returns the geometric representation of a unit box, including its vertices, indices, and a bounding sphere.
  790. * @returns {Geometry} The computed vertices and indices.
  791. *
  792. * @private
  793. */
  794. BoxGeometry.getUnitBox = function () {
  795. if (!defined(unitBoxGeometry)) {
  796. unitBoxGeometry = BoxGeometry.createGeometry(
  797. BoxGeometry.fromDimensions({
  798. dimensions: new Cartesian3(1.0, 1.0, 1.0),
  799. vertexFormat: VertexFormat.POSITION_ONLY,
  800. })
  801. );
  802. }
  803. return unitBoxGeometry;
  804. };
  805. export default BoxGeometry;