"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var THREE = _interopRequireWildcard(require("three"));
var _TileMesh = _interopRequireDefault(require("../Core/TileMesh"));
var _LayeredMaterial = _interopRequireDefault(require("../Renderer/LayeredMaterial"));
var _TileBuilder = _interopRequireDefault(require("../Core/Prefab/TileBuilder"));
var _ReferencingLayerProperties = _interopRequireDefault(require("../Layer/ReferencingLayerProperties"));
var _GeoidLayer = require("../Layer/GeoidLayer");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

const dimensions = new THREE.Vector2();
function setTileFromTiledLayer(tile, tileLayer) {
  if (tileLayer.diffuse) {
    tile.material.diffuse = tileLayer.diffuse;
  }
  if (tileLayer.isGlobeLayer) {
    // Computes a point used for horizon culling.
    // If the point is below the horizon,
    // the tile is guaranteed to be below the horizon as well.
    tile.horizonCullingPoint = tile.extent.center().as('EPSG:4978').toVector3();
    tile.extent.planarDimensions(dimensions).multiplyScalar(THREE.MathUtils.DEG2RAD);

    // alpha is maximum angle between two points of tile
    const alpha = dimensions.length();
    const h = Math.abs(1.0 / Math.cos(alpha * 0.5));
    tile.horizonCullingPoint.setLength(h * tile.horizonCullingPoint.length());
    tile.horizonCullingPointElevationScaled = tile.horizonCullingPoint.clone();
  }
}
var _default = {
  convert(requester, extent, layer) {
    const builder = layer.builder;
    const parent = requester;
    const level = parent !== undefined ? parent.level + 1 : 0;
    const paramsGeometry = {
      extent,
      level,
      segment: layer.segments || 16,
      disableSkirt: layer.disableSkirt
    };
    return (0, _TileBuilder.default)(builder, paramsGeometry).then(result => {
      // build tile mesh
      result.geometry._count++;
      const crsCount = layer.tileMatrixSets.length;
      const material = new _LayeredMaterial.default(layer.materialOptions, crsCount);
      (0, _ReferencingLayerProperties.default)(material, layer);
      const tile = new _TileMesh.default(result.geometry, material, layer, extent, level);
      if (parent && parent.isTileMesh) {
        // get parent extent transformation
        const pTrans = builder.computeSharableExtent(parent.extent);
        // place relative to his parent
        result.position.sub(pTrans.position).applyQuaternion(pTrans.quaternion.invert());
        result.quaternion.premultiply(pTrans.quaternion);
      }
      tile.position.copy(result.position);
      tile.quaternion.copy(result.quaternion);
      tile.visible = false;
      tile.updateMatrix();
      setTileFromTiledLayer(tile, layer);
      if (parent) {
        tile.geoidHeight = parent.geoidHeight;
        const geoidHeight = (0, _GeoidLayer.geoidLayerIsVisible)(layer) ? tile.geoidHeight : 0;
        tile.setBBoxZ({
          min: parent.obb.z.min,
          max: parent.obb.z.max,
          geoidHeight
        });
        tile.material.geoidHeight = geoidHeight;
      }
      return tile;
    });
  }
};
exports.default = _default;