var k = Object.defineProperty;
var A = (l, i, h) => i in l ? k(l, i, { enumerable: !0, configurable: !0, writable: !0, value: h }) : l[i] = h;
var b = (l, i, h) => A(l, typeof i != "symbol" ? i + "" : i, h);
import { defineComponent as M, watch as r, openBlock as E, createElementBlock as x, createElementVNode as G, normalizeClass as P, unref as B, createCommentVNode as V, ref as y, onMounted as F, onBeforeUnmount as N } from "vue";
import "@babylonjs/loaders/glTF";
import { useResizeObserver as q } from "@vueuse/core";
import { Vector3 as m, Engine as Z, Scene as j, Color4 as v, Color3 as _, GaussianSplattingMesh as H, ArcRotateCamera as J, Tools as w } from "@babylonjs/core";
import { cloneDeep as u, isEqual as c } from "lodash";
import { hex2Color4 as D, hex2Color3 as T } from "../../utils/babylon/ColorConvert.mjs";
const K = { class: "zerov-widget" }, Q = {
  key: 0,
  ref: "babylonLoadingRef",
  class: "babylon-loading"
}, X = M({ name: "zv-commons-gaussian-splatting-view" }), ne = /* @__PURE__ */ M({
  ...X,
  props: {
    basicOption: {},
    basicEvents: {}
  },
  setup(l) {
    const i = l, h = (s, a) => {
      const d = y(), S = y(!0);
      class U {
        // eslint-disable-next-line no-useless-constructor
        constructor(e) {
          // optional, but needed due to interface definitions
          b(this, "loadingUIBackgroundColor");
          this.loadingUIText = e;
        }
        displayLoadingUI() {
          d.value && (S.value = !0);
        }
        hideLoadingUI() {
          d.value && (S.value = !1);
        }
      }
      class L {
        constructor(e, n, o) {
          b(this, "scene");
          b(this, "engine");
          b(this, "gs");
          this.canvas = e, this.option = n, this.events = o;
          try {
            this.engine = new Z(this.canvas, !0, {}, !1), this.engine.loadingScreen = new U(""), window.addEventListener("resize", () => {
              this.engine.resize();
            }), this.scene = this.createScene();
          } catch {
          }
        }
        createScene() {
          return this.engine.displayLoadingUI(), this.scene = new j(this.engine), this.option.scene.colorShow ? this.scene.clearColor = D(this.option.scene.color, new v(0, 0, 0, 0)) : this.scene.clearColor = new v(0, 0, 0, 0), this.scene.fogEnabled = this.option.scene.fogEnabled, this.scene.fogMode = this.option.scene.fogMode || 1, this.scene.fogColor = T(this.option.scene.fogColor, new _(1, 1, 1)), this.scene.fogEnd = this.option.scene.fogEnd || 60, this.scene.fogStart = this.option.scene.fogStart || 1, this.scene.fogDensity = this.option.scene.fogDensity || 0.1, this.createCamera(this.option.camera, this.scene), this.gs = new H("", null, this.scene), this.gs.loadFileAsync(this.option.meshs.url).then(() => {
            this.setGSSize(this.option.meshs), setTimeout(() => {
              this.engine.hideLoadingUI();
            }, 100);
          }), this.scene;
        }
        setGSSize(e) {
          if (e.autoSize) {
            const o = this.gs.getBoundingInfo().boundingBox, f = 0.7 / (Math.max(
              o.maximumWorld.x - o.minimumWorld.x,
              o.maximumWorld.y - o.minimumWorld.y,
              o.maximumWorld.z - o.minimumWorld.z
            ) || 1);
            this.gs.scaling = new m(f, f, f);
            const W = o.centerWorld;
            this.gs.position = this.gs.position.subtract(W.multiplyByFloats(f, f, f)), this.gs.rotation = new m(0, 0, 0);
          } else {
            const { position: n, scaling: o, rotation: p } = e;
            this.gs.position.set(n[0] || 0, n[1] || 0, n[2] || 0), this.gs.scaling.set(o[0] || 1, o[1] || 1, o[2] || 1), this.gs.rotation = new m(p[0] || 0, p[1] || 0, p[2] || 0);
          }
        }
        updateScene(e) {
          e.colorShow ? this.scene.clearColor = D(e.color, new v(1, 1, 1, 1)) : this.scene.clearColor = new v(0, 0, 0, 0), this.scene.fogEnabled = e.fogEnabled, this.scene.fogMode = e.fogMode || 1, this.scene.fogColor = T(e.fogColor, new _(1, 1, 1)), this.scene.fogEnd = e.fogEnd || 60, this.scene.fogStart = e.fogStart || 1, this.scene.fogDensity = e.fogDensity || 0.1;
        }
        createCamera(e, n) {
          const o = new J("", w.ToRadians(e.alpha || 90), w.ToRadians(e.beta || 75), e.radius || 1, new m(...e.target), n);
          o.attachControl(d.value, !0), o.useAutoRotationBehavior = e.autoRotation, o.wheelDeltaPercentage = e.wheelDeltaPercentage || 0.01, o.zoomToMouseLocation = e.zoomToMouseLocation, o.lowerRadiusLimit = e.lowerRadiusLimit, o.upperRadiusLimit = e.upperRadiusLimit, o.minZ = 0.01;
        }
        updateCamera(e) {
          const n = this.scene.activeCamera;
          n && (e.radius && (n.radius = e.radius), n.beta = w.ToRadians(e.beta), n.alpha = w.ToRadians(e.alpha), n.useAutoRotationBehavior = e.autoRotation, n.zoomToMouseLocation = e.zoomToMouseLocation, n.wheelDeltaPercentage = e.wheelDeltaPercentage || 0.01, n.lowerRadiusLimit = e.lowerRadiusLimit, n.upperRadiusLimit = e.upperRadiusLimit, n.target = new m(...e.target));
        }
        run() {
          return this.engine.runRenderLoop(() => {
            this.scene.render();
          }), this;
        }
        dispose() {
          this.scene && this.engine && (this.scene.dispose(), this.engine.dispose(), this.gs.dispose(), this.gs = null);
        }
      }
      const g = y();
      return F(() => {
        s.meshs && (g.value = new L(d.value, s, a).run(), q(d.value, (z) => {
          g.value && g.value.engine.resize();
        }));
      }), N(() => {
        g.value && g.value.dispose();
      }), {
        canvasRef: d,
        babylon: g,
        Babylon: L,
        babylonLoadingStatus: S
      };
    }, { babylon: t, Babylon: I, canvasRef: C, babylonLoadingStatus: R } = h(i.basicOption, i.basicEvents);
    return r(() => u(i.basicOption.scene), (s, a) => {
      !c(s, a) && t.value && t.value.updateScene(s);
    }, {
      deep: !0
    }), r(() => u(i.basicOption.meshs.url), (s, a) => {
      !c(s, a) && t.value && (t.value.dispose(), t.value = new I(C.value, i.basicOption, i.basicEvents).run());
    }, {
      deep: !0
    }), r(() => u(i.basicOption.meshs.position), (s, a) => {
      !c(s, a) && t.value && t.value.gs && t.value.gs.position.set(s[0], s[1], s[2]);
    }, {
      deep: !0
    }), r(() => u(i.basicOption.meshs.scaling), (s, a) => {
      !c(s, a) && t.value && t.value.gs && t.value.gs.scaling.set(s[0], s[1], s[2]);
    }, {
      deep: !0
    }), r(() => u(i.basicOption.meshs.rotation), (s, a) => {
      !c(s, a) && t.value && t.value.gs && (t.value.gs.rotation = new m(s[0], s[1], s[2]));
    }, {
      deep: !0
    }), r(() => u(i.basicOption.meshs.autoSize), (s, a) => {
      !c(s, a) && t.value && t.value.gs && t.value.setGSSize(i.basicOption.meshs);
    }, {
      deep: !0
    }), r(() => u(i.basicOption.camera), (s, a) => {
      !c(s, a) && t.value && t.value.updateCamera(s);
    }, {
      deep: !0
    }), (s, a) => (E(), x("div", K, [
      G("canvas", {
        ref_key: "canvasRef",
        ref: C,
        class: P(["widget", { show: !B(R) }])
      }, null, 2),
      B(R) ? (E(), x("div", Q, " 正在加载模型... ", 512)) : V("", !0)
    ]));
  }
});
export {
  ne as default
};
