{
  "name": "load-gltf",
  "description": "Load a gltf model",
  "dependencies": [
    "three"
  ],
  "devDependencies": [
    "@types/three"
  ],
  "registryDependencies": [
    "create-material"
  ],
  "files": [
    {
      "path": "load-gltf.ts",
      "content": "import {\n  LinearSRGBColorSpace,\n  Mesh,\n  MeshPhysicalMaterial,\n  Object3D,\n  Scene,\n  WebGLRenderer,\n} from \"three\";\nimport { GLTFLoader } from \"three/examples/jsm/loaders/GLTFLoader\";\nimport createMaterial from \"../create-material/create-material\";\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 ||\n    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  gltfLoader.resourcePath = resourcePath;\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 =\n          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 materials = new Set();\n  const textures = new Set();\n\n  scene.traverse((object: Object3D) => {\n    if (object.type == \"Mesh\") {\n      const mesh: Mesh = object as Mesh;\n      mesh.geometry.dispose();\n      const material: MeshPhysicalMaterial =\n        mesh.material as MeshPhysicalMaterial;\n      materials.add(material);\n\n      textures.add(material.map);\n      textures.add(material.normalMap);\n      textures.add(material.roughnessMap);\n      textures.add(material.metalnessMap);\n      textures.add(material.alphaMap);\n      textures.add(material.aoMap);\n      textures.add(material.emissiveMap);\n    }\n  });\n\n  if (!scene.userData[\"materials\"]) {\n    scene.userData[\"materials\"] = new Set();\n  }\n  scene.userData[\"materials\"].add(...materials);\n\n  if (!scene.userData[\"textures\"]) {\n    scene.userData[\"textures\"] = new Set();\n  }\n  scene.userData[\"textures\"].add(...textures);\n};\n"
    }
  ]
}