{
  "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": "create-material-from-name.ts",
      "content": "import createMaterial from './create-material';\nimport { ImageLoader, RepeatWrapping, Texture, type Scene } from 'three';\nimport { addToDisposeTracker } from '@3dvf/dispose-tracker/add-to-dispose-tracker';\n\n/*\n* Strongly opiniated way of retrieving textures\n*/\nexport const getTextureUrl = (suffixe: string) => (basePath: string, name: string) => {\n\treturn `${basePath}${name}/${name}_${suffixe}`;\n};\n\nexport const createMaterialFromName = async (scene: Scene, basePath: string, name: string) => {\n\tconst loader = new ImageLoader();\n\n\tconst [albedoImage, normalImage, ormImage] = await Promise.all([\n\t\tloader.loadAsync(getTextureUrl('albedo.jpg')(basePath, name)),\n\t\tloader.loadAsync(getTextureUrl('normal.jpg')(basePath, name)),\n\t\tloader.loadAsync(getTextureUrl('orm.jpg')(basePath, name))\n\t]);\n\n  const material = createMaterial(scene);\n\n  material.name = name;\n\t\n\tmaterial.map = new Texture(albedoImage);\n  material.map.wrapS = RepeatWrapping;\n  material.map.wrapT = RepeatWrapping;\n  material.map.needsUpdate = true;\n  \n\tmaterial.normalMap = new Texture(normalImage);\n  material.normalMap.wrapS = RepeatWrapping;\n  material.normalMap.wrapT = RepeatWrapping;\n  material.normalMap.needsUpdate = true;\n\t\n\tconst ormTexture = new Texture(ormImage);\n\tormTexture.wrapS = RepeatWrapping;\n  ormTexture.wrapT = RepeatWrapping;\n  ormTexture.needsUpdate = true;\n  \n\tmaterial.roughnessMap = ormTexture;\n\tmaterial.metalnessMap = ormTexture;\n\tmaterial.aoMap = ormTexture;\n\n\tmaterial.needsUpdate = true;\n\n\taddToDisposeTracker(scene, material.map);\n\taddToDisposeTracker(scene, material.normalMap);\n\taddToDisposeTracker(scene, material.roughnessMap);\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 type IblSpace = \"view\" | \"world\";\nexport type ToneMapper = \"reinhard\" | \"aces\" | \"khronos_pbr_neutral\";\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  occlusionFactor: { 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\nexport type 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 occlusionFactor(): number {\n    return this.uniforms[\"occlusionFactor\"].value;\n  }\n  public set occlusionFactor(occlusionFactor: number) {\n    this.uniforms[\"occlusionFactor\"].value = occlusionFactor;\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 = \"world\";\n  public toneMapper: ToneMapper = \"khronos_pbr_neutral\";\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 ssaoMap(): Texture | null {\n    return this.uniforms[\"ssaoMap\"].value;\n  }\n  public set ssaoMap(ssaoMap: Texture | null) {\n    this.uniforms[\"ssaoMap\"].value = ssaoMap;\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 useTriplanarUvMapping: boolean = false;\n\n  public isMeshPhysicalMaterial: boolean;\n\n  public constructor(additionalUniforms: { [uniform: string]: IUniform<any> } = {}) {\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        occlusionFactor: { value: 2.0 },\n        ssaoMap: { value: null },\n      },\n      additionalUniforms\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  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    this.setDefine(\"REFLECTOR\", !!this.reflectorMap);\n    \n    this.setDefine(\"IBL\", !!this.ibl);\n    this.setDefine(\"IBL_IN_VIEW_SPACE\", this.iblSpace === \"view\");\n    \n    this.setDefine(\"USE_TRIPLANAR_UV_MAPPING\", this.useTriplanarUvMapping);\n    \n    this.setDefine(\"USE_REINHARD_TONEMAPPER\", this.toneMapper === \"reinhard\");\n    this.setDefine(\"USE_ACES_TONEMAPPER\", this.toneMapper === \"aces\");\n    this.setDefine(\"USE_KHRONOS_PBR_NEUTRAL_TONEMAPPER\", this.toneMapper === \"khronos_pbr_neutral\");\n    \n    this.setDefine(\"USE_SSAO\", !!this.ssaoMap);\n  }\n  \n  public override customProgramCacheKey(): string {\n    const reflectorCacheKey = this.reflectorMap ? \"reflector\" : \"\";\n    const iblCacheKey = this.ibl ? \"ibl\" : \"\";\n    const triplanarUvMapping = this.useTriplanarUvMapping ? \"triplanarUvMapping\" : \"\";\n    const iblInViewSpaceCacheKey =\n      this.iblSpace == \"view\" ? \"iblInViewSpace\" : \"\";\n    const ssaoCacheKey = this.ssaoMap ? \"ssao\" : \"\";\n    return `${reflectorCacheKey}${iblCacheKey}${iblInViewSpaceCacheKey}${triplanarUvMapping}${this.toneMapper}${ssaoCacheKey}`;\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.emissiveMap = material.emissiveMap;\n    this.emissive.copy(\n      material.emissive.multiplyScalar(material.emissiveIntensity),\n    );\n  }\n  \n  public override copy(source: Material) {\n    \n    // Ibl compressedCubeTexture appears to not be clonable, so we remove them temporary then add them as a shallow copy\n    const ibl = source.ibl;\n    source.ibl = null;\n    \n    super.copy(source);\n    \n    source.ibl = ibl;\n    this.ibl = ibl;\n    \n    this.useTriplanarUvMapping = source.useTriplanarUvMapping;\n    \n    if (source.outputOverride) {\n      this.outputOverride = source.outputOverride;\n    }\n    \n    this.iblSpace = source.iblSpace;\n    this.toneMapper = source.toneMapper;\n    \n    return this;\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 float occlusionFactor;\n\nuniform samplerCube irradiance_map;\nuniform samplerCube radiance_map;\nuniform sampler2D brdf_map;\n\nuniform mat4 ibl_matrix;\n\nvarying vec3 vViewPosition;\n\n#ifdef USE_SSAO\nvarying vec4 vClipPosition;\n#endif\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#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_MAP\nuniform sampler2D map;\n#endif\n\n#ifdef USE_TARGET_MAP\nuniform sampler2D targetMap;\n#endif\n\n#ifdef USE_TRANSITION\nuniform float ratio;\nuniform vec3 sphereCenter;\nuniform float sphereRadius;\n\nuniform vec3 targetDiffuse;\nuniform float targetRoughness;\nuniform float targetMetalness;\nuniform vec3 targetEmissive;\n#endif\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_TRIPLANAR_UV_MAPPING\nvarying vec3 vTriplanarUvMappingPosition;\nvarying vec3 vWorldNormal;\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#ifdef USE_REINHARD_TONEMAPPER\nvec3 toneMap(vec3 hdrColor) {\n  return hdrColor / (hdrColor + vec3(1.0));\n}\n#endif\n\n#ifdef USE_ACES_TONEMAPPER\nvec3 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#endif\n\n#ifdef USE_KHRONOS_PBR_NEUTRAL_TONEMAPPER\n// Input color is non-negative and resides in the Linear Rec. 709 color space.\n// Output color is also Linear Rec. 709, but in the [0, 1] range.\nvec3 toneMap(vec3 hdrColor) {\n  const float startCompression = 0.8 - 0.04;\n  const float desaturation = 0.15;\n\n  float x = min(hdrColor.r, min(hdrColor.g, hdrColor.b));\n  float offset = x < 0.08 ? x - 6.25 * x * x : 0.04;\n  hdrColor -= offset;\n\n  float peak = max(hdrColor.r, max(hdrColor.g, hdrColor.b));\n  if (peak < startCompression) return hdrColor;\n\n  const float d = 1. - startCompression;\n  float newPeak = 1. - d * d / (peak + d - startCompression);\n  hdrColor *= newPeak / peak;\n\n  float g = 1. - 1. / (desaturation * (peak - newPeak) + 1.);\n  return mix(hdrColor, newPeak * vec3(1, 1, 1), g);\n}\n#endif\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\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\n#ifdef USE_SSAO\nuniform sampler2D ssaoMap;\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 brdf_sample = texture2D(brdf_map, vec2(n_dot_v, roughness));\n  vec2 F_ab = brdf_sample.rg;\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 brdf_sample = texture2D(brdf_map, vec2(n_dot_v, roughness));\n  vec2 F_ab = brdf_sample.rg;\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\nfloat cubicBezier(vec4 controls, float t) {\n  return (1.0 - t) * (1.0 - t) * (1.0 - t) * controls.x + 3.0 * t * (1.0 - t) * (1.0 - t) * controls.y + 3.0 * t * t * (1.0 - t) * controls.z + t * t * t * controls.w;\n}\n\nvoid main() {\n\n  #ifdef USE_TRIPLANAR_UV_MAPPING\n  vec2 xProjectedUv = abs(dot(vWorldNormal, vec3(1.0, 0.0, 0.0))) * vTriplanarUvMappingPosition.yz;\n  vec2 yProjectedUv = abs(dot(vWorldNormal, vec3(0.0, 1.0, 0.0))) * vTriplanarUvMappingPosition.xz;\n  vec2 zProjectedUv = abs(dot(vWorldNormal, vec3(0.0, 0.0, 1.0))) * vTriplanarUvMappingPosition.xy;\n  vec2 world_uv = xProjectedUv + yProjectedUv + zProjectedUv;\n\n  vec2 mapUv = world_uv;\n  vec2 roughnessMapUv = world_uv;\n  vec2 metalnessMapUv = world_uv;\n  vec2 normalMapUv = world_uv;\n  #else\n  #if defined(USE_MAP) || defined(USE_TARGET_MAP)\n  vec2 mapUv = vMapUv;\n  #endif\n  #if defined(USE_ROUGHNESSMAP) || defined(USE_TARGET_ROUGHNESSMAP)\n  vec2 roughnessMapUv = vRoughnessMapUv;\n  #endif\n  #if defined(USE_METALNESSMAP) || defined(USE_TARGET_METALNESSMAP)\n  vec2 metalnessMapUv = vMetalnessMapUv;\n  #endif\n  #if defined(USE_NORMALMAP) || defined(USE_TARGET_NORMALMAP)\n  vec2 normalMapUv = vNormalMapUv;\n  #endif\n  #endif\n\n  vec4 diffuseColor = vec4(pow(diffuse, vec3(2.2)), opacity);\n  vec3 totalEmissiveRadiance = emissive;\n\n  #ifdef USE_TRANSITION\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 targetDiffuseColor = vec4(pow(targetDiffuse, vec3(2.2)), opacity);\n\n  vec3 targetTotalEmissiveRadiance = targetEmissive;\n  #endif\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      normalMapUv\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, normalMapUv).xyz * 2.0 - 1.0; // overrides both flatShading and attribute normals\n  normal = normalize(normalMatrix * normal);\n  #elif defined( USE_NORMALMAP_TANGENTSPACE )\n  vec3 mapN = texture2D(normalMap, normalMapUv).xyz * 2.0 - 1.0;\n  mapN.xy *= 1.0;\n  #ifdef USE_TARGET_NORMALMAP\n  vec3 targetMapN = texture2D(targetNormalMap, normalMapUv).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_MAP\n  vec4 diffuse_color_sample = texture2D(map, mapUv);\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, mapUv);\n  targetDiffuseColor *= pow(target_diffuse_color_sample, vec4(2.2));\n  #endif\n\n  #ifdef USE_TRANSITION\n  diffuseColor = mix(diffuseColor, targetDiffuseColor, ratio);\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, roughnessMapUv);\n  // reads channel G, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n  roughnessFactor *= texelRoughness.g;\n  #endif\n\n  #ifdef USE_TRANSITION\n  float targetRoughnessFactor = targetRoughness;\n  #ifdef USE_TARGET_ROUGHNESSMAP\n  vec4 texelTargetRoughness = texture2D(targetRoughnessMap, roughnessMapUv);\n  targetRoughnessFactor *= texelTargetRoughness.g;\n  #endif\n\n  roughnessFactor = mix(roughnessFactor, targetRoughnessFactor, ratio);\n  #endif\n\n  float metalnessFactor = metalness;\n  #ifdef USE_METALNESSMAP\n  vec4 texelMetalness = texture2D(metalnessMap, metalnessMapUv);\n  // reads channel B, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\n  metalnessFactor *= texelMetalness.b;\n  #endif\n\n  #ifdef USE_TRANSITION\n  float targetMetalnessFactor = targetMetalness;\n  #ifdef USE_TARGET_METALNESSMAP\n  vec4 texelTargetMetalness = texture2D(targetMetalnessMap, metalnessMapUv);\n  targetMetalnessFactor *= texelTargetMetalness.b;\n  #endif\n\n  metalnessFactor = mix(metalnessFactor, targetMetalnessFactor, ratio);\n  #endif\n\n  float faceDirection = gl_FrontFacing ? 1.0 : -1.0;\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  #ifdef USE_TRANSITION\n  totalEmissiveRadiance = mix(totalEmissiveRadiance, targetTotalEmissiveRadiance, ratio);\n  #endif\n\n  vec3 occlusion = vec3(1.0);\n  #ifdef USE_AOMAP\n  // One of many hacks, we assume that lightmaps have been tonemapped from and hdr bake, and we then clamp any hdr value greater than 1\n  vec3 occlusionSample = clamp(vec3(texture2D(aoMap, vAoMapUv).r), 0.0, 1.0);\n  occlusion = (occlusionSample - 1.0) * aoMapIntensity + 1.0;\n  #ifdef USE_TARGET_AOMAP\n  vec3 targetOcclusionSample = clamp(vec3(texture2D(targetAoMap, vAoMapUv).r), 0.0, 1.0);\n  vec3 targetOcclusion = (targetOcclusionSample - 1.0) * aoMapIntensity + 1.0;\n  occlusion = mix(occlusion, targetOcclusion, ratio);\n  #endif\n  #endif\n\n  #ifdef USE_SSAO\n  vec2 screenUv = (vClipPosition.xy / vClipPosition.w) * 0.5 + 0.5;\n  vec3 ssao = texture(ssaoMap, screenUv).rgb;\n  occlusion = min(occlusion, ssao);\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 = textureCube(irradiance_map, irradiance_normal).rgb;\n  // We apply occlusion to irradiance with an artistic factor\n  irradiance *= pow(occlusion, vec3(occlusionFactor));\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  radiance = textureCubeLodEXT(radiance_map, reflection_vector, roughnessFactor * kMaxRadianceLod).rgb;\n  // We add another artistic factor specifically for radiance, as a fake specular occlusion, with a smooth transition from no reflection to full reflection\n  vec3 radianceOcclusion = pow(vec3(smoothstep(0.6, 0.9, occlusion.r)), vec3(occlusionFactor));\n  radiance *= radianceOcclusion;\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  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\n#ifdef USE_SSAO\nvarying vec4 vClipPosition;\n#endif\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_TRIPLANAR_UV_MAPPING\nvarying vec3 vTriplanarUvMappingPosition;\nvarying vec3 vWorldNormal;\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  #ifdef USE_TRIPLANAR_UV_MAPPING\n  vWorldNormal = normal;\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  world_position_out = modelMatrix * vec4(position, 1.0);\n\n  #ifdef USE_TRIPLANAR_UV_MAPPING\n  vTriplanarUvMappingPosition = position + vec3(modelMatrix * vec4(0.0, 0.0, 0.0, 1.0)) * 0.5;\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\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  vViewPosition = -mvPosition.xyz;\n\n  gl_Position = projectionMatrix * mvPosition;\n\n  #ifdef USE_SSAO\n  vClipPosition = gl_Position;\n  #endif\n}\n"
    }
  ]
}