(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "three"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("three"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.three);
    global.TessellateModifier = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.TessellateModifier = void 0;

  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

  /**
   * Break faces with edges longer than maxEdgeLength
   */
  var TessellateModifier = /*#__PURE__*/function () {
    function TessellateModifier() {
      var maxEdgeLength = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0.1;
      var maxIterations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 6;

      _classCallCheck(this, TessellateModifier);

      this.maxEdgeLength = maxEdgeLength;
      this.maxIterations = maxIterations;
    }

    _createClass(TessellateModifier, [{
      key: "modify",
      value: function modify(geometry) {
        if (geometry.isGeometry === true) {
          console.error('THREE.TessellateModifier no longer supports Geometry. Use BufferGeometry instead.');
          return geometry;
        }

        if (geometry.index !== null) {
          geometry = geometry.toNonIndexed();
        } //


        var maxIterations = this.maxIterations;
        var maxEdgeLengthSquared = this.maxEdgeLength * this.maxEdgeLength;
        var va = new _three.Vector3();
        var vb = new _three.Vector3();
        var vc = new _three.Vector3();
        var vm = new _three.Vector3();
        var vs = [va, vb, vc, vm];
        var na = new _three.Vector3();
        var nb = new _three.Vector3();
        var nc = new _three.Vector3();
        var nm = new _three.Vector3();
        var ns = [na, nb, nc, nm];
        var ca = new _three.Color();
        var cb = new _three.Color();
        var cc = new _three.Color();
        var cm = new _three.Color();
        var cs = [ca, cb, cc, cm];
        var ua = new _three.Vector2();
        var ub = new _three.Vector2();
        var uc = new _three.Vector2();
        var um = new _three.Vector2();
        var us = [ua, ub, uc, um];
        var u2a = new _three.Vector2();
        var u2b = new _three.Vector2();
        var u2c = new _three.Vector2();
        var u2m = new _three.Vector2();
        var u2s = [u2a, u2b, u2c, u2m];
        var attributes = geometry.attributes;
        var hasNormals = attributes.normal !== undefined;
        var hasColors = attributes.color !== undefined;
        var hasUVs = attributes.uv !== undefined;
        var hasUV2s = attributes.uv2 !== undefined;
        var positions = attributes.position.array;
        var normals = hasNormals ? attributes.normal.array : null;
        var colors = hasColors ? attributes.color.array : null;
        var uvs = hasUVs ? attributes.uv.array : null;
        var uv2s = hasUV2s ? attributes.uv2.array : null;
        var positions2 = positions;
        var normals2 = normals;
        var colors2 = colors;
        var uvs2 = uvs;
        var uv2s2 = uv2s;
        var iteration = 0;
        var tessellating = true;

        function addTriangle(a, b, c) {
          var v1 = vs[a];
          var v2 = vs[b];
          var v3 = vs[c];
          positions2.push(v1.x, v1.y, v1.z);
          positions2.push(v2.x, v2.y, v2.z);
          positions2.push(v3.x, v3.y, v3.z);

          if (hasNormals) {
            var n1 = ns[a];
            var n2 = ns[b];
            var n3 = ns[c];
            normals2.push(n1.x, n1.y, n1.z);
            normals2.push(n2.x, n2.y, n2.z);
            normals2.push(n3.x, n3.y, n3.z);
          }

          if (hasColors) {
            var c1 = cs[a];
            var c2 = cs[b];
            var c3 = cs[c];
            colors2.push(c1.x, c1.y, c1.z);
            colors2.push(c2.x, c2.y, c2.z);
            colors2.push(c3.x, c3.y, c3.z);
          }

          if (hasUVs) {
            var u1 = us[a];
            var u2 = us[b];
            var u3 = us[c];
            uvs2.push(u1.x, u1.y);
            uvs2.push(u2.x, u2.y);
            uvs2.push(u3.x, u3.y);
          }

          if (hasUV2s) {
            var u21 = u2s[a];
            var u22 = u2s[b];
            var u23 = u2s[c];
            uv2s2.push(u21.x, u21.y);
            uv2s2.push(u22.x, u22.y);
            uv2s2.push(u23.x, u23.y);
          }
        }

        while (tessellating && iteration < maxIterations) {
          iteration++;
          tessellating = false;
          positions = positions2;
          positions2 = [];

          if (hasNormals) {
            normals = normals2;
            normals2 = [];
          }

          if (hasColors) {
            colors = colors2;
            colors2 = [];
          }

          if (hasUVs) {
            uvs = uvs2;
            uvs2 = [];
          }

          if (hasUV2s) {
            uv2s = uv2s2;
            uv2s2 = [];
          }

          for (var i = 0, i2 = 0, il = positions.length; i < il; i += 9, i2 += 6) {
            va.fromArray(positions, i + 0);
            vb.fromArray(positions, i + 3);
            vc.fromArray(positions, i + 6);

            if (hasNormals) {
              na.fromArray(normals, i + 0);
              nb.fromArray(normals, i + 3);
              nc.fromArray(normals, i + 6);
            }

            if (hasColors) {
              ca.fromArray(colors, i + 0);
              cb.fromArray(colors, i + 3);
              cc.fromArray(colors, i + 6);
            }

            if (hasUVs) {
              ua.fromArray(uvs, i2 + 0);
              ub.fromArray(uvs, i2 + 2);
              uc.fromArray(uvs, i2 + 4);
            }

            if (hasUV2s) {
              u2a.fromArray(uv2s, i2 + 0);
              u2b.fromArray(uv2s, i2 + 2);
              u2c.fromArray(uv2s, i2 + 4);
            }

            var dab = va.distanceToSquared(vb);
            var dbc = vb.distanceToSquared(vc);
            var dac = va.distanceToSquared(vc);

            if (dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared) {
              tessellating = true;

              if (dab >= dbc && dab >= dac) {
                vm.lerpVectors(va, vb, 0.5);
                if (hasNormals) nm.lerpVectors(na, nb, 0.5);
                if (hasColors) cm.lerpColors(ca, cb, 0.5);
                if (hasUVs) um.lerpVectors(ua, ub, 0.5);
                if (hasUV2s) u2m.lerpVectors(u2a, u2b, 0.5);
                addTriangle(0, 3, 2);
                addTriangle(3, 1, 2);
              } else if (dbc >= dab && dbc >= dac) {
                vm.lerpVectors(vb, vc, 0.5);
                if (hasNormals) nm.lerpVectors(nb, nc, 0.5);
                if (hasColors) cm.lerpColors(cb, cc, 0.5);
                if (hasUVs) um.lerpVectors(ub, uc, 0.5);
                if (hasUV2s) u2m.lerpVectors(u2b, u2c, 0.5);
                addTriangle(0, 1, 3);
                addTriangle(3, 2, 0);
              } else {
                vm.lerpVectors(va, vc, 0.5);
                if (hasNormals) nm.lerpVectors(na, nc, 0.5);
                if (hasColors) cm.lerpColors(ca, cc, 0.5);
                if (hasUVs) um.lerpVectors(ua, uc, 0.5);
                if (hasUV2s) u2m.lerpVectors(u2a, u2c, 0.5);
                addTriangle(0, 1, 3);
                addTriangle(3, 1, 2);
              }
            } else {
              addTriangle(0, 1, 2);
            }
          }
        }

        var geometry2 = new _three.BufferGeometry();
        geometry2.setAttribute('position', new _three.Float32BufferAttribute(positions2, 3));

        if (hasNormals) {
          geometry2.setAttribute('normal', new _three.Float32BufferAttribute(normals2, 3));
        }

        if (hasColors) {
          geometry2.setAttribute('color', new _three.Float32BufferAttribute(colors2, 3));
        }

        if (hasUVs) {
          geometry2.setAttribute('uv', new _three.Float32BufferAttribute(uvs2, 2));
        }

        if (hasUV2s) {
          geometry2.setAttribute('uv2', new _three.Float32BufferAttribute(uv2s2, 2));
        }

        return geometry2;
      }
    }]);

    return TessellateModifier;
  }();

  _exports.TessellateModifier = TessellateModifier;
});