ForEach.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import hasExtension from './hasExtension.js'
  2. import defined from '../../Core/defined.js'
  3. /**
  4. * Contains traversal functions for processing elements of the glTF hierarchy.
  5. * @constructor
  6. *
  7. * @private
  8. */
  9. function ForEach() {
  10. }
  11. /**
  12. * Fallback for glTF 1.0
  13. * @private
  14. */
  15. ForEach.objectLegacy = function(objects, handler) {
  16. if (defined(objects)) {
  17. for (var objectId in objects) {
  18. if (Object.prototype.hasOwnProperty.call(objects, objectId)) {
  19. var object = objects[objectId];
  20. var value = handler(object, objectId);
  21. if (defined(value)) {
  22. return value;
  23. }
  24. }
  25. }
  26. }
  27. };
  28. /**
  29. * @private
  30. */
  31. ForEach.object = function(arrayOfObjects, handler) {
  32. if (defined(arrayOfObjects)) {
  33. var length = arrayOfObjects.length;
  34. for (var i = 0; i < length; i++) {
  35. var object = arrayOfObjects[i];
  36. var value = handler(object, i);
  37. if (defined(value)) {
  38. return value;
  39. }
  40. }
  41. }
  42. };
  43. /**
  44. * Supports glTF 1.0 and 2.0
  45. * @private
  46. */
  47. ForEach.topLevel = function(gltf, name, handler) {
  48. var gltfProperty = gltf[name];
  49. if (defined(gltfProperty) && !Array.isArray(gltfProperty)) {
  50. return ForEach.objectLegacy(gltfProperty, handler);
  51. }
  52. return ForEach.object(gltfProperty, handler);
  53. };
  54. ForEach.accessor = function(gltf, handler) {
  55. return ForEach.topLevel(gltf, 'accessors', handler);
  56. };
  57. ForEach.accessorWithSemantic = function(gltf, semantic, handler) {
  58. var visited = {};
  59. return ForEach.mesh(gltf, function(mesh) {
  60. return ForEach.meshPrimitive(mesh, function(primitive) {
  61. var valueForEach = ForEach.meshPrimitiveAttribute(primitive, function(accessorId, attributeSemantic) {
  62. if (attributeSemantic.indexOf(semantic) === 0 && !defined(visited[accessorId])) {
  63. visited[accessorId] = true;
  64. var value = handler(accessorId);
  65. if (defined(value)) {
  66. return value;
  67. }
  68. }
  69. });
  70. if (defined(valueForEach)) {
  71. return valueForEach;
  72. }
  73. return ForEach.meshPrimitiveTarget(primitive, function(target) {
  74. return ForEach.meshPrimitiveTargetAttribute(target, function(accessorId, attributeSemantic) {
  75. if (attributeSemantic.indexOf(semantic) === 0 && !defined(visited[accessorId])) {
  76. visited[accessorId] = true;
  77. var value = handler(accessorId);
  78. if (defined(value)) {
  79. return value;
  80. }
  81. }
  82. });
  83. });
  84. });
  85. });
  86. };
  87. ForEach.accessorContainingVertexAttributeData = function(gltf, handler) {
  88. var visited = {};
  89. return ForEach.mesh(gltf, function(mesh) {
  90. return ForEach.meshPrimitive(mesh, function(primitive) {
  91. var valueForEach = ForEach.meshPrimitiveAttribute(primitive, function(accessorId) {
  92. if (!defined(visited[accessorId])) {
  93. visited[accessorId] = true;
  94. var value = handler(accessorId);
  95. if (defined(value)) {
  96. return value;
  97. }
  98. }
  99. });
  100. if (defined(valueForEach)) {
  101. return valueForEach;
  102. }
  103. return ForEach.meshPrimitiveTarget(primitive, function(target) {
  104. return ForEach.meshPrimitiveTargetAttribute(target, function(accessorId) {
  105. if (!defined(visited[accessorId])) {
  106. visited[accessorId] = true;
  107. var value = handler(accessorId);
  108. if (defined(value)) {
  109. return value;
  110. }
  111. }
  112. });
  113. });
  114. });
  115. });
  116. };
  117. ForEach.accessorContainingIndexData = function(gltf, handler) {
  118. var visited = {};
  119. return ForEach.mesh(gltf, function(mesh) {
  120. return ForEach.meshPrimitive(mesh, function(primitive) {
  121. var indices = primitive.indices;
  122. if (defined(indices) && !defined(visited[indices])) {
  123. visited[indices] = true;
  124. var value = handler(indices);
  125. if (defined(value)) {
  126. return value;
  127. }
  128. }
  129. });
  130. });
  131. };
  132. ForEach.animation = function(gltf, handler) {
  133. return ForEach.topLevel(gltf, 'animations', handler);
  134. };
  135. ForEach.animationChannel = function(animation, handler) {
  136. var channels = animation.channels;
  137. return ForEach.object(channels, handler);
  138. };
  139. ForEach.animationSampler = function(animation, handler) {
  140. var samplers = animation.samplers;
  141. return ForEach.object(samplers, handler);
  142. };
  143. ForEach.buffer = function(gltf, handler) {
  144. return ForEach.topLevel(gltf, 'buffers', handler);
  145. };
  146. ForEach.bufferView = function(gltf, handler) {
  147. return ForEach.topLevel(gltf, 'bufferViews', handler);
  148. };
  149. ForEach.camera = function(gltf, handler) {
  150. return ForEach.topLevel(gltf, 'cameras', handler);
  151. };
  152. ForEach.image = function(gltf, handler) {
  153. return ForEach.topLevel(gltf, 'images', handler);
  154. };
  155. ForEach.compressedImage = function(image, handler) {
  156. if (defined(image.extras)) {
  157. var compressedImages = image.extras.compressedImage3DTiles;
  158. for (var type in compressedImages) {
  159. if (Object.prototype.hasOwnProperty.call(compressedImages, type)) {
  160. var compressedImage = compressedImages[type];
  161. var value = handler(compressedImage, type);
  162. if (defined(value)) {
  163. return value;
  164. }
  165. }
  166. }
  167. }
  168. };
  169. ForEach.material = function(gltf, handler) {
  170. return ForEach.topLevel(gltf, 'materials', handler);
  171. };
  172. ForEach.materialValue = function(material, handler) {
  173. var values = material.values;
  174. if (defined(material.extensions) && defined(material.extensions.KHR_techniques_webgl)) {
  175. values = material.extensions.KHR_techniques_webgl.values;
  176. }
  177. for (var name in values) {
  178. if (Object.prototype.hasOwnProperty.call(values, name)) {
  179. var value = handler(values[name], name);
  180. if (defined(value)) {
  181. return value;
  182. }
  183. }
  184. }
  185. };
  186. ForEach.mesh = function(gltf, handler) {
  187. return ForEach.topLevel(gltf, 'meshes', handler);
  188. };
  189. ForEach.meshPrimitive = function(mesh, handler) {
  190. var primitives = mesh.primitives;
  191. if (defined(primitives)) {
  192. var primitivesLength = primitives.length;
  193. for (var i = 0; i < primitivesLength; i++) {
  194. var primitive = primitives[i];
  195. var value = handler(primitive, i);
  196. if (defined(value)) {
  197. return value;
  198. }
  199. }
  200. }
  201. };
  202. ForEach.meshPrimitiveAttribute = function(primitive, handler) {
  203. var attributes = primitive.attributes;
  204. for (var semantic in attributes) {
  205. if (Object.prototype.hasOwnProperty.call(attributes, semantic)) {
  206. var value = handler(attributes[semantic], semantic);
  207. if (defined(value)) {
  208. return value;
  209. }
  210. }
  211. }
  212. };
  213. ForEach.meshPrimitiveTarget = function(primitive, handler) {
  214. var targets = primitive.targets;
  215. if (defined(targets)) {
  216. var length = targets.length;
  217. for (var i = 0; i < length; ++i) {
  218. var value = handler(targets[i], i);
  219. if (defined(value)) {
  220. return value;
  221. }
  222. }
  223. }
  224. };
  225. ForEach.meshPrimitiveTargetAttribute = function(target, handler) {
  226. for (var semantic in target) {
  227. if (Object.prototype.hasOwnProperty.call(target, semantic)) {
  228. var accessorId = target[semantic];
  229. var value = handler(accessorId, semantic);
  230. if (defined(value)) {
  231. return value;
  232. }
  233. }
  234. }
  235. };
  236. ForEach.node = function(gltf, handler) {
  237. return ForEach.topLevel(gltf, 'nodes', handler);
  238. };
  239. ForEach.nodeInTree = function(gltf, nodeIds, handler) {
  240. var nodes = gltf.nodes;
  241. if (defined(nodes)) {
  242. var length = nodeIds.length;
  243. for (var i = 0; i < length; i++) {
  244. var nodeId = nodeIds[i];
  245. var node = nodes[nodeId];
  246. if (defined(node)) {
  247. var value = handler(node, nodeId);
  248. if (defined(value)) {
  249. return value;
  250. }
  251. var children = node.children;
  252. if (defined(children)) {
  253. value = ForEach.nodeInTree(gltf, children, handler);
  254. if (defined(value)) {
  255. return value;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. };
  262. ForEach.nodeInScene = function(gltf, scene, handler) {
  263. var sceneNodeIds = scene.nodes;
  264. if (defined(sceneNodeIds)) {
  265. return ForEach.nodeInTree(gltf, sceneNodeIds, handler);
  266. }
  267. };
  268. ForEach.program = function(gltf, handler) {
  269. if (hasExtension(gltf, 'KHR_techniques_webgl')) {
  270. return ForEach.object(gltf.extensions.KHR_techniques_webgl.programs, handler);
  271. }
  272. return ForEach.topLevel(gltf, 'programs', handler);
  273. };
  274. ForEach.sampler = function(gltf, handler) {
  275. return ForEach.topLevel(gltf, 'samplers', handler);
  276. };
  277. ForEach.scene = function(gltf, handler) {
  278. return ForEach.topLevel(gltf, 'scenes', handler);
  279. };
  280. ForEach.shader = function(gltf, handler) {
  281. if (hasExtension(gltf, 'KHR_techniques_webgl')) {
  282. return ForEach.object(gltf.extensions.KHR_techniques_webgl.shaders, handler);
  283. }
  284. return ForEach.topLevel(gltf, 'shaders', handler);
  285. };
  286. ForEach.skin = function(gltf, handler) {
  287. return ForEach.topLevel(gltf, 'skins', handler);
  288. };
  289. ForEach.skinJoint = function(skin, handler) {
  290. var joints = skin.joints;
  291. if (defined(joints)) {
  292. var jointsLength = joints.length;
  293. for (var i = 0; i < jointsLength; i++) {
  294. var joint = joints[i];
  295. var value = handler(joint);
  296. if (defined(value)) {
  297. return value;
  298. }
  299. }
  300. }
  301. };
  302. ForEach.techniqueAttribute = function(technique, handler) {
  303. var attributes = technique.attributes;
  304. for (var attributeName in attributes) {
  305. if (Object.prototype.hasOwnProperty.call(attributes, attributeName)) {
  306. var value = handler(attributes[attributeName], attributeName);
  307. if (defined(value)) {
  308. return value;
  309. }
  310. }
  311. }
  312. };
  313. ForEach.techniqueUniform = function(technique, handler) {
  314. var uniforms = technique.uniforms;
  315. for (var uniformName in uniforms) {
  316. if (Object.prototype.hasOwnProperty.call(uniforms, uniformName)) {
  317. var value = handler(uniforms[uniformName], uniformName);
  318. if (defined(value)) {
  319. return value;
  320. }
  321. }
  322. }
  323. };
  324. ForEach.techniqueParameter = function(technique, handler) {
  325. var parameters = technique.parameters;
  326. for (var parameterName in parameters) {
  327. if (Object.prototype.hasOwnProperty.call(parameters, parameterName)) {
  328. var value = handler(parameters[parameterName], parameterName);
  329. if (defined(value)) {
  330. return value;
  331. }
  332. }
  333. }
  334. };
  335. ForEach.technique = function(gltf, handler) {
  336. if (hasExtension(gltf, 'KHR_techniques_webgl')) {
  337. return ForEach.object(gltf.extensions.KHR_techniques_webgl.techniques, handler);
  338. }
  339. return ForEach.topLevel(gltf, 'techniques', handler);
  340. };
  341. ForEach.texture = function(gltf, handler) {
  342. return ForEach.topLevel(gltf, 'textures', handler);
  343. };
  344. export default ForEach;