ForEach.js 10 KB

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