PrimitivePipeline.js 24 KB

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