removeUnusedElements.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. import ForEach from "./ForEach.js";
  2. import forEachTextureInMaterial from "./forEachTextureInMaterial.js";
  3. import usesExtension from "./usesExtension.js";
  4. import defaultValue from "../../Core/defaultValue.js";
  5. import defined from "../../Core/defined.js";
  6. const allElementTypes = [
  7. "mesh",
  8. "node",
  9. "material",
  10. "accessor",
  11. "bufferView",
  12. "buffer",
  13. "texture",
  14. "sampler",
  15. "image",
  16. ];
  17. /**
  18. * Removes unused elements from gltf.
  19. *
  20. * @param {object} gltf A javascript object containing a glTF asset.
  21. * @param {string[]} [elementTypes=['mesh', 'node', 'material', 'accessor', 'bufferView', 'buffer']] Element types to be removed. Needs to be a subset of ['mesh', 'node', 'material', 'accessor', 'bufferView', 'buffer'], other items will be ignored.
  22. *
  23. * @private
  24. */
  25. function removeUnusedElements(gltf, elementTypes) {
  26. elementTypes = defaultValue(elementTypes, allElementTypes);
  27. allElementTypes.forEach(function (type) {
  28. if (elementTypes.indexOf(type) > -1) {
  29. removeUnusedElementsByType(gltf, type);
  30. }
  31. });
  32. return gltf;
  33. }
  34. const TypeToGltfElementName = {
  35. accessor: "accessors",
  36. buffer: "buffers",
  37. bufferView: "bufferViews",
  38. image: "images",
  39. node: "nodes",
  40. material: "materials",
  41. mesh: "meshes",
  42. sampler: "samplers",
  43. texture: "textures",
  44. };
  45. function removeUnusedElementsByType(gltf, type) {
  46. const name = TypeToGltfElementName[type];
  47. const arrayOfObjects = gltf[name];
  48. if (defined(arrayOfObjects)) {
  49. let removed = 0;
  50. const usedIds = getListOfElementsIdsInUse[type](gltf);
  51. const length = arrayOfObjects.length;
  52. for (let i = 0; i < length; ++i) {
  53. if (!usedIds[i]) {
  54. Remove[type](gltf, i - removed);
  55. removed++;
  56. }
  57. }
  58. }
  59. }
  60. /**
  61. * Contains functions for removing elements from a glTF hierarchy.
  62. * Since top-level glTF elements are arrays, when something is removed, referring
  63. * indices need to be updated.
  64. * @constructor
  65. *
  66. * @private
  67. */
  68. function Remove() {}
  69. Remove.accessor = function (gltf, accessorId) {
  70. const accessors = gltf.accessors;
  71. accessors.splice(accessorId, 1);
  72. ForEach.mesh(gltf, function (mesh) {
  73. ForEach.meshPrimitive(mesh, function (primitive) {
  74. // Update accessor ids for the primitives.
  75. ForEach.meshPrimitiveAttribute(
  76. primitive,
  77. function (attributeAccessorId, semantic) {
  78. if (attributeAccessorId > accessorId) {
  79. primitive.attributes[semantic]--;
  80. }
  81. }
  82. );
  83. // Update accessor ids for the targets.
  84. ForEach.meshPrimitiveTarget(primitive, function (target) {
  85. ForEach.meshPrimitiveTargetAttribute(
  86. target,
  87. function (attributeAccessorId, semantic) {
  88. if (attributeAccessorId > accessorId) {
  89. target[semantic]--;
  90. }
  91. }
  92. );
  93. });
  94. const indices = primitive.indices;
  95. if (defined(indices) && indices > accessorId) {
  96. primitive.indices--;
  97. }
  98. });
  99. });
  100. ForEach.skin(gltf, function (skin) {
  101. if (
  102. defined(skin.inverseBindMatrices) &&
  103. skin.inverseBindMatrices > accessorId
  104. ) {
  105. skin.inverseBindMatrices--;
  106. }
  107. });
  108. ForEach.animation(gltf, function (animation) {
  109. ForEach.animationSampler(animation, function (sampler) {
  110. if (defined(sampler.input) && sampler.input > accessorId) {
  111. sampler.input--;
  112. }
  113. if (defined(sampler.output) && sampler.output > accessorId) {
  114. sampler.output--;
  115. }
  116. });
  117. });
  118. };
  119. Remove.buffer = function (gltf, bufferId) {
  120. const buffers = gltf.buffers;
  121. buffers.splice(bufferId, 1);
  122. ForEach.bufferView(gltf, function (bufferView) {
  123. if (defined(bufferView.buffer) && bufferView.buffer > bufferId) {
  124. bufferView.buffer--;
  125. }
  126. if (
  127. defined(bufferView.extensions) &&
  128. defined(bufferView.extensions.EXT_meshopt_compression)
  129. ) {
  130. bufferView.extensions.EXT_meshopt_compression.buffer--;
  131. }
  132. });
  133. };
  134. Remove.bufferView = function (gltf, bufferViewId) {
  135. const bufferViews = gltf.bufferViews;
  136. bufferViews.splice(bufferViewId, 1);
  137. ForEach.accessor(gltf, function (accessor) {
  138. if (defined(accessor.bufferView) && accessor.bufferView > bufferViewId) {
  139. accessor.bufferView--;
  140. }
  141. });
  142. ForEach.shader(gltf, function (shader) {
  143. if (defined(shader.bufferView) && shader.bufferView > bufferViewId) {
  144. shader.bufferView--;
  145. }
  146. });
  147. ForEach.image(gltf, function (image) {
  148. if (defined(image.bufferView) && image.bufferView > bufferViewId) {
  149. image.bufferView--;
  150. }
  151. });
  152. if (usesExtension(gltf, "KHR_draco_mesh_compression")) {
  153. ForEach.mesh(gltf, function (mesh) {
  154. ForEach.meshPrimitive(mesh, function (primitive) {
  155. if (
  156. defined(primitive.extensions) &&
  157. defined(primitive.extensions.KHR_draco_mesh_compression)
  158. ) {
  159. if (
  160. primitive.extensions.KHR_draco_mesh_compression.bufferView >
  161. bufferViewId
  162. ) {
  163. primitive.extensions.KHR_draco_mesh_compression.bufferView--;
  164. }
  165. }
  166. });
  167. });
  168. }
  169. if (usesExtension(gltf, "EXT_feature_metadata")) {
  170. const extension = gltf.extensions.EXT_feature_metadata;
  171. const featureTables = extension.featureTables;
  172. for (const featureTableId in featureTables) {
  173. if (featureTables.hasOwnProperty(featureTableId)) {
  174. const featureTable = featureTables[featureTableId];
  175. const properties = featureTable.properties;
  176. if (defined(properties)) {
  177. for (const propertyId in properties) {
  178. if (properties.hasOwnProperty(propertyId)) {
  179. const property = properties[propertyId];
  180. if (
  181. defined(property.bufferView) &&
  182. property.bufferView > bufferViewId
  183. ) {
  184. property.bufferView--;
  185. }
  186. if (
  187. defined(property.arrayOffsetBufferView) &&
  188. property.arrayOffsetBufferView > bufferViewId
  189. ) {
  190. property.arrayOffsetBufferView--;
  191. }
  192. if (
  193. defined(property.stringOffsetBufferView) &&
  194. property.stringOffsetBufferView > bufferViewId
  195. ) {
  196. property.stringOffsetBufferView--;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. if (usesExtension(gltf, "EXT_structural_metadata")) {
  205. const extension = gltf.extensions.EXT_structural_metadata;
  206. const propertyTables = extension.propertyTables;
  207. if (defined(propertyTables)) {
  208. const propertyTablesLength = propertyTables.length;
  209. for (let i = 0; i < propertyTablesLength; ++i) {
  210. const propertyTable = propertyTables[i];
  211. const properties = propertyTable.properties;
  212. for (const propertyId in properties) {
  213. if (properties.hasOwnProperty(propertyId)) {
  214. const property = properties[propertyId];
  215. if (defined(property.values) && property.values > bufferViewId) {
  216. property.values--;
  217. }
  218. if (
  219. defined(property.arrayOffsets) &&
  220. property.arrayOffsets > bufferViewId
  221. ) {
  222. property.arrayOffsets--;
  223. }
  224. if (
  225. defined(property.stringOffsets) &&
  226. property.stringOffsets > bufferViewId
  227. ) {
  228. property.stringOffsets--;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. };
  236. Remove.image = function (gltf, imageId) {
  237. const images = gltf.images;
  238. images.splice(imageId, 1);
  239. ForEach.texture(gltf, function (texture) {
  240. if (defined(texture.source)) {
  241. if (texture.source > imageId) {
  242. --texture.source;
  243. }
  244. }
  245. const ext = texture.extensions;
  246. if (
  247. defined(ext) &&
  248. defined(ext.EXT_texture_webp) &&
  249. ext.EXT_texture_webp.source > imageId
  250. ) {
  251. --texture.extensions.EXT_texture_webp.source;
  252. } else if (
  253. defined(ext) &&
  254. defined(ext.KHR_texture_basisu) &&
  255. ext.KHR_texture_basisu.source > imageId
  256. ) {
  257. --texture.extensions.KHR_texture_basisu.source;
  258. }
  259. });
  260. };
  261. Remove.mesh = function (gltf, meshId) {
  262. const meshes = gltf.meshes;
  263. meshes.splice(meshId, 1);
  264. ForEach.node(gltf, function (node) {
  265. if (defined(node.mesh)) {
  266. if (node.mesh > meshId) {
  267. node.mesh--;
  268. } else if (node.mesh === meshId) {
  269. // Remove reference to deleted mesh
  270. delete node.mesh;
  271. }
  272. }
  273. });
  274. };
  275. Remove.node = function (gltf, nodeId) {
  276. const nodes = gltf.nodes;
  277. nodes.splice(nodeId, 1);
  278. // Shift all node references
  279. ForEach.skin(gltf, function (skin) {
  280. if (defined(skin.skeleton) && skin.skeleton > nodeId) {
  281. skin.skeleton--;
  282. }
  283. skin.joints = skin.joints.map(function (x) {
  284. return x > nodeId ? x - 1 : x;
  285. });
  286. });
  287. ForEach.animation(gltf, function (animation) {
  288. ForEach.animationChannel(animation, function (channel) {
  289. if (
  290. defined(channel.target) &&
  291. defined(channel.target.node) &&
  292. channel.target.node > nodeId
  293. ) {
  294. channel.target.node--;
  295. }
  296. });
  297. });
  298. ForEach.technique(gltf, function (technique) {
  299. ForEach.techniqueUniform(technique, function (uniform) {
  300. if (defined(uniform.node) && uniform.node > nodeId) {
  301. uniform.node--;
  302. }
  303. });
  304. });
  305. ForEach.node(gltf, function (node) {
  306. if (!defined(node.children)) {
  307. return;
  308. }
  309. node.children = node.children
  310. .filter(function (x) {
  311. return x !== nodeId; // Remove
  312. })
  313. .map(function (x) {
  314. return x > nodeId ? x - 1 : x; // Shift indices
  315. });
  316. });
  317. ForEach.scene(gltf, function (scene) {
  318. scene.nodes = scene.nodes
  319. .filter(function (x) {
  320. return x !== nodeId; // Remove
  321. })
  322. .map(function (x) {
  323. return x > nodeId ? x - 1 : x; // Shift indices
  324. });
  325. });
  326. };
  327. Remove.material = function (gltf, materialId) {
  328. const materials = gltf.materials;
  329. materials.splice(materialId, 1);
  330. // Shift other material ids
  331. ForEach.mesh(gltf, function (mesh) {
  332. ForEach.meshPrimitive(mesh, function (primitive) {
  333. if (defined(primitive.material) && primitive.material > materialId) {
  334. primitive.material--;
  335. }
  336. });
  337. });
  338. };
  339. Remove.sampler = function (gltf, samplerId) {
  340. const samplers = gltf.samplers;
  341. samplers.splice(samplerId, 1);
  342. ForEach.texture(gltf, function (texture) {
  343. if (defined(texture.sampler)) {
  344. if (texture.sampler > samplerId) {
  345. --texture.sampler;
  346. }
  347. }
  348. });
  349. };
  350. Remove.texture = function (gltf, textureId) {
  351. const textures = gltf.textures;
  352. textures.splice(textureId, 1);
  353. ForEach.material(gltf, function (material) {
  354. forEachTextureInMaterial(material, function (textureIndex, textureInfo) {
  355. if (textureInfo.index > textureId) {
  356. --textureInfo.index;
  357. }
  358. });
  359. });
  360. if (usesExtension(gltf, "EXT_feature_metadata")) {
  361. ForEach.mesh(gltf, function (mesh) {
  362. ForEach.meshPrimitive(mesh, function (primitive) {
  363. const extensions = primitive.extensions;
  364. if (defined(extensions) && defined(extensions.EXT_feature_metadata)) {
  365. const extension = extensions.EXT_feature_metadata;
  366. const featureIdTextures = extension.featureIdTextures;
  367. if (defined(featureIdTextures)) {
  368. const featureIdTexturesLength = featureIdTextures.length;
  369. for (let i = 0; i < featureIdTexturesLength; ++i) {
  370. const featureIdTexture = featureIdTextures[i];
  371. const textureInfo = featureIdTexture.featureIds.texture;
  372. if (textureInfo.index > textureId) {
  373. --textureInfo.index;
  374. }
  375. }
  376. }
  377. }
  378. });
  379. });
  380. const extension = gltf.extensions.EXT_feature_metadata;
  381. const featureTextures = extension.featureTextures;
  382. for (const featureTextureId in featureTextures) {
  383. if (featureTextures.hasOwnProperty(featureTextureId)) {
  384. const featureTexture = featureTextures[featureTextureId];
  385. const properties = featureTexture.properties;
  386. if (defined(properties)) {
  387. for (const propertyId in properties) {
  388. if (properties.hasOwnProperty(propertyId)) {
  389. const property = properties[propertyId];
  390. const textureInfo = property.texture;
  391. if (textureInfo.index > textureId) {
  392. --textureInfo.index;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. }
  399. }
  400. if (usesExtension(gltf, "EXT_mesh_features")) {
  401. ForEach.mesh(gltf, function (mesh) {
  402. ForEach.meshPrimitive(mesh, function (primitive) {
  403. const extensions = primitive.extensions;
  404. if (defined(extensions) && defined(extensions.EXT_mesh_features)) {
  405. const extension = extensions.EXT_mesh_features;
  406. const featureIds = extension.featureIds;
  407. if (defined(featureIds)) {
  408. const featureIdsLength = featureIds.length;
  409. for (let i = 0; i < featureIdsLength; ++i) {
  410. const featureId = featureIds[i];
  411. if (defined(featureId.texture)) {
  412. if (featureId.texture.index > textureId) {
  413. --featureId.texture.index;
  414. }
  415. }
  416. }
  417. }
  418. }
  419. });
  420. });
  421. }
  422. if (usesExtension(gltf, "EXT_structural_metadata")) {
  423. const extension = gltf.extensions.EXT_structural_metadata;
  424. const propertyTextures = extension.propertyTextures;
  425. if (defined(propertyTextures)) {
  426. const propertyTexturesLength = propertyTextures.length;
  427. for (let i = 0; i < propertyTexturesLength; ++i) {
  428. const propertyTexture = propertyTextures[i];
  429. const properties = propertyTexture.properties;
  430. for (const propertyId in properties) {
  431. if (properties.hasOwnProperty(propertyId)) {
  432. const property = properties[propertyId];
  433. if (property.index > textureId) {
  434. --property.index;
  435. }
  436. }
  437. }
  438. }
  439. }
  440. }
  441. };
  442. /**
  443. * Contains functions for getting a list of element ids in use by the glTF asset.
  444. * @constructor
  445. *
  446. * @private
  447. */
  448. function getListOfElementsIdsInUse() {}
  449. getListOfElementsIdsInUse.accessor = function (gltf) {
  450. // Calculate accessor's that are currently in use.
  451. const usedAccessorIds = {};
  452. ForEach.mesh(gltf, function (mesh) {
  453. ForEach.meshPrimitive(mesh, function (primitive) {
  454. ForEach.meshPrimitiveAttribute(primitive, function (accessorId) {
  455. usedAccessorIds[accessorId] = true;
  456. });
  457. ForEach.meshPrimitiveTarget(primitive, function (target) {
  458. ForEach.meshPrimitiveTargetAttribute(target, function (accessorId) {
  459. usedAccessorIds[accessorId] = true;
  460. });
  461. });
  462. const indices = primitive.indices;
  463. if (defined(indices)) {
  464. usedAccessorIds[indices] = true;
  465. }
  466. });
  467. });
  468. ForEach.skin(gltf, function (skin) {
  469. if (defined(skin.inverseBindMatrices)) {
  470. usedAccessorIds[skin.inverseBindMatrices] = true;
  471. }
  472. });
  473. ForEach.animation(gltf, function (animation) {
  474. ForEach.animationSampler(animation, function (sampler) {
  475. if (defined(sampler.input)) {
  476. usedAccessorIds[sampler.input] = true;
  477. }
  478. if (defined(sampler.output)) {
  479. usedAccessorIds[sampler.output] = true;
  480. }
  481. });
  482. });
  483. if (usesExtension(gltf, "EXT_mesh_gpu_instancing")) {
  484. ForEach.node(gltf, function (node) {
  485. if (
  486. defined(node.extensions) &&
  487. defined(node.extensions.EXT_mesh_gpu_instancing)
  488. ) {
  489. Object.keys(node.extensions.EXT_mesh_gpu_instancing.attributes).forEach(
  490. function (key) {
  491. const attributeAccessorId =
  492. node.extensions.EXT_mesh_gpu_instancing.attributes[key];
  493. usedAccessorIds[attributeAccessorId] = true;
  494. }
  495. );
  496. }
  497. });
  498. }
  499. return usedAccessorIds;
  500. };
  501. getListOfElementsIdsInUse.buffer = function (gltf) {
  502. // Calculate buffer's that are currently in use.
  503. const usedBufferIds = {};
  504. ForEach.bufferView(gltf, function (bufferView) {
  505. if (defined(bufferView.buffer)) {
  506. usedBufferIds[bufferView.buffer] = true;
  507. }
  508. if (
  509. defined(bufferView.extensions) &&
  510. defined(bufferView.extensions.EXT_meshopt_compression)
  511. ) {
  512. usedBufferIds[
  513. bufferView.extensions.EXT_meshopt_compression.buffer
  514. ] = true;
  515. }
  516. });
  517. return usedBufferIds;
  518. };
  519. getListOfElementsIdsInUse.bufferView = function (gltf) {
  520. // Calculate bufferView's that are currently in use.
  521. const usedBufferViewIds = {};
  522. ForEach.accessor(gltf, function (accessor) {
  523. if (defined(accessor.bufferView)) {
  524. usedBufferViewIds[accessor.bufferView] = true;
  525. }
  526. });
  527. ForEach.shader(gltf, function (shader) {
  528. if (defined(shader.bufferView)) {
  529. usedBufferViewIds[shader.bufferView] = true;
  530. }
  531. });
  532. ForEach.image(gltf, function (image) {
  533. if (defined(image.bufferView)) {
  534. usedBufferViewIds[image.bufferView] = true;
  535. }
  536. });
  537. if (usesExtension(gltf, "KHR_draco_mesh_compression")) {
  538. ForEach.mesh(gltf, function (mesh) {
  539. ForEach.meshPrimitive(mesh, function (primitive) {
  540. if (
  541. defined(primitive.extensions) &&
  542. defined(primitive.extensions.KHR_draco_mesh_compression)
  543. ) {
  544. usedBufferViewIds[
  545. primitive.extensions.KHR_draco_mesh_compression.bufferView
  546. ] = true;
  547. }
  548. });
  549. });
  550. }
  551. if (usesExtension(gltf, "EXT_feature_metadata")) {
  552. const extension = gltf.extensions.EXT_feature_metadata;
  553. const featureTables = extension.featureTables;
  554. for (const featureTableId in featureTables) {
  555. if (featureTables.hasOwnProperty(featureTableId)) {
  556. const featureTable = featureTables[featureTableId];
  557. const properties = featureTable.properties;
  558. if (defined(properties)) {
  559. for (const propertyId in properties) {
  560. if (properties.hasOwnProperty(propertyId)) {
  561. const property = properties[propertyId];
  562. if (defined(property.bufferView)) {
  563. usedBufferViewIds[property.bufferView] = true;
  564. }
  565. if (defined(property.arrayOffsetBufferView)) {
  566. usedBufferViewIds[property.arrayOffsetBufferView] = true;
  567. }
  568. if (defined(property.stringOffsetBufferView)) {
  569. usedBufferViewIds[property.stringOffsetBufferView] = true;
  570. }
  571. }
  572. }
  573. }
  574. }
  575. }
  576. }
  577. if (usesExtension(gltf, "EXT_structural_metadata")) {
  578. const extension = gltf.extensions.EXT_structural_metadata;
  579. const propertyTables = extension.propertyTables;
  580. if (defined(propertyTables)) {
  581. const propertyTablesLength = propertyTables.length;
  582. for (let i = 0; i < propertyTablesLength; ++i) {
  583. const propertyTable = propertyTables[i];
  584. const properties = propertyTable.properties;
  585. for (const propertyId in properties) {
  586. if (properties.hasOwnProperty(propertyId)) {
  587. const property = properties[propertyId];
  588. if (defined(property.values)) {
  589. usedBufferViewIds[property.values] = true;
  590. }
  591. if (defined(property.arrayOffsets)) {
  592. usedBufferViewIds[property.arrayOffsets] = true;
  593. }
  594. if (defined(property.stringOffsets)) {
  595. usedBufferViewIds[property.stringOffsets] = true;
  596. }
  597. }
  598. }
  599. }
  600. }
  601. }
  602. return usedBufferViewIds;
  603. };
  604. getListOfElementsIdsInUse.image = function (gltf) {
  605. const usedImageIds = {};
  606. ForEach.texture(gltf, function (texture) {
  607. if (defined(texture.source)) {
  608. usedImageIds[texture.source] = true;
  609. }
  610. if (
  611. defined(texture.extensions) &&
  612. defined(texture.extensions.EXT_texture_webp)
  613. ) {
  614. usedImageIds[texture.extensions.EXT_texture_webp.source] = true;
  615. } else if (
  616. defined(texture.extensions) &&
  617. defined(texture.extensions.KHR_texture_basisu)
  618. ) {
  619. usedImageIds[texture.extensions.KHR_texture_basisu.source] = true;
  620. }
  621. });
  622. return usedImageIds;
  623. };
  624. getListOfElementsIdsInUse.mesh = function (gltf) {
  625. const usedMeshIds = {};
  626. ForEach.node(gltf, function (node) {
  627. if (defined(node.mesh && defined(gltf.meshes))) {
  628. const mesh = gltf.meshes[node.mesh];
  629. if (
  630. defined(mesh) &&
  631. defined(mesh.primitives) &&
  632. mesh.primitives.length > 0
  633. ) {
  634. usedMeshIds[node.mesh] = true;
  635. }
  636. }
  637. });
  638. return usedMeshIds;
  639. };
  640. // Check if node is empty. It is considered empty if neither referencing
  641. // mesh, camera, extensions and has no children
  642. function nodeIsEmpty(gltf, nodeId, usedNodeIds) {
  643. const node = gltf.nodes[nodeId];
  644. if (
  645. defined(node.mesh) ||
  646. defined(node.camera) ||
  647. defined(node.skin) ||
  648. defined(node.weights) ||
  649. defined(node.extras) ||
  650. (defined(node.extensions) && Object.keys(node.extensions).length !== 0) ||
  651. defined(usedNodeIds[nodeId])
  652. ) {
  653. return false;
  654. }
  655. // Empty if no children or children are all empty nodes
  656. return (
  657. !defined(node.children) ||
  658. node.children.filter(function (n) {
  659. return !nodeIsEmpty(gltf, n, usedNodeIds);
  660. }).length === 0
  661. );
  662. }
  663. getListOfElementsIdsInUse.node = function (gltf) {
  664. const usedNodeIds = {};
  665. ForEach.skin(gltf, function (skin) {
  666. if (defined(skin.skeleton)) {
  667. usedNodeIds[skin.skeleton] = true;
  668. }
  669. ForEach.skinJoint(skin, function (joint) {
  670. usedNodeIds[joint] = true;
  671. });
  672. });
  673. ForEach.animation(gltf, function (animation) {
  674. ForEach.animationChannel(animation, function (channel) {
  675. if (defined(channel.target) && defined(channel.target.node)) {
  676. usedNodeIds[channel.target.node] = true;
  677. }
  678. });
  679. });
  680. ForEach.technique(gltf, function (technique) {
  681. ForEach.techniqueUniform(technique, function (uniform) {
  682. if (defined(uniform.node)) {
  683. usedNodeIds[uniform.node] = true;
  684. }
  685. });
  686. });
  687. ForEach.node(gltf, function (node, nodeId) {
  688. if (!nodeIsEmpty(gltf, nodeId, usedNodeIds)) {
  689. usedNodeIds[nodeId] = true;
  690. }
  691. });
  692. return usedNodeIds;
  693. };
  694. getListOfElementsIdsInUse.material = function (gltf) {
  695. const usedMaterialIds = {};
  696. ForEach.mesh(gltf, function (mesh) {
  697. ForEach.meshPrimitive(mesh, function (primitive) {
  698. if (defined(primitive.material)) {
  699. usedMaterialIds[primitive.material] = true;
  700. }
  701. });
  702. });
  703. return usedMaterialIds;
  704. };
  705. getListOfElementsIdsInUse.texture = function (gltf) {
  706. const usedTextureIds = {};
  707. ForEach.material(gltf, function (material) {
  708. forEachTextureInMaterial(material, function (textureId) {
  709. usedTextureIds[textureId] = true;
  710. });
  711. });
  712. if (usesExtension(gltf, "EXT_feature_metadata")) {
  713. ForEach.mesh(gltf, function (mesh) {
  714. ForEach.meshPrimitive(mesh, function (primitive) {
  715. const extensions = primitive.extensions;
  716. if (defined(extensions) && defined(extensions.EXT_feature_metadata)) {
  717. const extension = extensions.EXT_feature_metadata;
  718. const featureIdTextures = extension.featureIdTextures;
  719. if (defined(featureIdTextures)) {
  720. const featureIdTexturesLength = featureIdTextures.length;
  721. for (let i = 0; i < featureIdTexturesLength; ++i) {
  722. const featureIdTexture = featureIdTextures[i];
  723. const textureInfo = featureIdTexture.featureIds.texture;
  724. usedTextureIds[textureInfo.index] = true;
  725. }
  726. }
  727. }
  728. });
  729. });
  730. const extension = gltf.extensions.EXT_feature_metadata;
  731. const featureTextures = extension.featureTextures;
  732. for (const featureTextureId in featureTextures) {
  733. if (featureTextures.hasOwnProperty(featureTextureId)) {
  734. const featureTexture = featureTextures[featureTextureId];
  735. const properties = featureTexture.properties;
  736. if (defined(properties)) {
  737. for (const propertyId in properties) {
  738. if (properties.hasOwnProperty(propertyId)) {
  739. const property = properties[propertyId];
  740. const textureInfo = property.texture;
  741. usedTextureIds[textureInfo.index] = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. }
  748. if (usesExtension(gltf, "EXT_mesh_features")) {
  749. ForEach.mesh(gltf, function (mesh) {
  750. ForEach.meshPrimitive(mesh, function (primitive) {
  751. const extensions = primitive.extensions;
  752. if (defined(extensions) && defined(extensions.EXT_mesh_features)) {
  753. const extension = extensions.EXT_mesh_features;
  754. const featureIds = extension.featureIds;
  755. if (defined(featureIds)) {
  756. const featureIdsLength = featureIds.length;
  757. for (let i = 0; i < featureIdsLength; ++i) {
  758. const featureId = featureIds[i];
  759. if (defined(featureId.texture)) {
  760. usedTextureIds[featureId.texture.index] = true;
  761. }
  762. }
  763. }
  764. }
  765. });
  766. });
  767. }
  768. if (usesExtension(gltf, "EXT_structural_metadata")) {
  769. const extension = gltf.extensions.EXT_structural_metadata;
  770. const propertyTextures = extension.propertyTextures;
  771. if (defined(propertyTextures)) {
  772. const propertyTexturesLength = propertyTextures.length;
  773. for (let i = 0; i < propertyTexturesLength; ++i) {
  774. const propertyTexture = propertyTextures[i];
  775. const properties = propertyTexture.properties;
  776. for (const propertyId in properties) {
  777. if (properties.hasOwnProperty(propertyId)) {
  778. const property = properties[propertyId];
  779. usedTextureIds[property.index] = true;
  780. }
  781. }
  782. }
  783. }
  784. }
  785. return usedTextureIds;
  786. };
  787. getListOfElementsIdsInUse.sampler = function (gltf) {
  788. const usedSamplerIds = {};
  789. ForEach.texture(gltf, function (texture) {
  790. if (defined(texture.sampler)) {
  791. usedSamplerIds[texture.sampler] = true;
  792. }
  793. });
  794. return usedSamplerIds;
  795. };
  796. export default removeUnusedElements;