var q = Object.defineProperty;
var N = (f, u, b) => u in f ? q(f, u, { enumerable: !0, configurable: !0, writable: !0, value: b }) : f[u] = b;
var v = (f, u, b) => N(f, typeof u != "symbol" ? u + "" : u, b);
import { defineComponent as _, watch as r, openBlock as M, createElementBlock as I, createElementVNode as V, normalizeClass as K, unref as C, toDisplayString as J, createCommentVNode as Z, ref as w, onMounted as j, onBeforeUnmount as Q } from "vue";
import "@babylonjs/loaders/glTF";
import { cloneDeep as h, isEqual as d } from "lodash";
import { Vector3 as c, Engine as X, Scene as Y, Color4 as P, Color3 as y, DefaultRenderingPipeline as $, DepthOfFieldEffectBlurLevel as R, ArcRotateCamera as O, Tools as A, HemisphericLight as ee, DirectionalLight as ie, PointLight as te, SceneLoader as se } from "@babylonjs/core";
import { useResizeObserver as oe } from "@vueuse/core";
import { hex2Color4 as W, hex2Color3 as L } from "../../utils/babylon/ColorConvert.mjs";
const ne = { class: "zerov-widget" }, ae = _({ name: "zv-commons-three-loader" }), be = /* @__PURE__ */ _({
  ...ae,
  props: {
    basicOption: {},
    basicEvents: {},
    useEvents: {}
  },
  emits: [
    "on-load-success",
    "on-play-animation"
  ],
  setup(f, { expose: u, emit: b }) {
    const x = b, a = f, G = (t, n) => {
      const p = w(), z = w(), S = w(!0);
      class k {
        // eslint-disable-next-line no-useless-constructor
        constructor(e) {
          // optional, but needed due to interface definitions
          v(this, "loadingUIBackgroundColor");
          this.loadingUIText = e;
        }
        displayLoadingUI() {
          p.value && (S.value = !0);
        }
        hideLoadingUI() {
          p.value && (S.value = !1);
        }
      }
      class F {
        constructor(e, s, i) {
          v(this, "scene");
          v(this, "engine");
          v(this, "meshe");
          v(this, "defaultPipeline");
          this.canvas = e, this.option = s, this.events = i;
          try {
            this.engine = new X(this.canvas, !0, {}, !1), this.engine.loadingScreen = new k(""), window.addEventListener("resize", () => {
              this.engine.resize();
            }), this.scene = this.createScene();
          } catch {
          }
        }
        createScene() {
          return this.scene = new Y(this.engine), this.option.scene.colorShow ? this.scene.clearColor = W(this.option.scene.color, new P(1, 1, 1, 1)) : this.scene.clearColor = new P(0, 0, 0, 0), this.scene.fogEnabled = this.option.scene.fogEnabled, this.scene.fogMode = this.option.scene.fogMode || 1, this.scene.fogColor = L(this.option.scene.fogColor, new y(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.createLights(this.option.lights, this.scene), this.createMeshs(this.scene), this.scene;
        }
        updateScenePipeline(e) {
          this.scene.activeCamera && e && (e.enabled ? (this.defaultPipeline || (this.defaultPipeline = new $(
            "defaultPipeline",
            !0,
            this.scene,
            [this.scene.activeCamera]
          )), this.defaultPipeline.samples = e.samples ? 4 : 1, this.defaultPipeline.fxaaEnabled = e.fxaaEnabled, this.defaultPipeline.grainEnabled = e.grainEnabled, this.defaultPipeline.grain.intensity = e.grainIntensity, this.defaultPipeline.grain.animated = e.grainAnimated, this.defaultPipeline.bloomEnabled = e.bloomEnabled, this.defaultPipeline.bloomThreshold = e.bloomThreshold, this.defaultPipeline.bloomWeight = e.bloomWeight, this.defaultPipeline.bloomKernel = e.bloomKernel, this.defaultPipeline.bloomScale = e.bloomScale, this.defaultPipeline.depthOfFieldEnabled = e.depthOfFieldEnabled, e.depthOfFieldBlurLevel < 1 ? this.defaultPipeline.depthOfFieldBlurLevel = R.Low : e.depthOfFieldBlurLevel < 2 ? this.defaultPipeline.depthOfFieldBlurLevel = R.Medium : e.depthOfFieldBlurLevel < 3 && (this.defaultPipeline.depthOfFieldBlurLevel = R.High), this.defaultPipeline.depthOfField.focusDistance = e.depthOfFieldFocusDistance, this.defaultPipeline.depthOfField.fStop = e.depthOfFieldFStop, this.defaultPipeline.depthOfField.focalLength = e.depthOfFieldFocalLength, this.defaultPipeline.chromaticAberrationEnabled = e.chromaticAberrationEnabled, this.defaultPipeline.chromaticAberration.aberrationAmount = e.chromaticAberrationAberrationAmount, this.defaultPipeline.chromaticAberration.radialIntensity = e.chromaticAberrationRadialIntensity, e.chromaticAberrationDirection === 0 ? (this.defaultPipeline.chromaticAberration.direction.x = 0, this.defaultPipeline.chromaticAberration.direction.y = 0) : (this.defaultPipeline.chromaticAberration.direction.x = Math.sin(e.chromaticAberrationDirection), this.defaultPipeline.chromaticAberration.direction.y = Math.cos(e.chromaticAberrationDirection)), this.defaultPipeline.sharpenEnabled = e.sharpenEnabled, this.defaultPipeline.sharpen.edgeAmount = e.sharpenEdgeAmount, this.defaultPipeline.sharpen.colorAmount = e.sharpenColorAmount) : this.defaultPipeline && (this.defaultPipeline.dispose(), this.defaultPipeline = null));
        }
        updateScene(e) {
          e.colorShow ? this.scene.clearColor = W(e.color, new P(1, 1, 1, 1)) : this.scene.clearColor = new P(0, 0, 0, 0), this.scene.fogEnabled = e.fogEnabled, this.scene.fogMode = e.fogMode || 1, this.scene.fogColor = L(e.fogColor, new y(1, 1, 1)), this.scene.fogEnd = e.fogEnd || 60, this.scene.fogStart = e.fogStart || 1, this.scene.fogDensity = e.fogDensity || 0.1;
        }
        createCamera(e, s) {
          const i = new O("", A.ToRadians(e.alpha || 90), A.ToRadians(e.beta || 75), e.radius || 1, new c(...e.target), s);
          i.attachControl(p.value, !0), i.useAutoRotationBehavior = e.autoRotation, i.wheelDeltaPercentage = e.wheelDeltaPercentage || 0.01, i.zoomToMouseLocation = e.zoomToMouseLocation, i.lowerRadiusLimit = e.lowerRadiusLimit, i.upperRadiusLimit = e.upperRadiusLimit, i.minZ = 0.01;
        }
        updateCamera(e) {
          const s = this.scene.activeCamera;
          s && (e.radius && (s.radius = e.radius), s.beta = A.ToRadians(e.beta), s.alpha = A.ToRadians(e.alpha), s.useAutoRotationBehavior = e.autoRotation, s.zoomToMouseLocation = e.zoomToMouseLocation, s.wheelDeltaPercentage = e.wheelDeltaPercentage || 0.01, s.lowerRadiusLimit = e.lowerRadiusLimit, s.upperRadiusLimit = e.upperRadiusLimit, s.target = new c(...e.target));
        }
        createLights(e, s) {
          this.scene && this.scene.lights && this.scene.lights.forEach((i, l) => {
            setTimeout(() => {
              i.dispose();
            });
          }), setTimeout(() => {
            e.forEach((i) => {
              if (i.type === "HemisphericLight") {
                const l = new ee("", new c(...i.direction), s);
                l.intensity = i.intensity !== void 0 ? i.intensity : 0.5, l.groundColor = L(i.groundColor, y.White()), l.diffuse = L(i.diffuse, y.White());
              }
              if (i.type === "DirectionalLight") {
                const l = new ie("", new c(...i.direction), s);
                l.intensity = i.intensity !== void 0 ? i.intensity : 0.5, l.diffuse = L(i.diffuse, y.White());
              }
              if (i.type === "PointLight") {
                const l = new te("", new c(...i.position), s);
                l.intensity = i.intensity !== void 0 ? i.intensity : 0.5, l.diffuse = L(i.diffuse, y.White());
              }
            });
          });
        }
        createMeshs(e) {
          e.meshes.forEach((s) => setTimeout(() => s.dispose())), se.ImportMeshAsync("", this.option.meshs.url, "", e, (s) => {
            z.value = (s.loaded / s.total * 100).toFixed(2);
          }).then((s) => {
            this.meshe = s.meshes, this.updateScenePipeline(this.option.pipelineOption), this.option.animationGroups = e.animationGroups.map((i) => ({
              uniqueId: i.uniqueId,
              name: i.name
            })), this.events && (this.events.function[0].args[0].select = e.animationGroups.map((i) => ({
              name: i.name,
              value: i.name
            }))), this.option.defaultAnimation && this.option.defaultAnimation.name ? this.playAnimation(this.option.defaultAnimation.name, {
              loop: this.option.defaultAnimation.loop,
              speedRatio: this.option.defaultAnimation.speedRatio
            }) : e.animationGroups && e.animationGroups[0] && e.animationGroups[0].stop(), this.setMeshSize(this.option.meshs), setTimeout(() => {
              this.engine.hideLoadingUI(), x("on-load-success"), this.engine.hideLoadingUI();
            }, 100);
          });
        }
        setMeshSize(e) {
          if (e.autoSize) {
            const i = this.meshe[0].getBoundingInfo().boundingBox, g = 0.7 / (Math.max(
              i.maximumWorld.x - i.minimumWorld.x,
              i.maximumWorld.y - i.minimumWorld.y,
              i.maximumWorld.z - i.minimumWorld.z
            ) || 1);
            this.meshe[0].scaling = new c(g, g, g);
            const H = i.centerWorld;
            this.meshe[0].position = this.meshe[0].position.subtract(H.multiplyByFloats(g, g, g)), this.meshe[0].rotation = new c(0, 0, 0);
          } else {
            const { position: s, scaling: i, rotation: l } = e;
            this.meshe[0].position.set(s[0] || 0, s[1] || 0, s[2] || 0), this.meshe[0].scaling.set(i[0] || 1, i[1] || 1, i[2] || 1), this.meshe[0].rotation = new c(l[0] || 0, l[1] || 0, l[2] || 0);
          }
        }
        run() {
          return this.engine.runRenderLoop(() => this.scene.render()), this;
        }
        dispose() {
          this.scene && this.engine && (this.scene.dispose(), this.engine.dispose());
        }
        playAnimation(e, s = {
          loop: !1,
          speedRatio: 1
        }) {
          if (this.scene) {
            this.scene.stopAllAnimations();
            const i = this.scene.getAnimationGroupByName(e);
            i && (x("on-play-animation"), window.SHJParseEvent(a.useEvents, "on-play-animation"), i.start(s.loop, s.speedRatio, i.from, i.to, !1));
          }
        }
      }
      const m = w();
      return j(() => {
        t.meshs && (m.value = new F(p.value, t, n).run(), oe(p.value, (T) => {
          m.value && m.value.engine.resize();
        }));
      }), Q(() => {
        m.value && (m.value.dispose(), m.value.meshe = []);
      }), {
        canvasRef: p,
        babylon: m,
        Babylon: F,
        babylonLoadingStatus: S,
        babylonLoadingTitle: z
      };
    }, { canvasRef: E, babylon: o, Babylon: B, babylonLoadingStatus: D, babylonLoadingTitle: U } = G(a.basicOption, a.basicEvents);
    return u({
      playAnimation: (t, n) => {
        o.value.playAnimation(t, n);
      }
    }), r(() => h(a.basicOption.camera), (t, n) => {
      !d(t, n) && o.value && o.value.updateCamera(t);
    }, {
      deep: !0
    }), r(() => h(a.basicOption.scene), (t, n) => {
      !d(t, n) && o.value && o.value.updateScene(t);
    }, {
      deep: !0
    }), r(() => h(a.basicOption.lights), (t, n) => {
      !d(t, n) && o.value && o.value.createLights(t, o.value.scene);
    }, {
      deep: !0
    }), r(() => h(a.basicOption.meshs.url), (t, n) => {
      !d(t, n) && o.value && (o.value.dispose(), o.value = new B(E.value, a.basicOption, a.basicEvents).run());
    }, {
      deep: !0
    }), r(() => h(a.basicOption.meshs.position), (t, n) => {
      !d(t, n) && o.value && o.value.meshe[0] && o.value.meshe[0].position.set(t[0], t[1], t[2]);
    }, {
      deep: !0
    }), r(() => h(a.basicOption.meshs.scaling), (t, n) => {
      !d(t, n) && o.value && o.value.meshe[0] && o.value.meshe[0].scaling.set(t[0], t[1], t[2]);
    }, {
      deep: !0
    }), r(() => h(a.basicOption.meshs.rotation), (t, n) => {
      !d(t, n) && o.value && o.value.meshe[0] && (o.value.meshe[0].rotation = new c(t[0], t[1], t[2]));
    }, {
      deep: !0
    }), r(() => h(a.basicOption.meshs.autoSize), (t, n) => {
      !d(t, n) && o.value && o.value.meshe[0] && o.value.setMeshSize(a.basicOption.meshs);
    }, {
      deep: !0
    }), r(() => h(a.basicOption.defaultAnimation), (t, n) => {
      !d(t, n) && o.value && (o.value.dispose(), o.value = new B(E.value, a.basicOption, a.basicEvents).run());
    }, {
      deep: !0
    }), r(() => h(a.basicOption.pipelineOption), (t, n) => {
      !d(t, n) && o.value && o.value.updateScenePipeline(t);
    }, {
      deep: !0
    }), (t, n) => (M(), I("div", ne, [
      V("canvas", {
        ref_key: "canvasRef",
        ref: E,
        class: K(["widget", { show: !C(D) }])
      }, null, 2),
      C(D) ? (M(), I("div", {
        key: 0,
        ref: "babylonLoadingRef",
        class: "babylon-loading"
      }, " 正在加载模型...(" + J(C(U)) + "%) ", 513)) : Z("", !0)
    ]));
  }
});
export {
  be as default
};
