"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 image = require("./image.js");
const texture = require("./texture.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, _c;
const $texture = Symbol("texture");
const $transform = Symbol("transform");
const $materials = Symbol("materials");
const $usage = Symbol("usage");
const $onUpdate = Symbol("onUpdate");
const $activeVideo = Symbol("activeVideo");
exports.TextureUsage = void 0;
(function(TextureUsage) {
  TextureUsage[TextureUsage["Base"] = 0] = "Base";
  TextureUsage[TextureUsage["MetallicRoughness"] = 1] = "MetallicRoughness";
  TextureUsage[TextureUsage["Normal"] = 2] = "Normal";
  TextureUsage[TextureUsage["Occlusion"] = 3] = "Occlusion";
  TextureUsage[TextureUsage["Emissive"] = 4] = "Emissive";
  TextureUsage[TextureUsage["Clearcoat"] = 5] = "Clearcoat";
  TextureUsage[TextureUsage["ClearcoatRoughness"] = 6] = "ClearcoatRoughness";
  TextureUsage[TextureUsage["ClearcoatNormal"] = 7] = "ClearcoatNormal";
  TextureUsage[TextureUsage["SheenColor"] = 8] = "SheenColor";
  TextureUsage[TextureUsage["SheenRoughness"] = 9] = "SheenRoughness";
  TextureUsage[TextureUsage["Transmission"] = 10] = "Transmission";
  TextureUsage[TextureUsage["Thickness"] = 11] = "Thickness";
  TextureUsage[TextureUsage["Specular"] = 12] = "Specular";
  TextureUsage[TextureUsage["SpecularColor"] = 13] = "SpecularColor";
  TextureUsage[TextureUsage["Iridescence"] = 14] = "Iridescence";
  TextureUsage[TextureUsage["IridescenceThickness"] = 15] = "IridescenceThickness";
  TextureUsage[TextureUsage["Anisotropy"] = 16] = "Anisotropy";
})(exports.TextureUsage || (exports.TextureUsage = {}));
class TextureInfo {
  constructor(onUpdate, usage, threeTexture, material) {
    this[_a] = null;
    this[_b] = {
      rotation: 0,
      scale: new three_module.Vector2(1, 1),
      offset: new three_module.Vector2(0, 0)
    };
    this[_c] = false;
    if (threeTexture) {
      this[$transform].rotation = threeTexture.rotation;
      this[$transform].scale.copy(threeTexture.repeat);
      this[$transform].offset.copy(threeTexture.offset);
      this[$texture] = new texture.Texture(onUpdate, threeTexture);
    }
    this[$onUpdate] = onUpdate;
    this[$materials] = material;
    this[$usage] = usage;
  }
  get texture() {
    return this[$texture];
  }
  setTexture(texture2) {
    var _d, _e;
    const threeTexture = texture2 != null ? texture2.source[image.$threeTexture] : null;
    const oldTexture = (_d = this[$texture]) === null || _d === void 0 ? void 0 : _d.source[image.$threeTexture];
    if (oldTexture != null && oldTexture.isVideoTexture) {
      this[$activeVideo] = false;
    } else if ((_e = this[$texture]) === null || _e === void 0 ? void 0 : _e.source.animation) {
      this[$texture].source.animation.removeEventListener("enterFrame", this[$onUpdate]);
    }
    this[$texture] = texture2;
    if (threeTexture != null && threeTexture.isVideoTexture) {
      const element = threeTexture.image;
      this[$activeVideo] = true;
      if (element.requestVideoFrameCallback != null) {
        const update = () => {
          if (!this[$activeVideo]) {
            return;
          }
          this[$onUpdate]();
          element.requestVideoFrameCallback(update);
        };
        element.requestVideoFrameCallback(update);
      } else {
        const update = () => {
          if (!this[$activeVideo]) {
            return;
          }
          this[$onUpdate]();
          requestAnimationFrame(update);
        };
        requestAnimationFrame(update);
      }
    } else if ((texture2 === null || texture2 === void 0 ? void 0 : texture2.source.animation) != null) {
      texture2.source.animation.addEventListener("enterFrame", this[$onUpdate]);
    }
    let colorSpace = three_module.SRGBColorSpace;
    if (this[$materials]) {
      for (const material of this[$materials]) {
        switch (this[$usage]) {
          case exports.TextureUsage.Base:
            material.map = threeTexture;
            break;
          case exports.TextureUsage.MetallicRoughness:
            colorSpace = three_module.LinearSRGBColorSpace;
            material.metalnessMap = threeTexture;
            material.roughnessMap = threeTexture;
            break;
          case exports.TextureUsage.Normal:
            colorSpace = three_module.LinearSRGBColorSpace;
            material.normalMap = threeTexture;
            break;
          case exports.TextureUsage.Occlusion:
            colorSpace = three_module.LinearSRGBColorSpace;
            material.aoMap = threeTexture;
            break;
          case exports.TextureUsage.Emissive:
            material.emissiveMap = threeTexture;
            break;
          case exports.TextureUsage.Clearcoat:
            material.clearcoatMap = threeTexture;
            break;
          case exports.TextureUsage.ClearcoatRoughness:
            material.clearcoatRoughnessMap = threeTexture;
            break;
          case exports.TextureUsage.ClearcoatNormal:
            material.clearcoatNormalMap = threeTexture;
            break;
          case exports.TextureUsage.SheenColor:
            material.sheenColorMap = threeTexture;
            break;
          case exports.TextureUsage.SheenRoughness:
            material.sheenRoughnessMap = threeTexture;
            break;
          case exports.TextureUsage.Transmission:
            material.transmissionMap = threeTexture;
            break;
          case exports.TextureUsage.Thickness:
            material.thicknessMap = threeTexture;
            break;
          case exports.TextureUsage.Specular:
            material.specularIntensityMap = threeTexture;
            break;
          case exports.TextureUsage.SpecularColor:
            material.specularColorMap = threeTexture;
            break;
          case exports.TextureUsage.Iridescence:
            material.iridescenceMap = threeTexture;
            break;
          case exports.TextureUsage.IridescenceThickness:
            material.iridescenceThicknessMap = threeTexture;
            break;
          case exports.TextureUsage.Anisotropy:
            material.anisotropyMap = threeTexture;
            break;
        }
        material.needsUpdate = true;
      }
    }
    if (threeTexture) {
      threeTexture.colorSpace = colorSpace;
      threeTexture.rotation = this[$transform].rotation;
      threeTexture.repeat = this[$transform].scale;
      threeTexture.offset = this[$transform].offset;
    }
    this[$onUpdate]();
  }
}
_a = $texture, _b = $transform, _c = $activeVideo;
exports.$materials = $materials;
exports.$usage = $usage;
exports.TextureInfo = TextureInfo;
//# sourceMappingURL=texture-info.js.map
