import EMeshType from "./enum/EMeshType.js" import XDecalTextureError from "./error/XDecalTextureError.js" import XDecalError from "./error/XDecalError.js" import Logger from "./Logger.js" const logger = new Logger('DecalManager') export default class XDecal { constructor(o) { this._low_model = [], this._mat = null, this.sourceMatId = "", this.loadModel = async()=>new Promise((k,j)=>{ typeof this.meshPath == "string" ? BABYLON.SceneLoader.LoadAssetContainerAsync("", this.meshPath, this.scene, null, ".glb").then($=>{ for (let _e = $.materials.length - 1; _e >= 0; --_e) $.materials[_e].dispose(); for (let _e = 0; _e < $.meshes.length; ++_e) $.meshes[_e].visibility = 1, $.meshes[_e].isPickable = !0, $.meshes[_e].checkCollisions = !1, "hasVertexAlpha"in $.meshes[_e] && ($.meshes[_e].hasVertexAlpha = !1), this.scene.addMesh($.meshes[_e]), this._low_model.push(new XStaticMesh({ id: this._id, mesh: $.meshes[_e], xtype: EMeshType.Decal, skinInfo: this._skinInfo })), this.toggle(!1); k(!0) } ).catch($=>{ j(new XDecalError(`[Engine] decal load model error! ${$}`)) } ) : j(new XDecalError("[Engine] decal inport mesh is not string!")) } ).catch(k=>{ new XDecalError(`[Engine] decal loadModel ${k}`) } ); const {id: s, scene: c, meshPath: _, skinInfo: b="default"} = o; this._id = s, this.scene = c, this.meshPath = _, this._skinInfo = b } get skinInfo() { return this._skinInfo } getMesh() { return this._low_model } getMat() { return this._mat } get id() { return this._id } toggle(o) { for (let s = 0; s < this._low_model.length; ++s) o == !0 ? this._low_model[s].show() : this._low_model[s].hide() } setMat(o) { this._mat = o; for (let s = 0; s < this._low_model.length; ++s) this._low_model[s].mesh.material = this._mat; GEngine.engineOption.bDisableLocalRender ? this.toggle(!1) : this.toggle(!0) } changeModel(o="") { return o != "" && (this.meshPath = o), this.meshPath == "" ? (logger.error("[Engine] changeModel Error! meshPath is empty"), Promise.reject(new XDecalTextureError("[Engine] changeModel Error! meshPath is empty"))) : new Promise((s,c)=>BABYLON.SceneLoader.LoadAssetContainerAsync("", this.meshPath, this.scene, null, ".glb").then(_=>{ for (let k = _.materials.length - 1; k >= 0; --k) _.materials[k].dispose(); const b = []; for (let k = 0; k < _.meshes.length; ++k) _.meshes[k].visibility = 0, _.meshes[k].isPickable = !0, _.meshes[k].checkCollisions = !1, "hasVertexAlpha"in _.meshes[k] && (_.meshes[k].hasVertexAlpha = !1), this._mat != null && (_.meshes[k].material = this._mat), this.scene.addMesh(_.meshes[k]), b.push(new XStaticMesh({ id: this._id, mesh: _.meshes[k], xtype: EMeshType.Decal, skinInfo: this._skinInfo })); o != "" && this.cleanMesh(), this._low_model = b, this._mat != null && (GEngine.engineOption.bDisableLocalRender ? this.toggle(!1) : this.toggle(!0)), s(this) } ).catch(_=>{ logger.error("[Engine] Create decal error! " + _), c(new XDecalError("[Engine] Create decal error! " + _)) } )) } cleanMesh(o=!1, s=!1) { logger.info("[Engine] Decal Model clean mesh"); for (let c = 0; c < this._low_model.length; ++c) this._low_model[c].dispose(o, s) } }