import { defineComponent as k, ref as I, onMounted as Z, onBeforeUnmount as q, watch as m, createElementBlock as K, openBlock as N, createElementVNode as J, normalizeClass as W } from "vue";
import "@babylonjs/loaders/glTF";
import * as t from "@babylonjs/core";
import { cloneDeep as b, isEqual as u } from "lodash";
const Y = { class: "zerov-widget" }, U = k({ name: "zv-commons-three-loader3" }), $ = /* @__PURE__ */ k({
  ...U,
  props: {
    basicOption: {},
    basicEvents: {},
    useEvents: {}
  },
  emits: [
    "on-load-success",
    "on-play-animation"
  ],
  setup(z, { expose: V, emit: P }) {
    const e = z, T = P;
    let v, o, s, p, f, l, L, g, d, O, r, a;
    const A = () => {
      s || (s = new t.ArcRotateCamera(
        "Camera",
        e.basicOption.camera.alpha,
        e.basicOption.camera.beta,
        e.basicOption.camera.radius,
        new t.Vector3(
          e.basicOption.camera.target[0],
          e.basicOption.camera.target[1],
          e.basicOption.camera.target[2]
        ),
        o
      )), s.alpha = e.basicOption.camera.alpha, s.beta = e.basicOption.camera.beta, s.radius = e.basicOption.camera.radius, s.target = new t.Vector3(
        e.basicOption.camera.target[0],
        e.basicOption.camera.target[1],
        e.basicOption.camera.target[2]
      ), s.lowerRadiusLimit = e.basicOption.camera.lowerRadiusLimit, s.upperRadiusLimit = e.basicOption.camera.upperRadiusLimit, s.lowerBetaLimit = t.Tools.ToRadians(e.basicOption.camera.lowerBetaLimit), s.upperBetaLimit = t.Tools.ToRadians(e.basicOption.camera.upperBetaLimit), s.useBouncingBehavior = e.basicOption.camera.useBouncingBehavior, s.wheelDeltaPercentage = e.basicOption.camera.wheelDeltaPercentage || 0.01, s.useAutoRotationBehavior = e.basicOption.camera.autoRotation, s.panningSensibility = 1 / (e.basicOption.camera.panningSensibility || 0.01), s.attachControl(S.value, !0);
    }, E = () => {
      o || (o = new t.Scene(v)), o.environmentTexture = t.CubeTexture.CreateFromPrefilteredData(e.basicOption.scene.environmentTexture, o), o.environmentIntensity = e.basicOption.scene.environmentIntensity, e.basicOption.scene.colorShow ? o.clearColor = t.Color4.FromHexString(e.basicOption.scene.color || "#000000") : o.clearColor = new t.Color4(0, 0, 0, 0), o.fogEnabled = e.basicOption.scene.fogEnabled, o.fogMode = e.basicOption.scene.fogMode || 1, o.fogColor = t.Color3.FromHexString(e.basicOption.scene.fogColor || "#ffffff"), o.fogEnd = e.basicOption.scene.fogEnd || 2e3, o.fogStart = e.basicOption.scene.fogStart || 1, o.fogDensity = e.basicOption.scene.fogDensity * 1e-4;
    }, B = () => {
      if (p || (p = new t.DirectionalLight("dirLight", new t.Vector3(
        e.basicOption.directionalLight.direction[0],
        e.basicOption.directionalLight.direction[1],
        e.basicOption.directionalLight.direction[2]
      ), o)), p.direction = new t.Vector3(
        e.basicOption.directionalLight.direction[0],
        e.basicOption.directionalLight.direction[1],
        e.basicOption.directionalLight.direction[2]
      ), p.position = new t.Vector3(
        e.basicOption.directionalLight.position[0],
        e.basicOption.directionalLight.position[1],
        e.basicOption.directionalLight.position[2]
      ), p.diffuse = t.Color3.FromHexString(e.basicOption.directionalLight.diffuse || "#ffffff"), p.intensity = e.basicOption.directionalLight.intensity, p.shadowMinZ = e.basicOption.directionalLight.shadowMinZ, p.shadowMaxZ = e.basicOption.directionalLight.shadowMaxZ, p.autoCalcShadowZBounds = e.basicOption.directionalLight.autoCalcShadowZBounds, e.basicOption.directionalLight.shadowGenerator.enable) {
        const { mapSize: i, transparencyShadow: n, usePercentageCloserFiltering: c, bias: h } = e.basicOption.directionalLight.shadowGenerator;
        if (!l && (l = new t.ShadowGenerator(i, p), r))
          for (let w = 0; w < r.length; w++)
            e.basicOption.meshes.isCasterShadow && l && l.addShadowCaster(r[w], !0);
        l.mapSize = i, l.transparencyShadow = n, l.usePercentageCloserFiltering = c, l.bias = h;
      } else
        l && (l.dispose(), l = null);
      e.basicOption.directionalLight.lightGizmo && e.basicOption.directionalLight.lightGizmo.enable ? (f || (f = new t.LightGizmo(), f.light = p), f.scaleRatio = e.basicOption.directionalLight.lightGizmo.scaleRatio) : f && (f.dispose(), f = null);
    }, F = () => {
      L || (L = new t.HemisphericLight("light", new t.Vector3(
        e.basicOption.hemisphericLight.direction[0],
        e.basicOption.hemisphericLight.direction[1],
        e.basicOption.hemisphericLight.direction[2]
      ), o)), L.direction = new t.Vector3(
        e.basicOption.hemisphericLight.direction[0],
        e.basicOption.hemisphericLight.direction[1],
        e.basicOption.hemisphericLight.direction[2]
      ), L.intensity = e.basicOption.hemisphericLight.intensity, L.diffuse = t.Color3.FromHexString(e.basicOption.hemisphericLight.diffuse || "#ffffff"), e.basicOption.hemisphericLight.lightGizmo && e.basicOption.hemisphericLight.lightGizmo.enable ? (g || (g = new t.LightGizmo(), g.light = L), g.scaleRatio = e.basicOption.hemisphericLight.lightGizmo.scaleRatio) : g && (g.dispose(), g = null);
    }, R = () => {
      if (e.basicOption.ground.enable) {
        const { width: i, height: n, position: c, rotation: h, scaling: w, material: y, receiveShadows: H } = e.basicOption.ground;
        d || (d = t.MeshBuilder.CreateGround("ground", { width: i, height: n }, o)), d.position = new t.Vector3(
          c[0],
          c[1],
          c[2]
        ), d.rotation = new t.Vector3(
          t.Tools.ToRadians(h[0]),
          t.Tools.ToRadians(h[1]),
          t.Tools.ToRadians(h[2])
        ), d.scaling = new t.Vector3(
          w[0],
          w[1],
          w[2]
        );
        const x = new t.StandardMaterial("", o);
        x.emissiveTexture = new t.Texture(y.emissiveTexture, o), x.diffuseTexture = new t.Texture(y.emissiveTexture, o), x.ambientTexture = new t.Texture(y.emissiveTexture, o), x.diffuseColor = new t.Color3(1, 1, 1), x.opacityTexture = new t.Texture(y.opacityTexture, o), d.material = x, d.receiveShadows = H;
      } else
        d && (d.dispose(), d = null);
    }, G = () => {
      if (e.basicOption.skybox.enable) {
        const { size: i } = e.basicOption.skybox;
        O || (O = t.MeshBuilder.CreateBox("skyBox", { size: i }, o));
        const n = new t.StandardMaterial("skyBox", o);
        n.backFaceCulling = !1, n.reflectionTexture = t.CubeTexture.CreateFromPrefilteredData(e.basicOption.scene.environmentTexture, o), n.reflectionTexture.coordinatesMode = t.Texture.SKYBOX_MODE, n.diffuseColor = new t.Color3(1, 1, 1), n.specularColor = new t.Color3(1, 1, 1), n.disableLighting = !0, O.material = n, O.isPickable = !1;
      } else
        O && (O.dispose(), O = null);
    }, M = (i, n = {
      loop: !1,
      speedRatio: 1
    }) => {
      if (o) {
        o.stopAllAnimations();
        const c = o.getAnimationGroupByName(i);
        c && (T("on-play-animation"), window.SHJParseEvent(e.useEvents, "on-play-animation"), c.start(n.loop, n.speedRatio, c.from, c.to, !1));
      }
    }, C = async () => {
      r || (r = (await t.SceneLoader.ImportMeshAsync("", e.basicOption.meshes.url, "", o)).meshes), r[0].position = new t.Vector3(
        e.basicOption.meshes.position[0],
        e.basicOption.meshes.position[1],
        e.basicOption.meshes.position[2]
      ), r[0].scaling = new t.Vector3(
        e.basicOption.meshes.scaling[0],
        e.basicOption.meshes.scaling[1],
        e.basicOption.meshes.scaling[2]
      ), r[0].rotation = new t.Vector3(
        t.Tools.ToRadians(e.basicOption.meshes.rotation[0]),
        t.Tools.ToRadians(e.basicOption.meshes.rotation[1]),
        t.Tools.ToRadians(e.basicOption.meshes.rotation[2])
      );
      for (let i = 0; i < r.length; i++)
        r[i].isPickable = !1, r[i].receiveShadows = e.basicOption.meshes.isCasterShadow, e.basicOption.meshes.isCasterShadow && l && l.addShadowCaster(r[i], !0);
      e.basicOption.animationGroups = o.animationGroups.map((i) => ({ uniqueId: i.uniqueId, name: i.name })), e.basicEvents && (e.basicEvents.function[0].args[0].select = o.animationGroups.map((i) => ({ name: i.name, value: i.name }))), e.basicOption.defaultAnimation && e.basicOption.defaultAnimation.name ? M(e.basicOption.defaultAnimation.name, {
        loop: e.basicOption.defaultAnimation.loop,
        speedRatio: e.basicOption.defaultAnimation.speedRatio
      }) : o.animationGroups && o.animationGroups[0] && o.animationGroups[0].stop();
    }, D = () => {
      const i = e.basicOption.pipelineOption;
      i && i.enabled ? (a || (a = new t.DefaultRenderingPipeline(
        "defaultPipeline",
        !0,
        o,
        [s]
      )), a.samples = i.samples ? 4 : 1, a.fxaaEnabled = i.fxaaEnabled, a.grainEnabled = i.grainEnabled, a.grain.intensity = i.grainIntensity, a.grain.animated = i.grainAnimated, a.bloomEnabled = i.bloomEnabled, a.bloomThreshold = i.bloomThreshold, a.bloomWeight = i.bloomWeight, a.bloomKernel = i.bloomKernel, a.bloomScale = i.bloomScale, a.depthOfFieldEnabled = i.depthOfFieldEnabled, i.depthOfFieldBlurLevel < 1 ? a.depthOfFieldBlurLevel = t.DepthOfFieldEffectBlurLevel.Low : i.depthOfFieldBlurLevel < 2 ? a.depthOfFieldBlurLevel = t.DepthOfFieldEffectBlurLevel.Medium : i.depthOfFieldBlurLevel < 3 && (a.depthOfFieldBlurLevel = t.DepthOfFieldEffectBlurLevel.High), a.depthOfField.focusDistance = i.depthOfFieldFocusDistance, a.depthOfField.fStop = i.depthOfFieldFStop, a.depthOfField.focalLength = i.depthOfFieldFocalLength, a.chromaticAberrationEnabled = i.chromaticAberrationEnabled, a.chromaticAberration.aberrationAmount = i.chromaticAberrationAberrationAmount, a.chromaticAberration.radialIntensity = i.chromaticAberrationRadialIntensity, i.chromaticAberrationDirection === 0 ? (a.chromaticAberration.direction.x = 0, a.chromaticAberration.direction.y = 0) : (a.chromaticAberration.direction.x = Math.sin(i.chromaticAberrationDirection), a.chromaticAberration.direction.y = Math.cos(i.chromaticAberrationDirection)), a.sharpenEnabled = i.sharpenEnabled, a.sharpen.edgeAmount = i.sharpenEdgeAmount, a.sharpen.colorAmount = i.sharpenColorAmount) : a && (a.dispose(), a = null);
    }, _ = function(i) {
      E(), A(), F(), B(), R(), G(), C(), D(), T("on-load-success"), window.SHJParseEvent(e.useEvents, "on-load-success");
    }, S = I();
    return Z(() => {
      v = new t.Engine(S.value, !0), v && e.basicOption.meshes.url && (_(), v.runRenderLoop(function() {
        o && o.render();
      }));
    }), q(() => {
      o.dispose(), v.dispose();
    }), V({
      playAnimation: (i, n) => {
        const c = i[0].value, h = {
          loop: i[1].keys[0].defaultValue,
          speedRatio: i[1].keys[1].defaultValue
        };
        M(c, h);
      }
    }), m(() => b(e.basicOption.camera), (i, n) => {
      !u(i, n) && s && A();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.scene), (i, n) => {
      !u(i, n) && o && E();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.directionalLight), (i, n) => {
      u(i, n) || B();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.hemisphericLight), (i, n) => {
      u(i, n) || F();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.ground), (i, n) => {
      u(i, n) || R();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.skybox), (i, n) => {
      u(i, n) || G();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.meshes), (i, n) => {
      !u(i, n) && r && i.url === n.url && C();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.meshes.url), (i, n) => {
      u(i, n) || (o.meshes.forEach((c, h) => {
        h > 1 && c.dispose();
      }), r = null, C());
    }, {
      deep: !0
    }), m(() => b(e.basicOption.pipelineOption), (i, n) => {
      u(i, n) || D();
    }, {
      deep: !0
    }), (i, n) => (N(), K("div", Y, [
      J("canvas", {
        ref_key: "canvasRef",
        ref: S,
        class: W(["widget", { show: !0 }])
      }, null, 512)
    ]));
  }
});
export {
  $ as default
};
