{
  "name": "create-material",
  "description": "Create a custom ibl based material.",
  "dependencies": [
    "three"
  ],
  "devDependencies": [
    "@types/three"
  ],
  "registryDependencies": [
    "dispose-tracker",
    "typings"
  ],
  "files": [
    {
      "path": "create-material.ts",
      "content": "import type { Scene } from 'three';\nimport { Material } from './material';\nimport { addToDisposeTracker } from '@3dvf/dispose-tracker/add-to-dispose-tracker';\n\nexport default function createMaterial(scene: Scene): Material {\n\tconst material = new Material();\n\tif (scene.userData['ibl']) {\n\t\tmaterial.ibl = scene.userData['ibl'];\n\t}\n\tmaterial.brdf = scene.userData['brdf'];\n\n\taddToDisposeTracker(scene, material);\n\n\treturn material;\n}\n"
    },
    {
      "path": "material.ts",
      "content": "import fragmentShader from \"./shaders/material.fragment.glsl\";\nimport vertexShader from \"./shaders/material.vertex.glsl\";\nimport {\n  Color,\n  type IUniform,\n  Matrix4,\n  MeshPhysicalMaterial,\n  ShaderMaterial,\n  TangentSpaceNormalMap,\n  Texture,\n  UniformsLib,\n  UniformsUtils,\n} from \"three\";\n\nexport type Ibl = {\n  radiance: Texture;\n  irradiance: Texture;\n  matrix: Matrix4;\n};\n\nexport enum IblSpace {\n  View,\n  World,\n}\n\nexport type MaterialUniforms = {\n  [uniform: string]: IUniform;\n  diffuse: { value: Color };\n  map: { value: Texture | null };\n  normalMap: { value: Texture | null };\n  roughness: { value: number };\n  roughnessMap: { value: Texture | null };\n  metalness: { value: number };\n  metalnessMap: { value: Texture | null };\n  alphaMap: { value: Texture | null };\n  aoMap: { value: Texture | null };\n  aoMapIntensity: { value: number };\n  emissiveMap: { value: Texture | null };\n  emissive: { value: Color };\n  radiance_map: { value: Texture | null };\n  irradiance_map: { value: Texture | null };\n  ibl_matrix: { value: Matrix4 };\n  brdf_map: { value: Texture | null };\n  reflectorMap: { value: Texture | null };\n  reflectorDepthMap: { value: Texture | null };\n  reflectorMatrix: { value: Matrix4 };\n  reflectorProjectionMatrix: { value: Matrix4 };\n  reflectorViewMatrix: { value: Matrix4 };\n};\n\ntype Defines = {\n  [define: string]: string | number;\n  OUTPUT_OVERRIDE: string;\n};\n\nexport class Material extends ShaderMaterial {\n  declare public uniforms: MaterialUniforms;\n  declare public defines: Defines;\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  public normalMapType: number;\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  private internalIbl: Ibl | null = null;\n  public get ibl(): Ibl | null {\n    return this.internalIbl;\n  }\n  public set ibl(ibl: Ibl | null) {\n    this.internalIbl = ibl;\n    this.uniforms[\"radiance_map\"].value = ibl?.radiance ?? null;\n    this.uniforms[\"irradiance_map\"].value = ibl?.irradiance ?? null;\n    this.uniforms[\"ibl_matrix\"].value = ibl?.matrix ?? new Matrix4();\n  }\n\n  public iblSpace: IblSpace = IblSpace.View;\n\n  private internalBrdf: Texture | null = null;\n  public get brdf(): Texture | null {\n    return this.internalBrdf;\n  }\n  public set brdf(brdf: Texture | null) {\n    this.internalBrdf = brdf;\n    this.uniforms[\"brdf_map\"].value = brdf;\n  }\n\n  public get reflectorMap(): Texture | null {\n    return this.uniforms[\"reflectorMap\"].value;\n  }\n  public set reflectorMap(reflectorMap: Texture | null) {\n    this.uniforms[\"reflectorMap\"].value = reflectorMap;\n    this.needsUpdate = true;\n  }\n  public get reflectorDepthMap(): Texture | null {\n    return this.uniforms[\"reflectorDepthMap\"].value;\n  }\n  public set reflectorDepthMap(reflectorDepthMap: Texture | null) {\n    this.uniforms[\"reflectorDepthMap\"].value = reflectorDepthMap;\n  }\n  public get reflectorMatrix(): Matrix4 {\n    return this.uniforms[\"reflectorMatrix\"].value;\n  }\n  public set reflectorMatrix(reflectorMatrix: Matrix4) {\n    this.uniforms[\"reflectorMatrix\"].value = reflectorMatrix;\n  }\n  public get reflectorProjectionMatrix(): Matrix4 {\n    return this.uniforms[\"reflectorProjectionMatrix\"].value;\n  }\n  public set reflectorProjectionMatrix(reflectorProjectionMatrix: Matrix4) {\n    this.uniforms[\"reflectorProjectionMatrix\"].value =\n      reflectorProjectionMatrix;\n  }\n  public get reflectorViewMatrix(): Matrix4 {\n    return this.uniforms[\"reflectorViewMatrix\"].value;\n  }\n  public set reflectorViewMatrix(reflectorViewMatrix: Matrix4) {\n    this.uniforms[\"reflectorViewMatrix\"].value = reflectorViewMatrix;\n  }\n\n  public get outputOverride(): string {\n    return this.defines[\"OUTPUT_OVERRIDE\"];\n  }\n  public set outputOverride(value: string) {\n    this.defines[\"OUTPUT_OVERRIDE\"] = value;\n  }\n\n  public isMeshPhysicalMaterial: boolean;\n\n  public constructor() {\n    const uniforms: MaterialUniforms = UniformsUtils.merge([\n      UniformsLib.common,\n      UniformsLib.roughnessmap,\n      UniformsLib.metalnessmap,\n      UniformsLib.normalmap,\n      UniformsLib.aomap,\n      UniformsLib.emissivemap,\n      {\n        roughness: { value: 1.0 },\n        metalness: { value: 1.0 },\n        emissive: { value: new Color(0x000000) },\n        radiance_map: { value: null },\n        irradiance_map: { value: null },\n        ibl_matrix: { value: new Matrix4() },\n        brdf_map: { value: null },\n        reflectorMap: { value: null },\n        reflectorDepthMap: { value: null },\n        reflectorMatrix: { value: new Matrix4() },\n        reflectorProjectionMatrix: { value: new Matrix4() },\n        reflectorViewMatrix: { value: new Matrix4() },\n      },\n    ]) as MaterialUniforms;\n\n    super({\n      name: \"material\",\n      uniforms,\n      vertexShader,\n      fragmentShader,\n    });\n\n    this.isMeshPhysicalMaterial = true;\n\n    this.normalMapType = TangentSpaceNormalMap;\n  }\n\n  public override onBeforeCompile() {\n    if (this.reflectorMap) {\n      this.defines[\"REFLECTOR\"] = \"\";\n    } else {\n      delete this.defines[\"REFLECTOR\"];\n    }\n\n    if (this.ibl) {\n      this.defines[\"IBL\"] = \"\";\n    } else {\n      delete this.defines[\"IBL\"];\n    }\n\n    if (this.iblSpace == IblSpace.View) {\n      this.defines[\"IBL_IN_VIEW_SPACE\"] = \"\";\n    } else {\n      delete this.defines[\"IBL_IN_VIEW_SPACE\"];\n    }\n  }\n\n  public override customProgramCacheKey(): string {\n    const reflectorCacheKey = this.reflectorMap ? \"reflector\" : \"\";\n    const iblCacheKey = this.ibl ? \"ibl\" : \"\";\n    const iblInViewSpaceCacheKey =\n      this.iblSpace == IblSpace.View ? \"iblInViewSpace\" : \"\";\n    return `${reflectorCacheKey}${iblCacheKey}${iblInViewSpaceCacheKey}`;\n  }\n\n  public vampMeshPhysicalMaterial(material: MeshPhysicalMaterial) {\n    this.name = material.name;\n    this.color.copy(material.color);\n    this.map = material.map;\n    this.normalMap = material.normalMap;\n    this.roughness = material.roughness;\n    this.roughnessMap = material.roughnessMap;\n    this.metalness = material.metalness;\n    this.metalnessMap = material.metalnessMap;\n    this.alphaMap = material.alphaMap;\n    this.aoMap = material.aoMap;\n    this.aoMapIntensity = material.aoMapIntensity;\n    this.emissiveMap = material.emissiveMap;\n    this.emissive.copy(\n      material.emissive.multiplyScalar(material.emissiveIntensity),\n    );\n  }\n}\n"
    },
    {
      "path": "shaders/material.fragment.glsl",
      "content": "#define STANDARD\n\nconst float kMaxRadianceLod = 9.0;\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 ) || defined( USE_ANISOTROPY )\nvarying vec2 vUv;\n#endif\n\n#ifdef USE_MAP\nvarying vec2 vMapUv;\n#endif\n\n#ifdef USE_ALPHAMAP\nvarying vec2 vAlphaMapUv;\n#endif\n\n#ifdef USE_AOMAP\nvarying vec2 vAoMapUv;\n#endif\n\n#ifdef USE_NORMALMAP\nvarying vec2 vNormalMapUv;\n#endif\n\n#ifdef USE_EMISSIVEMAP\nvarying vec2 vEmissiveMapUv;\n#endif\n\n#ifdef USE_METALNESSMAP\nvarying vec2 vMetalnessMapUv;\n#endif\n\n#ifdef USE_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_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  vec4 diffuseColor = vec4(pow(diffuse, vec3(2.2)), opacity);\n  vec3 totalEmissiveRadiance = emissive;\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_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 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 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  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  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);\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/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#ifdef USE_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#ifdef USE_AOMAP\nuniform mat3 aoMapTransform;\nvarying vec2 vAoMapUv;\n#endif\n#ifdef USE_NORMALMAP\nuniform mat3 normalMapTransform;\nvarying vec2 vNormalMapUv;\n#endif\n#ifdef USE_DISPLACEMENTMAP\nuniform mat3 displacementMapTransform;\nvarying vec2 vDisplacementMapUv;\n#endif\n#ifdef USE_EMISSIVEMAP\nuniform mat3 emissiveMapTransform;\nvarying vec2 vEmissiveMapUv;\n#endif\n#ifdef USE_METALNESSMAP\nuniform mat3 metalnessMapTransform;\nvarying vec2 vMetalnessMapUv;\n#endif\n#ifdef USE_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  #ifdef USE_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  #ifdef USE_AOMAP\n  vAoMapUv = (aoMapTransform * vec3(AOMAP_UV, 1)).xy;\n  #endif\n  #ifdef USE_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  #ifdef USE_EMISSIVEMAP\n  vEmissiveMapUv = (emissiveMapTransform * vec3(EMISSIVEMAP_UV, 1)).xy;\n  #endif\n  #ifdef USE_METALNESSMAP\n  vMetalnessMapUv = (metalnessMapTransform * vec3(METALNESSMAP_UV, 1)).xy;\n  #endif\n  #ifdef USE_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"
    }
  ]
}