PrimitivePipeline-8b63e52c.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './when-e6985d2a', './Check-24cae389', './Cartesian2-a5d6dde9', './Transforms-81680c41', './ComponentDatatype-cb08e294', './GeometryAttribute-6d403cd9', './GeometryAttributes-d6ea8c2b', './GeometryPipeline-e8a01ba6', './IndexDatatype-1be7d1f8', './WebMercatorProjection-a06fbf17'], function (exports, when, Check, Cartesian2, Transforms, ComponentDatatype, GeometryAttribute, GeometryAttributes, GeometryPipeline, IndexDatatype, WebMercatorProjection) { 'use strict';
  3. /**
  4. * Value and type information for per-instance geometry attribute that determines the geometry instance offset
  5. *
  6. * @alias OffsetGeometryInstanceAttribute
  7. * @constructor
  8. *
  9. * @param {Number} [x=0] The x translation
  10. * @param {Number} [y=0] The y translation
  11. * @param {Number} [z=0] The z translation
  12. *
  13. * @private
  14. *
  15. * @see GeometryInstance
  16. * @see GeometryInstanceAttribute
  17. */
  18. function OffsetGeometryInstanceAttribute(x, y, z) {
  19. x = when.defaultValue(x, 0);
  20. y = when.defaultValue(y, 0);
  21. z = when.defaultValue(z, 0);
  22. /**
  23. * The values for the attributes stored in a typed array.
  24. *
  25. * @type Float32Array
  26. */
  27. this.value = new Float32Array([x, y, z]);
  28. }
  29. Object.defineProperties(OffsetGeometryInstanceAttribute.prototype, {
  30. /**
  31. * The datatype of each component in the attribute, e.g., individual elements in
  32. * {@link OffsetGeometryInstanceAttribute#value}.
  33. *
  34. * @memberof OffsetGeometryInstanceAttribute.prototype
  35. *
  36. * @type {ComponentDatatype}
  37. * @readonly
  38. *
  39. * @default {@link ComponentDatatype.FLOAT}
  40. */
  41. componentDatatype: {
  42. get: function () {
  43. return ComponentDatatype.ComponentDatatype.FLOAT;
  44. },
  45. },
  46. /**
  47. * The number of components in the attributes, i.e., {@link OffsetGeometryInstanceAttribute#value}.
  48. *
  49. * @memberof OffsetGeometryInstanceAttribute.prototype
  50. *
  51. * @type {Number}
  52. * @readonly
  53. *
  54. * @default 3
  55. */
  56. componentsPerAttribute: {
  57. get: function () {
  58. return 3;
  59. },
  60. },
  61. /**
  62. * When <code>true</code> and <code>componentDatatype</code> is an integer format,
  63. * indicate that the components should be mapped to the range [0, 1] (unsigned)
  64. * or [-1, 1] (signed) when they are accessed as floating-point for rendering.
  65. *
  66. * @memberof OffsetGeometryInstanceAttribute.prototype
  67. *
  68. * @type {Boolean}
  69. * @readonly
  70. *
  71. * @default false
  72. */
  73. normalize: {
  74. get: function () {
  75. return false;
  76. },
  77. },
  78. });
  79. /**
  80. * Creates a new {@link OffsetGeometryInstanceAttribute} instance given the provided an enabled flag and {@link DistanceDisplayCondition}.
  81. *
  82. * @param {Cartesian3} offset The cartesian offset
  83. * @returns {OffsetGeometryInstanceAttribute} The new {@link OffsetGeometryInstanceAttribute} instance.
  84. */
  85. OffsetGeometryInstanceAttribute.fromCartesian3 = function (offset) {
  86. //>>includeStart('debug', pragmas.debug);
  87. Check.Check.defined("offset", offset);
  88. //>>includeEnd('debug');
  89. return new OffsetGeometryInstanceAttribute(offset.x, offset.y, offset.z);
  90. };
  91. /**
  92. * Converts a distance display condition to a typed array that can be used to assign a distance display condition attribute.
  93. *
  94. * @param {Cartesian3} offset The cartesian offset
  95. * @param {Float32Array} [result] The array to store the result in, if undefined a new instance will be created.
  96. * @returns {Float32Array} The modified result parameter or a new instance if result was undefined.
  97. *
  98. * @example
  99. * var attributes = primitive.getGeometryInstanceAttributes('an id');
  100. * attributes.modelMatrix = Cesium.OffsetGeometryInstanceAttribute.toValue(modelMatrix, attributes.modelMatrix);
  101. */
  102. OffsetGeometryInstanceAttribute.toValue = function (offset, result) {
  103. //>>includeStart('debug', pragmas.debug);
  104. Check.Check.defined("offset", offset);
  105. //>>includeEnd('debug');
  106. if (!when.defined(result)) {
  107. result = new Float32Array([offset.x, offset.y, offset.z]);
  108. }
  109. result[0] = offset.x;
  110. result[1] = offset.y;
  111. result[2] = offset.z;
  112. return result;
  113. };
  114. function transformToWorldCoordinates(
  115. instances,
  116. primitiveModelMatrix,
  117. scene3DOnly
  118. ) {
  119. var toWorld = !scene3DOnly;
  120. var length = instances.length;
  121. var i;
  122. if (!toWorld && length > 1) {
  123. var modelMatrix = instances[0].modelMatrix;
  124. for (i = 1; i < length; ++i) {
  125. if (!Transforms.Matrix4.equals(modelMatrix, instances[i].modelMatrix)) {
  126. toWorld = true;
  127. break;
  128. }
  129. }
  130. }
  131. if (toWorld) {
  132. for (i = 0; i < length; ++i) {
  133. if (when.defined(instances[i].geometry)) {
  134. GeometryPipeline.GeometryPipeline.transformToWorldCoordinates(instances[i]);
  135. }
  136. }
  137. } else {
  138. // Leave geometry in local coordinate system; auto update model-matrix.
  139. Transforms.Matrix4.multiplyTransformation(
  140. primitiveModelMatrix,
  141. instances[0].modelMatrix,
  142. primitiveModelMatrix
  143. );
  144. }
  145. }
  146. function addGeometryBatchId(geometry, batchId) {
  147. var attributes = geometry.attributes;
  148. var positionAttr = attributes.position;
  149. var numberOfComponents =
  150. positionAttr.values.length / positionAttr.componentsPerAttribute;
  151. attributes.batchId = new GeometryAttribute.GeometryAttribute({
  152. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  153. componentsPerAttribute: 1,
  154. values: new Float32Array(numberOfComponents),
  155. });
  156. var values = attributes.batchId.values;
  157. for (var j = 0; j < numberOfComponents; ++j) {
  158. values[j] = batchId;
  159. }
  160. }
  161. function addBatchIds(instances) {
  162. var length = instances.length;
  163. for (var i = 0; i < length; ++i) {
  164. var instance = instances[i];
  165. if (when.defined(instance.geometry)) {
  166. addGeometryBatchId(instance.geometry, i);
  167. } else if (
  168. when.defined(instance.westHemisphereGeometry) &&
  169. when.defined(instance.eastHemisphereGeometry)
  170. ) {
  171. addGeometryBatchId(instance.westHemisphereGeometry, i);
  172. addGeometryBatchId(instance.eastHemisphereGeometry, i);
  173. }
  174. }
  175. }
  176. function geometryPipeline(parameters) {
  177. var instances = parameters.instances;
  178. var projection = parameters.projection;
  179. var uintIndexSupport = parameters.elementIndexUintSupported;
  180. var scene3DOnly = parameters.scene3DOnly;
  181. var vertexCacheOptimize = parameters.vertexCacheOptimize;
  182. var compressVertices = parameters.compressVertices;
  183. var modelMatrix = parameters.modelMatrix;
  184. var i;
  185. var geometry;
  186. var primitiveType;
  187. var length = instances.length;
  188. for (i = 0; i < length; ++i) {
  189. if (when.defined(instances[i].geometry)) {
  190. primitiveType = instances[i].geometry.primitiveType;
  191. break;
  192. }
  193. }
  194. //>>includeStart('debug', pragmas.debug);
  195. for (i = 1; i < length; ++i) {
  196. if (
  197. when.defined(instances[i].geometry) &&
  198. instances[i].geometry.primitiveType !== primitiveType
  199. ) {
  200. throw new Check.DeveloperError(
  201. "All instance geometries must have the same primitiveType."
  202. );
  203. }
  204. }
  205. //>>includeEnd('debug');
  206. // Unify to world coordinates before combining.
  207. transformToWorldCoordinates(instances, modelMatrix, scene3DOnly);
  208. // Clip to IDL
  209. if (!scene3DOnly) {
  210. for (i = 0; i < length; ++i) {
  211. if (when.defined(instances[i].geometry)) {
  212. GeometryPipeline.GeometryPipeline.splitLongitude(instances[i]);
  213. }
  214. }
  215. }
  216. addBatchIds(instances);
  217. // Optimize for vertex shader caches
  218. if (vertexCacheOptimize) {
  219. for (i = 0; i < length; ++i) {
  220. var instance = instances[i];
  221. if (when.defined(instance.geometry)) {
  222. GeometryPipeline.GeometryPipeline.reorderForPostVertexCache(instance.geometry);
  223. GeometryPipeline.GeometryPipeline.reorderForPreVertexCache(instance.geometry);
  224. } else if (
  225. when.defined(instance.westHemisphereGeometry) &&
  226. when.defined(instance.eastHemisphereGeometry)
  227. ) {
  228. GeometryPipeline.GeometryPipeline.reorderForPostVertexCache(
  229. instance.westHemisphereGeometry
  230. );
  231. GeometryPipeline.GeometryPipeline.reorderForPreVertexCache(
  232. instance.westHemisphereGeometry
  233. );
  234. GeometryPipeline.GeometryPipeline.reorderForPostVertexCache(
  235. instance.eastHemisphereGeometry
  236. );
  237. GeometryPipeline.GeometryPipeline.reorderForPreVertexCache(
  238. instance.eastHemisphereGeometry
  239. );
  240. }
  241. }
  242. }
  243. // Combine into single geometry for better rendering performance.
  244. var geometries = GeometryPipeline.GeometryPipeline.combineInstances(instances);
  245. length = geometries.length;
  246. for (i = 0; i < length; ++i) {
  247. geometry = geometries[i];
  248. // Split positions for GPU RTE
  249. var attributes = geometry.attributes;
  250. var name;
  251. if (!scene3DOnly) {
  252. for (name in attributes) {
  253. if (
  254. attributes.hasOwnProperty(name) &&
  255. attributes[name].componentDatatype === ComponentDatatype.ComponentDatatype.DOUBLE
  256. ) {
  257. var name3D = name + "3D";
  258. var name2D = name + "2D";
  259. // Compute 2D positions
  260. GeometryPipeline.GeometryPipeline.projectTo2D(
  261. geometry,
  262. name,
  263. name3D,
  264. name2D,
  265. projection
  266. );
  267. if (when.defined(geometry.boundingSphere) && name === "position") {
  268. geometry.boundingSphereCV = Transforms.BoundingSphere.fromVertices(
  269. geometry.attributes.position2D.values
  270. );
  271. }
  272. GeometryPipeline.GeometryPipeline.encodeAttribute(
  273. geometry,
  274. name3D,
  275. name3D + "High",
  276. name3D + "Low"
  277. );
  278. GeometryPipeline.GeometryPipeline.encodeAttribute(
  279. geometry,
  280. name2D,
  281. name2D + "High",
  282. name2D + "Low"
  283. );
  284. }
  285. }
  286. } else {
  287. for (name in attributes) {
  288. if (
  289. attributes.hasOwnProperty(name) &&
  290. attributes[name].componentDatatype === ComponentDatatype.ComponentDatatype.DOUBLE
  291. ) {
  292. GeometryPipeline.GeometryPipeline.encodeAttribute(
  293. geometry,
  294. name,
  295. name + "3DHigh",
  296. name + "3DLow"
  297. );
  298. }
  299. }
  300. }
  301. // oct encode and pack normals, compress texture coordinates
  302. if (compressVertices) {
  303. GeometryPipeline.GeometryPipeline.compressVertices(geometry);
  304. }
  305. }
  306. if (!uintIndexSupport) {
  307. // Break into multiple geometries to fit within unsigned short indices if needed
  308. var splitGeometries = [];
  309. length = geometries.length;
  310. for (i = 0; i < length; ++i) {
  311. geometry = geometries[i];
  312. splitGeometries = splitGeometries.concat(
  313. GeometryPipeline.GeometryPipeline.fitToUnsignedShortIndices(geometry)
  314. );
  315. }
  316. geometries = splitGeometries;
  317. }
  318. return geometries;
  319. }
  320. function createPickOffsets(instances, geometryName, geometries, pickOffsets) {
  321. var offset;
  322. var indexCount;
  323. var geometryIndex;
  324. var offsetIndex = pickOffsets.length - 1;
  325. if (offsetIndex >= 0) {
  326. var pickOffset = pickOffsets[offsetIndex];
  327. offset = pickOffset.offset + pickOffset.count;
  328. geometryIndex = pickOffset.index;
  329. indexCount = geometries[geometryIndex].indices.length;
  330. } else {
  331. offset = 0;
  332. geometryIndex = 0;
  333. indexCount = geometries[geometryIndex].indices.length;
  334. }
  335. var length = instances.length;
  336. for (var i = 0; i < length; ++i) {
  337. var instance = instances[i];
  338. var geometry = instance[geometryName];
  339. if (!when.defined(geometry)) {
  340. continue;
  341. }
  342. var count = geometry.indices.length;
  343. if (offset + count > indexCount) {
  344. offset = 0;
  345. indexCount = geometries[++geometryIndex].indices.length;
  346. }
  347. pickOffsets.push({
  348. index: geometryIndex,
  349. offset: offset,
  350. count: count,
  351. });
  352. offset += count;
  353. }
  354. }
  355. function createInstancePickOffsets(instances, geometries) {
  356. var pickOffsets = [];
  357. createPickOffsets(instances, "geometry", geometries, pickOffsets);
  358. createPickOffsets(
  359. instances,
  360. "westHemisphereGeometry",
  361. geometries,
  362. pickOffsets
  363. );
  364. createPickOffsets(
  365. instances,
  366. "eastHemisphereGeometry",
  367. geometries,
  368. pickOffsets
  369. );
  370. return pickOffsets;
  371. }
  372. /**
  373. * @private
  374. */
  375. var PrimitivePipeline = {};
  376. /**
  377. * @private
  378. */
  379. PrimitivePipeline.combineGeometry = function (parameters) {
  380. var geometries;
  381. var attributeLocations;
  382. var instances = parameters.instances;
  383. var length = instances.length;
  384. var pickOffsets;
  385. var offsetInstanceExtend;
  386. var hasOffset = false;
  387. if (length > 0) {
  388. geometries = geometryPipeline(parameters);
  389. if (geometries.length > 0) {
  390. attributeLocations = GeometryPipeline.GeometryPipeline.createAttributeLocations(
  391. geometries[0]
  392. );
  393. if (parameters.createPickOffsets) {
  394. pickOffsets = createInstancePickOffsets(instances, geometries);
  395. }
  396. }
  397. if (
  398. when.defined(instances[0].attributes) &&
  399. when.defined(instances[0].attributes.offset)
  400. ) {
  401. offsetInstanceExtend = new Array(length);
  402. hasOffset = true;
  403. }
  404. }
  405. var boundingSpheres = new Array(length);
  406. var boundingSpheresCV = new Array(length);
  407. for (var i = 0; i < length; ++i) {
  408. var instance = instances[i];
  409. var geometry = instance.geometry;
  410. if (when.defined(geometry)) {
  411. boundingSpheres[i] = geometry.boundingSphere;
  412. boundingSpheresCV[i] = geometry.boundingSphereCV;
  413. if (hasOffset) {
  414. offsetInstanceExtend[i] = instance.geometry.offsetAttribute;
  415. }
  416. }
  417. var eastHemisphereGeometry = instance.eastHemisphereGeometry;
  418. var westHemisphereGeometry = instance.westHemisphereGeometry;
  419. if (when.defined(eastHemisphereGeometry) && when.defined(westHemisphereGeometry)) {
  420. if (
  421. when.defined(eastHemisphereGeometry.boundingSphere) &&
  422. when.defined(westHemisphereGeometry.boundingSphere)
  423. ) {
  424. boundingSpheres[i] = Transforms.BoundingSphere.union(
  425. eastHemisphereGeometry.boundingSphere,
  426. westHemisphereGeometry.boundingSphere
  427. );
  428. }
  429. if (
  430. when.defined(eastHemisphereGeometry.boundingSphereCV) &&
  431. when.defined(westHemisphereGeometry.boundingSphereCV)
  432. ) {
  433. boundingSpheresCV[i] = Transforms.BoundingSphere.union(
  434. eastHemisphereGeometry.boundingSphereCV,
  435. westHemisphereGeometry.boundingSphereCV
  436. );
  437. }
  438. }
  439. }
  440. return {
  441. geometries: geometries,
  442. modelMatrix: parameters.modelMatrix,
  443. attributeLocations: attributeLocations,
  444. pickOffsets: pickOffsets,
  445. offsetInstanceExtend: offsetInstanceExtend,
  446. boundingSpheres: boundingSpheres,
  447. boundingSpheresCV: boundingSpheresCV,
  448. };
  449. };
  450. function transferGeometry(geometry, transferableObjects) {
  451. var attributes = geometry.attributes;
  452. for (var name in attributes) {
  453. if (attributes.hasOwnProperty(name)) {
  454. var attribute = attributes[name];
  455. if (when.defined(attribute) && when.defined(attribute.values)) {
  456. transferableObjects.push(attribute.values.buffer);
  457. }
  458. }
  459. }
  460. if (when.defined(geometry.indices)) {
  461. transferableObjects.push(geometry.indices.buffer);
  462. }
  463. }
  464. function transferGeometries(geometries, transferableObjects) {
  465. var length = geometries.length;
  466. for (var i = 0; i < length; ++i) {
  467. transferGeometry(geometries[i], transferableObjects);
  468. }
  469. }
  470. // This function was created by simplifying packCreateGeometryResults into a count-only operation.
  471. function countCreateGeometryResults(items) {
  472. var count = 1;
  473. var length = items.length;
  474. for (var i = 0; i < length; i++) {
  475. var geometry = items[i];
  476. ++count;
  477. if (!when.defined(geometry)) {
  478. continue;
  479. }
  480. var attributes = geometry.attributes;
  481. count +=
  482. 7 +
  483. 2 * Transforms.BoundingSphere.packedLength +
  484. (when.defined(geometry.indices) ? geometry.indices.length : 0);
  485. for (var property in attributes) {
  486. if (
  487. attributes.hasOwnProperty(property) &&
  488. when.defined(attributes[property])
  489. ) {
  490. var attribute = attributes[property];
  491. count += 5 + attribute.values.length;
  492. }
  493. }
  494. }
  495. return count;
  496. }
  497. /**
  498. * @private
  499. */
  500. PrimitivePipeline.packCreateGeometryResults = function (
  501. items,
  502. transferableObjects
  503. ) {
  504. var packedData = new Float64Array(countCreateGeometryResults(items));
  505. var stringTable = [];
  506. var stringHash = {};
  507. var length = items.length;
  508. var count = 0;
  509. packedData[count++] = length;
  510. for (var i = 0; i < length; i++) {
  511. var geometry = items[i];
  512. var validGeometry = when.defined(geometry);
  513. packedData[count++] = validGeometry ? 1.0 : 0.0;
  514. if (!validGeometry) {
  515. continue;
  516. }
  517. packedData[count++] = geometry.primitiveType;
  518. packedData[count++] = geometry.geometryType;
  519. packedData[count++] = when.defaultValue(geometry.offsetAttribute, -1);
  520. var validBoundingSphere = when.defined(geometry.boundingSphere) ? 1.0 : 0.0;
  521. packedData[count++] = validBoundingSphere;
  522. if (validBoundingSphere) {
  523. Transforms.BoundingSphere.pack(geometry.boundingSphere, packedData, count);
  524. }
  525. count += Transforms.BoundingSphere.packedLength;
  526. var validBoundingSphereCV = when.defined(geometry.boundingSphereCV) ? 1.0 : 0.0;
  527. packedData[count++] = validBoundingSphereCV;
  528. if (validBoundingSphereCV) {
  529. Transforms.BoundingSphere.pack(geometry.boundingSphereCV, packedData, count);
  530. }
  531. count += Transforms.BoundingSphere.packedLength;
  532. var attributes = geometry.attributes;
  533. var attributesToWrite = [];
  534. for (var property in attributes) {
  535. if (
  536. attributes.hasOwnProperty(property) &&
  537. when.defined(attributes[property])
  538. ) {
  539. attributesToWrite.push(property);
  540. if (!when.defined(stringHash[property])) {
  541. stringHash[property] = stringTable.length;
  542. stringTable.push(property);
  543. }
  544. }
  545. }
  546. packedData[count++] = attributesToWrite.length;
  547. for (var q = 0; q < attributesToWrite.length; q++) {
  548. var name = attributesToWrite[q];
  549. var attribute = attributes[name];
  550. packedData[count++] = stringHash[name];
  551. packedData[count++] = attribute.componentDatatype;
  552. packedData[count++] = attribute.componentsPerAttribute;
  553. packedData[count++] = attribute.normalize ? 1 : 0;
  554. packedData[count++] = attribute.values.length;
  555. packedData.set(attribute.values, count);
  556. count += attribute.values.length;
  557. }
  558. var indicesLength = when.defined(geometry.indices) ? geometry.indices.length : 0;
  559. packedData[count++] = indicesLength;
  560. if (indicesLength > 0) {
  561. packedData.set(geometry.indices, count);
  562. count += indicesLength;
  563. }
  564. }
  565. transferableObjects.push(packedData.buffer);
  566. return {
  567. stringTable: stringTable,
  568. packedData: packedData,
  569. };
  570. };
  571. /**
  572. * @private
  573. */
  574. PrimitivePipeline.unpackCreateGeometryResults = function (
  575. createGeometryResult
  576. ) {
  577. var stringTable = createGeometryResult.stringTable;
  578. var packedGeometry = createGeometryResult.packedData;
  579. var i;
  580. var result = new Array(packedGeometry[0]);
  581. var resultIndex = 0;
  582. var packedGeometryIndex = 1;
  583. while (packedGeometryIndex < packedGeometry.length) {
  584. var valid = packedGeometry[packedGeometryIndex++] === 1.0;
  585. if (!valid) {
  586. result[resultIndex++] = undefined;
  587. continue;
  588. }
  589. var primitiveType = packedGeometry[packedGeometryIndex++];
  590. var geometryType = packedGeometry[packedGeometryIndex++];
  591. var offsetAttribute = packedGeometry[packedGeometryIndex++];
  592. if (offsetAttribute === -1) {
  593. offsetAttribute = undefined;
  594. }
  595. var boundingSphere;
  596. var boundingSphereCV;
  597. var validBoundingSphere = packedGeometry[packedGeometryIndex++] === 1.0;
  598. if (validBoundingSphere) {
  599. boundingSphere = Transforms.BoundingSphere.unpack(
  600. packedGeometry,
  601. packedGeometryIndex
  602. );
  603. }
  604. packedGeometryIndex += Transforms.BoundingSphere.packedLength;
  605. var validBoundingSphereCV = packedGeometry[packedGeometryIndex++] === 1.0;
  606. if (validBoundingSphereCV) {
  607. boundingSphereCV = Transforms.BoundingSphere.unpack(
  608. packedGeometry,
  609. packedGeometryIndex
  610. );
  611. }
  612. packedGeometryIndex += Transforms.BoundingSphere.packedLength;
  613. var length;
  614. var values;
  615. var componentsPerAttribute;
  616. var attributes = new GeometryAttributes.GeometryAttributes();
  617. var numAttributes = packedGeometry[packedGeometryIndex++];
  618. for (i = 0; i < numAttributes; i++) {
  619. var name = stringTable[packedGeometry[packedGeometryIndex++]];
  620. var componentDatatype = packedGeometry[packedGeometryIndex++];
  621. componentsPerAttribute = packedGeometry[packedGeometryIndex++];
  622. var normalize = packedGeometry[packedGeometryIndex++] !== 0;
  623. length = packedGeometry[packedGeometryIndex++];
  624. values = ComponentDatatype.ComponentDatatype.createTypedArray(componentDatatype, length);
  625. for (var valuesIndex = 0; valuesIndex < length; valuesIndex++) {
  626. values[valuesIndex] = packedGeometry[packedGeometryIndex++];
  627. }
  628. attributes[name] = new GeometryAttribute.GeometryAttribute({
  629. componentDatatype: componentDatatype,
  630. componentsPerAttribute: componentsPerAttribute,
  631. normalize: normalize,
  632. values: values,
  633. });
  634. }
  635. var indices;
  636. length = packedGeometry[packedGeometryIndex++];
  637. if (length > 0) {
  638. var numberOfVertices = values.length / componentsPerAttribute;
  639. indices = IndexDatatype.IndexDatatype.createTypedArray(numberOfVertices, length);
  640. for (i = 0; i < length; i++) {
  641. indices[i] = packedGeometry[packedGeometryIndex++];
  642. }
  643. }
  644. result[resultIndex++] = new GeometryAttribute.Geometry({
  645. primitiveType: primitiveType,
  646. geometryType: geometryType,
  647. boundingSphere: boundingSphere,
  648. boundingSphereCV: boundingSphereCV,
  649. indices: indices,
  650. attributes: attributes,
  651. offsetAttribute: offsetAttribute,
  652. });
  653. }
  654. return result;
  655. };
  656. function packInstancesForCombine(instances, transferableObjects) {
  657. var length = instances.length;
  658. var packedData = new Float64Array(1 + length * 19);
  659. var count = 0;
  660. packedData[count++] = length;
  661. for (var i = 0; i < length; i++) {
  662. var instance = instances[i];
  663. Transforms.Matrix4.pack(instance.modelMatrix, packedData, count);
  664. count += Transforms.Matrix4.packedLength;
  665. if (when.defined(instance.attributes) && when.defined(instance.attributes.offset)) {
  666. var values = instance.attributes.offset.value;
  667. packedData[count] = values[0];
  668. packedData[count + 1] = values[1];
  669. packedData[count + 2] = values[2];
  670. }
  671. count += 3;
  672. }
  673. transferableObjects.push(packedData.buffer);
  674. return packedData;
  675. }
  676. function unpackInstancesForCombine(data) {
  677. var packedInstances = data;
  678. var result = new Array(packedInstances[0]);
  679. var count = 0;
  680. var i = 1;
  681. while (i < packedInstances.length) {
  682. var modelMatrix = Transforms.Matrix4.unpack(packedInstances, i);
  683. var attributes;
  684. i += Transforms.Matrix4.packedLength;
  685. if (when.defined(packedInstances[i])) {
  686. attributes = {
  687. offset: new OffsetGeometryInstanceAttribute(
  688. packedInstances[i],
  689. packedInstances[i + 1],
  690. packedInstances[i + 2]
  691. ),
  692. };
  693. }
  694. i += 3;
  695. result[count++] = {
  696. modelMatrix: modelMatrix,
  697. attributes: attributes,
  698. };
  699. }
  700. return result;
  701. }
  702. /**
  703. * @private
  704. */
  705. PrimitivePipeline.packCombineGeometryParameters = function (
  706. parameters,
  707. transferableObjects
  708. ) {
  709. var createGeometryResults = parameters.createGeometryResults;
  710. var length = createGeometryResults.length;
  711. for (var i = 0; i < length; i++) {
  712. transferableObjects.push(createGeometryResults[i].packedData.buffer);
  713. }
  714. return {
  715. createGeometryResults: parameters.createGeometryResults,
  716. packedInstances: packInstancesForCombine(
  717. parameters.instances,
  718. transferableObjects
  719. ),
  720. ellipsoid: parameters.ellipsoid,
  721. isGeographic: parameters.projection instanceof Transforms.GeographicProjection,
  722. elementIndexUintSupported: parameters.elementIndexUintSupported,
  723. scene3DOnly: parameters.scene3DOnly,
  724. vertexCacheOptimize: parameters.vertexCacheOptimize,
  725. compressVertices: parameters.compressVertices,
  726. modelMatrix: parameters.modelMatrix,
  727. createPickOffsets: parameters.createPickOffsets,
  728. };
  729. };
  730. /**
  731. * @private
  732. */
  733. PrimitivePipeline.unpackCombineGeometryParameters = function (
  734. packedParameters
  735. ) {
  736. var instances = unpackInstancesForCombine(packedParameters.packedInstances);
  737. var createGeometryResults = packedParameters.createGeometryResults;
  738. var length = createGeometryResults.length;
  739. var instanceIndex = 0;
  740. for (var resultIndex = 0; resultIndex < length; resultIndex++) {
  741. var geometries = PrimitivePipeline.unpackCreateGeometryResults(
  742. createGeometryResults[resultIndex]
  743. );
  744. var geometriesLength = geometries.length;
  745. for (
  746. var geometryIndex = 0;
  747. geometryIndex < geometriesLength;
  748. geometryIndex++
  749. ) {
  750. var geometry = geometries[geometryIndex];
  751. var instance = instances[instanceIndex];
  752. instance.geometry = geometry;
  753. ++instanceIndex;
  754. }
  755. }
  756. var ellipsoid = Cartesian2.Ellipsoid.clone(packedParameters.ellipsoid);
  757. var projection = packedParameters.isGeographic
  758. ? new Transforms.GeographicProjection(ellipsoid)
  759. : new WebMercatorProjection.WebMercatorProjection(ellipsoid);
  760. return {
  761. instances: instances,
  762. ellipsoid: ellipsoid,
  763. projection: projection,
  764. elementIndexUintSupported: packedParameters.elementIndexUintSupported,
  765. scene3DOnly: packedParameters.scene3DOnly,
  766. vertexCacheOptimize: packedParameters.vertexCacheOptimize,
  767. compressVertices: packedParameters.compressVertices,
  768. modelMatrix: Transforms.Matrix4.clone(packedParameters.modelMatrix),
  769. createPickOffsets: packedParameters.createPickOffsets,
  770. };
  771. };
  772. function packBoundingSpheres(boundingSpheres) {
  773. var length = boundingSpheres.length;
  774. var bufferLength = 1 + (Transforms.BoundingSphere.packedLength + 1) * length;
  775. var buffer = new Float32Array(bufferLength);
  776. var bufferIndex = 0;
  777. buffer[bufferIndex++] = length;
  778. for (var i = 0; i < length; ++i) {
  779. var bs = boundingSpheres[i];
  780. if (!when.defined(bs)) {
  781. buffer[bufferIndex++] = 0.0;
  782. } else {
  783. buffer[bufferIndex++] = 1.0;
  784. Transforms.BoundingSphere.pack(boundingSpheres[i], buffer, bufferIndex);
  785. }
  786. bufferIndex += Transforms.BoundingSphere.packedLength;
  787. }
  788. return buffer;
  789. }
  790. function unpackBoundingSpheres(buffer) {
  791. var result = new Array(buffer[0]);
  792. var count = 0;
  793. var i = 1;
  794. while (i < buffer.length) {
  795. if (buffer[i++] === 1.0) {
  796. result[count] = Transforms.BoundingSphere.unpack(buffer, i);
  797. }
  798. ++count;
  799. i += Transforms.BoundingSphere.packedLength;
  800. }
  801. return result;
  802. }
  803. /**
  804. * @private
  805. */
  806. PrimitivePipeline.packCombineGeometryResults = function (
  807. results,
  808. transferableObjects
  809. ) {
  810. if (when.defined(results.geometries)) {
  811. transferGeometries(results.geometries, transferableObjects);
  812. }
  813. var packedBoundingSpheres = packBoundingSpheres(results.boundingSpheres);
  814. var packedBoundingSpheresCV = packBoundingSpheres(results.boundingSpheresCV);
  815. transferableObjects.push(
  816. packedBoundingSpheres.buffer,
  817. packedBoundingSpheresCV.buffer
  818. );
  819. return {
  820. geometries: results.geometries,
  821. attributeLocations: results.attributeLocations,
  822. modelMatrix: results.modelMatrix,
  823. pickOffsets: results.pickOffsets,
  824. offsetInstanceExtend: results.offsetInstanceExtend,
  825. boundingSpheres: packedBoundingSpheres,
  826. boundingSpheresCV: packedBoundingSpheresCV,
  827. };
  828. };
  829. /**
  830. * @private
  831. */
  832. PrimitivePipeline.unpackCombineGeometryResults = function (packedResult) {
  833. return {
  834. geometries: packedResult.geometries,
  835. attributeLocations: packedResult.attributeLocations,
  836. modelMatrix: packedResult.modelMatrix,
  837. pickOffsets: packedResult.pickOffsets,
  838. offsetInstanceExtend: packedResult.offsetInstanceExtend,
  839. boundingSpheres: unpackBoundingSpheres(packedResult.boundingSpheres),
  840. boundingSpheresCV: unpackBoundingSpheres(packedResult.boundingSpheresCV),
  841. };
  842. };
  843. exports.PrimitivePipeline = PrimitivePipeline;
  844. });