{
  "name": "create-transition-material",
  "description": "Create a transition material between two custom ibl based material.",
  "dependencies": [
    "three"
  ],
  "devDependencies": [
    "@types/three"
  ],
  "registryDependencies": [
    "create-material"
  ],
  "files": [
    {
      "path": "create-transition-material.ts",
      "content": "import { Material } from \"../create-material/material\";\nimport TransitionMaterial from \"./transition-material\";\n\nexport const createTransitionMaterial = (\n  sourceMaterial: Material,\n  targetMaterial: Material,\n) => {\n  return new TransitionMaterial(sourceMaterial, targetMaterial);\n};\n"
    },
    {
      "path": "transition-material.ts",
      "content": "import {\n  Color,\n  EventDispatcher,\n  Matrix3,\n  Mesh,\n  Quaternion,\n  ShaderMaterial,\n  TangentSpaceNormalMap,\n  Texture,\n  Vector2,\n  Vector3,\n} from \"three\";\nimport fragmentShader from \"./shaders/transition-material.fragment.glsl\";\nimport vertexShader from \"./shaders/transition-material.vertex.glsl\";\nimport type {\n  Material,\n  MaterialUniforms,\n} from \"@3dvf/create-material/material\";\n\nconst SPEED = 3.0;\n\ntype Defines = {\n  [define: string]: string | number;\n};\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: MaterialUniforms;\n  declare public defines: Defines;\n\n  public isMeshPhysicalMaterial: boolean;\n  public normalMapType: number = TangentSpaceNormalMap;\n\n  public get color(): Color {\n    return this.uniforms[\"diffuse\"].value;\n  }\n  public set color(color: Color) {\n    this.uniforms[\"diffuse\"].value = color;\n  }\n\n  public get map(): Texture | null {\n    return this.uniforms[\"map\"].value;\n  }\n  public set map(map: Texture | null) {\n    this.uniforms[\"map\"].value = map;\n  }\n\n  public get normalMap(): Texture | null {\n    return this.uniforms[\"normalMap\"].value;\n  }\n  public set normalMap(normalMap: Texture | null) {\n    this.uniforms[\"normalMap\"].value = normalMap;\n  }\n\n  public get roughness(): number {\n    return this.uniforms[\"roughness\"].value;\n  }\n  public set roughness(roughness: number) {\n    this.uniforms[\"roughness\"].value = roughness;\n  }\n  public get roughnessMap(): Texture | null {\n    return this.uniforms[\"roughnessMap\"].value;\n  }\n  public set roughnessMap(roughnessMap: Texture | null) {\n    this.uniforms[\"roughnessMap\"].value = roughnessMap;\n  }\n\n  public get metalness(): number {\n    return this.uniforms[\"metalness\"].value;\n  }\n  public set metalness(metalness: number) {\n    this.uniforms[\"metalness\"].value = metalness;\n  }\n  public get metalnessMap(): Texture | null {\n    return this.uniforms[\"metalnessMap\"].value;\n  }\n  public set metalnessMap(metalnessMap: Texture | null) {\n    this.uniforms[\"metalnessMap\"].value = metalnessMap;\n  }\n\n  public get alphaMap(): Texture | null {\n    return this.uniforms[\"alphaMap\"].value;\n  }\n  public set alphaMap(alphaMap: Texture | null) {\n    this.uniforms[\"alphaMap\"].value = alphaMap;\n  }\n\n  public get aoMap(): Texture | null {\n    return this.uniforms[\"aoMap\"].value;\n  }\n  public set aoMap(aoMap: Texture | null) {\n    this.uniforms[\"aoMap\"].value = aoMap;\n  }\n  public get aoMapIntensity(): number {\n    return this.uniforms[\"aoMapIntensity\"].value;\n  }\n  public set aoMapIntensity(aoMapIntensity: number) {\n    this.uniforms[\"aoMapIntensity\"].value = aoMapIntensity;\n  }\n\n  public get emissiveMap(): Texture | null {\n    return this.uniforms[\"emissiveMap\"].value;\n  }\n  public set emissiveMap(emissiveMap: Texture | null) {\n    this.uniforms[\"emissiveMap\"].value = emissiveMap;\n  }\n  public get emissive(): Color {\n    return this.uniforms[\"emissive\"].value;\n  }\n  public set emissive(emissive: Color) {\n    this.uniforms[\"emissive\"].value = emissive;\n  }\n\n  public get targetMap(): Texture | null {\n    return this.uniforms[\"targetMap\"].value;\n  }\n  public set targetMap(map: Texture | null) {\n    this.uniforms[\"targetMap\"].value = map;\n  }\n\n  public get targetDiffuse(): Color {\n    return this.uniforms[\"targetDiffuse\"].value;\n  }\n  public set targetDiffuse(color: Color) {\n    this.uniforms[\"targetDiffuse\"].value = color;\n  }\n\n  public get targetNormalMap(): Texture | null {\n    return this.uniforms[\"targetNormalMap\"].value;\n  }\n  public set targetNormalMap(map: Texture | null) {\n    this.uniforms[\"targetNormalMap\"].value = map;\n  }\n  public get targetNormalScale(): Vector2 {\n    return this.uniforms[\"targetNormalScale\"].value;\n  }\n  public set targetNormalScale(scale: Vector2) {\n    this.uniforms[\"targetNormalScale\"].value = scale;\n  }\n\n  public get targetRoughness(): number {\n    return this.uniforms[\"targetRoughness\"].value;\n  }\n  public set targetRoughness(roughness: number) {\n    this.uniforms[\"targetRoughness\"].value = roughness;\n  }\n  public get targetRoughnessMap(): Texture | null {\n    return this.uniforms[\"targetRoughnessMap\"].value;\n  }\n  public set targetRoughnessMap(map: Texture | null) {\n    this.uniforms[\"targetRoughnessMap\"].value = map;\n  }\n\n  public get targetMetalness(): number {\n    return this.uniforms[\"targetMetalness\"].value;\n  }\n  public set targetMetalness(metalness: number) {\n    this.uniforms[\"targetMetalness\"].value = metalness;\n  }\n  public get targetMetalnessMap(): Texture | null {\n    return this.uniforms[\"targetMetalnessMap\"].value;\n  }\n  public set targetMetalnessMap(map: Texture | null) {\n    this.uniforms[\"targetMetalnessMap\"].value = map;\n  }\n\n  public get targetAoMap(): Texture | null {\n    return this.uniforms[\"targetAoMap\"].value;\n  }\n  public set targetAoMap(map: Texture | null) {\n    this.uniforms[\"targetAoMap\"].value = map;\n  }\n  public get targetAoMapIntensity(): number {\n    return this.uniforms[\"targetAoMapIntensity\"].value;\n  }\n  public set targetAoMapIntensity(v: number) {\n    this.uniforms[\"targetAoMapIntensity\"].value = v;\n  }\n\n  public get targetEmissiveMap(): Texture | null {\n    return this.uniforms[\"targetEmissiveMap\"].value;\n  }\n  public set targetEmissiveMap(map: Texture | null) {\n    this.uniforms[\"targetEmissiveMap\"].value = map;\n  }\n\n  public get targetEmissive(): Color {\n    return this.uniforms[\"targetEmissive\"].value;\n  }\n  public set targetEmissive(color: Color) {\n    this.uniforms[\"targetEmissive\"].value = color;\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.isMeshPhysicalMaterial = true;\n\n    this.sourceMaterial = sourceMaterial;\n  }\n\n  public override onBeforeCompile() {\n    Object.keys(this.sourceMaterial.defines).forEach((key: string) => {\n      this.defines[key] = \"\";\n    });\n\n    const setDefine = (\n      define: string,\n      condition: boolean,\n      value: string = \"\",\n    ) => {\n      if (condition) {\n        this.defines[define] = value;\n      } else {\n        delete this.defines[define];\n      }\n    };\n\n    function getChannel(value: number) {\n      if (value === 0) return \"uv\";\n      return `uv${value}`;\n    }\n\n    setDefine(\"USE_TARGET_MAP\", !!this.uniforms.targetMap.value);\n    setDefine(\n      \"MAP_UV\",\n      !this.uniforms.map.value && !!this.uniforms.targetMap.value,\n      getChannel(this.uniforms.targetMap.value?.channel),\n    );\n    setDefine(\"USE_TARGET_NORMALMAP\", !!this.uniforms.targetNormalMap.value);\n    setDefine(\n      \"NORMALMAP_UV\",\n      !this.uniforms.normalMap.value && !!this.uniforms.targetNormalMap.value,\n      getChannel(this.uniforms.targetNormalMap.value?.channel),\n    );\n    setDefine(\n      \"USE_TARGET_ROUGHNESSMAP\",\n      !!this.uniforms.targetRoughnessMap.value,\n    );\n    setDefine(\n      \"ROUGHNESSMAP_UV\",\n      !this.uniforms.roughnessMap.value &&\n        !!this.uniforms.targetRoughnessMap.value,\n      getChannel(this.uniforms.targetRoughnessMap.value?.channel),\n    );\n    setDefine(\n      \"USE_TARGET_METALNESSMAP\",\n      !!this.uniforms.targetMetalnessMap.value,\n    );\n    setDefine(\n      \"METALNESSMAP_UV\",\n      !this.uniforms.metalnessMap.value &&\n        !!this.uniforms.targetMetalnessMap.value,\n      getChannel(this.uniforms.targetMetalnessMap.value?.channel),\n    );\n    setDefine(\"USE_TARGET_AOMAP\", !!this.uniforms.targetAoMap.value);\n    setDefine(\n      \"AOMAP_UV\",\n      !this.uniforms.aoMap.value && !!this.uniforms.targetAoMap.value,\n      getChannel(this.uniforms.targetAoMap.value?.channel),\n    );\n    setDefine(\n      \"USE_TARGET_EMISSIVEMAP\",\n      !!this.uniforms.targetEmissiveMap.value,\n    );\n    setDefine(\n      \"EMISSIVEMAP_UV\",\n      !this.uniforms.emissiveMap.value &&\n        !!this.uniforms.targetEmissiveMap.value,\n      getChannel(this.uniforms.targetEmissiveMap.value?.channel),\n    );\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 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    this.uniforms[\"sphereCenter\"].value = worldCenter;\n    this.uniforms[\"sphereRadius\"].value = worldRadius;\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"
    },
    {
      "path": "shaders/transition-material.fragment.glsl",
      "content": "#define STANDARD\n\nconst float kMaxRadianceLod = 9.0;\n\nuniform float ratio;\nuniform vec3 sphereCenter;\nuniform float sphereRadius;\n\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float roughness;\nuniform float metalness;\nuniform float opacity;\n\nuniform samplerCube irradiance_map;\nuniform samplerCube radiance_map;\nuniform sampler2D brdf_map;\n\nuniform mat4 ibl_matrix;\n\nvarying vec3 vViewPosition;\n\n#define PI 3.141592653589793\n#define PI2 6.283185307179586\n#define PI_HALF 1.5707963267948966\n#define RECIPROCAL_PI 0.3183098861837907\n#define RECIPROCAL_PI2 0.15915494309189535\n#define EPSILON 1e-6\n\nvec3 inverseTransformDirection(in vec3 dir, in mat4 matrix) {\n  // dir can be either a direction vector or a normal vector\n  // upper-left 3x3 of matrix is assumed to be orthogonal\n  return normalize((vec4(dir, 0.0) * matrix).xyz);\n}\n\n#if defined(USE_COLOR) || defined(USE_COLOR_ALPHA)\nvarying vec4 vColor;\n#endif\n\n#if defined(USE_UV)\nvarying vec2 vUv;\n#endif\n\n#if defined(USE_MAP) || defined(USE_TARGET_MAP)\nvarying vec2 vMapUv;\n#endif\n\n#ifdef USE_ALPHAMAP\nvarying vec2 vAlphaMapUv;\n#endif\n\n#if defined(USE_AOMAP) || defined(USE_TARGET_AOMAP)\nvarying vec2 vAoMapUv;\n#endif\n\n#if defined(USE_NORMALMAP) || defined(USE_TARGET_NORMALMAP)\nvarying vec2 vNormalMapUv;\n#endif\n\n#if defined(USE_EMISSIVEMAP) || defined(USE_TARGET_EMISSIVEMAP)\nvarying vec2 vEmissiveMapUv;\n#endif\n\n#if defined(USE_METALNESSMAP) || defined(USE_TARGET_METALNESSMAP)\nvarying vec2 vMetalnessMapUv;\n#endif\n\n#if defined(USE_ROUGHNESSMAP) || defined(USE_TARGET_ROUGHNESSMAP)\nvarying vec2 vRoughnessMapUv;\n#endif\n\n#ifdef USE_ANISOTROPYMAP\nvarying vec2 vAnisotropyMapUv;\n#endif\n\n#ifdef USE_MAP\nuniform sampler2D map;\n#endif\n\n#ifdef USE_TARGET_MAP\nuniform sampler2D targetMap;\n#endif\n\nuniform vec3 targetDiffuse;\nuniform float targetRoughness;\nuniform float targetMetalness;\nuniform vec3 targetEmissive;\n\n#ifdef USE_TARGET_NORMALMAP\nuniform sampler2D targetNormalMap;\nuniform vec2 targetNormalScale;\n#endif\n\n#ifdef USE_TARGET_ROUGHNESSMAP\nuniform sampler2D targetRoughnessMap;\n#endif\n\n#ifdef USE_TARGET_METALNESSMAP\nuniform sampler2D targetMetalnessMap;\n#endif\n\n#ifdef USE_TARGET_AOMAP\nuniform sampler2D targetAoMap;\nuniform float targetAoMapIntensity;\n#endif\n\n#ifdef USE_TARGET_EMISSIVEMAP\nuniform sampler2D targetEmissiveMap;\n#endif\n\n#ifdef USE_ALPHAMAP\nuniform sampler2D alphaMap;\n#endif\n\n#ifdef USE_ALPHATEST\nuniform float alphaTest;\n#endif\n\n#ifdef USE_AOMAP\nuniform sampler2D aoMap;\nuniform float aoMapIntensity;\n#endif\n\n#ifdef USE_EMISSIVEMAP\nuniform sampler2D emissiveMap;\n#endif\n\n#ifndef FLAT_SHADED\nvarying vec3 vNormal;\n#ifdef USE_TANGENT\nvarying vec3 vTangent;\nvarying vec3 vBitangent;\n#endif\n#endif\n\n#ifdef USE_NORMALMAP\nuniform sampler2D normalMap;\nuniform vec2 normalScale;\n#endif\n\n#ifdef USE_NORMALMAP_OBJECTSPACE\nuniform mat3 normalMatrix;\n#endif\n\n#if ! defined(USE_TANGENT) && defined(USE_NORMALMAP_TANGENTSPACE)\n// Normal Mapping Without Precomputed Tangents\n// http://www.thetenthplanet.de/archives/1180\nmat3 getTangentFrame(vec3 eye_pos, vec3 surf_norm, vec2 uv) {\n  vec3 q0 = dFdx(eye_pos.xyz);\n  vec3 q1 = dFdy(eye_pos.xyz);\n  vec2 st0 = dFdx(uv.st);\n  vec2 st1 = dFdy(uv.st);\n\n  vec3 N = surf_norm; // normalized\n\n  vec3 q1perp = cross(q1, N);\n  vec3 q0perp = cross(N, q0);\n\n  vec3 T = q1perp * st0.x + q0perp * st1.x;\n  vec3 B = q1perp * st0.y + q0perp * st1.y;\n\n  float det = max(dot(T, T), dot(B, B));\n  float scale = (det == 0.0) ? 0.0 : inversesqrt(det);\n\n  return mat3(T * scale, B * scale, N);\n}\n#endif\n\n#ifdef USE_ROUGHNESSMAP\nuniform sampler2D roughnessMap;\n#endif\n\n#ifdef USE_METALNESSMAP\nuniform sampler2D metalnessMap;\n#endif\n\n/*vec3 toneMap(vec3 hdrColor) {\n  // ACES APPROXIMATION from https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/\n  hdrColor *= 0.6;\n  float a = 2.51;\n  float b = 0.03;\n  float c = 2.43;\n  float d = 0.59;\n  float e = 0.14;\n  hdrColor = clamp((hdrColor*(a*hdrColor+b))/(hdrColor*(c*hdrColor+d)+e), 0.0, 1.0);\n\n  return hdrColor;\n}*/\n\nvec3 untoneMap(vec3 hdrColor) {\n  // ACES INVERSE APPROXIMATION from https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/\n  // See https://www.symbolab.com/solver/function-inverse-calculator/inverse%20f%5Cleft(x%5Cright)%3D%5Cfrac%7B%5Cleft(2.51%5Ccdot%20x%5E%7B2%7D%20%2B%200.03%5Ccdot%20x%5Cright)%7D%7B2.43%5Ccdot%20x%5E%7B2%7D%20%2B0.59%5Ccdot%20x%20%2B%200.14%7D\n  float a = -0.59;\n  float b = 0.03;\n  float c = -1.0127;\n  float d = 1.3702;\n  float e = 0.0009;\n  float f = 2.43;\n  float g = 2.51;\n  hdrColor = (a * hdrColor + b - sqrt(c * hdrColor * hdrColor + d * hdrColor + e)) / (2.0 * (f * hdrColor - g));\n  hdrColor /= 0.6;\n\n  return hdrColor;\n}\n\nvec3 RGBDToHDR(vec4 rgbd) {\n  return rgbd.bgr / rgbd.a;\n}\n\nvec3 toneMap(vec3 hdrColor) {\n  return hdrColor / (hdrColor + vec3(1.0));\n}\n\n/*vec3 untoneMap(vec3 ldrColor) {\n  return ldrColor/(vec3(1.0) - ldrColor);\n}*/\n\nfloat UnpackFrom16Bits(vec2 packed_value) {\n  const vec2 bit_shift = vec2(1.0 / 255.0, 1.0);\n  return dot(packed_value, bit_shift);\n}\n\n#ifdef REFLECTOR\nuniform sampler2D reflectorMap;\nuniform sampler2D reflectorDepthMap;\n\nuniform mat4 projectionMatrix;\n\nuniform mat4 reflectorViewMatrix;\nuniform mat4 reflectorProjectionMatrix;\nuniform mat4 reflectorMatrix;\n#endif\n\nvarying vec4 world_position_out;\n\nvec3 FresnelSchlickRoughness(float cos_theta, vec3 f_0, float roughness) {\n  return f_0 + (max(vec3(1.0 - roughness), f_0) - f_0) * pow(clamp(1.0 - cos_theta, 0.0, 1.0), 5.0);\n}\n\nvec3 GetConductorMultipleScattering(float n_dot_v, float roughness, vec3 color, vec3 irradiance, vec3 radiance) {\n  vec3 f_0 = color;\n\n  // Correction at low roughness for dielectrics, from Fdez-Aguera\n  vec3 k_s = FresnelSchlickRoughness(n_dot_v, f_0, roughness);\n\n  vec4 packed_sample = texture2D(brdf_map, vec2(n_dot_v, roughness));\n  vec2 F_ab = vec2(UnpackFrom16Bits(packed_sample.xy), UnpackFrom16Bits(packed_sample.zw));\n  vec3 FssEss = k_s * F_ab.x + F_ab.y;\n\n  // Adding multiple scattering, from Fdez-Aguera\n  float Ess = F_ab.x + F_ab.y;\n  float Ems = 1.0 - Ess;\n  vec3 Favg = f_0 + (1.0 - f_0) / 21.0;\n  vec3 Fms = FssEss * Favg / (1.0 - (1.0 - Ess) * Favg);\n\n  return FssEss * radiance + (Fms * Ems) * irradiance;\n}\n\nvec3 GetDielectricMultipleScattering(float n_dot_v, float roughness, vec3 color, vec3 irradiance, vec3 radiance) {\n  vec3 f_0 = vec3(0.04);\n\n  // Correction at low roughness for dielectrics, from Fdez-Aguera\n  vec3 k_s = FresnelSchlickRoughness(n_dot_v, f_0, roughness);\n\n  vec4 packed_sample = texture2D(brdf_map, vec2(n_dot_v, roughness));\n  vec2 F_ab = vec2(UnpackFrom16Bits(packed_sample.xy), UnpackFrom16Bits(packed_sample.zw));\n  vec3 FssEss = k_s * F_ab.x + F_ab.y;\n\n  // Adding multiple scattering, from Fdez-Aguera\n  float Ess = F_ab.x + F_ab.y;\n  float Ems = 1.0 - Ess;\n  vec3 Favg = f_0 + (1.0 - f_0) / 21.0;\n  vec3 Fms = FssEss * Favg / (1.0 - (1.0 - Ess) * Favg);\n\n  vec3 Edss = 1.0 - (FssEss + Fms * Ems);\n  vec3 k_d = color * Edss;\n\n  return FssEss * radiance + (Fms * Ems + k_d) * irradiance;\n}\n\nvoid main() {\n  float sphereDist = length(world_position_out.xyz - sphereCenter) / max(sphereRadius, 0.001);\n  float edge = 0.1;\n  float front = ratio * (1.0 + 2.0 * edge) - edge;\n  float ratio = 1.0 - smoothstep(front - edge, front + edge, sphereDist);\n\n  vec4 diffuseColor = vec4(pow(diffuse, vec3(2.2)), opacity);\n  vec4 targetDiffuseColor = vec4(pow(targetDiffuse, vec3(2.2)), opacity);\n\n  vec3 totalEmissiveRadiance = emissive;\n  vec3 targetTotalEmissiveRadiance = targetEmissive;\n\n  #ifdef USE_MAP\n  vec4 diffuse_color_sample = texture2D(map, vMapUv);\n  diffuseColor *= pow(diffuse_color_sample, vec4(2.2));\n  #endif\n\n  #ifdef USE_TARGET_MAP\n  vec4 target_diffuse_color_sample = texture2D(targetMap, vMapUv);\n  targetDiffuseColor *= pow(target_diffuse_color_sample, vec4(2.2));\n  #endif\n\n  diffuseColor = mix(diffuseColor, targetDiffuseColor, ratio);\n\n  #ifdef USE_ALPHAMAP\n  diffuseColor.a *= texture2D(alphaMap, vAlphaMapUv).g;\n  #endif\n\n  #ifdef USE_ALPHATEST\n  #ifdef ALPHA_TO_COVERAGE\n  diffuseColor.a = smoothstep(alphaTest, alphaTest + fwidth(diffuseColor.a), diffuseColor.a);\n  if (diffuseColor.a == 0.0) discard;\n  #else\n  if (diffuseColor.a < alphaTest) discard;\n  #endif\n  #endif\n\n  float roughnessFactor = roughness;\n  #ifdef USE_ROUGHNESSMAP\n  vec4 texelRoughness = texture2D(roughnessMap, vRoughnessMapUv);\n  // reads channel G, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n  roughnessFactor *= texelRoughness.g;\n  #endif\n\n  float targetRoughnessFactor = targetRoughness;\n  #ifdef USE_TARGET_ROUGHNESSMAP\n  vec4 texelTargetRoughness = texture2D(targetRoughnessMap, vRoughnessMapUv);\n  targetRoughnessFactor *= texelTargetRoughness.g;\n  #endif\n\n  roughnessFactor = mix(roughnessFactor, targetRoughnessFactor, ratio);\n\n  float metalnessFactor = metalness;\n  #ifdef USE_METALNESSMAP\n  vec4 texelMetalness = texture2D(metalnessMap, vMetalnessMapUv);\n  // reads channel B, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n  metalnessFactor *= texelMetalness.b;\n  #endif\n\n  float targetMetalnessFactor = targetMetalness;\n  #ifdef USE_TARGET_METALNESSMAP\n  vec4 texelTargetMetalness = texture2D(targetMetalnessMap, vMetalnessMapUv);\n  targetMetalnessFactor *= texelTargetMetalness.b;\n  #endif\n\n  metalnessFactor = mix(metalnessFactor, targetMetalnessFactor, ratio);\n\n  float faceDirection = gl_FrontFacing ? 1.0 : -1.0;\n\n  vec3 normal = normalize(vNormal);\n  #ifdef DOUBLE_SIDED\n  normal *= faceDirection;\n  #endif\n  #ifdef USE_NORMALMAP_TANGENTSPACE\n  #ifdef USE_TANGENT\n  mat3 tbn = mat3(normalize(vTangent), normalize(vBitangent), normal);\n  #else\n  mat3 tbn = getTangentFrame(-vViewPosition, normal,\n      #ifdef USE_NORMALMAP\n      vNormalMapUv\n      #else\n      vUv\n    #endif\n    );\n  #endif\n\n  #if defined( DOUBLE_SIDED ) && ! defined( FLAT_SHADED )\n\n  tbn[0] *= faceDirection;\n  tbn[1] *= faceDirection;\n\n  #endif\n\n  #endif\n\n  #ifdef USE_NORMALMAP_OBJECTSPACE\n  normal = texture2D(normalMap, vNormalMapUv).xyz * 2.0 - 1.0; // overrides both flatShading and attribute normals\n  #ifdef FLIP_SIDED\n  normal = -normal;\n  #endif\n  #ifdef DOUBLE_SIDED\n  normal = normal * faceDirection;\n  #endif\n  normal = normalize(normalMatrix * normal);\n  #elif defined( USE_NORMALMAP_TANGENTSPACE )\n  vec3 mapN = texture2D(normalMap, vNormalMapUv).xyz * 2.0 - 1.0;\n  mapN.xy *= normalScale;\n  #ifdef USE_TARGET_NORMALMAP\n  vec3 targetMapN = texture2D(targetNormalMap, vNormalMapUv).xyz * 2.0 - 1.0;\n  targetMapN.xy *= targetNormalScale;\n  mapN = mix(mapN, targetMapN, ratio);\n  #endif\n  normal = normalize(tbn * mapN);\n  #endif\n\n  #ifdef USE_EMISSIVEMAP\n  vec4 emissiveColor = texture2D(emissiveMap, vEmissiveMapUv);\n  totalEmissiveRadiance *= emissiveColor.rgb;\n  #endif\n\n  #ifdef USE_TARGET_EMISSIVEMAP\n  vec4 targetEmissiveColor = texture2D(targetEmissiveMap, vEmissiveMapUv);\n  targetTotalEmissiveRadiance *= targetEmissiveColor.rgb;\n  #endif\n\n  totalEmissiveRadiance = mix(totalEmissiveRadiance, targetTotalEmissiveRadiance, ratio);\n\n  vec3 radiance = vec3(0.0);\n  vec3 irradiance = vec3(0.0);\n  vec3 ambient = vec3(0.0);\n\n  #ifdef IBL\n  vec3 world_normal = inverseTransformDirection(normal, viewMatrix);\n  vec3 view_vector = normalize(cameraPosition - world_position_out.xyz);\n\n  float n_dot_v = max(dot(world_normal, view_vector), 0.0);\n\n  #ifdef IBL_IN_VIEW_SPACE\n  vec3 irradiance_normal = vec3(ibl_matrix * vec4(normal, 0.0));\n  #else\n  vec3 irradiance_normal = vec3(ibl_matrix * vec4(world_normal, 0.0));\n  #endif\n  irradiance = RGBDToHDR(textureCube(irradiance_map, irradiance_normal));\n\n  float lowerLevel = floor(roughnessFactor * kMaxRadianceLod);\n  float upperLevel = ceil(roughnessFactor * kMaxRadianceLod);\n\n  vec3 reflection_vector = reflect(-view_vector, world_normal);\n  // Mixing the reflection with the normal is more accurate and keeps rough objects from gathering light from behind their tangent plane.\n  reflection_vector = normalize(mix(reflection_vector, world_normal, roughnessFactor * roughnessFactor));\n  reflection_vector = vec3(ibl_matrix * vec4(reflection_vector, 0.0));\n  #ifdef IBL_IN_VIEW_SPACE\n  reflection_vector = vec3(viewMatrix * vec4(reflection_vector, 0.0));\n  #endif\n\n  vec3 lowerRadianceSample = RGBDToHDR(textureCubeLodEXT(radiance_map, reflection_vector, lowerLevel));\n  vec3 upperRadianceSample = RGBDToHDR(textureCubeLodEXT(radiance_map, reflection_vector, upperLevel));\n  radiance = mix(lowerRadianceSample, upperRadianceSample, (roughnessFactor * kMaxRadianceLod - lowerLevel));\n\n  #ifdef REFLECTOR\n  vec4 positionClip = projectionMatrix * viewMatrix * world_position_out;\n  vec3 positionNDC = positionClip.xyz / positionClip.w;\n  vec2 uv = positionNDC.xy * 0.5 + 0.5;\n\n  vec4 reflectorNormal = reflectorMatrix * vec4(0.0, 0.0, 1.0, 0.0);\n  float orientationFactor = step(0.0, dot(reflectorNormal.xyz, normal));\n\n  float reflectorDepth = texture2D(reflectorDepthMap, uv).x;\n\n  vec4 reflectorTexel = texture2D(reflectorMap, uv);\n\n  reflectorTexel.rgb = pow(reflectorTexel.rgb, vec3(2.2));\n  reflectorTexel.rgb = untoneMap(reflectorTexel.rgb);\n\n  radiance = mix(radiance, reflectorTexel.rgb, orientationFactor * (1.0 - step(1.0, reflectorDepth)));\n  #endif\n\n  ambient = mix(GetDielectricMultipleScattering(n_dot_v, roughnessFactor, diffuseColor.rgb, irradiance, radiance),\n      GetConductorMultipleScattering(n_dot_v, roughnessFactor, diffuseColor.rgb, irradiance, radiance), metalnessFactor);\n\n  #endif\n\n  vec3 outgoingLight = ambient;\n\n  #ifdef OPAQUE\n  diffuseColor.a = 1.0;\n  #endif\n\n  gl_FragColor = vec4(outgoingLight, diffuseColor.a);\n  gl_FragColor.xyz = toneMap(gl_FragColor.xyz);\n\n  // We assume emissive component is already tonemapped.\n  // TODO: Could be better to use a emissive only material, as combining emissive with other pbr component does not make much senses.\n  gl_FragColor.xyz += totalEmissiveRadiance;\n\n  #ifdef USE_AOMAP\n  vec3 occlusion = vec3((texture2D(aoMap, vAoMapUv).r - 1.0) * aoMapIntensity + 1.0);\n  #ifdef USE_TARGET_AOMAP\n  vec3 targetOcclusion = vec3((texture2D(targetAoMap, vAoMapUv).r - 1.0) * targetAoMapIntensity + 1.0);\n  occlusion = mix(occlusion, targetOcclusion, ratio);\n  #endif\n  gl_FragColor.xyz *= occlusion;\n  #endif\n\n  gl_FragColor.xyz = pow(gl_FragColor.xyz, vec3(1.0 / 2.2));\n\n  #ifdef PREMULTIPLIED_ALPHA\n  // Get normal blending with premultipled, use with CustomBlending, OneFactor, OneMinusSrcAlphaFactor, AddEquation.\n  gl_FragColor.rgb *= gl_FragColor.a;\n  #endif\n\n  #ifdef OUTPUT_OVERRIDE\n  gl_FragColor.xyz = OUTPUT_OVERRIDE;\n  #endif\n}\n"
    },
    {
      "path": "shaders/transition-material.vertex.glsl",
      "content": "#define STANDARD\nvarying vec3 vViewPosition;\n\nvarying vec4 world_position_out;\nvarying vec3 world_normal_out;\n\n#define PI 3.141592653589793\n#define PI2 6.283185307179586\n#define PI_HALF 1.5707963267948966\n#define RECIPROCAL_PI 0.3183098861837907\n#define RECIPROCAL_PI2 0.15915494309189535\n#define EPSILON 1e-6\n\n#ifdef USE_UV\nvarying vec2 vUv;\n#endif\n#if defined(USE_MAP) || defined(USE_TARGET_MAP)\nuniform mat3 mapTransform;\nvarying vec2 vMapUv;\n#endif\n#ifdef USE_ALPHAMAP\nuniform mat3 alphaMapTransform;\nvarying vec2 vAlphaMapUv;\n#endif\n#ifdef USE_LIGHTMAP\nuniform mat3 lightMapTransform;\nvarying vec2 vLightMapUv;\n#endif\n#if defined(USE_AOMAP) || defined(USE_TARGET_AOMAP)\nuniform mat3 aoMapTransform;\nvarying vec2 vAoMapUv;\n#endif\n#if defined(USE_NORMALMAP) || defined(USE_TARGET_NORMALMAP)\nuniform mat3 normalMapTransform;\nvarying vec2 vNormalMapUv;\n#endif\n#ifdef USE_DISPLACEMENTMAP\nuniform mat3 displacementMapTransform;\nvarying vec2 vDisplacementMapUv;\n#endif\n#if defined(USE_EMISSIVEMAP) || defined(USE_TARGET_EMISSIVEMAP)\nuniform mat3 emissiveMapTransform;\nvarying vec2 vEmissiveMapUv;\n#endif\n#if defined(USE_METALNESSMAP) || defined(USE_TARGET_METALNESSMAP)\nuniform mat3 metalnessMapTransform;\nvarying vec2 vMetalnessMapUv;\n#endif\n#if defined(USE_ROUGHNESSMAP) || defined(USE_TARGET_ROUGHNESSMAP)\nuniform mat3 roughnessMapTransform;\nvarying vec2 vRoughnessMapUv;\n#endif\n\nvarying vec3 vNormal;\n#ifdef USE_TANGENT\nvarying vec3 vTangent;\nvarying vec3 vBitangent;\n#endif\n\n#ifdef USE_SKINNING\nuniform mat4 bindMatrix;\nuniform mat4 bindMatrixInverse;\nuniform highp sampler2D boneTexture;\nmat4 getBoneMatrix(const in float i) {\n  int size = textureSize(boneTexture, 0).x;\n  int j = int(i) * 4;\n  int x = j % size;\n  int y = j / size;\n  vec4 v1 = texelFetch(boneTexture, ivec2(x, y), 0);\n  vec4 v2 = texelFetch(boneTexture, ivec2(x + 1, y), 0);\n  vec4 v3 = texelFetch(boneTexture, ivec2(x + 2, y), 0);\n  vec4 v4 = texelFetch(boneTexture, ivec2(x + 3, y), 0);\n  return mat4(v1, v2, v3, v4);\n}\n#endif\n\n#ifdef REFLECTOR\nuniform mat4 reflectorViewMatrix;\nuniform mat4 reflectorProjectionMatrix;\n#endif\n\nvoid main() {\n\n  #ifdef USE_UV\n  vUv = vec3(uv, 1).xy;\n  #endif\n  #if defined(USE_MAP) || defined(USE_TARGET_MAP)\n  vMapUv = (mapTransform * vec3(MAP_UV, 1)).xy;\n  #endif\n  #ifdef USE_ALPHAMAP\n  vAlphaMapUv = (alphaMapTransform * vec3(ALPHAMAP_UV, 1)).xy;\n  #endif\n  #ifdef USE_LIGHTMAP\n  vLightMapUv = (lightMapTransform * vec3(LIGHTMAP_UV, 1)).xy;\n  #endif\n  #if defined(USE_AOMAP) || defined(USE_TARGET_AOMAP)\n  vAoMapUv = (aoMapTransform * vec3(AOMAP_UV, 1)).xy;\n  #endif\n  #if defined(USE_NORMALMAP) || defined(USE_TARGET_NORMALMAP)\n  vNormalMapUv = (normalMapTransform * vec3(NORMALMAP_UV, 1)).xy;\n  #endif\n  #ifdef USE_DISPLACEMENTMAP\n  vDisplacementMapUv = (displacementMapTransform * vec3(DISPLACEMENTMAP_UV, 1)).xy;\n  #endif\n  #if defined(USE_EMISSIVEMAP) || defined(USE_TARGET_EMISSIVEMAP)\n  vEmissiveMapUv = (emissiveMapTransform * vec3(EMISSIVEMAP_UV, 1)).xy;\n  #endif\n  #if defined(USE_METALNESSMAP) || defined(USE_TARGET_METALNESSMAP)\n  vMetalnessMapUv = (metalnessMapTransform * vec3(METALNESSMAP_UV, 1)).xy;\n  #endif\n  #if defined(USE_ROUGHNESSMAP) || defined(USE_TARGET_ROUGHNESSMAP)\n  vRoughnessMapUv = (roughnessMapTransform * vec3(ROUGHNESSMAP_UV, 1)).xy;\n  #endif\n\n  vec3 objectNormal = vec3(normal);\n  #ifdef USE_TANGENT\n  vec3 objectTangent = vec3(tangent.xyz);\n  #endif\n\n  #ifdef USE_SKINNING\n  mat4 boneMatX = getBoneMatrix(skinIndex.x);\n  mat4 boneMatY = getBoneMatrix(skinIndex.y);\n  mat4 boneMatZ = getBoneMatrix(skinIndex.z);\n  mat4 boneMatW = getBoneMatrix(skinIndex.w);\n  #endif\n\n  #ifdef USE_SKINNING\n  mat4 skinMatrix = mat4(0.0);\n  skinMatrix += skinWeight.x * boneMatX;\n  skinMatrix += skinWeight.y * boneMatY;\n  skinMatrix += skinWeight.z * boneMatZ;\n  skinMatrix += skinWeight.w * boneMatW;\n  skinMatrix = bindMatrixInverse * skinMatrix * bindMatrix;\n  objectNormal = vec4(skinMatrix * vec4(objectNormal, 0.0)).xyz;\n  #ifdef USE_TANGENT\n  objectTangent = vec4(skinMatrix * vec4(objectTangent, 0.0)).xyz;\n  #endif\n  #endif\n\n  vec3 transformedNormal = objectNormal;\n  #ifdef USE_TANGENT\n  vec3 transformedTangent = objectTangent;\n  #endif\n\n  #ifdef USE_BATCHING\n  // this is in lieu of a per-instance normal-matrix\n  // shear transforms in the instance matrix are not supported\n  mat3 bm = mat3(batchingMatrix);\n  transformedNormal /= vec3(dot(bm[0], bm[0]), dot(bm[1], bm[1]), dot(bm[2], bm[2]));\n  transformedNormal = bm * transformedNormal;\n  #ifdef USE_TANGENT\n  transformedTangent = bm * transformedTangent;\n  #endif\n  #endif\n\n  #ifdef USE_INSTANCING\n  // this is in lieu of a per-instance normal-matrix\n  // shear transforms in the instance matrix are not supported\n  mat3 im = mat3(instanceMatrix);\n  transformedNormal /= vec3(dot(im[0], im[0]), dot(im[1], im[1]), dot(im[2], im[2]));\n  transformedNormal = im * transformedNormal;\n  #ifdef USE_TANGENT\n  transformedTangent = im * transformedTangent;\n  #endif\n  #endif\n\n  transformedNormal = normalMatrix * transformedNormal;\n\n  #ifdef FLIP_SIDED\n  transformedNormal = -transformedNormal;\n  #endif\n\n  #ifdef USE_TANGENT\n  transformedTangent = (modelViewMatrix * vec4(transformedTangent, 0.0)).xyz;\n  #ifdef FLIP_SIDED\n  transformedTangent = -transformedTangent;\n  #endif\n  #endif\n\n  vNormal = normalize(transformedNormal);\n  #ifdef USE_TANGENT\n  vTangent = normalize(transformedTangent);\n  vBitangent = normalize(cross(vNormal, vTangent) * tangent.w);\n  #endif\n\n  vec3 transformed = vec3(position);\n\n  #ifdef USE_SKINNING\n  vec4 skinVertex = bindMatrix * vec4(transformed, 1.0);\n\n  vec4 skinned = vec4(0.0);\n  skinned += boneMatX * skinVertex * skinWeight.x;\n  skinned += boneMatY * skinVertex * skinWeight.y;\n  skinned += boneMatZ * skinVertex * skinWeight.z;\n  skinned += boneMatW * skinVertex * skinWeight.w;\n\n  transformed = (bindMatrixInverse * skinned).xyz;\n  #endif\n  // #include <project_vertex>\n  vec4 mvPosition = vec4(transformed, 1.0);\n\n  #ifdef USE_BATCHING\n  mvPosition = batchingMatrix * mvPosition;\n  #endif\n\n  #ifdef USE_INSTANCING\n  mvPosition = instanceMatrix * mvPosition;\n  #endif\n\n  mvPosition = modelViewMatrix * mvPosition;\n\n  gl_Position = projectionMatrix * mvPosition;\n\n  vViewPosition = -mvPosition.xyz;\n\n  world_position_out = modelMatrix * vec4(position, 1.0);\n}\n"
    }
  ]
}