import { d as bind_this } from './i18n-CGlVUOvE.js';
import { M as push, a8 as onMount, ad as user_effect, a as append, O as pop, R as from_html, $ as set, Z as get, ab as state, a2 as user_derived } from './index-Bq2njFOY.js';

class R {
  constructor(t = 0, n = 0, i = 0) {
    this.x = t, this.y = n, this.z = i;
  }
  equals(t) {
    return !(this.x !== t.x || this.y !== t.y || this.z !== t.z);
  }
  add(t) {
    return typeof t == "number" ? new R(this.x + t, this.y + t, this.z + t) : new R(this.x + t.x, this.y + t.y, this.z + t.z);
  }
  subtract(t) {
    return typeof t == "number" ? new R(this.x - t, this.y - t, this.z - t) : new R(this.x - t.x, this.y - t.y, this.z - t.z);
  }
  multiply(t) {
    return typeof t == "number" ? new R(this.x * t, this.y * t, this.z * t) : t instanceof R ? new R(this.x * t.x, this.y * t.y, this.z * t.z) : new R(
      this.x * t.buffer[0] + this.y * t.buffer[4] + this.z * t.buffer[8] + t.buffer[12],
      this.x * t.buffer[1] + this.y * t.buffer[5] + this.z * t.buffer[9] + t.buffer[13],
      this.x * t.buffer[2] + this.y * t.buffer[6] + this.z * t.buffer[10] + t.buffer[14]
    );
  }
  divide(t) {
    return typeof t == "number" ? new R(this.x / t, this.y / t, this.z / t) : new R(this.x / t.x, this.y / t.y, this.z / t.z);
  }
  cross(t) {
    const n = this.y * t.z - this.z * t.y, i = this.z * t.x - this.x * t.z, e = this.x * t.y - this.y * t.x;
    return new R(n, i, e);
  }
  dot(t) {
    return this.x * t.x + this.y * t.y + this.z * t.z;
  }
  lerp(t, n) {
    return new R(this.x + (t.x - this.x) * n, this.y + (t.y - this.y) * n, this.z + (t.z - this.z) * n);
  }
  min(t) {
    return new R(Math.min(this.x, t.x), Math.min(this.y, t.y), Math.min(this.z, t.z));
  }
  max(t) {
    return new R(Math.max(this.x, t.x), Math.max(this.y, t.y), Math.max(this.z, t.z));
  }
  getComponent(t) {
    switch (t) {
      case 0:
        return this.x;
      case 1:
        return this.y;
      case 2:
        return this.z;
      default:
        throw new Error(`Invalid component index: ${t}`);
    }
  }
  minComponent() {
    return this.x < this.y && this.x < this.z ? 0 : this.y < this.z ? 1 : 2;
  }
  maxComponent() {
    return this.x > this.y && this.x > this.z ? 0 : this.y > this.z ? 1 : 2;
  }
  magnitude() {
    return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  }
  distanceTo(t) {
    return Math.sqrt((this.x - t.x) ** 2 + (this.y - t.y) ** 2 + (this.z - t.z) ** 2);
  }
  normalize() {
    const t = this.magnitude();
    return new R(this.x / t, this.y / t, this.z / t);
  }
  flat() {
    return [this.x, this.y, this.z];
  }
  clone() {
    return new R(this.x, this.y, this.z);
  }
  toString() {
    return `[${this.flat().join(", ")}]`;
  }
  static One(t = 1) {
    return new R(t, t, t);
  }
}
class y {
  constructor(t = 0, n = 0, i = 0, e = 1) {
    this.x = t, this.y = n, this.z = i, this.w = e;
  }
  equals(t) {
    return !(this.x !== t.x || this.y !== t.y || this.z !== t.z || this.w !== t.w);
  }
  normalize() {
    const t = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
    return new y(this.x / t, this.y / t, this.z / t, this.w / t);
  }
  multiply(t) {
    const n = this.w, i = this.x, e = this.y, A = this.z, o = t.w, s = t.x, r = t.y, Q = t.z;
    return new y(
      n * s + i * o + e * Q - A * r,
      n * r - i * Q + e * o + A * s,
      n * Q + i * r - e * s + A * o,
      n * o - i * s - e * r - A * Q
    );
  }
  inverse() {
    const t = this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
    return new y(-this.x / t, -this.y / t, -this.z / t, this.w / t);
  }
  apply(t) {
    const n = new y(t.x, t.y, t.z, 0), i = new y(-this.x, -this.y, -this.z, this.w), e = this.multiply(n).multiply(i);
    return new R(e.x, e.y, e.z);
  }
  flat() {
    return [this.x, this.y, this.z, this.w];
  }
  clone() {
    return new y(this.x, this.y, this.z, this.w);
  }
  static FromEuler(t) {
    const n = t.x / 2, i = t.y / 2, e = t.z / 2, A = Math.cos(i), o = Math.sin(i), s = Math.cos(n), r = Math.sin(n), Q = Math.cos(e), I = Math.sin(e);
    return new y(
      A * r * Q + o * s * I,
      o * s * Q - A * r * I,
      A * s * I - o * r * Q,
      A * s * Q + o * r * I
    );
  }
  toEuler() {
    const t = 2 * (this.w * this.x + this.y * this.z), n = 1 - 2 * (this.x * this.x + this.y * this.y), i = Math.atan2(t, n);
    let e;
    const A = 2 * (this.w * this.y - this.z * this.x);
    Math.abs(A) >= 1 ? e = Math.sign(A) * Math.PI / 2 : e = Math.asin(A);
    const o = 2 * (this.w * this.z + this.x * this.y), s = 1 - 2 * (this.y * this.y + this.z * this.z), r = Math.atan2(o, s);
    return new R(i, e, r);
  }
  static FromMatrix3(t) {
    const n = t.buffer, i = n[0] + n[4] + n[8];
    let e, A, o, s;
    if (i > 0) {
      const r = 0.5 / Math.sqrt(i + 1);
      s = 0.25 / r, e = (n[7] - n[5]) * r, A = (n[2] - n[6]) * r, o = (n[3] - n[1]) * r;
    } else if (n[0] > n[4] && n[0] > n[8]) {
      const r = 2 * Math.sqrt(1 + n[0] - n[4] - n[8]);
      s = (n[7] - n[5]) / r, e = 0.25 * r, A = (n[1] + n[3]) / r, o = (n[2] + n[6]) / r;
    } else if (n[4] > n[8]) {
      const r = 2 * Math.sqrt(1 + n[4] - n[0] - n[8]);
      s = (n[2] - n[6]) / r, e = (n[1] + n[3]) / r, A = 0.25 * r, o = (n[5] + n[7]) / r;
    } else {
      const r = 2 * Math.sqrt(1 + n[8] - n[0] - n[4]);
      s = (n[3] - n[1]) / r, e = (n[2] + n[6]) / r, A = (n[5] + n[7]) / r, o = 0.25 * r;
    }
    return new y(e, A, o, s);
  }
  static FromAxisAngle(t, n) {
    const i = n / 2, e = Math.sin(i), A = Math.cos(i);
    return new y(t.x * e, t.y * e, t.z * e, A);
  }
  static LookRotation(t) {
    const n = new R(0, 0, 1), i = n.dot(t);
    if (Math.abs(i - -1) < 1e-6)
      return new y(0, 1, 0, Math.PI);
    if (Math.abs(i - 1) < 1e-6)
      return new y();
    const e = Math.acos(i), A = n.cross(t).normalize();
    return y.FromAxisAngle(A, e);
  }
  toString() {
    return `[${this.flat().join(", ")}]`;
  }
}
class Ft {
  constructor() {
    const t = /* @__PURE__ */ new Map();
    this.addEventListener = (n, i) => {
      t.has(n) || t.set(n, /* @__PURE__ */ new Set()), t.get(n).add(i);
    }, this.removeEventListener = (n, i) => {
      t.has(n) && t.get(n).delete(i);
    }, this.hasEventListener = (n, i) => t.has(n) ? t.get(n).has(i) : false, this.dispatchEvent = (n) => {
      if (t.has(n.type))
        for (const i of t.get(n.type))
          i(n);
    };
  }
}
class K {
  // prettier-ignore
  constructor(t = 1, n = 0, i = 0, e = 0, A = 0, o = 1, s = 0, r = 0, Q = 0, I = 0, d = 1, a = 0, U = 0, F = 0, g = 0, B = 1) {
    this.buffer = [
      t,
      n,
      i,
      e,
      A,
      o,
      s,
      r,
      Q,
      I,
      d,
      a,
      U,
      F,
      g,
      B
    ];
  }
  equals(t) {
    if (this.buffer.length !== t.buffer.length)
      return false;
    if (this.buffer === t.buffer)
      return true;
    for (let n = 0; n < this.buffer.length; n++)
      if (this.buffer[n] !== t.buffer[n])
        return false;
    return true;
  }
  multiply(t) {
    const n = this.buffer, i = t.buffer;
    return new K(
      i[0] * n[0] + i[1] * n[4] + i[2] * n[8] + i[3] * n[12],
      i[0] * n[1] + i[1] * n[5] + i[2] * n[9] + i[3] * n[13],
      i[0] * n[2] + i[1] * n[6] + i[2] * n[10] + i[3] * n[14],
      i[0] * n[3] + i[1] * n[7] + i[2] * n[11] + i[3] * n[15],
      i[4] * n[0] + i[5] * n[4] + i[6] * n[8] + i[7] * n[12],
      i[4] * n[1] + i[5] * n[5] + i[6] * n[9] + i[7] * n[13],
      i[4] * n[2] + i[5] * n[6] + i[6] * n[10] + i[7] * n[14],
      i[4] * n[3] + i[5] * n[7] + i[6] * n[11] + i[7] * n[15],
      i[8] * n[0] + i[9] * n[4] + i[10] * n[8] + i[11] * n[12],
      i[8] * n[1] + i[9] * n[5] + i[10] * n[9] + i[11] * n[13],
      i[8] * n[2] + i[9] * n[6] + i[10] * n[10] + i[11] * n[14],
      i[8] * n[3] + i[9] * n[7] + i[10] * n[11] + i[11] * n[15],
      i[12] * n[0] + i[13] * n[4] + i[14] * n[8] + i[15] * n[12],
      i[12] * n[1] + i[13] * n[5] + i[14] * n[9] + i[15] * n[13],
      i[12] * n[2] + i[13] * n[6] + i[14] * n[10] + i[15] * n[14],
      i[12] * n[3] + i[13] * n[7] + i[14] * n[11] + i[15] * n[15]
    );
  }
  clone() {
    const t = this.buffer;
    return new K(
      t[0],
      t[1],
      t[2],
      t[3],
      t[4],
      t[5],
      t[6],
      t[7],
      t[8],
      t[9],
      t[10],
      t[11],
      t[12],
      t[13],
      t[14],
      t[15]
    );
  }
  determinant() {
    const t = this.buffer;
    return t[12] * t[9] * t[6] * t[3] - t[8] * t[13] * t[6] * t[3] - t[12] * t[5] * t[10] * t[3] + t[4] * t[13] * t[10] * t[3] + t[8] * t[5] * t[14] * t[3] - t[4] * t[9] * t[14] * t[3] - t[12] * t[9] * t[2] * t[7] + t[8] * t[13] * t[2] * t[7] + t[12] * t[1] * t[10] * t[7] - t[0] * t[13] * t[10] * t[7] - t[8] * t[1] * t[14] * t[7] + t[0] * t[9] * t[14] * t[7] + t[12] * t[5] * t[2] * t[11] - t[4] * t[13] * t[2] * t[11] - t[12] * t[1] * t[6] * t[11] + t[0] * t[13] * t[6] * t[11] + t[4] * t[1] * t[14] * t[11] - t[0] * t[5] * t[14] * t[11] - t[8] * t[5] * t[2] * t[15] + t[4] * t[9] * t[2] * t[15] + t[8] * t[1] * t[6] * t[15] - t[0] * t[9] * t[6] * t[15] - t[4] * t[1] * t[10] * t[15] + t[0] * t[5] * t[10] * t[15];
  }
  invert() {
    const t = this.buffer, n = this.determinant();
    if (n === 0)
      throw new Error("Matrix is not invertible.");
    const i = 1 / n;
    return new K(
      i * (t[5] * t[10] * t[15] - t[5] * t[11] * t[14] - t[9] * t[6] * t[15] + t[9] * t[7] * t[14] + t[13] * t[6] * t[11] - t[13] * t[7] * t[10]),
      i * (-t[1] * t[10] * t[15] + t[1] * t[11] * t[14] + t[9] * t[2] * t[15] - t[9] * t[3] * t[14] - t[13] * t[2] * t[11] + t[13] * t[3] * t[10]),
      i * (t[1] * t[6] * t[15] - t[1] * t[7] * t[14] - t[5] * t[2] * t[15] + t[5] * t[3] * t[14] + t[13] * t[2] * t[7] - t[13] * t[3] * t[6]),
      i * (-t[1] * t[6] * t[11] + t[1] * t[7] * t[10] + t[5] * t[2] * t[11] - t[5] * t[3] * t[10] - t[9] * t[2] * t[7] + t[9] * t[3] * t[6]),
      i * (-t[4] * t[10] * t[15] + t[4] * t[11] * t[14] + t[8] * t[6] * t[15] - t[8] * t[7] * t[14] - t[12] * t[6] * t[11] + t[12] * t[7] * t[10]),
      i * (t[0] * t[10] * t[15] - t[0] * t[11] * t[14] - t[8] * t[2] * t[15] + t[8] * t[3] * t[14] + t[12] * t[2] * t[11] - t[12] * t[3] * t[10]),
      i * (-t[0] * t[6] * t[15] + t[0] * t[7] * t[14] + t[4] * t[2] * t[15] - t[4] * t[3] * t[14] - t[12] * t[2] * t[7] + t[12] * t[3] * t[6]),
      i * (t[0] * t[6] * t[11] - t[0] * t[7] * t[10] - t[4] * t[2] * t[11] + t[4] * t[3] * t[10] + t[8] * t[2] * t[7] - t[8] * t[3] * t[6]),
      i * (t[4] * t[9] * t[15] - t[4] * t[11] * t[13] - t[8] * t[5] * t[15] + t[8] * t[7] * t[13] + t[12] * t[5] * t[11] - t[12] * t[7] * t[9]),
      i * (-t[0] * t[9] * t[15] + t[0] * t[11] * t[13] + t[8] * t[1] * t[15] - t[8] * t[3] * t[13] - t[12] * t[1] * t[11] + t[12] * t[3] * t[9]),
      i * (t[0] * t[5] * t[15] - t[0] * t[7] * t[13] - t[4] * t[1] * t[15] + t[4] * t[3] * t[13] + t[12] * t[1] * t[7] - t[12] * t[3] * t[5]),
      i * (-t[0] * t[5] * t[11] + t[0] * t[7] * t[9] + t[4] * t[1] * t[11] - t[4] * t[3] * t[9] - t[8] * t[1] * t[7] + t[8] * t[3] * t[5]),
      i * (-t[4] * t[9] * t[14] + t[4] * t[10] * t[13] + t[8] * t[5] * t[14] - t[8] * t[6] * t[13] - t[12] * t[5] * t[10] + t[12] * t[6] * t[9]),
      i * (t[0] * t[9] * t[14] - t[0] * t[10] * t[13] - t[8] * t[1] * t[14] + t[8] * t[2] * t[13] + t[12] * t[1] * t[10] - t[12] * t[2] * t[9]),
      i * (-t[0] * t[5] * t[14] + t[0] * t[6] * t[13] + t[4] * t[1] * t[14] - t[4] * t[2] * t[13] - t[12] * t[1] * t[6] + t[12] * t[2] * t[5]),
      i * (t[0] * t[5] * t[10] - t[0] * t[6] * t[9] - t[4] * t[1] * t[10] + t[4] * t[2] * t[9] + t[8] * t[1] * t[6] - t[8] * t[2] * t[5])
    );
  }
  static Compose(t, n, i) {
    const e = n.x, A = n.y, o = n.z, s = n.w, r = e + e, Q = A + A, I = o + o, d = e * r, a = e * Q, U = e * I, F = A * Q, g = A * I, B = o * I, C = s * r, c = s * Q, p = s * I, u = i.x, S = i.y, W = i.z;
    return new K(
      (1 - (F + B)) * u,
      (a + p) * u,
      (U - c) * u,
      0,
      (a - p) * S,
      (1 - (d + B)) * S,
      (g + C) * S,
      0,
      (U + c) * W,
      (g - C) * W,
      (1 - (d + F)) * W,
      0,
      t.x,
      t.y,
      t.z,
      1
    );
  }
  toString() {
    return `[${this.buffer.join(", ")}]`;
  }
}
class It extends Event {
  constructor(t) {
    super("objectAdded"), this.object = t;
  }
}
class ht extends Event {
  constructor(t) {
    super("objectRemoved"), this.object = t;
  }
}
class ct extends Event {
  constructor(t) {
    super("objectChanged"), this.object = t;
  }
}
class st extends Ft {
  constructor() {
    super(), this.positionChanged = false, this.rotationChanged = false, this.scaleChanged = false, this._position = new R(), this._rotation = new y(), this._scale = new R(1, 1, 1), this._transform = new K(), this._changeEvent = new ct(this), this.update = () => {
    }, this.applyPosition = () => {
      this.position = new R();
    }, this.applyRotation = () => {
      this.rotation = new y();
    }, this.applyScale = () => {
      this.scale = new R(1, 1, 1);
    }, this.raiseChangeEvent = () => {
      this.dispatchEvent(this._changeEvent);
    };
  }
  _updateMatrix() {
    this._transform = K.Compose(this._position, this._rotation, this._scale);
  }
  get position() {
    return this._position;
  }
  set position(t) {
    this._position.equals(t) || (this._position = t, this.positionChanged = true, this._updateMatrix(), this.dispatchEvent(this._changeEvent));
  }
  get rotation() {
    return this._rotation;
  }
  set rotation(t) {
    this._rotation.equals(t) || (this._rotation = t, this.rotationChanged = true, this._updateMatrix(), this.dispatchEvent(this._changeEvent));
  }
  get scale() {
    return this._scale;
  }
  set scale(t) {
    this._scale.equals(t) || (this._scale = t, this.scaleChanged = true, this._updateMatrix(), this.dispatchEvent(this._changeEvent));
  }
  get forward() {
    let t = new R(0, 0, 1);
    return t = this.rotation.apply(t), t;
  }
  get transform() {
    return this._transform;
  }
}
class _ {
  // prettier-ignore
  constructor(t = 1, n = 0, i = 0, e = 0, A = 1, o = 0, s = 0, r = 0, Q = 1) {
    this.buffer = [
      t,
      n,
      i,
      e,
      A,
      o,
      s,
      r,
      Q
    ];
  }
  equals(t) {
    if (this.buffer.length !== t.buffer.length)
      return false;
    if (this.buffer === t.buffer)
      return true;
    for (let n = 0; n < this.buffer.length; n++)
      if (this.buffer[n] !== t.buffer[n])
        return false;
    return true;
  }
  multiply(t) {
    const n = this.buffer, i = t.buffer;
    return new _(
      i[0] * n[0] + i[3] * n[1] + i[6] * n[2],
      i[1] * n[0] + i[4] * n[1] + i[7] * n[2],
      i[2] * n[0] + i[5] * n[1] + i[8] * n[2],
      i[0] * n[3] + i[3] * n[4] + i[6] * n[5],
      i[1] * n[3] + i[4] * n[4] + i[7] * n[5],
      i[2] * n[3] + i[5] * n[4] + i[8] * n[5],
      i[0] * n[6] + i[3] * n[7] + i[6] * n[8],
      i[1] * n[6] + i[4] * n[7] + i[7] * n[8],
      i[2] * n[6] + i[5] * n[7] + i[8] * n[8]
    );
  }
  clone() {
    const t = this.buffer;
    return new _(
      t[0],
      t[1],
      t[2],
      t[3],
      t[4],
      t[5],
      t[6],
      t[7],
      t[8]
    );
  }
  static Eye(t = 1) {
    return new _(t, 0, 0, 0, t, 0, 0, 0, t);
  }
  static Diagonal(t) {
    return new _(t.x, 0, 0, 0, t.y, 0, 0, 0, t.z);
  }
  static RotationFromQuaternion(t) {
    return new _(
      1 - 2 * t.y * t.y - 2 * t.z * t.z,
      2 * t.x * t.y - 2 * t.z * t.w,
      2 * t.x * t.z + 2 * t.y * t.w,
      2 * t.x * t.y + 2 * t.z * t.w,
      1 - 2 * t.x * t.x - 2 * t.z * t.z,
      2 * t.y * t.z - 2 * t.x * t.w,
      2 * t.x * t.z - 2 * t.y * t.w,
      2 * t.y * t.z + 2 * t.x * t.w,
      1 - 2 * t.x * t.x - 2 * t.y * t.y
    );
  }
  static RotationFromEuler(t) {
    const n = Math.cos(t.x), i = Math.sin(t.x), e = Math.cos(t.y), A = Math.sin(t.y), o = Math.cos(t.z), s = Math.sin(t.z), r = [
      e * o + A * i * s,
      -e * s + A * i * o,
      A * n,
      n * s,
      n * o,
      -i,
      -A * o + e * i * s,
      A * s + e * i * o,
      e * n
    ];
    return new _(...r);
  }
  toString() {
    return `[${this.buffer.join(", ")}]`;
  }
}
class Y {
  constructor(t = 0, n = null, i = null, e = null, A = null) {
    this.changed = false, this.detached = false, this._vertexCount = t, this._positions = n || new Float32Array(0), this._rotations = i || new Float32Array(0), this._scales = e || new Float32Array(0), this._colors = A || new Uint8Array(0), this._selection = new Uint8Array(this.vertexCount), this.translate = (o) => {
      for (let s = 0; s < this.vertexCount; s++)
        this.positions[3 * s + 0] += o.x, this.positions[3 * s + 1] += o.y, this.positions[3 * s + 2] += o.z;
      this.changed = true;
    }, this.rotate = (o) => {
      const s = _.RotationFromQuaternion(o).buffer;
      for (let r = 0; r < this.vertexCount; r++) {
        const Q = this.positions[3 * r + 0], I = this.positions[3 * r + 1], d = this.positions[3 * r + 2];
        this.positions[3 * r + 0] = s[0] * Q + s[1] * I + s[2] * d, this.positions[3 * r + 1] = s[3] * Q + s[4] * I + s[5] * d, this.positions[3 * r + 2] = s[6] * Q + s[7] * I + s[8] * d;
        const a = new y(
          this.rotations[4 * r + 1],
          this.rotations[4 * r + 2],
          this.rotations[4 * r + 3],
          this.rotations[4 * r + 0]
        ), U = o.multiply(a);
        this.rotations[4 * r + 1] = U.x, this.rotations[4 * r + 2] = U.y, this.rotations[4 * r + 3] = U.z, this.rotations[4 * r + 0] = U.w;
      }
      this.changed = true;
    }, this.scale = (o) => {
      for (let s = 0; s < this.vertexCount; s++)
        this.positions[3 * s + 0] *= o.x, this.positions[3 * s + 1] *= o.y, this.positions[3 * s + 2] *= o.z, this.scales[3 * s + 0] *= o.x, this.scales[3 * s + 1] *= o.y, this.scales[3 * s + 2] *= o.z;
      this.changed = true;
    }, this.serialize = () => {
      const o = new Uint8Array(this.vertexCount * Y.RowLength), s = new Float32Array(o.buffer), r = new Uint8Array(o.buffer);
      for (let Q = 0; Q < this.vertexCount; Q++)
        s[8 * Q + 0] = this.positions[3 * Q + 0], s[8 * Q + 1] = this.positions[3 * Q + 1], s[8 * Q + 2] = this.positions[3 * Q + 2], r[32 * Q + 24 + 0] = this.colors[4 * Q + 0], r[32 * Q + 24 + 1] = this.colors[4 * Q + 1], r[32 * Q + 24 + 2] = this.colors[4 * Q + 2], r[32 * Q + 24 + 3] = this.colors[4 * Q + 3], s[8 * Q + 3 + 0] = this.scales[3 * Q + 0], s[8 * Q + 3 + 1] = this.scales[3 * Q + 1], s[8 * Q + 3 + 2] = this.scales[3 * Q + 2], r[32 * Q + 28 + 0] = this.rotations[4 * Q + 0] * 128 + 128 & 255, r[32 * Q + 28 + 1] = this.rotations[4 * Q + 1] * 128 + 128 & 255, r[32 * Q + 28 + 2] = this.rotations[4 * Q + 2] * 128 + 128 & 255, r[32 * Q + 28 + 3] = this.rotations[4 * Q + 3] * 128 + 128 & 255;
      return o;
    }, this.reattach = (o, s, r, Q, I) => {
      console.assert(
        o.byteLength === this.vertexCount * 3 * 4,
        `Expected ${this.vertexCount * 3 * 4} bytes, got ${o.byteLength} bytes`
      ), this._positions = new Float32Array(o), this._rotations = new Float32Array(s), this._scales = new Float32Array(r), this._colors = new Uint8Array(Q), this._selection = new Uint8Array(I), this.detached = false;
    };
  }
  static {
    this.RowLength = 3 * 4 + 3 * 4 + 4 + 4;
  }
  static Deserialize(t) {
    const n = t.length / Y.RowLength, i = new Float32Array(3 * n), e = new Float32Array(4 * n), A = new Float32Array(3 * n), o = new Uint8Array(4 * n), s = new Float32Array(t.buffer), r = new Uint8Array(t.buffer);
    for (let Q = 0; Q < n; Q++)
      i[3 * Q + 0] = s[8 * Q + 0], i[3 * Q + 1] = s[8 * Q + 1], i[3 * Q + 2] = s[8 * Q + 2], e[4 * Q + 0] = (r[32 * Q + 28 + 0] - 128) / 128, e[4 * Q + 1] = (r[32 * Q + 28 + 1] - 128) / 128, e[4 * Q + 2] = (r[32 * Q + 28 + 2] - 128) / 128, e[4 * Q + 3] = (r[32 * Q + 28 + 3] - 128) / 128, A[3 * Q + 0] = s[8 * Q + 3 + 0], A[3 * Q + 1] = s[8 * Q + 3 + 1], A[3 * Q + 2] = s[8 * Q + 3 + 2], o[4 * Q + 0] = r[32 * Q + 24 + 0], o[4 * Q + 1] = r[32 * Q + 24 + 1], o[4 * Q + 2] = r[32 * Q + 24 + 2], o[4 * Q + 3] = r[32 * Q + 24 + 3];
    return new Y(n, i, e, A, o);
  }
  get vertexCount() {
    return this._vertexCount;
  }
  get positions() {
    return this._positions;
  }
  get rotations() {
    return this._rotations;
  }
  get scales() {
    return this._scales;
  }
  get colors() {
    return this._colors;
  }
  get selection() {
    return this._selection;
  }
  clone() {
    return new Y(
      this.vertexCount,
      new Float32Array(this.positions),
      new Float32Array(this.rotations),
      new Float32Array(this.scales),
      new Uint8Array(this.colors)
    );
  }
}
class et {
  static {
    this.SH_C0 = 0.28209479177387814;
  }
  static SplatToPLY(t, n) {
    let i = `ply
format binary_little_endian 1.0
`;
    i += `element vertex ${n}
`;
    const e = ["x", "y", "z", "nx", "ny", "nz", "f_dc_0", "f_dc_1", "f_dc_2"];
    for (let B = 0; B < 45; B++)
      e.push(`f_rest_${B}`);
    e.push("opacity"), e.push("scale_0"), e.push("scale_1"), e.push("scale_2"), e.push("rot_0"), e.push("rot_1"), e.push("rot_2"), e.push("rot_3");
    for (const B of e)
      i += `property float ${B}
`;
    i += `end_header
`;
    const A = new TextEncoder().encode(i), o = 4 * 3 + 4 * 3 + 4 * 3 + 4 * 45 + 4 + 4 * 3 + 4 * 4, s = n * o, r = new DataView(new ArrayBuffer(A.length + s));
    new Uint8Array(r.buffer).set(A, 0);
    const Q = new Float32Array(t), I = new Uint8Array(t), d = A.length, a = 4 * 3 + 4 * 3, U = a + 4 * 3 + 4 * 45, F = U + 4, g = F + 4 * 3;
    for (let B = 0; B < n; B++) {
      const C = Q[8 * B + 0], c = Q[8 * B + 1], p = Q[8 * B + 2], u = (I[32 * B + 24 + 0] / 255 - 0.5) / this.SH_C0, S = (I[32 * B + 24 + 1] / 255 - 0.5) / this.SH_C0, W = (I[32 * B + 24 + 2] / 255 - 0.5) / this.SH_C0, Z = I[32 * B + 24 + 3] / 255, k = Math.log(Z / (1 - Z)), f = Math.log(Q[8 * B + 3 + 0]), H = Math.log(Q[8 * B + 3 + 1]), L = Math.log(Q[8 * B + 3 + 2]);
      let M = new y(
        (I[32 * B + 28 + 1] - 128) / 128,
        (I[32 * B + 28 + 2] - 128) / 128,
        (I[32 * B + 28 + 3] - 128) / 128,
        (I[32 * B + 28 + 0] - 128) / 128
      );
      M = M.normalize();
      const V = M.w, w = M.x, P = M.y, T = M.z;
      r.setFloat32(d + o * B + 0, C, true), r.setFloat32(d + o * B + 4, c, true), r.setFloat32(d + o * B + 8, p, true), r.setFloat32(d + o * B + a + 0, u, true), r.setFloat32(d + o * B + a + 4, S, true), r.setFloat32(d + o * B + a + 8, W, true), r.setFloat32(d + o * B + U, k, true), r.setFloat32(d + o * B + F + 0, f, true), r.setFloat32(d + o * B + F + 4, H, true), r.setFloat32(d + o * B + F + 8, L, true), r.setFloat32(d + o * B + g + 0, V, true), r.setFloat32(d + o * B + g + 4, w, true), r.setFloat32(d + o * B + g + 8, P, true), r.setFloat32(d + o * B + g + 12, T, true);
    }
    return r.buffer;
  }
}
class nt {
  constructor(t, n) {
    this.min = t, this.max = n;
  }
  contains(t) {
    return t.x >= this.min.x && t.x <= this.max.x && t.y >= this.min.y && t.y <= this.max.y && t.z >= this.min.z && t.z <= this.max.z;
  }
  intersects(t) {
    return this.max.x >= t.min.x && this.min.x <= t.max.x && this.max.y >= t.min.y && this.min.y <= t.max.y && this.max.z >= t.min.z && this.min.z <= t.max.z;
  }
  size() {
    return this.max.subtract(this.min);
  }
  center() {
    return this.min.add(this.max).divide(2);
  }
  expand(t) {
    this.min = this.min.min(t), this.max = this.max.max(t);
  }
  permute() {
    const t = this.min, n = this.max;
    this.min = new R(Math.min(t.x, n.x), Math.min(t.y, n.y), Math.min(t.z, n.z)), this.max = new R(Math.max(t.x, n.x), Math.max(t.y, n.y), Math.max(t.z, n.z));
  }
}
class X extends st {
  constructor(t = void 0) {
    super(), this.selectedChanged = false, this.colorTransformChanged = false, this._selected = false, this._colorTransforms = [], this._colorTransformsMap = /* @__PURE__ */ new Map(), this._data = t || new Y(), this._bounds = new nt(
      new R(1 / 0, 1 / 0, 1 / 0),
      new R(-1 / 0, -1 / 0, -1 / 0)
    ), this.recalculateBounds = () => {
      this._bounds = new nt(
        new R(1 / 0, 1 / 0, 1 / 0),
        new R(-1 / 0, -1 / 0, -1 / 0)
      );
      for (let n = 0; n < this._data.vertexCount; n++)
        this._bounds.expand(
          new R(
            this._data.positions[3 * n],
            this._data.positions[3 * n + 1],
            this._data.positions[3 * n + 2]
          )
        );
    }, this.applyPosition = () => {
      this.data.translate(this.position), this.position = new R();
    }, this.applyRotation = () => {
      this.data.rotate(this.rotation), this.rotation = new y();
    }, this.applyScale = () => {
      this.data.scale(this.scale), this.scale = new R(1, 1, 1);
    }, this.recalculateBounds();
  }
  saveToFile(t = null, n = "splat") {
    if (!document) return;
    if (!t) {
      const s = /* @__PURE__ */ new Date();
      t = `splat-${s.getFullYear()}-${s.getMonth() + 1}-${s.getDate()}.${n}`;
    }
    const i = this.clone();
    i.applyRotation(), i.applyScale(), i.applyPosition();
    const e = i.data.serialize();
    let A;
    if (n === "ply") {
      const s = et.SplatToPLY(e.buffer, i.data.vertexCount);
      A = new Blob([s], { type: "application/octet-stream" });
    } else
      A = new Blob([e.buffer], { type: "application/octet-stream" });
    const o = document.createElement("a");
    o.download = t, o.href = URL.createObjectURL(A), o.click();
  }
  get data() {
    return this._data;
  }
  get selected() {
    return this._selected;
  }
  set selected(t) {
    this._selected !== t && (this._selected = t, this.selectedChanged = true, this.dispatchEvent(this._changeEvent));
  }
  get colorTransforms() {
    return this._colorTransforms;
  }
  get colorTransformsMap() {
    return this._colorTransformsMap;
  }
  get bounds() {
    let t = this._bounds.center();
    t = t.add(this.position);
    let n = this._bounds.size();
    return n = n.multiply(this.scale), new nt(t.subtract(n.divide(2)), t.add(n.divide(2)));
  }
  clone() {
    const t = new X(this.data.clone());
    return t.position = this.position.clone(), t.rotation = this.rotation.clone(), t.scale = this.scale.clone(), t;
  }
}
class Rt {
  constructor() {
    this._fx = 1132, this._fy = 1132, this._near = 0.1, this._far = 100, this._width = 512, this._height = 512, this._projectionMatrix = new K(), this._viewMatrix = new K(), this._viewProj = new K(), this._updateProjectionMatrix = () => {
      this._projectionMatrix = new K(
        2 * this.fx / this.width,
        0,
        0,
        0,
        0,
        -2 * this.fy / this.height,
        0,
        0,
        0,
        0,
        this.far / (this.far - this.near),
        1,
        0,
        0,
        -(this.far * this.near) / (this.far - this.near),
        0
      ), this._viewProj = this.projectionMatrix.multiply(this.viewMatrix);
    }, this.update = (t, n) => {
      const i = _.RotationFromQuaternion(n).buffer, e = t.flat();
      this._viewMatrix = new K(
        i[0],
        i[1],
        i[2],
        0,
        i[3],
        i[4],
        i[5],
        0,
        i[6],
        i[7],
        i[8],
        0,
        -e[0] * i[0] - e[1] * i[3] - e[2] * i[6],
        -e[0] * i[1] - e[1] * i[4] - e[2] * i[7],
        -e[0] * i[2] - e[1] * i[5] - e[2] * i[8],
        1
      ), this._viewProj = this.projectionMatrix.multiply(this.viewMatrix);
    }, this.setSize = (t, n) => {
      this._width = t, this._height = n, this._updateProjectionMatrix();
    };
  }
  get fx() {
    return this._fx;
  }
  set fx(t) {
    this._fx !== t && (this._fx = t, this._updateProjectionMatrix());
  }
  get fy() {
    return this._fy;
  }
  set fy(t) {
    this._fy !== t && (this._fy = t, this._updateProjectionMatrix());
  }
  get near() {
    return this._near;
  }
  set near(t) {
    this._near !== t && (this._near = t, this._updateProjectionMatrix());
  }
  get far() {
    return this._far;
  }
  set far(t) {
    this._far !== t && (this._far = t, this._updateProjectionMatrix());
  }
  get width() {
    return this._width;
  }
  get height() {
    return this._height;
  }
  get projectionMatrix() {
    return this._projectionMatrix;
  }
  get viewMatrix() {
    return this._viewMatrix;
  }
  get viewProj() {
    return this._viewProj;
  }
}
class z {
  constructor(t = 0, n = 0, i = 0, e = 0) {
    this.x = t, this.y = n, this.z = i, this.w = e;
  }
  equals(t) {
    return !(this.x !== t.x || this.y !== t.y || this.z !== t.z || this.w !== t.w);
  }
  add(t) {
    return typeof t == "number" ? new z(this.x + t, this.y + t, this.z + t, this.w + t) : new z(this.x + t.x, this.y + t.y, this.z + t.z, this.w + t.w);
  }
  subtract(t) {
    return typeof t == "number" ? new z(this.x - t, this.y - t, this.z - t, this.w - t) : new z(this.x - t.x, this.y - t.y, this.z - t.z, this.w - t.w);
  }
  multiply(t) {
    return typeof t == "number" ? new z(this.x * t, this.y * t, this.z * t, this.w * t) : t instanceof z ? new z(this.x * t.x, this.y * t.y, this.z * t.z, this.w * t.w) : new z(
      this.x * t.buffer[0] + this.y * t.buffer[4] + this.z * t.buffer[8] + this.w * t.buffer[12],
      this.x * t.buffer[1] + this.y * t.buffer[5] + this.z * t.buffer[9] + this.w * t.buffer[13],
      this.x * t.buffer[2] + this.y * t.buffer[6] + this.z * t.buffer[10] + this.w * t.buffer[14],
      this.x * t.buffer[3] + this.y * t.buffer[7] + this.z * t.buffer[11] + this.w * t.buffer[15]
    );
  }
  dot(t) {
    return this.x * t.x + this.y * t.y + this.z * t.z + this.w * t.w;
  }
  lerp(t, n) {
    return new z(
      this.x + (t.x - this.x) * n,
      this.y + (t.y - this.y) * n,
      this.z + (t.z - this.z) * n,
      this.w + (t.w - this.w) * n
    );
  }
  magnitude() {
    return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  }
  distanceTo(t) {
    return Math.sqrt((this.x - t.x) ** 2 + (this.y - t.y) ** 2 + (this.z - t.z) ** 2 + (this.w - t.w) ** 2);
  }
  normalize() {
    const t = this.magnitude();
    return new z(this.x / t, this.y / t, this.z / t, this.w / t);
  }
  flat() {
    return [this.x, this.y, this.z, this.w];
  }
  clone() {
    return new z(this.x, this.y, this.z, this.w);
  }
  toString() {
    return `[${this.flat().join(", ")}]`;
  }
}
class wt extends st {
  constructor(t = void 0) {
    super(), this._data = t || new Rt(), this._position = new R(0, 0, -5), this.update = () => {
      this.data.update(this.position, this.rotation);
    }, this.screenPointToRay = (n, i) => {
      const e = new z(n, i, -1, 1), A = this._data.projectionMatrix.invert(), o = e.multiply(A), s = this._data.viewMatrix.invert(), r = o.multiply(s);
      return new R(
        r.x / r.w,
        r.y / r.w,
        r.z / r.w
      ).subtract(this.position).normalize();
    };
  }
  get data() {
    return this._data;
  }
}
class kt extends Ft {
  constructor() {
    super(), this._objects = [], this.addObject = (t) => {
      this.objects.push(t), this.dispatchEvent(new It(t));
    }, this.removeObject = (t) => {
      const n = this.objects.indexOf(t);
      if (n < 0)
        throw new Error("Object not found in scene");
      this.objects.splice(n, 1), this.dispatchEvent(new ht(t));
    }, this.findObject = (t) => {
      for (const n of this.objects)
        if (t(n))
          return n;
    }, this.findObjectOfType = (t) => {
      for (const n of this.objects)
        if (n instanceof t)
          return n;
    }, this.reset = () => {
      const t = this.objects.slice();
      for (const n of t)
        this.removeObject(n);
    }, this.reset();
  }
  getMergedSceneDataBuffer(t = "splat") {
    const n = [];
    let i = 0;
    for (const o of this.objects)
      if (o instanceof X) {
        const s = o.clone();
        s.applyRotation(), s.applyScale(), s.applyPosition();
        const r = s.data.serialize();
        n.push(r), i += s.data.vertexCount;
      }
    const e = new Uint8Array(i * Y.RowLength);
    let A = 0;
    for (const o of n)
      e.set(o, A), A += o.length;
    return t === "ply" ? et.SplatToPLY(e.buffer, i) : e.buffer;
  }
  saveToFile(t = null, n = "splat") {
    if (!document) return;
    if (!t) {
      const o = /* @__PURE__ */ new Date();
      t = `scene-${o.getFullYear()}-${o.getMonth() + 1}-${o.getDate()}.${n}`;
    }
    const i = this.getMergedSceneDataBuffer(n), e = new Blob([i], { type: "application/octet-stream" }), A = document.createElement("a");
    A.download = t, A.href = URL.createObjectURL(e), A.click();
  }
  get objects() {
    return this._objects;
  }
}
async function rt(E, t) {
  const n = await fetch(E, {
    mode: "cors",
    credentials: "omit",
    cache: t ? "force-cache" : "default"
  });
  if (n.status != 200)
    throw new Error(n.status + " Unable to load " + n.url);
  return n;
}
async function At(E, t) {
  const n = E.body.getReader(), i = E.headers.get("content-length"), e = i && !isNaN(parseInt(i)) ? parseInt(i) : void 0, A = [];
  let o = 0;
  for (; ; ) {
    const { done: Q, value: I } = await n.read();
    if (Q) break;
    if (A.push(I), o += I.length, t && e) {
      const d = o / e, a = Math.min(d * 0.95, 0.95);
      t(a);
    }
  }
  const s = new Uint8Array(o);
  let r = 0;
  for (const Q of A)
    s.set(Q, r), r += Q.length;
  return t && t(1), s;
}
class Tt {
  static async LoadAsync(t, n, i, e = false) {
    const A = await rt(t, e), o = await At(A, i);
    return this.LoadFromArrayBuffer(o.buffer, n);
  }
  static async LoadFromFileAsync(t, n, i) {
    const e = new FileReader();
    let A = new X();
    return e.onload = (o) => {
      A = this.LoadFromArrayBuffer(o.target.result, n);
    }, e.onprogress = (o) => {
      i?.(o.loaded / o.total);
    }, e.readAsArrayBuffer(t), await new Promise((o) => {
      e.onloadend = () => {
        o();
      };
    }), A;
  }
  static LoadFromArrayBuffer(t, n) {
    const i = new Uint8Array(t), e = Y.Deserialize(i), A = new X(e);
    return n.addObject(A), A;
  }
}
class bt {
  static async LoadAsync(t, n, i, e = "", A = false) {
    const o = await rt(t, A), s = await At(o, i);
    if (s[0] !== 112 || s[1] !== 108 || s[2] !== 121 || s[3] !== 10)
      throw new Error("Invalid PLY file");
    return this.LoadFromArrayBuffer(s.buffer, n, e);
  }
  static async LoadFromFileAsync(t, n, i, e = "") {
    const A = new FileReader();
    let o = new X();
    return A.onload = (s) => {
      o = this.LoadFromArrayBuffer(s.target.result, n, e);
    }, A.onprogress = (s) => {
      i?.(s.loaded / s.total);
    }, A.readAsArrayBuffer(t), await new Promise((s) => {
      A.onloadend = () => {
        s();
      };
    }), o;
  }
  static LoadFromArrayBuffer(t, n, i = "") {
    const e = new Uint8Array(this._ParsePLYBuffer(t, i)), A = Y.Deserialize(e), o = new X(A);
    return n.addObject(o), o;
  }
  static _ParsePLYBuffer(t, n) {
    const i = new Uint8Array(t), e = new TextDecoder().decode(i.slice(0, 1024 * 10)), A = `end_header
`, o = e.indexOf(A);
    if (o < 0) throw new Error("Unable to read .ply file header");
    const s = parseInt(/element vertex (\d+)\n/.exec(e)[1]);
    let r = 0;
    const Q = {
      double: 8,
      int: 4,
      uint: 4,
      float: 4,
      short: 2,
      ushort: 2,
      uchar: 1
    }, I = [];
    for (const F of e.slice(0, o).split(`
`).filter((g) => g.startsWith("property "))) {
      const [g, B, C] = F.split(" ");
      if (I.push({ name: C, type: B, offset: r }), !Q[B]) throw new Error(`Unsupported property type: ${B}`);
      r += Q[B];
    }
    const d = new DataView(t, o + A.length), a = new ArrayBuffer(Y.RowLength * s), U = y.FromEuler(new R(Math.PI / 2, 0, 0));
    for (let F = 0; F < s; F++) {
      const g = new Float32Array(a, F * Y.RowLength, 3), B = new Float32Array(a, F * Y.RowLength + 12, 3), C = new Uint8ClampedArray(a, F * Y.RowLength + 24, 4), c = new Uint8ClampedArray(a, F * Y.RowLength + 28, 4);
      let p = 255, u = 0, S = 0, W = 0;
      I.forEach((k) => {
        let f;
        switch (k.type) {
          case "float":
            f = d.getFloat32(k.offset + F * r, true);
            break;
          case "int":
            f = d.getInt32(k.offset + F * r, true);
            break;
          default:
            throw new Error(`Unsupported property type: ${k.type}`);
        }
        switch (k.name) {
          case "x":
            g[0] = f;
            break;
          case "y":
            g[1] = f;
            break;
          case "z":
            g[2] = f;
            break;
          case "scale_0":
          case "scaling_0":
            B[0] = Math.exp(f);
            break;
          case "scale_1":
          case "scaling_1":
            B[1] = Math.exp(f);
            break;
          case "scale_2":
          case "scaling_2":
            B[2] = Math.exp(f);
            break;
          case "red":
            C[0] = f;
            break;
          case "green":
            C[1] = f;
            break;
          case "blue":
            C[2] = f;
            break;
          case "f_dc_0":
          case "features_0":
            C[0] = (0.5 + et.SH_C0 * f) * 255;
            break;
          case "f_dc_1":
          case "features_1":
            C[1] = (0.5 + et.SH_C0 * f) * 255;
            break;
          case "f_dc_2":
          case "features_2":
            C[2] = (0.5 + et.SH_C0 * f) * 255;
            break;
          case "f_dc_3":
            C[3] = (0.5 + et.SH_C0 * f) * 255;
            break;
          case "opacity":
          case "opacity_0":
            C[3] = 1 / (1 + Math.exp(-f)) * 255;
            break;
          case "rot_0":
          case "rotation_0":
            p = f;
            break;
          case "rot_1":
          case "rotation_1":
            u = f;
            break;
          case "rot_2":
          case "rotation_2":
            S = f;
            break;
          case "rot_3":
          case "rotation_3":
            W = f;
            break;
        }
      });
      let Z = new y(u, S, W, p);
      switch (n) {
        case "polycam": {
          const k = g[1];
          g[1] = -g[2], g[2] = k, Z = U.multiply(Z);
          break;
        }
        case "":
          break;
        default:
          throw new Error(`Unsupported format: ${n}`);
      }
      Z = Z.normalize(), c[0] = Z.w * 128 + 128, c[1] = Z.x * 128 + 128, c[2] = Z.y * 128 + 128, c[3] = Z.z * 128 + 128;
    }
    return a;
  }
}
const Bt = " ", Et = (E) => Uint8Array.from(atob(E), (t) => t.charCodeAt(0)), Qt = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", Et(Bt)], { type: "text/javascript;charset=utf-8" });
function St(E) {
  let t;
  try {
    if (t = Qt && (self.URL || self.webkitURL).createObjectURL(Qt), !t) throw "";
    const n = new Worker(t, {
      type: "module",
      name: E?.name
    });
    return n.addEventListener("error", () => {
      (self.URL || self.webkitURL).revokeObjectURL(t);
    }), n;
  } catch {
    return new Worker(
      "data:text/javascript;base64," + Bt,
      {
        type: "module",
        name: E?.name
      }
    );
  }
}
class lt {
  constructor(t, n) {
    this._scene = null, this._camera = null, this._started = false, this._initialized = false, this._renderer = t;
    const i = t.gl;
    this._program = i.createProgram(), this._passes = n || [];
    const e = i.createShader(i.VERTEX_SHADER);
    i.shaderSource(e, this._getVertexSource()), i.compileShader(e), i.getShaderParameter(e, i.COMPILE_STATUS) || console.error(i.getShaderInfoLog(e));
    const A = i.createShader(i.FRAGMENT_SHADER);
    i.shaderSource(A, this._getFragmentSource()), i.compileShader(A), i.getShaderParameter(A, i.COMPILE_STATUS) || console.error(i.getShaderInfoLog(A)), i.attachShader(this.program, e), i.attachShader(this.program, A), i.linkProgram(this.program), i.getProgramParameter(this.program, i.LINK_STATUS) || console.error(i.getProgramInfoLog(this.program)), this.resize = () => {
      i.useProgram(this._program), this._resize();
    }, this.initialize = () => {
      console.assert(!this._initialized, "ShaderProgram already initialized"), i.useProgram(this._program), this._initialize();
      for (const o of this.passes)
        o.initialize(this);
      this._initialized = true, this._started = true;
    }, this.render = (o, s) => {
      i.useProgram(this._program), (this._scene !== o || this._camera !== s) && (this.dispose(), this._scene = o, this._camera = s, this.initialize());
      for (const r of this.passes)
        r.render();
      this._render();
    }, this.dispose = () => {
      if (this._initialized) {
        i.useProgram(this._program);
        for (const o of this.passes)
          o.dispose();
        this._dispose(), this._scene = null, this._camera = null, this._initialized = false;
      }
    };
  }
  get renderer() {
    return this._renderer;
  }
  get scene() {
    return this._scene;
  }
  get camera() {
    return this._camera;
  }
  get program() {
    return this._program;
  }
  get passes() {
    return this._passes;
  }
  get started() {
    return this._started;
  }
}
const gt = " ", ut = (E) => Uint8Array.from(atob(E), (t) => t.charCodeAt(0)), at = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", ut(gt)], { type: "text/javascript;charset=utf-8" });
function Jt(E) {
  let t;
  try {
    if (t = at && (self.URL || self.webkitURL).createObjectURL(at), !t) throw "";
    const n = new Worker(t, {
      type: "module",
      name: E?.name
    });
    return n.addEventListener("error", () => {
      (self.URL || self.webkitURL).revokeObjectURL(t);
    }), n;
  } catch {
    return new Worker(
      "data:text/javascript;base64," + gt,
      {
        type: "module",
        name: E?.name
      }
    );
  }
}
var mt = function(E = {}) {
  var t, n = E, i = import.meta.url, e = "", A;
  {
    try {
      e = new URL(".", i).href;
    } catch {
    }
    A = (h) => {
      var m = new XMLHttpRequest();
      return m.open("GET", h, false), m.responseType = "arraybuffer", m.send(null), new Uint8Array(m.response);
    };
  }
  console.log.bind(console), console.error.bind(console);
  var o, s, r;
  function Q() {
    var h = s.buffer;
    n.HEAPU8 = r = new Uint8Array(h), n.HEAPU32 = new Uint32Array(h), n.HEAPF32 = new Float32Array(h), new BigInt64Array(h), new BigUint64Array(h);
  }
  function I() {
    if (n.preRun)
      for (typeof n.preRun == "function" && (n.preRun = [n.preRun]); n.preRun.length; )
        L(n.preRun.shift());
    Z(H);
  }
  function d() {
    j.c();
  }
  function a() {
    if (n.postRun)
      for (typeof n.postRun == "function" && (n.postRun = [n.postRun]); n.postRun.length; )
        f(n.postRun.shift());
    Z(k);
  }
  var U = 0, F = null;
  function g(h) {
    U++, n.monitorRunDependencies?.(U);
  }
  function B(h) {
    if (U--, n.monitorRunDependencies?.(U), U == 0 && F) {
      var m = F;
      F = null, m();
    }
  }
  var C;
  function c() {
    return M(" ");
  }
  function p(h) {
    if (ArrayBuffer.isView(h))
      return h;
    if (h == C && o)
      return new Uint8Array(o);
    if (A)
      return A(h);
    throw 'sync fetching of the wasm failed: you can preload it to Module["wasmBinary"] manually, or emcc.py will do that for you when generating HTML (but not JS)';
  }
  function u(h, m) {
    var x, N = p(h);
    x = new WebAssembly.Module(N);
    var v = new WebAssembly.Instance(x, m);
    return [v, x];
  }
  function S() {
    return { a: b };
  }
  function W() {
    function h(N, v) {
      return j = N.exports, s = j.b, Q(), D(j), B(), j;
    }
    g();
    var m = S();
    if (n.instantiateWasm)
      return new Promise((N, v) => {
        n.instantiateWasm(m, (O, $) => {
          N(h(O));
        });
      });
    C ??= c();
    var x = u(C, m);
    return h(x[0]);
  }
  for (var Z = (h) => {
    for (; h.length > 0; )
      h.shift()(n);
  }, k = [], f = (h) => k.push(h), H = [], L = (h) => H.push(h), M = (h) => {
    for (var m, x, N = 0, v = 0, O = h.length, $ = new Uint8Array((O * 3 >> 2) - (h[O - 2] == "=") - (h[O - 1] == "=")); N < O; N += 4, v += 3)
      m = l[h.charCodeAt(N + 1)], x = l[h.charCodeAt(N + 2)], $[v] = l[h.charCodeAt(N)] << 2 | m >> 4, $[v + 1] = m << 4 | x >> 2, $[v + 2] = x << 6 | l[h.charCodeAt(N + 3)];
    return $;
  }, V = () => 2147483648, w = (h, m) => Math.ceil(h / m) * m, P = (h) => {
    var m = s.buffer, x = (h - m.byteLength + 65535) / 65536 | 0;
    try {
      return s.grow(x), Q(), 1;
    } catch {
    }
  }, T = (h) => {
    var m = r.length;
    h >>>= 0;
    var x = V();
    if (h > x)
      return false;
    for (var N = 1; N <= 4; N *= 2) {
      var v = m * (1 + 0.2 / N);
      v = Math.min(v, h + 100663296);
      var O = Math.min(x, w(Math.max(h, v), 65536)), $ = P(O);
      if ($)
        return true;
    }
    return false;
  }, l = new Uint8Array(123), J = 25; J >= 0; --J)
    l[48 + J] = 52 + J, l[65 + J] = J, l[97 + J] = 26 + J;
  l[43] = 62, l[47] = 63, n.noExitRuntime && n.noExitRuntime, n.print && n.print, n.printErr && n.printErr, n.wasmBinary && (o = n.wasmBinary), n.arguments && n.arguments, n.thisProgram && n.thisProgram;
  function D(h) {
    n._pack = h.d, n._malloc = h.e, n._free = h.f;
  }
  var b = { a: T }, j = W();
  function q() {
    if (U > 0) {
      F = q;
      return;
    }
    if (I(), U > 0) {
      F = q;
      return;
    }
    function h() {
      n.calledRun = true, d(), n.onRuntimeInitialized?.(), a();
    }
    n.setStatus ? (n.setStatus("Running..."), setTimeout(() => {
      setTimeout(() => n.setStatus(""), 1), h();
    }, 1)) : h();
  }
  function G() {
    if (n.preInit)
      for (typeof n.preInit == "function" && (n.preInit = [n.preInit]); n.preInit.length > 0; )
        n.preInit.shift()();
  }
  return G(), q(), t = n, t;
};
const ft = () => new Jt();
class Ut {
  constructor(t) {
    this.dataChanged = false, this.transformsChanged = false, this.colorTransformsChanged = false, this._updating = /* @__PURE__ */ new Set(), this._dirty = /* @__PURE__ */ new Set();
    let n = 0, i = 0;
    this._splatIndices = /* @__PURE__ */ new Map(), this._offsets = /* @__PURE__ */ new Map();
    const e = /* @__PURE__ */ new Map();
    for (const a of t.objects)
      a instanceof X && (this._splatIndices.set(a, i), this._offsets.set(a, n), e.set(n, a), n += a.data.vertexCount, i++);
    this._vertexCount = n, this._width = 2048, this._height = Math.ceil(2 * this.vertexCount / this.width), this._data = new Uint32Array(this.width * this.height * 4), this._transformsWidth = 5, this._transformsHeight = e.size, this._transforms = new Float32Array(this._transformsWidth * this._transformsHeight * 4), this._transformIndicesWidth = 1024, this._transformIndicesHeight = Math.ceil(this.vertexCount / this._transformIndicesWidth), this._transformIndices = new Uint32Array(this._transformIndicesWidth * this._transformIndicesHeight), this._colorTransformsWidth = 4, this._colorTransformsHeight = 64, this._colorTransforms = new Float32Array(this._colorTransformsWidth * this._colorTransformsHeight * 4), this._colorTransforms.fill(0), this._colorTransforms[0] = 1, this._colorTransforms[5] = 1, this._colorTransforms[10] = 1, this._colorTransforms[15] = 1, this._colorTransformIndicesWidth = 1024, this._colorTransformIndicesHeight = Math.ceil(this.vertexCount / this._colorTransformIndicesWidth), this._colorTransformIndices = new Uint32Array(
      this._colorTransformIndicesWidth * this._colorTransformIndicesHeight
    ), this.colorTransformIndices.fill(0), this._positions = new Float32Array(this.vertexCount * 3), this._rotations = new Float32Array(this.vertexCount * 4), this._scales = new Float32Array(this.vertexCount * 3), this._worker = ft();
    const A = (a) => {
      const U = this._splatIndices.get(a);
      this._transforms.set(a.transform.buffer, U * 20), this._transforms[U * 20 + 16] = a.selected ? 1 : 0, a.positionChanged = false, a.rotationChanged = false, a.scaleChanged = false, a.selectedChanged = false, this.transformsChanged = true;
    }, o = () => {
      let a = false;
      for (const g of this._splatIndices.keys())
        if (g.colorTransformChanged) {
          a = true;
          break;
        }
      if (!a)
        return;
      const U = [new K()];
      this._colorTransformIndices.fill(0);
      let F = 1;
      for (const g of this._splatIndices.keys()) {
        const B = this._offsets.get(g);
        for (const C of g.colorTransforms)
          U.includes(C) || (U.push(C), F++);
        for (const C of g.colorTransformsMap.keys()) {
          const c = g.colorTransformsMap.get(C);
          this._colorTransformIndices[C + B] = c + F - 1;
        }
        g.colorTransformChanged = false;
      }
      for (let g = 0; g < U.length; g++) {
        const B = U[g];
        this._colorTransforms.set(B.buffer, g * 16);
      }
      this.colorTransformsChanged = true;
    };
    this._worker.onmessage = (a) => {
      if (a.data.response) {
        const U = a.data.response, F = e.get(U.offset);
        A(F), o();
        const g = this._splatIndices.get(F);
        for (let B = 0; B < F.data.vertexCount; B++)
          this._transformIndices[U.offset + B] = g;
        this._data.set(U.data, U.offset * 8), F.data.reattach(
          U.positions,
          U.rotations,
          U.scales,
          U.colors,
          U.selection
        ), this._positions.set(U.worldPositions, U.offset * 3), this._rotations.set(U.worldRotations, U.offset * 4), this._scales.set(U.worldScales, U.offset * 3), this._updating.delete(F), F.selectedChanged = false, this.dataChanged = true;
      }
    };
    let s;
    async function r() {
      s = await mt();
    }
    r();
    async function Q() {
      for (; !s; )
        await new Promise((a) => setTimeout(a, 0));
    }
    const I = (a) => {
      if (!s) {
        Q().then(() => {
          I(a);
        });
        return;
      }
      A(a);
      const U = s._malloc(3 * a.data.vertexCount * 4), F = s._malloc(4 * a.data.vertexCount * 4), g = s._malloc(3 * a.data.vertexCount * 4), B = s._malloc(4 * a.data.vertexCount), C = s._malloc(a.data.vertexCount), c = s._malloc(8 * a.data.vertexCount * 4), p = s._malloc(3 * a.data.vertexCount * 4), u = s._malloc(4 * a.data.vertexCount * 4), S = s._malloc(3 * a.data.vertexCount * 4);
      s.HEAPF32.set(a.data.positions, U / 4), s.HEAPF32.set(a.data.rotations, F / 4), s.HEAPF32.set(a.data.scales, g / 4), s.HEAPU8.set(a.data.colors, B), s.HEAPU8.set(a.data.selection, C), s._pack(
        a.selected,
        a.data.vertexCount,
        U,
        F,
        g,
        B,
        C,
        c,
        p,
        u,
        S
      );
      const W = new Uint32Array(s.HEAPU32.buffer, c, a.data.vertexCount * 8), Z = new Float32Array(
        s.HEAPF32.buffer,
        p,
        a.data.vertexCount * 3
      ), k = new Float32Array(
        s.HEAPF32.buffer,
        u,
        a.data.vertexCount * 4
      ), f = new Float32Array(s.HEAPF32.buffer, S, a.data.vertexCount * 3), H = this._splatIndices.get(a), L = this._offsets.get(a);
      for (let M = 0; M < a.data.vertexCount; M++)
        this._transformIndices[L + M] = H;
      this._data.set(W, L * 8), this._positions.set(Z, L * 3), this._rotations.set(k, L * 4), this._scales.set(f, L * 3), s._free(U), s._free(F), s._free(g), s._free(B), s._free(C), s._free(c), s._free(p), s._free(u), s._free(S), this.dataChanged = true, this.colorTransformsChanged = true;
    }, d = (a) => {
      if ((a.positionChanged || a.rotationChanged || a.scaleChanged || a.selectedChanged) && A(a), a.colorTransformChanged && o(), !a.data.changed || a.data.detached) return;
      const U = {
        position: new Float32Array(a.position.flat()),
        rotation: new Float32Array(a.rotation.flat()),
        scale: new Float32Array(a.scale.flat()),
        selected: a.selected,
        vertexCount: a.data.vertexCount,
        positions: a.data.positions,
        rotations: a.data.rotations,
        scales: a.data.scales,
        colors: a.data.colors,
        selection: a.data.selection,
        offset: this._offsets.get(a)
      };
      this._worker.postMessage(
        {
          splat: U
        },
        [
          U.position.buffer,
          U.rotation.buffer,
          U.scale.buffer,
          U.positions.buffer,
          U.rotations.buffer,
          U.scales.buffer,
          U.colors.buffer,
          U.selection.buffer
        ]
      ), this._updating.add(a), a.data.detached = true;
    };
    this.getSplat = (a) => {
      let U = null;
      for (const [F, g] of this._offsets)
        if (a >= g)
          U = F;
        else
          break;
      return U;
    }, this.getLocalIndex = (a, U) => {
      const F = this._offsets.get(a);
      return U - F;
    }, this.markDirty = (a) => {
      this._dirty.add(a);
    }, this.rebuild = () => {
      for (const a of this._dirty)
        d(a);
      this._dirty.clear();
    }, this.dispose = () => {
      this._worker.terminate();
    };
    for (const a of this._splatIndices.keys())
      I(a);
    o();
  }
  get offsets() {
    return this._offsets;
  }
  get data() {
    return this._data;
  }
  get width() {
    return this._width;
  }
  get height() {
    return this._height;
  }
  get transforms() {
    return this._transforms;
  }
  get transformsWidth() {
    return this._transformsWidth;
  }
  get transformsHeight() {
    return this._transformsHeight;
  }
  get transformIndices() {
    return this._transformIndices;
  }
  get transformIndicesWidth() {
    return this._transformIndicesWidth;
  }
  get transformIndicesHeight() {
    return this._transformIndicesHeight;
  }
  get colorTransforms() {
    return this._colorTransforms;
  }
  get colorTransformsWidth() {
    return this._colorTransformsWidth;
  }
  get colorTransformsHeight() {
    return this._colorTransformsHeight;
  }
  get colorTransformIndices() {
    return this._colorTransformIndices;
  }
  get colorTransformIndicesWidth() {
    return this._colorTransformIndicesWidth;
  }
  get colorTransformIndicesHeight() {
    return this._colorTransformIndicesHeight;
  }
  get positions() {
    return this._positions;
  }
  get rotations() {
    return this._rotations;
  }
  get scales() {
    return this._scales;
  }
  get vertexCount() {
    return this._vertexCount;
  }
  get needsRebuild() {
    return this._dirty.size > 0;
  }
  get updating() {
    return this._updating.size > 0;
  }
}
class dt {
  constructor(t = 0, n = 0, i = 0, e = 255) {
    this.r = t, this.g = n, this.b = i, this.a = e;
  }
  flat() {
    return [this.r, this.g, this.b, this.a];
  }
  flatNorm() {
    return [this.r / 255, this.g / 255, this.b / 255, this.a / 255];
  }
  toHexString() {
    return "#" + this.flat().map((t) => t.toString(16).padStart(2, "0")).join("");
  }
  toString() {
    return `[${this.flat().join(", ")}]`;
  }
}
const Vt = () => new St(), Zt = (
  /* glsl */
  `#version 300 es
precision highp float;
precision highp int;

uniform highp usampler2D u_texture;
uniform highp sampler2D u_transforms;
uniform highp usampler2D u_transformIndices;
uniform highp sampler2D u_colorTransforms;
uniform highp usampler2D u_colorTransformIndices;
uniform mat4 projection, view;
uniform vec2 focal;
uniform vec2 viewport;

uniform bool useDepthFade;
uniform float depthFade;

in vec2 position;
in int index;

out vec4 vColor;
out vec2 vPosition;
out float vSize;
out float vSelected;

void main () {
    uvec4 cen = texelFetch(u_texture, ivec2((uint(index) & 0x3ffu) << 1, uint(index) >> 10), 0);
    float selected = float((cen.w >> 24) & 0xffu);

    uint transformIndex = texelFetch(u_transformIndices, ivec2(uint(index) & 0x3ffu, uint(index) >> 10), 0).x;
    mat4 transform = mat4(
        texelFetch(u_transforms, ivec2(0, transformIndex), 0),
        texelFetch(u_transforms, ivec2(1, transformIndex), 0),
        texelFetch(u_transforms, ivec2(2, transformIndex), 0),
        texelFetch(u_transforms, ivec2(3, transformIndex), 0)
    );

    if (selected < 0.5) {
        selected = texelFetch(u_transforms, ivec2(4, transformIndex), 0).x;
    }

    mat4 viewTransform = view * transform;

    vec4 cam = viewTransform * vec4(uintBitsToFloat(cen.xyz), 1);
    vec4 pos2d = projection * cam;

    float clip = 1.2 * pos2d.w;
    if (pos2d.z < -pos2d.w || pos2d.z > pos2d.w || pos2d.x < -clip || pos2d.x > clip || pos2d.y < -clip || pos2d.y > clip) {
        gl_Position = vec4(0.0, 0.0, 2.0, 1.0);
        return;
    }

    uvec4 cov = texelFetch(u_texture, ivec2(((uint(index) & 0x3ffu) << 1) | 1u, uint(index) >> 10), 0);
    vec2 u1 = unpackHalf2x16(cov.x), u2 = unpackHalf2x16(cov.y), u3 = unpackHalf2x16(cov.z);
    mat3 Vrk = mat3(u1.x, u1.y, u2.x, u1.y, u2.y, u3.x, u2.x, u3.x, u3.y);

    mat3 J = mat3(
        focal.x / cam.z, 0., -(focal.x * cam.x) / (cam.z * cam.z), 
        0., -focal.y / cam.z, (focal.y * cam.y) / (cam.z * cam.z), 
        0., 0., 0.
    );

    mat3 T = transpose(mat3(viewTransform)) * J;
    mat3 cov2d = transpose(T) * Vrk * T;

    //ref: https://github.com/graphdeco-inria/diff-gaussian-rasterization/blob/main/cuda_rasterizer/forward.cu#L110-L111
    cov2d[0][0] += 0.3;
    cov2d[1][1] += 0.3;

    float mid = (cov2d[0][0] + cov2d[1][1]) / 2.0;
    float radius = length(vec2((cov2d[0][0] - cov2d[1][1]) / 2.0, cov2d[0][1]));
    float lambda1 = mid + radius, lambda2 = mid - radius;

    if (lambda2 < 0.0) return;
    vec2 diagonalVector = normalize(vec2(cov2d[0][1], lambda1 - cov2d[0][0]));
    vec2 majorAxis = min(sqrt(2.0 * lambda1), 1024.0) * diagonalVector;
    vec2 minorAxis = min(sqrt(2.0 * lambda2), 1024.0) * vec2(diagonalVector.y, -diagonalVector.x);

    uint colorTransformIndex = texelFetch(u_colorTransformIndices, ivec2(uint(index) & 0x3ffu, uint(index) >> 10), 0).x;
    mat4 colorTransform = mat4(
        texelFetch(u_colorTransforms, ivec2(0, colorTransformIndex), 0),
        texelFetch(u_colorTransforms, ivec2(1, colorTransformIndex), 0),
        texelFetch(u_colorTransforms, ivec2(2, colorTransformIndex), 0),
        texelFetch(u_colorTransforms, ivec2(3, colorTransformIndex), 0)
    );

    vec4 color = vec4((cov.w) & 0xffu, (cov.w >> 8) & 0xffu, (cov.w >> 16) & 0xffu, (cov.w >> 24) & 0xffu) / 255.0;
    vColor = colorTransform * color;

    vPosition = position;
    vSize = length(majorAxis);
    vSelected = selected;

    float scalingFactor = 1.0;

    if (useDepthFade) {
        float depthNorm = (pos2d.z / pos2d.w + 1.0) / 2.0;
        float near = 0.1; float far = 100.0;
        float normalizedDepth = (2.0 * near) / (far + near - depthNorm * (far - near));
        float start = max(normalizedDepth - 0.1, 0.0);
        float end = min(normalizedDepth + 0.1, 1.0);
        scalingFactor = clamp((depthFade - start) / (end - start), 0.0, 1.0);
    }

    vec2 vCenter = vec2(pos2d) / pos2d.w;
    gl_Position = vec4(
        vCenter 
        + position.x * majorAxis * scalingFactor / viewport
        + position.y * minorAxis * scalingFactor / viewport, 0.0, 1.0);
}
`
), pt = (
  /* glsl */
  `#version 300 es
precision highp float;

uniform float outlineThickness;
uniform vec4 outlineColor;

in vec4 vColor;
in vec2 vPosition;
in float vSize;
in float vSelected;

out vec4 fragColor;

void main () {
    float A = -dot(vPosition, vPosition);

    if (A < -4.0) discard;

    if (vSelected < 0.5) {
        float B = exp(A) * vColor.a;
        fragColor = vec4(B * vColor.rgb, B);
        return;
    }

    float outlineThreshold = -4.0 + (outlineThickness / vSize);

    if (A < outlineThreshold) {
        fragColor = outlineColor;
    } 
    else {
        float B = exp(A) * vColor.a;
        fragColor = vec4(B * vColor.rgb, B);
    }
}
`
);
class Ct extends lt {
  constructor(t, n) {
    super(t, n), this._outlineThickness = 10, this._outlineColor = new dt(255, 165, 0, 255), this._renderData = null, this._depthIndex = new Uint32Array(), this._splatTexture = null, this._worker = null;
    const i = t.canvas, e = t.gl;
    let A, o, s, r, Q, I, d, a, U, F, g, B, C, c, p, u, S, W, Z;
    this._resize = () => {
      this._camera && (this._camera.data.setSize(i.width, i.height), this._camera.update(), A = e.getUniformLocation(this.program, "projection"), e.uniformMatrix4fv(A, false, this._camera.data.projectionMatrix.buffer), o = e.getUniformLocation(this.program, "viewport"), e.uniform2fv(o, new Float32Array([i.width, i.height])));
    };
    const k = () => {
      this._worker = Vt(), this._worker.onmessage = (V) => {
        if (V.data.depthIndex) {
          const { depthIndex: w } = V.data;
          this._depthIndex = w, e.bindBuffer(e.ARRAY_BUFFER, Z), e.bufferData(e.ARRAY_BUFFER, w, e.STATIC_DRAW);
        }
      };
    };
    this._initialize = () => {
      if (!this._scene || !this._camera) {
        console.error("Cannot render without scene and camera");
        return;
      }
      this._resize(), this._scene.addEventListener("objectAdded", f), this._scene.addEventListener("objectRemoved", H);
      for (const V of this._scene.objects)
        V instanceof X && V.addEventListener("objectChanged", L);
      this._renderData = new Ut(this._scene), s = e.getUniformLocation(this.program, "focal"), e.uniform2fv(s, new Float32Array([this._camera.data.fx, this._camera.data.fy])), r = e.getUniformLocation(this.program, "view"), e.uniformMatrix4fv(r, false, this._camera.data.viewMatrix.buffer), F = e.getUniformLocation(this.program, "outlineThickness"), e.uniform1f(F, this.outlineThickness), g = e.getUniformLocation(this.program, "outlineColor"), e.uniform4fv(g, new Float32Array(this.outlineColor.flatNorm())), this._splatTexture = e.createTexture(), Q = e.getUniformLocation(this.program, "u_texture"), e.uniform1i(Q, 0), c = e.createTexture(), I = e.getUniformLocation(this.program, "u_transforms"), e.uniform1i(I, 1), p = e.createTexture(), d = e.getUniformLocation(this.program, "u_transformIndices"), e.uniform1i(d, 2), u = e.createTexture(), a = e.getUniformLocation(this.program, "u_colorTransforms"), e.uniform1i(a, 3), S = e.createTexture(), U = e.getUniformLocation(
        this.program,
        "u_colorTransformIndices"
      ), e.uniform1i(U, 4), W = e.createBuffer(), e.bindBuffer(e.ARRAY_BUFFER, W), e.bufferData(e.ARRAY_BUFFER, new Float32Array([-2, -2, 2, -2, 2, 2, -2, 2]), e.STATIC_DRAW), B = e.getAttribLocation(this.program, "position"), e.enableVertexAttribArray(B), e.vertexAttribPointer(B, 2, e.FLOAT, false, 0, 0), Z = e.createBuffer(), C = e.getAttribLocation(this.program, "index"), e.enableVertexAttribArray(C), e.bindBuffer(e.ARRAY_BUFFER, Z), k();
    };
    const f = (V) => {
      const w = V;
      w.object instanceof X && w.object.addEventListener("objectChanged", L), M();
    }, H = (V) => {
      const w = V;
      w.object instanceof X && w.object.removeEventListener("objectChanged", L), M();
    }, L = (V) => {
      const w = V;
      w.object instanceof X && this._renderData && this._renderData.markDirty(w.object);
    }, M = () => {
      this._renderData?.dispose(), this._renderData = new Ut(this._scene), this._worker?.terminate(), k();
    };
    this._render = () => {
      if (!this._scene || !this._camera || !this.renderData) {
        console.error("Cannot render without scene and camera");
        return;
      }
      if (this.renderData.needsRebuild && this.renderData.rebuild(), this.renderData.dataChanged || this.renderData.transformsChanged || this.renderData.colorTransformsChanged) {
        this.renderData.dataChanged && (e.activeTexture(e.TEXTURE0), e.bindTexture(e.TEXTURE_2D, this.splatTexture), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.NEAREST), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.NEAREST), e.texImage2D(
          e.TEXTURE_2D,
          0,
          e.RGBA32UI,
          this.renderData.width,
          this.renderData.height,
          0,
          e.RGBA_INTEGER,
          e.UNSIGNED_INT,
          this.renderData.data
        )), this.renderData.transformsChanged && (e.activeTexture(e.TEXTURE1), e.bindTexture(e.TEXTURE_2D, c), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.NEAREST), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.NEAREST), e.texImage2D(
          e.TEXTURE_2D,
          0,
          e.RGBA32F,
          this.renderData.transformsWidth,
          this.renderData.transformsHeight,
          0,
          e.RGBA,
          e.FLOAT,
          this.renderData.transforms
        ), e.activeTexture(e.TEXTURE2), e.bindTexture(e.TEXTURE_2D, p), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.NEAREST), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.NEAREST), e.texImage2D(
          e.TEXTURE_2D,
          0,
          e.R32UI,
          this.renderData.transformIndicesWidth,
          this.renderData.transformIndicesHeight,
          0,
          e.RED_INTEGER,
          e.UNSIGNED_INT,
          this.renderData.transformIndices
        )), this.renderData.colorTransformsChanged && (e.activeTexture(e.TEXTURE3), e.bindTexture(e.TEXTURE_2D, u), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.NEAREST), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.NEAREST), e.texImage2D(
          e.TEXTURE_2D,
          0,
          e.RGBA32F,
          this.renderData.colorTransformsWidth,
          this.renderData.colorTransformsHeight,
          0,
          e.RGBA,
          e.FLOAT,
          this.renderData.colorTransforms
        ), e.activeTexture(e.TEXTURE4), e.bindTexture(e.TEXTURE_2D, S), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.NEAREST), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.NEAREST), e.texImage2D(
          e.TEXTURE_2D,
          0,
          e.R32UI,
          this.renderData.colorTransformIndicesWidth,
          this.renderData.colorTransformIndicesHeight,
          0,
          e.RED_INTEGER,
          e.UNSIGNED_INT,
          this.renderData.colorTransformIndices
        ));
        const V = new Float32Array(this.renderData.positions.slice().buffer), w = new Float32Array(this.renderData.transforms.slice().buffer), P = new Uint32Array(this.renderData.transformIndices.slice().buffer);
        this._worker?.postMessage(
          {
            sortData: {
              positions: V,
              transforms: w,
              transformIndices: P,
              vertexCount: this.renderData.vertexCount
            }
          },
          [V.buffer, w.buffer, P.buffer]
        ), this.renderData.dataChanged = false, this.renderData.transformsChanged = false, this.renderData.colorTransformsChanged = false;
      }
      this._camera.update(), this._worker?.postMessage({ viewProj: this._camera.data.viewProj.buffer }), e.viewport(0, 0, i.width, i.height), e.clearColor(0, 0, 0, 0), e.clear(e.COLOR_BUFFER_BIT), e.disable(e.DEPTH_TEST), e.enable(e.BLEND), e.blendFuncSeparate(e.ONE_MINUS_DST_ALPHA, e.ONE, e.ONE_MINUS_DST_ALPHA, e.ONE), e.blendEquationSeparate(e.FUNC_ADD, e.FUNC_ADD), e.uniformMatrix4fv(A, false, this._camera.data.projectionMatrix.buffer), e.uniformMatrix4fv(r, false, this._camera.data.viewMatrix.buffer), e.bindBuffer(e.ARRAY_BUFFER, W), e.vertexAttribPointer(B, 2, e.FLOAT, false, 0, 0), e.bindBuffer(e.ARRAY_BUFFER, Z), e.bufferData(e.ARRAY_BUFFER, this.depthIndex, e.STATIC_DRAW), e.vertexAttribIPointer(C, 1, e.INT, 0, 0), e.vertexAttribDivisor(C, 1), e.drawArraysInstanced(e.TRIANGLE_FAN, 0, 4, this.depthIndex.length);
    }, this._dispose = () => {
      if (!this._scene || !this._camera || !this.renderData) {
        console.error("Cannot dispose without scene and camera");
        return;
      }
      this._scene.removeEventListener("objectAdded", f), this._scene.removeEventListener("objectRemoved", H);
      for (const V of this._scene.objects)
        V instanceof X && V.removeEventListener("objectChanged", L);
      this._worker?.terminate(), this.renderData.dispose(), e.deleteTexture(this.splatTexture), e.deleteTexture(c), e.deleteTexture(p), e.deleteBuffer(Z), e.deleteBuffer(W);
    }, this._setOutlineThickness = (V) => {
      this._outlineThickness = V, this._initialized && e.uniform1f(F, V);
    }, this._setOutlineColor = (V) => {
      this._outlineColor = V, this._initialized && e.uniform4fv(g, new Float32Array(V.flatNorm()));
    };
  }
  get renderData() {
    return this._renderData;
  }
  get depthIndex() {
    return this._depthIndex;
  }
  get splatTexture() {
    return this._splatTexture;
  }
  get outlineThickness() {
    return this._outlineThickness;
  }
  set outlineThickness(t) {
    this._setOutlineThickness(t);
  }
  get outlineColor() {
    return this._outlineColor;
  }
  set outlineColor(t) {
    this._setOutlineColor(t);
  }
  get worker() {
    return this._worker;
  }
  _getVertexSource() {
    return Zt;
  }
  _getFragmentSource() {
    return pt;
  }
}
class Nt {
  constructor(t = 1) {
    let n = 0, i = false, e, A, o, s;
    this.initialize = (r) => {
      if (!(r instanceof Ct))
        throw new Error("FadeInPass requires a RenderProgram");
      n = r.started ? 1 : 0, i = true, e = r, A = r.renderer.gl, o = A.getUniformLocation(e.program, "useDepthFade"), A.uniform1i(o, 1), s = A.getUniformLocation(e.program, "depthFade"), A.uniform1f(s, n);
    }, this.render = () => {
      !i || e.renderData?.updating || (A.useProgram(e.program), n = Math.min(n + t * 0.01, 1), n >= 1 && (i = false, A.uniform1i(o, 0)), A.uniform1f(s, n));
    };
  }
  dispose() {
  }
}
class Mt {
  constructor(t = null, n = null) {
    this._backgroundColor = new dt();
    const i = t || document.createElement("canvas");
    t || (i.style.display = "block", i.style.boxSizing = "border-box", i.style.width = "100%", i.style.height = "100%", i.style.margin = "0", i.style.padding = "0", document.body.appendChild(i)), i.style.background = this._backgroundColor.toHexString(), this._canvas = i, this._gl = i.getContext("webgl2", { antialias: false });
    const e = n || [];
    n || e.push(new Nt()), this._renderProgram = new Ct(this, e);
    const A = [this._renderProgram];
    this.resize = () => {
      const o = i.clientWidth, s = i.clientHeight;
      (i.width !== o || i.height !== s) && this.setSize(o, s);
    }, this.setSize = (o, s) => {
      i.width = o, i.height = s, this._gl.viewport(0, 0, i.width, i.height);
      for (const r of A)
        r.resize();
    }, this.render = (o, s) => {
      for (const r of A)
        r.render(o, s);
    }, this.dispose = () => {
      for (const o of A)
        o.dispose();
    }, this.addProgram = (o) => {
      A.push(o);
    }, this.removeProgram = (o) => {
      const s = A.indexOf(o);
      if (s < 0)
        throw new Error("Program not found");
      A.splice(s, 1);
    }, this.resize();
  }
  get canvas() {
    return this._canvas;
  }
  get gl() {
    return this._gl;
  }
  get renderProgram() {
    return this._renderProgram;
  }
  get backgroundColor() {
    return this._backgroundColor;
  }
  set backgroundColor(t) {
    this._backgroundColor = t, this._canvas.style.background = t.toHexString();
  }
}
class Lt {
  constructor(t, n, i = 0.5, e = 0.5, A = 5, o = true, s = new R()) {
    this.minAngle = -90, this.maxAngle = 90, this.minZoom = 0.1, this.maxZoom = 30, this.orbitSpeed = 1, this.panSpeed = 1, this.zoomSpeed = 1, this.dampening = 0.12, this.setCameraTarget = () => {
    };
    let r = s.clone(), Q = r.clone(), I = i, d = e, a = A, U = false, F = false, g = 0, B = 0, C = 0;
    const c = {};
    let p = false;
    const u = () => {
      if (p) return;
      const l = t.rotation.toEuler();
      I = -l.y, d = -l.x;
      const J = t.position.x - a * Math.sin(I) * Math.cos(d), D = t.position.y + a * Math.sin(d), b = t.position.z + a * Math.cos(I) * Math.cos(d);
      Q = new R(J, D, b);
    };
    t.addEventListener("objectChanged", u), this.setCameraTarget = (l) => {
      const J = l.x - t.position.x, D = l.y - t.position.y, b = l.z - t.position.z;
      a = Math.sqrt(J * J + D * D + b * b), d = Math.atan2(D, Math.sqrt(J * J + b * b)), I = -Math.atan2(J, b), Q = new R(l.x, l.y, l.z);
    };
    const S = () => 0.1 + 0.9 * (a - this.minZoom) / (this.maxZoom - this.minZoom), W = (l) => {
      c[l.code] = true, l.code === "ArrowUp" && (c.KeyW = true), l.code === "ArrowDown" && (c.KeyS = true), l.code === "ArrowLeft" && (c.KeyA = true), l.code === "ArrowRight" && (c.KeyD = true);
    }, Z = (l) => {
      c[l.code] = false, l.code === "ArrowUp" && (c.KeyW = false), l.code === "ArrowDown" && (c.KeyS = false), l.code === "ArrowLeft" && (c.KeyA = false), l.code === "ArrowRight" && (c.KeyD = false);
    }, k = (l) => {
      T(l), U = true, F = l.button === 2, B = l.clientX, C = l.clientY, window.addEventListener("mouseup", f);
    }, f = (l) => {
      T(l), U = false, F = false, window.removeEventListener("mouseup", f);
    }, H = (l) => {
      if (T(l), !U || !t) return;
      const J = l.clientX - B, D = l.clientY - C;
      if (F) {
        const b = S(), j = -J * this.panSpeed * 0.01 * b, q = -D * this.panSpeed * 0.01 * b, G = _.RotationFromQuaternion(t.rotation).buffer, h = new R(G[0], G[3], G[6]), m = new R(G[1], G[4], G[7]);
        Q = Q.add(h.multiply(j)), Q = Q.add(m.multiply(q));
      } else
        I -= J * this.orbitSpeed * 3e-3, d += D * this.orbitSpeed * 3e-3, d = Math.min(
          Math.max(d, this.minAngle * Math.PI / 180),
          this.maxAngle * Math.PI / 180
        );
      B = l.clientX, C = l.clientY;
    }, L = (l) => {
      T(l);
      const J = S();
      a += l.deltaY * this.zoomSpeed * 0.025 * J, a = Math.min(Math.max(a, this.minZoom), this.maxZoom);
    }, M = (l) => {
      if (T(l), l.touches.length === 1)
        U = true, F = false, B = l.touches[0].clientX, C = l.touches[0].clientY, g = 0;
      else if (l.touches.length === 2) {
        U = true, F = true, B = (l.touches[0].clientX + l.touches[1].clientX) / 2, C = (l.touches[0].clientY + l.touches[1].clientY) / 2;
        const J = l.touches[0].clientX - l.touches[1].clientX, D = l.touches[0].clientY - l.touches[1].clientY;
        g = Math.sqrt(J * J + D * D);
      }
    }, V = (l) => {
      T(l), U = false, F = false;
    }, w = (l) => {
      if (T(l), !(!U || !t))
        if (F) {
          const J = S(), D = l.touches[0].clientX - l.touches[1].clientX, b = l.touches[0].clientY - l.touches[1].clientY, j = Math.sqrt(D * D + b * b), q = g - j;
          a += q * this.zoomSpeed * 0.1 * J, a = Math.min(Math.max(a, this.minZoom), this.maxZoom), g = j;
          const G = (l.touches[0].clientX + l.touches[1].clientX) / 2, h = (l.touches[0].clientY + l.touches[1].clientY) / 2, m = G - B, x = h - C, N = _.RotationFromQuaternion(t.rotation).buffer, v = new R(N[0], N[3], N[6]), O = new R(N[1], N[4], N[7]);
          Q = Q.add(v.multiply(-m * this.panSpeed * 0.025 * J)), Q = Q.add(O.multiply(-x * this.panSpeed * 0.025 * J)), B = G, C = h;
        } else {
          const J = l.touches[0].clientX - B, D = l.touches[0].clientY - C;
          I -= J * this.orbitSpeed * 3e-3, d += D * this.orbitSpeed * 3e-3, d = Math.min(
            Math.max(d, this.minAngle * Math.PI / 180),
            this.maxAngle * Math.PI / 180
          ), B = l.touches[0].clientX, C = l.touches[0].clientY;
        }
    }, P = (l, J, D) => (1 - D) * l + D * J;
    this.update = () => {
      p = true, i = P(i, I, this.dampening), e = P(e, d, this.dampening), A = P(A, a, this.dampening), r = r.lerp(Q, this.dampening);
      const l = r.x + A * Math.sin(i) * Math.cos(e), J = r.y - A * Math.sin(e), D = r.z - A * Math.cos(i) * Math.cos(e);
      t.position = new R(l, J, D);
      const b = r.subtract(t.position).normalize(), j = Math.asin(-b.y), q = Math.atan2(b.x, b.z);
      t.rotation = y.FromEuler(new R(j, q, 0));
      const G = 0.025, h = 0.01, m = _.RotationFromQuaternion(t.rotation).buffer, x = new R(-m[2], -m[5], -m[8]), N = new R(m[0], m[3], m[6]);
      c.KeyS && (Q = Q.add(x.multiply(G))), c.KeyW && (Q = Q.subtract(x.multiply(G))), c.KeyA && (Q = Q.subtract(N.multiply(G))), c.KeyD && (Q = Q.add(N.multiply(G))), c.KeyE && (I += h), c.KeyQ && (I -= h), c.KeyR && (d += h), c.KeyF && (d -= h), p = false;
    };
    const T = (l) => {
      l.preventDefault(), l.stopPropagation();
    };
    this.dispose = () => {
      n.removeEventListener("dragenter", T), n.removeEventListener("dragover", T), n.removeEventListener("dragleave", T), n.removeEventListener("contextmenu", T), n.removeEventListener("mousedown", k), n.removeEventListener("mousemove", H), n.removeEventListener("wheel", L), n.removeEventListener("touchstart", M), n.removeEventListener("touchend", V), n.removeEventListener("touchmove", w), o && (window.removeEventListener("keydown", W), window.removeEventListener("keyup", Z));
    }, o && (window.addEventListener("keydown", W), window.addEventListener("keyup", Z)), n.addEventListener("dragenter", T), n.addEventListener("dragover", T), n.addEventListener("dragleave", T), n.addEventListener("contextmenu", T), n.addEventListener("mousedown", k), n.addEventListener("mousemove", H), n.addEventListener("wheel", L), n.addEventListener("touchstart", M), n.addEventListener("touchend", V), n.addEventListener("touchmove", w), this.update();
  }
}

