XDecal.js 4.0 KB

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