gltfPathToObjectConverter.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * A converter that takes a glTF Object Model JSON Pointer
  3. * and transforms it into an ObjectAccessorContainer, allowing
  4. * objects referenced in the glTF to be associated with their
  5. * respective Babylon.js objects.
  6. */
  7. export class GLTFPathToObjectConverter {
  8. constructor(_gltf, _infoTree) {
  9. this._gltf = _gltf;
  10. this._infoTree = _infoTree;
  11. }
  12. /**
  13. * The pointer string is represented by a [JSON pointer](https://datatracker.ietf.org/doc/html/rfc6901).
  14. * <animationPointer> := /<rootNode>/<assetIndex>/<propertyPath>
  15. * <rootNode> := "nodes" | "materials" | "meshes" | "cameras" | "extensions"
  16. * <assetIndex> := <digit> | <name>
  17. * <propertyPath> := <extensionPath> | <standardPath>
  18. * <extensionPath> := "extensions"/<name>/<standardPath>
  19. * <standardPath> := <name> | <name>/<standardPath>
  20. * <name> := W+
  21. * <digit> := D+
  22. *
  23. * Examples:
  24. * - "/nodes/0/rotation"
  25. * - "/materials/2/emissiveFactor"
  26. * - "/materials/2/pbrMetallicRoughness/baseColorFactor"
  27. * - "/materials/2/extensions/KHR_materials_emissive_strength/emissiveStrength"
  28. *
  29. * @param path The path to convert
  30. * @returns The object and info associated with the path
  31. */
  32. convert(path) {
  33. let objectTree = this._gltf;
  34. let infoTree = this._infoTree;
  35. let target = undefined;
  36. if (!path.startsWith("/")) {
  37. throw new Error("Path must start with a /");
  38. }
  39. const parts = path.split("/");
  40. parts.shift();
  41. for (const part of parts) {
  42. if (infoTree.__array__) {
  43. infoTree = infoTree.__array__;
  44. }
  45. else {
  46. infoTree = infoTree[part];
  47. if (!infoTree) {
  48. throw new Error(`Path ${path} is invalid`);
  49. }
  50. }
  51. if (objectTree === undefined) {
  52. throw new Error(`Path ${path} is invalid`);
  53. }
  54. objectTree = objectTree[part];
  55. if (infoTree.__target__) {
  56. target = objectTree;
  57. }
  58. }
  59. return {
  60. object: target,
  61. info: infoTree,
  62. };
  63. }
  64. }
  65. //# sourceMappingURL=gltfPathToObjectConverter.js.map