"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 GLTFInstance = require("../GLTFInstance.js");
const correlatedSceneGraph = require("./correlated-scene-graph.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.
 */
const $correlatedSceneGraph = Symbol("correlatedSceneGraph");
class ModelViewerGLTFInstance extends GLTFInstance.GLTFInstance {
  /**
   * @override
   */
  static [GLTFInstance.$prepare](source) {
    const prepared = super[GLTFInstance.$prepare](source);
    if (prepared[$correlatedSceneGraph] == null) {
      prepared[$correlatedSceneGraph] = correlatedSceneGraph.CorrelatedSceneGraph.from(prepared);
    }
    const { scene } = prepared;
    const nullSphere = new three_module.Sphere(void 0, Infinity);
    scene.traverse((node) => {
      node.renderOrder = 1e3;
      node.frustumCulled = false;
      if (!node.name) {
        node.name = node.uuid;
      }
      const mesh = node;
      if (mesh.material) {
        const { geometry } = mesh;
        mesh.castShadow = true;
        if (mesh.isSkinnedMesh) {
          geometry.boundingSphere = nullSphere;
          geometry.boundingBox = null;
        }
        const material = mesh.material;
        if (material.isMeshBasicMaterial === true) {
          material.toneMapped = false;
        }
        material.shadowSide = three_module.FrontSide;
        if (material.aoMap) {
          const { gltf, threeObjectMap } = prepared[$correlatedSceneGraph];
          const gltfRef = threeObjectMap.get(material);
          if (gltf.materials != null && gltfRef != null && gltfRef.materials != null) {
            const gltfMaterial = gltf.materials[gltfRef.materials];
            if (gltfMaterial.occlusionTexture && gltfMaterial.occlusionTexture.texCoord === 0 && geometry.attributes.uv != null) {
              geometry.setAttribute("uv2", geometry.attributes.uv);
            }
          }
        }
      }
    });
    return prepared;
  }
  get correlatedSceneGraph() {
    return this[GLTFInstance.$preparedGLTF][$correlatedSceneGraph];
  }
  /**
   * @override
   */
  [GLTFInstance.$clone]() {
    const clone = super[GLTFInstance.$clone]();
    const sourceUUIDToClonedMaterial = /* @__PURE__ */ new Map();
    clone.scene.traverse((node) => {
      const mesh = node;
      if (mesh.material) {
        const material = mesh.material;
        if (material != null) {
          if (sourceUUIDToClonedMaterial.has(material.uuid)) {
            mesh.material = sourceUUIDToClonedMaterial.get(material.uuid);
            return;
          }
          mesh.material = material.clone();
          sourceUUIDToClonedMaterial.set(material.uuid, mesh.material);
        }
      }
    });
    clone[$correlatedSceneGraph] = correlatedSceneGraph.CorrelatedSceneGraph.from(clone, this.correlatedSceneGraph);
    return clone;
  }
}
exports.ModelViewerGLTFInstance = ModelViewerGLTFInstance;
//# sourceMappingURL=ModelViewerGLTFInstance.js.map
