{
  "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\tBufferGeometry,\n\tLinearSRGBColorSpace,\n\tMaterial,\n\tMesh,\n\tMeshPhysicalMaterial,\n\tObject3D,\n\tScene,\n\tTexture,\n\tWebGLRenderer\n} from 'three';\nimport { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';\n\nexport const loadGLTF = async (\n\trenderer: WebGLRenderer,\n\tscene: Scene,\n\turl: string,\n\toptionalResourcePath?: string\n) => {\n\tconst gltfLoader = new GLTFLoader();\n\n\tconst resourcePath =\n\t\toptionalResourcePath || url.match(/(?<base>.+\\/)(?:\\w|\\.)+/)?.groups?.['base'];\n\n\tif (!resourcePath) {\n\t\tthrow new Error(`couldn't find asset base url from ${url}`);\n\t}\n\n\tgltfLoader.resourcePath = resourcePath;\n\n\tconst asset = await gltfLoader.loadAsync(url);\n\tasset.scene.name = 'main';\n\tscene.add(asset.scene);\n\n\t// dispatch animations\n\tasset.animations.forEach((animation) => {\n\t\tconst name = animation.name.split('|')[0];\n\n\t\tif (!name) {\n\t\t\tthrow new Error(`couldn't find animation name from ${animation.name}`);\n\t\t}\n\n\t\tconst object = scene.getObjectByName(name);\n\t\tif (object) {\n\t\t\tobject.animations.push(animation);\n\t\t} else {\n\t\t\tscene.animations.push(animation);\n\t\t}\n\t});\n\n\t// fix imported textures\n\tscene.traverse((object: Object3D) => {\n\t\tconst mesh = object as Mesh;\n\n\t\tconst physicalMaterial = mesh.material as MeshPhysicalMaterial;\n\n\t\tif (physicalMaterial) {\n\t\t\tconst material = createMaterial(scene);\n\t\t\tmaterial.vampMeshPhysicalMaterial(physicalMaterial);\n\n\t\t\tif (material.map != null) {\n\t\t\t\t// We do our own decoding within fragment shader\n\t\t\t\tmaterial.map.colorSpace = LinearSRGBColorSpace;\n\t\t\t}\n\n\t\t\tif (material.normalMap != null) {\n\t\t\t\tmaterial.normalMap.anisotropy = renderer.capabilities.getMaxAnisotropy();\n\t\t\t}\n\n\t\t\tif (material.aoMap != null) {\n\t\t\t\tmaterial.aoMap.channel = 1;\n\t\t\t\tmaterial.aoMap.generateMipmaps = false;\n\t\t\t}\n\n\t\t\tmesh.material = material;\n\t\t\tphysicalMaterial.dispose();\n\t\t}\n\t});\n\n\tconst geometries = new Set<BufferGeometry>();\n\tconst materials = new Set<Material>();\n\tconst textures = new Set<Texture>();\n\n\tscene.traverse((object: Object3D) => {\n\t\tif (object.type == 'Mesh') {\n\t\t\tconst mesh: Mesh = object as Mesh;\n\t\t\tgeometries.add(mesh.geometry);\n\n\t\t\tconst material: MeshPhysicalMaterial = mesh.material as MeshPhysicalMaterial;\n\t\t\tmaterials.add(material);\n\n\t\t\t[\n\t\t\t\tmaterial.map,\n\t\t\t\tmaterial.normalMap,\n\t\t\t\tmaterial.roughnessMap,\n\t\t\t\tmaterial.metalnessMap,\n\t\t\t\tmaterial.alphaMap,\n\t\t\t\tmaterial.aoMap,\n\t\t\t\tmaterial.emissiveMap\n\t\t\t]\n\t\t\t\t.filter((map) => map != null)\n\t\t\t\t.forEach((map) => textures.add(map));\n\t\t}\n\t});\n\n\ttextures.forEach((texture) => addToDisposeTracker(scene, texture));\n\tmaterials.forEach((material) => addToDisposeTracker(scene, material));\n\tgeometries.forEach((geometry) => addToDisposeTracker(scene, geometry));\n};\n"
    }
  ]
}