{
  "name": "create-transition-material",
  "description": "Create a transition material between two custom ibl based material.",
  "dependencies": [
    "three"
  ],
  "devDependencies": [
    "@types/three"
  ],
  "registryDependencies": [
    "create-material",
    "dispose-tracker",
    "typings"
  ],
  "files": [
    {
      "path": "create-transition-material.ts",
      "content": "import type { Scene } from 'three';\nimport { Material } from '../create-material/material';\nimport TransitionMaterial from './transition-material';\nimport { addToDisposeTracker } from '@3dvf/dispose-tracker/add-to-dispose-tracker';\n\nexport const createTransitionMaterial = (\n\tscene: Scene,\n\tsourceMaterial: Material,\n\ttargetMaterial: Material\n) => {\n\tconst transitionMaterial = new TransitionMaterial(sourceMaterial, targetMaterial);\n\taddToDisposeTracker(scene, transitionMaterial);\n\treturn transitionMaterial;\n};\n"
    },
    {
      "path": "transition-material.ts",
      "content": "import {\n  Box3,\n  Color,\n  EventDispatcher,\n  Matrix3,\n  Mesh,\n  Quaternion,\n  ShaderMaterial,\n  Sphere,\n  Texture,\n  Vector2,\n  Vector3,\n} from \"three\";\nimport {\n  Material,\n  type Defines,\n  type MaterialUniforms,\n} from \"@3dvf/create-material/material\";\nimport vertexShader from \"@3dvf/create-material/shaders/material.vertex.glsl\";\nimport fragmentShader from \"@3dvf/create-material/shaders/material.fragment.glsl\";\n\nconst SPEED = 3.0;\n\ntype TransitionMaterialUniforms = MaterialUniforms & {\n  ratio: { value: number };\n  sphereCenter: { value: Vector3 };\n  sphereRadius: { value: number };\n  targetMap: { value: Texture | null };\n  targetMapTransform: { value: Matrix3 };\n  targetDiffuse: { value: Color };\n  targetNormalMap: { value: Texture | null };\n  targetNormalMapTransform: { value: Matrix3 };\n  targetNormalScale: { value: Vector2 };\n  targetRoughness: { value: number };\n  targetRoughnessMap: { value: Texture | null };\n  targetRoughnessMapTransform: { value: Matrix3 };\n  targetMetalness: { value: number };\n  targetMetalnessMap: { value: Texture | null };\n  targetMetalnessMapTransform: { value: Matrix3 };\n  targetAoMap: { value: Texture | null };\n  targetAoMapTransform: { value: Matrix3 };\n  targetAoMapIntensity: { value: number };\n  targetEmissiveMap: { value: Texture | null };\n  targetEmissiveMapTransform: { value: Matrix3 };\n  targetEmissive: { value: Color };\n};\n\nexport default class TransitionMaterial extends ShaderMaterial {\n  declare public uniforms: TransitionMaterialUniforms;\n  declare public defines: Defines;\n  \n  public get map(): Texture | null {\n    return this.sourceMaterial.map;\n  }\n  public get normalMap(): Texture | null {\n    return this.sourceMaterial.normalMap;\n  }\n  public get roughnessMap(): Texture | null {\n    return this.sourceMaterial.roughnessMap;\n  }\n  public get metalnessMap(): Texture | null {\n    return this.sourceMaterial.metalnessMap;\n  }\n  public get aoMap(): Texture | null {\n    return this.sourceMaterial.aoMap;\n  }\n\n  private sourceMaterial: Material;\n\n  public constructor(sourceMaterial: Material, targetMaterial: Material) {\n    const uniforms: TransitionMaterialUniforms = {\n      ...sourceMaterial.uniforms,\n      ...{\n        ratio: { value: 0.0 },\n        sphereCenter: { value: new Vector3() },\n        sphereRadius: { value: 1.0 },\n        targetMap: { value: targetMaterial.uniforms.map.value },\n        targetMapTransform: {\n          value: targetMaterial.uniforms.mapTransform.value,\n        },\n        targetDiffuse: { value: targetMaterial.uniforms.diffuse.value.clone() },\n        targetNormalMap: { value: targetMaterial.uniforms.normalMap.value },\n        targetNormalMapTransform: {\n          value:\n            targetMaterial.uniforms[\"normalMapTransform\"]?.value ??\n            new Matrix3(),\n        },\n        targetNormalScale: {\n          value:\n            targetMaterial.uniforms[\"normalScale\"]?.value ?? new Vector2(1, 1),\n        },\n        targetRoughness: { value: targetMaterial.uniforms.roughness.value },\n        targetRoughnessMap: {\n          value: targetMaterial.uniforms.roughnessMap.value,\n        },\n        targetRoughnessMapTransform: {\n          value:\n            targetMaterial.uniforms[\"roughnessMapTransform\"]?.value ??\n            new Matrix3(),\n        },\n        targetMetalness: { value: targetMaterial.uniforms.metalness.value },\n        targetMetalnessMap: {\n          value: targetMaterial.uniforms.metalnessMap.value,\n        },\n        targetMetalnessMapTransform: {\n          value:\n            targetMaterial.uniforms[\"metalnessMapTransform\"]?.value ??\n            new Matrix3(),\n        },\n        targetAoMap: { value: targetMaterial.uniforms.aoMap.value },\n        targetAoMapTransform: {\n          value:\n            targetMaterial.uniforms[\"aoMapTransform\"]?.value ?? new Matrix3(),\n        },\n        targetAoMapIntensity: {\n          value: targetMaterial.uniforms.aoMapIntensity.value,\n        },\n        targetEmissiveMap: { value: targetMaterial.uniforms.emissiveMap.value },\n        targetEmissiveMapTransform: {\n          value:\n            targetMaterial.uniforms[\"emissiveMapTransform\"]?.value ??\n            new Matrix3(),\n        },\n        targetEmissive: {\n          value: targetMaterial.uniforms.emissive.value.clone(),\n        },\n      },\n    };\n\n    super({\n      name: \"transitionMaterial\",\n      uniforms,\n      vertexShader,\n      fragmentShader\n    });\n\n    this.sourceMaterial = sourceMaterial;\n  }\n\n  private setDefine(define: string, condition: boolean, value: string = \"\") {\n    if (condition) {\n      this.defines[define] = value;\n    } else {\n      delete this.defines[define];\n    }\n  }\n  \n  public override onBeforeCompile() {\n    Object.keys(this.sourceMaterial.defines).forEach((key: string) => {\n      this.defines[key] = \"\";\n    });\n\n    function getChannel(value?: number) {\n      if (!value) {\n        return \"uv\";\n      }\n      return `uv${value}`;\n    }\n\n    this.setDefine(\"USE_TARGET_MAP\", !!this.uniforms.targetMap.value);\n    this.setDefine(\n      \"MAP_UV\",\n      !this.uniforms.map.value && !!this.uniforms.targetMap.value,\n      getChannel(this.uniforms.targetMap.value?.channel),\n    );\n    this.setDefine(\"USE_TARGET_NORMALMAP\", !!this.uniforms.targetNormalMap.value);\n    this.setDefine(\n      \"NORMALMAP_UV\",\n      !this.uniforms.normalMap.value && !!this.uniforms.targetNormalMap.value,\n      getChannel(this.uniforms.targetNormalMap.value?.channel),\n    );\n    this.setDefine(\n      \"USE_TARGET_ROUGHNESSMAP\",\n      !!this.uniforms.targetRoughnessMap.value,\n    );\n    this.setDefine(\n      \"ROUGHNESSMAP_UV\",\n      !this.uniforms.roughnessMap.value &&\n      !!this.uniforms.targetRoughnessMap.value,\n      getChannel(this.uniforms.targetRoughnessMap.value?.channel),\n    );\n    this.setDefine(\n      \"USE_TARGET_METALNESSMAP\",\n      !!this.uniforms.targetMetalnessMap.value,\n    );\n    this.setDefine(\n      \"METALNESSMAP_UV\",\n      !this.uniforms.metalnessMap.value &&\n      !!this.uniforms.targetMetalnessMap.value,\n      getChannel(this.uniforms.targetMetalnessMap.value?.channel),\n    );\n    this.setDefine(\"USE_TARGET_AOMAP\", !!this.uniforms.targetAoMap.value);\n    this.setDefine(\n      \"AOMAP_UV\",\n      !this.uniforms.aoMap.value && !!this.uniforms.targetAoMap.value,\n      getChannel(this.uniforms.targetAoMap.value?.channel),\n    );\n    this.setDefine(\n      \"USE_TARGET_EMISSIVEMAP\",\n      !!this.uniforms.targetEmissiveMap.value,\n    );\n    this.setDefine(\n      \"EMISSIVEMAP_UV\",\n      !this.uniforms.emissiveMap.value &&\n      !!this.uniforms.targetEmissiveMap.value,\n      getChannel(this.uniforms.targetEmissiveMap.value?.channel),\n    );\n    this.setDefine(\"USE_TRANSITION\", true);\n  }\n\n  public override customProgramCacheKey(): string {\n    const u = this.uniforms;\n    const cacheKey = [\n      u.targetMap.value ? \"targetMap\" : \"\",\n      u.targetNormalMap.value ? \"targetNormalMap\" : \"\",\n      u.targetRoughnessMap.value ? \"targetRoughnessMap\" : \"\",\n      u.targetMetalnessMap.value ? \"targetMetalnessMap\" : \"\",\n      u.targetAoMap.value ? \"targetAoMap\" : \"\",\n      u.targetEmissiveMap.value ? \"targetEmissiveMap\" : \"\"\n    ].join(\"\");\n    return `${this.sourceMaterial.customProgramCacheKey()}${cacheKey}`;\n  }\n  \n  public async transitFromSphere(\n    center: Vector3,\n    radius: number,\n    eventDispatcher: EventDispatcher<{ animate: { delta: number } }>,\n  ): Promise<void> {\n    this.uniforms[\"sphereCenter\"].value = center;\n    this.uniforms[\"sphereRadius\"].value = radius;\n    \n    this.uniforms.ratio.value = 0.0;\n    return new Promise((resolve) => {\n      const updateRatio = ({ delta }: { delta: number }) => {\n        const ratio = this.uniforms.ratio.value + SPEED * delta;\n    \n        if (ratio > 1.0) {\n          eventDispatcher.removeEventListener(\"animate\", updateRatio);\n          resolve();\n          return;\n        }\n    \n        this.uniforms.ratio.value = ratio;\n      };\n    \n      eventDispatcher.addEventListener(\"animate\", updateRatio);\n    });\n  }\n\n  public async transit(\n    mesh: Mesh,\n    eventDispatcher: EventDispatcher<{ animate: { delta: number } }>,\n  ): Promise<void> {\n    mesh.updateMatrixWorld();\n    if (!mesh.geometry.boundingSphere) {\n      mesh.geometry.computeBoundingSphere();\n    }\n    const sphere = mesh.geometry.boundingSphere!;\n    const worldCenter = sphere.center.clone().applyMatrix4(mesh.matrixWorld);\n    const scale = new Vector3();\n    mesh.matrixWorld.decompose(new Vector3(), new Quaternion(), scale);\n    const worldRadius = sphere.radius * Math.max(scale.x, scale.y, scale.z);\n    return this.transitFromSphere(worldCenter, worldRadius, eventDispatcher)\n  }\n  \n  public async transitFromMeshes(meshes: Mesh[],\n    eventDispatcher: EventDispatcher<{ animate: { delta: number } }>,\n  ): Promise<void> {\n    const box = new Box3();\n    meshes.forEach((mesh: Mesh) => {\n      box.expandByObject(mesh);\n    })\n    const sphere = new Sphere();\n    box.getBoundingSphere(sphere);\n    return this.transitFromSphere(sphere.center, sphere.radius, eventDispatcher);\n    \n  }\n    \n}\n"
    }
  ]
}