"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const three_module = require("../../../../../../../three@0.154.0/node_modules/three/build/three.module.js");
const pbrMetallicRoughness = require("./pbr-metallic-roughness.js");
const textureInfo = require("./texture-info.js");
const threeDomElement = require("./three-dom-element.js");
/* @license
 * Copyright 2020 Google LLC. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var _a, _b;
const $pbrMetallicRoughness = Symbol("pbrMetallicRoughness");
const $normalTexture = Symbol("normalTexture");
const $occlusionTexture = Symbol("occlusionTexture");
const $emissiveTexture = Symbol("emissiveTexture");
const $backingThreeMaterial = Symbol("backingThreeMaterial");
const $applyAlphaCutoff = Symbol("applyAlphaCutoff");
const $getAlphaMode = Symbol("getAlphaMode");
const $lazyLoadGLTFInfo = Symbol("lazyLoadGLTFInfo");
const $initialize = Symbol("initialize");
const $getLoadedMaterial = Symbol("getLoadedMaterial");
const $ensureMaterialIsLoaded = Symbol("ensureMaterialIsLoaded");
const $gltfIndex = Symbol("gltfIndex");
const $setActive = Symbol("setActive");
const $variantIndices = Symbol("variantIndices");
const $isActive = Symbol("isActive");
const $variantSet = Symbol("variantSet");
const $modelVariants = Symbol("modelVariants");
const $name = Symbol("name");
const $pbrTextures = Symbol("pbrTextures");
class Material extends threeDomElement.ThreeDOMElement {
  constructor(onUpdate, gltfIndex, isActive, modelVariants, correlatedMaterials, name, lazyLoadInfo = void 0) {
    super(onUpdate, correlatedMaterials);
    this[_a] = /* @__PURE__ */ new Set();
    this[_b] = /* @__PURE__ */ new Map();
    this[$gltfIndex] = gltfIndex;
    this[$isActive] = isActive;
    this[$modelVariants] = modelVariants;
    this[$name] = name;
    if (lazyLoadInfo == null) {
      this[$initialize]();
    } else {
      this[$lazyLoadGLTFInfo] = lazyLoadInfo;
    }
  }
  get [(_a = $variantSet, _b = $pbrTextures, $backingThreeMaterial)]() {
    return this[threeDomElement.$correlatedObjects].values().next().value;
  }
  [$initialize]() {
    const onUpdate = this[threeDomElement.$onUpdate];
    const correlatedMaterials = this[threeDomElement.$correlatedObjects];
    this[$pbrMetallicRoughness] = new pbrMetallicRoughness.PBRMetallicRoughness(onUpdate, correlatedMaterials);
    const { normalMap, aoMap, emissiveMap } = correlatedMaterials.values().next().value;
    this[$normalTexture] = new textureInfo.TextureInfo(onUpdate, textureInfo.TextureUsage.Normal, normalMap, correlatedMaterials);
    this[$occlusionTexture] = new textureInfo.TextureInfo(onUpdate, textureInfo.TextureUsage.Occlusion, aoMap, correlatedMaterials);
    this[$emissiveTexture] = new textureInfo.TextureInfo(onUpdate, textureInfo.TextureUsage.Emissive, emissiveMap, correlatedMaterials);
    const createTextureInfo = (usage) => {
      this[$pbrTextures].set(usage, new textureInfo.TextureInfo(onUpdate, usage, null, correlatedMaterials));
    };
    createTextureInfo(textureInfo.TextureUsage.Clearcoat);
    createTextureInfo(textureInfo.TextureUsage.ClearcoatRoughness);
    createTextureInfo(textureInfo.TextureUsage.ClearcoatNormal);
    createTextureInfo(textureInfo.TextureUsage.SheenColor);
    createTextureInfo(textureInfo.TextureUsage.SheenRoughness);
    createTextureInfo(textureInfo.TextureUsage.Transmission);
    createTextureInfo(textureInfo.TextureUsage.Thickness);
    createTextureInfo(textureInfo.TextureUsage.Specular);
    createTextureInfo(textureInfo.TextureUsage.SpecularColor);
    createTextureInfo(textureInfo.TextureUsage.Iridescence);
    createTextureInfo(textureInfo.TextureUsage.IridescenceThickness);
    createTextureInfo(textureInfo.TextureUsage.Anisotropy);
  }
  async [$getLoadedMaterial]() {
    if (this[$lazyLoadGLTFInfo] != null) {
      const { set, material } = await this[$lazyLoadGLTFInfo].doLazyLoad();
      this[threeDomElement.$correlatedObjects] = set;
      this[$initialize]();
      this[$lazyLoadGLTFInfo] = void 0;
      this.ensureLoaded = async () => {
      };
      return material;
    }
    return this[threeDomElement.$correlatedObjects].values().next().value;
  }
  colorFromRgb(rgb) {
    const color = new three_module.Color();
    if (rgb instanceof Array) {
      color.fromArray(rgb);
    } else {
      color.set(rgb);
    }
    return color;
  }
  [$ensureMaterialIsLoaded]() {
    if (this[$lazyLoadGLTFInfo] == null) {
      return;
    }
    throw new Error(`Material "${this.name}" has not been loaded, call 'await
    myMaterial.ensureLoaded()' before using an unloaded material.`);
  }
  async ensureLoaded() {
    await this[$getLoadedMaterial]();
  }
  get isLoaded() {
    return this[$lazyLoadGLTFInfo] == null;
  }
  get isActive() {
    return this[$isActive];
  }
  [$setActive](isActive) {
    this[$isActive] = isActive;
  }
  get name() {
    return this[$name] || "";
  }
  set name(name) {
    this[$name] = name;
    if (this[threeDomElement.$correlatedObjects] != null) {
      for (const threeMaterial of this[threeDomElement.$correlatedObjects]) {
        threeMaterial.name = name;
      }
    }
  }
  get pbrMetallicRoughness() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrMetallicRoughness];
  }
  get normalTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$normalTexture];
  }
  get occlusionTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$occlusionTexture];
  }
  get emissiveTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$emissiveTexture];
  }
  get emissiveFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].emissive.toArray();
  }
  get index() {
    return this[$gltfIndex];
  }
  [$variantIndices]() {
    return this[$variantSet];
  }
  hasVariant(name) {
    const variantData = this[$modelVariants].get(name);
    return variantData != null && this[$variantSet].has(variantData.index);
  }
  setEmissiveFactor(rgb) {
    this[$ensureMaterialIsLoaded]();
    const color = this.colorFromRgb(rgb);
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.emissive.set(color);
    }
    this[threeDomElement.$onUpdate]();
  }
  [$getAlphaMode]() {
    if (this[$backingThreeMaterial].transparent) {
      return "BLEND";
    } else {
      if (this[$backingThreeMaterial].alphaTest > 0) {
        return "MASK";
      }
    }
    return "OPAQUE";
  }
  [$applyAlphaCutoff]() {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      if (this[$getAlphaMode]() === "MASK") {
        if (material.alphaTest == void 0) {
          material.alphaTest = 0.5;
        }
      } else {
        material.alphaTest = void 0;
      }
      material.needsUpdate = true;
    }
  }
  setAlphaCutoff(cutoff) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.alphaTest = cutoff;
      material.needsUpdate = true;
    }
    this[$applyAlphaCutoff]();
    this[threeDomElement.$onUpdate]();
  }
  getAlphaCutoff() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].alphaTest;
  }
  setDoubleSided(doubleSided) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.side = doubleSided ? three_module.DoubleSide : three_module.FrontSide;
      material.needsUpdate = true;
    }
    this[threeDomElement.$onUpdate]();
  }
  getDoubleSided() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].side == three_module.DoubleSide;
  }
  setAlphaMode(alphaMode) {
    this[$ensureMaterialIsLoaded]();
    const enableTransparency = (material, enabled) => {
      material.transparent = enabled;
      material.depthWrite = !enabled;
    };
    for (const material of this[threeDomElement.$correlatedObjects]) {
      enableTransparency(material, alphaMode === "BLEND");
      if (alphaMode === "MASK") {
        material.alphaTest = 0.5;
      } else {
        material.alphaTest = void 0;
      }
      material.needsUpdate = true;
    }
    this[threeDomElement.$onUpdate]();
  }
  getAlphaMode() {
    this[$ensureMaterialIsLoaded]();
    return this[$getAlphaMode]();
  }
  /**
   * PBR Next properties.
   */
  // KHR_materials_emissive_strength
  get emissiveStrength() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].emissiveIntensity;
  }
  setEmissiveStrength(emissiveStrength) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.emissiveIntensity = emissiveStrength;
    }
    this[threeDomElement.$onUpdate]();
  }
  // KHR_materials_clearcoat
  get clearcoatFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].clearcoat;
  }
  get clearcoatRoughnessFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].clearcoatRoughness;
  }
  get clearcoatTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.Clearcoat);
  }
  get clearcoatRoughnessTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.ClearcoatRoughness);
  }
  get clearcoatNormalTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.ClearcoatNormal);
  }
  get clearcoatNormalScale() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].clearcoatNormalScale.x;
  }
  setClearcoatFactor(clearcoatFactor) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.clearcoat = clearcoatFactor;
    }
    this[threeDomElement.$onUpdate]();
  }
  setClearcoatRoughnessFactor(clearcoatRoughnessFactor) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.clearcoatRoughness = clearcoatRoughnessFactor;
    }
    this[threeDomElement.$onUpdate]();
  }
  setClearcoatNormalScale(clearcoatNormalScale) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.clearcoatNormalScale = new three_module.Vector2(clearcoatNormalScale, clearcoatNormalScale);
    }
    this[threeDomElement.$onUpdate]();
  }
  // KHR_materials_ior
  get ior() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].ior;
  }
  setIor(ior) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.ior = ior;
    }
    this[threeDomElement.$onUpdate]();
  }
  // KHR_materials_sheen
  get sheenColorFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].sheenColor.toArray();
  }
  get sheenColorTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.SheenColor);
  }
  get sheenRoughnessFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].sheenRoughness;
  }
  get sheenRoughnessTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.SheenRoughness);
  }
  setSheenColorFactor(rgb) {
    this[$ensureMaterialIsLoaded]();
    const color = this.colorFromRgb(rgb);
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.sheenColor.set(color);
      material.sheen = 1;
    }
    this[threeDomElement.$onUpdate]();
  }
  setSheenRoughnessFactor(roughness) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.sheenRoughness = roughness;
      material.sheen = 1;
    }
    this[threeDomElement.$onUpdate]();
  }
  // KHR_materials_transmission
  get transmissionFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].transmission;
  }
  get transmissionTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.Transmission);
  }
  setTransmissionFactor(transmission) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.transmission = transmission;
    }
    this[threeDomElement.$onUpdate]();
  }
  // KHR_materials_volume
  get thicknessFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].thickness;
  }
  get thicknessTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.Thickness);
  }
  get attenuationDistance() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].attenuationDistance;
  }
  get attenuationColor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].attenuationColor.toArray();
  }
  setThicknessFactor(thickness) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.thickness = thickness;
    }
    this[threeDomElement.$onUpdate]();
  }
  setAttenuationDistance(attenuationDistance) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.attenuationDistance = attenuationDistance;
    }
    this[threeDomElement.$onUpdate]();
  }
  setAttenuationColor(rgb) {
    this[$ensureMaterialIsLoaded]();
    const color = this.colorFromRgb(rgb);
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.attenuationColor.set(color);
    }
    this[threeDomElement.$onUpdate]();
  }
  // KHR_materials_specular
  get specularFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].specularIntensity;
  }
  get specularTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.Specular);
  }
  get specularColorFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].specularColor.toArray();
  }
  get specularColorTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.SheenColor);
  }
  setSpecularFactor(specularFactor) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.specularIntensity = specularFactor;
    }
    this[threeDomElement.$onUpdate]();
  }
  setSpecularColorFactor(rgb) {
    this[$ensureMaterialIsLoaded]();
    const color = this.colorFromRgb(rgb);
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.specularColor.set(color);
    }
    this[threeDomElement.$onUpdate]();
  }
  // KHR_materials_iridescence
  get iridescenceFactor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].iridescence;
  }
  get iridescenceTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.Iridescence);
  }
  get iridescenceIor() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].iridescenceIOR;
  }
  get iridescenceThicknessMinimum() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].iridescenceThicknessRange[0];
  }
  get iridescenceThicknessMaximum() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].iridescenceThicknessRange[1];
  }
  get iridescenceThicknessTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.IridescenceThickness);
  }
  setIridescenceFactor(iridescence) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.iridescence = iridescence;
    }
    this[threeDomElement.$onUpdate]();
  }
  setIridescenceIor(ior) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.iridescenceIOR = ior;
    }
    this[threeDomElement.$onUpdate]();
  }
  setIridescenceThicknessMinimum(thicknessMin) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.iridescenceThicknessRange[0] = thicknessMin;
    }
    this[threeDomElement.$onUpdate]();
  }
  setIridescenceThicknessMaximum(thicknessMax) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.iridescenceThicknessRange[1] = thicknessMax;
    }
    this[threeDomElement.$onUpdate]();
  }
  // KHR_materials_anisotropy
  get anisotropyStrength() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].anisotropy;
  }
  get anisotropyRotation() {
    this[$ensureMaterialIsLoaded]();
    return this[$backingThreeMaterial].anisotropyRotation;
  }
  get anisotropyTexture() {
    this[$ensureMaterialIsLoaded]();
    return this[$pbrTextures].get(textureInfo.TextureUsage.Anisotropy);
  }
  setAnisotropyStrength(strength) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.anisotropy = strength;
    }
    this[threeDomElement.$onUpdate]();
  }
  setAnisotropyRotation(rotation) {
    this[$ensureMaterialIsLoaded]();
    for (const material of this[threeDomElement.$correlatedObjects]) {
      material.anisotropyRotation = rotation;
    }
    this[threeDomElement.$onUpdate]();
  }
}
exports.$ensureMaterialIsLoaded = $ensureMaterialIsLoaded;
exports.$getLoadedMaterial = $getLoadedMaterial;
exports.$gltfIndex = $gltfIndex;
exports.$lazyLoadGLTFInfo = $lazyLoadGLTFInfo;
exports.$setActive = $setActive;
exports.$variantIndices = $variantIndices;
exports.$variantSet = $variantSet;
exports.Material = Material;
//# sourceMappingURL=material.js.map