var root = from_html(`<canvas></canvas>`);

function Canvas3DGS($$anchor, $$props) {
	push($$props, true);

	let url = user_derived(() => $$props.value.url);
	let canvas;
	let scene;
	let camera;
	let renderer = state(null);
	let controls;
	let mounted = state(false);
	let frameId = state(null);

	function reset_scene() {
		if (get(frameId) !== null) {
			cancelAnimationFrame(get(frameId));
			set(frameId, null);
		}

		if (get(renderer) !== null) {
			get(renderer).dispose();
			set(renderer, null);
		}

		scene = new kt();
		camera = new wt();
		set(renderer, new Mt(canvas), true);
		controls = new Lt(camera, canvas);
		controls.zoomSpeed = $$props.zoom_speed;
		controls.panSpeed = $$props.pan_speed;

		if (!$$props.value) {
			return;
		}

		let loading = false;

		const load = async () => {
			if (loading) {
				console.error("Already loading");

				return;
			}

			if (!get(url)) {
				throw new Error("No resolved URL");
			}

			loading = true;

			if (get(url).endsWith(".ply")) {
				await bt.LoadAsync(get(url), scene, undefined);
			} else if (get(url).endsWith(".splat")) {
				await Tt.LoadAsync(get(url), scene, undefined);
			} else {
				throw new Error("Unsupported file type");
			}

			loading = false;
		};

		const frame = () => {
			if (!get(renderer)) {
				return;
			}

			if (loading) {
				set(frameId, requestAnimationFrame(frame), true);

				return;
			}

			controls.update();
			get(renderer).render(scene, camera);
			set(frameId, requestAnimationFrame(frame), true);
		};

		load();
		set(frameId, requestAnimationFrame(frame), true);
	}

	onMount(() => {
		if ($$props.value != null) {
			reset_scene();
		}

		set(mounted, true);

		return () => {
			if (get(renderer)) {
				get(renderer).dispose();
			}
		};
	});

	let path = user_derived(() => $$props.value?.path);

	user_effect(() => {
		if (canvas && get(mounted) && get(path)) {
			reset_scene();
		}
	});

	var canvas_1 = root();

	bind_this(canvas_1, ($$value) => canvas = $$value, () => canvas);
	append($$anchor, canvas_1);
	pop();
}

export { Canvas3DGS as default };
//# sourceMappingURL=Canvas3DGS-Ba2XF_ih.js.map
