PrimitivePipeline.js 38 KB

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