import { defineComponent as z, ref as H, onMounted as q, onBeforeUnmount as K, watch as m, openBlock as N, createElementBlock as J, createElementVNode as W, normalizeClass as Y } from "vue";
import "@babylonjs/loaders/glTF";
import * as t from "@babylonjs/core";
import { cloneDeep as b, isEqual as u } from "lodash";
import { hex2Color4 as U, hex2Color3 as A } from "../../utils/babylon/ColorConvert.mjs";
const X = { class: "zerov-widget" }, j = z({ name: "zv-commons-three-loader3" }), te = /* @__PURE__ */ z({
  ...j,
  props: {
    basicOption: {},
    basicEvents: {},
    useEvents: {}
  },
  emits: [
    "on-load-success",
    "on-play-animation"
  ],
  setup(V, { expose: P, emit: _ }) {
    const e = V, E = _;
    let v, o, s, p, f, l, L, O, d, g, r, a;
    const S = () => {
      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(T.value, !0);
    }, B = () => {
      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 = U(e.basicOption.scene.color, new t.Color4(1, 1, 1, 1)) : o.clearColor = new t.Color4(0, 0, 0, 0), o.fogEnabled = e.basicOption.scene.fogEnabled, o.fogMode = e.basicOption.scene.fogMode || 1, o.fogColor = A(e.basicOption.scene.fogColor, new t.Color3(1, 1, 1)), o.fogEnd = e.basicOption.scene.fogEnd || 2e3, o.fogStart = e.basicOption.scene.fogStart || 1, o.fogDensity = e.basicOption.scene.fogDensity * 1e-4;
    }, R = () => {
      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 = A(e.basicOption.directionalLight.diffuse, new t.Color3(1, 1, 1)), 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 = A(e.basicOption.hemisphericLight.diffuse, new t.Color3(1, 1, 1)), e.basicOption.hemisphericLight.lightGizmo && e.basicOption.hemisphericLight.lightGizmo.enable ? (O || (O = new t.LightGizmo(), O.light = L), O.scaleRatio = e.basicOption.hemisphericLight.lightGizmo.scaleRatio) : O && (O.dispose(), O = null);
    }, G = () => {
      if (e.basicOption.ground.enable) {
        const { width: i, height: n, position: c, rotation: h, scaling: w, material: C, receiveShadows: Z } = 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(C.emissiveTexture, o), x.diffuseTexture = new t.Texture(C.emissiveTexture, o), x.ambientTexture = new t.Texture(C.emissiveTexture, o), x.diffuseColor = new t.Color3(1, 1, 1), x.opacityTexture = new t.Texture(C.opacityTexture, o), d.material = x, d.receiveShadows = Z;
      } else
        d && (d.dispose(), d = null);
    }, M = () => {
      if (e.basicOption.skybox.enable) {
        const { size: i } = e.basicOption.skybox;
        g || (g = 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, g.material = n, g.isPickable = !1;
      } else
        g && (g.dispose(), g = null);
    }, D = (i, n = {
      loop: !1,
      speedRatio: 1
    }) => {
      if (o) {
        o.stopAllAnimations();
        const c = o.getAnimationGroupByName(i);
        c && (E("on-play-animation"), window.SHJParseEvent(e.useEvents, "on-play-animation"), c.start(n.loop, n.speedRatio, c.from, c.to, !1));
      }
    }, y = 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 ? D(e.basicOption.defaultAnimation.name, {
        loop: e.basicOption.defaultAnimation.loop,
        speedRatio: e.basicOption.defaultAnimation.speedRatio
      }) : o.animationGroups && o.animationGroups[0] && o.animationGroups[0].stop();
    }, k = () => {
      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);
    }, I = function(i) {
      B(), S(), F(), R(), G(), M(), y(), k(), E("on-load-success"), window.SHJParseEvent(e.useEvents, "on-load-success");
    }, T = H();
    return q(() => {
      v = new t.Engine(T.value, !0), v && e.basicOption.meshes.url && (I(), v.runRenderLoop(function() {
        o && o.render();
      }));
    }), K(() => {
      o.dispose(), v.dispose();
    }), P({
      playAnimation: (i, n) => {
        const c = i[0].value, h = {
          loop: i[1].keys[0].defaultValue,
          speedRatio: i[1].keys[1].defaultValue
        };
        D(c, h);
      }
    }), m(() => b(e.basicOption.camera), (i, n) => {
      !u(i, n) && s && S();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.scene), (i, n) => {
      !u(i, n) && o && B();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.directionalLight), (i, n) => {
      u(i, n) || R();
    }, {
      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) || G();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.skybox), (i, n) => {
      u(i, n) || M();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.meshes), (i, n) => {
      !u(i, n) && r && i.url === n.url && y();
    }, {
      deep: !0
    }), m(() => b(e.basicOption.meshes.url), (i, n) => {
      u(i, n) || (o.meshes.forEach((c, h) => {
        h > 1 && c.dispose();
      }), r = null, y());
    }, {
      deep: !0
    }), m(() => b(e.basicOption.pipelineOption), (i, n) => {
      u(i, n) || k();
    }, {
      deep: !0
    }), (i, n) => (N(), J("div", X, [
      W("canvas", {
        ref_key: "canvasRef",
        ref: T,
        class: Y(["widget", { show: !0 }])
      }, null, 512)
    ]));
  }
});
export {
  te as default
};
