BoxGeometry.js 29 KB

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