{
  "name": "load-gltf",
  "description": "Load a gltf model",
  "dependencies": [
    "three"
  ],
  "devDependencies": [
    "@types/three"
  ],
  "registryDependencies": [
    "create-material",
    "dispose-tracker"
  ],
  "files": [
    {
      "path": "load-gltf.ts",
      "content": "import createMaterial from '@3dvf/create-material/create-material';\nimport { addToDisposeTracker } from '@3dvf/dispose-tracker/add-to-dispose-tracker';\nimport {\n  BufferGeometry,\n  LinearSRGBColorSpace,\n  Material,\n  Mesh,\n  MeshPhysicalMaterial,\n  Object3D,\n  Scene,\n  Texture,\n  WebGLRenderer\n} from 'three';\nimport { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';\n\nexport const loadGLTF = async (\n  renderer: WebGLRenderer,\n  scene: Scene,\n  url: string,\n  optionalResourcePath?: string\n) => {\n  const gltfLoader = new GLTFLoader();\n\n  const resourcePath =\n    optionalResourcePath || url.match(/(?<base>.+\\/)(?:\\w|\\.)+/)?.groups?.['base'];\n\n  if (!resourcePath) {\n    throw new Error(`couldn't find asset base url from ${url}`);\n  }\n\n  const trailingSlash = resourcePath.endsWith(\"/\") ? \"\" : \"/\";\n  gltfLoader.resourcePath = `${resourcePath}${trailingSlash}`;\n\n  const asset = await gltfLoader.loadAsync(url);\n  asset.scene.name = 'main';\n  scene.add(asset.scene);\n\n  // dispatch animations\n  asset.animations.forEach((animation) => {\n    const name = animation.name.split('|')[0];\n\n    if (!name) {\n      throw new Error(`couldn't find animation name from ${animation.name}`);\n    }\n\n    const object = scene.getObjectByName(name);\n    if (object) {\n      object.animations.push(animation);\n    } else {\n      scene.animations.push(animation);\n    }\n  });\n\n  // fix imported textures\n  scene.traverse((object: Object3D) => {\n    const mesh = object as Mesh;\n\n    const physicalMaterial = mesh.material as MeshPhysicalMaterial;\n\n    if (physicalMaterial) {\n      const material = createMaterial(scene);\n      material.vampMeshPhysicalMaterial(physicalMaterial);\n\n      if (material.map != null) {\n        // We do our own decoding within fragment shader\n        material.map.colorSpace = LinearSRGBColorSpace;\n      }\n\n      if (material.normalMap != null) {\n        material.normalMap.anisotropy = renderer.capabilities.getMaxAnisotropy();\n      }\n\n      if (material.aoMap != null) {\n        material.aoMap.channel = 1;\n        material.aoMap.generateMipmaps = false;\n      }\n\n      mesh.material = material;\n      physicalMaterial.dispose();\n    }\n  });\n\n  const geometries = new Set<BufferGeometry>();\n  const materials = new Set<Material>();\n  const textures = new Set<Texture>();\n\n  scene.traverse((object: Object3D) => {\n    if (object.type == 'Mesh') {\n      const mesh: Mesh = object as Mesh;\n      geometries.add(mesh.geometry);\n\n      const material: MeshPhysicalMaterial = mesh.material as MeshPhysicalMaterial;\n      materials.add(material);\n\n      [\n        material.map,\n        material.normalMap,\n        material.roughnessMap,\n        material.metalnessMap,\n        material.alphaMap,\n        material.aoMap,\n        material.emissiveMap\n      ]\n        .filter((map) => map != null)\n        .forEach((map) => textures.add(map));\n    }\n  });\n\n  textures.forEach((texture) => addToDisposeTracker(scene, texture));\n  materials.forEach((material) => addToDisposeTracker(scene, material));\n  geometries.forEach((geometry) => addToDisposeTracker(scene, geometry));\n};\n"
    }
  ]
}