XDecal.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. class XDecal {
  2. constructor(e) {
  3. E(this, "_id");
  4. E(this, "meshPath");
  5. E(this, "_low_model", []);
  6. E(this, "_mat", null);
  7. E(this, "scene");
  8. E(this, "_skinInfo");
  9. E(this, "sourceMatId", "");
  10. E(this, "loadModel", async()=>new Promise((e,t)=>{
  11. typeof this.meshPath == "string" ? SceneLoader.LoadAssetContainerAsync("", this.meshPath, this.scene, null, ".glb").then(r=>{
  12. for (let n = r.materials.length - 1; n >= 0; --n)
  13. r.materials[n].dispose();
  14. for (let n = 0; n < r.meshes.length; ++n)
  15. r.meshes[n].visibility = 1,
  16. r.meshes[n].isPickable = !0,
  17. r.meshes[n].checkCollisions = !1,
  18. "hasVertexAlpha"in r.meshes[n] && (r.meshes[n].hasVertexAlpha = !1),
  19. this.scene.addMesh(r.meshes[n]),
  20. this._low_model.push(new XStaticMesh({
  21. id: this._id,
  22. mesh: r.meshes[n],
  23. xtype: EMeshType.Decal,
  24. skinInfo: this._skinInfo
  25. })),
  26. this.toggle(!1);
  27. e(!0)
  28. }
  29. ).catch(r=>{
  30. t(new XDecalError(`[Engine] decal load model error! ${r}`))
  31. }
  32. ) : t(new XDecalError("[Engine] decal inport mesh is not string!"))
  33. }
  34. ).catch(e=>{
  35. new XDecalError(`[Engine] decal loadModel ${e}`)
  36. }
  37. ));
  38. const {id: t, scene: r, meshPath: n, skinInfo: o="default"} = e;
  39. this._id = t,
  40. this.scene = r,
  41. this.meshPath = n,
  42. this._skinInfo = o
  43. }
  44. get skinInfo() {
  45. return this._skinInfo
  46. }
  47. getMesh() {
  48. return this._low_model
  49. }
  50. getMat() {
  51. return this._mat
  52. }
  53. get id() {
  54. return this._id
  55. }
  56. toggle(e) {
  57. for (let t = 0; t < this._low_model.length; ++t)
  58. e == !0 ? this._low_model[t].show() : this._low_model[t].hide()
  59. }
  60. setMat(e) {
  61. this._mat = e;
  62. for (let t = 0; t < this._low_model.length; ++t)
  63. this._low_model[t].mesh.material = this._mat;
  64. this.toggle(!0)
  65. }
  66. changeModel(e="") {
  67. return e != "" && (this.meshPath = e),
  68. this.meshPath == "" ? (log$x.error("[Engine] changeModel Error! meshPath is empty"),
  69. Promise.reject(new XDecalTextureError("[Engine] changeModel Error! meshPath is empty"))) : new Promise((t,r)=>SceneLoader.LoadAssetContainerAsync("", this.meshPath, this.scene, null, ".glb").then(n=>{
  70. for (let a = n.materials.length - 1; a >= 0; --a)
  71. n.materials[a].dispose();
  72. const o = [];
  73. for (let a = 0; a < n.meshes.length; ++a)
  74. n.meshes[a].visibility = 0,
  75. n.meshes[a].isPickable = !0,
  76. n.meshes[a].checkCollisions = !1,
  77. "hasVertexAlpha"in n.meshes[a] && (n.meshes[a].hasVertexAlpha = !1),
  78. this._mat != null && (n.meshes[a].material = this._mat),
  79. this.scene.addMesh(n.meshes[a]),
  80. o.push(new XStaticMesh({
  81. id: this._id,
  82. mesh: n.meshes[a],
  83. xtype: EMeshType.Decal,
  84. skinInfo: this._skinInfo
  85. }));
  86. e != "" && this.cleanMesh(),
  87. this._low_model = o,
  88. this._mat != null && this.toggle(!0),
  89. t(this)
  90. }
  91. ).catch(n=>{
  92. log$x.error("[Engine] Create decal error! " + n),
  93. r(new XDecalError("[Engine] Create decal error! " + n))
  94. }
  95. ))
  96. }
  97. cleanMesh(e=!1, t=!1) {
  98. log$x.info("[Engine] Decal Model clean mesh");
  99. for (let r = 0; r < this._low_model.length; ++r)
  100. this._low_model[r].dispose(e, t)
  101. }
  102. }