XDecal.js 4.1 KB

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