XDecalMaterial.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import XDecalTextureError from "./error/XDecalTextureError"
  2. export default class XDecalMaterial {
  3. constructor(e, t) {
  4. E(this, "_id");
  5. E(this, "_tex");
  6. E(this, "scene");
  7. E(this, "_mat");
  8. E(this, "_speed", .001);
  9. E(this, "_slots", 1);
  10. E(this, "_visibleSlots", 1);
  11. E(this, "_isRegisterAnimation");
  12. E(this, "_animeObserver", null);
  13. E(this, "_uOffsetObserverable");
  14. E(this, "reg_mat_update", ()=>{
  15. const e = this._mat.diffuseTexture;
  16. e != null && (e.uOffset = e.uOffset + this._speed,
  17. e.uOffset > 1 && (e.uOffset -= 1),
  18. Math.round(e.uOffset % (1 / this._slots) / this._speed) == 0 && this._uOffsetObserverable.notifyObservers(this))
  19. }
  20. );
  21. E(this, "setTexture", async(e,t=!0,r=!1,n=1,o=1,a=1,s=1)=>new Promise((l,u)=>{
  22. this._slots = a,
  23. this._visibleSlots = s;
  24. const c = this._tex;
  25. r ? (this._tex = new BABYLON.DynamicTexture("dyTex",{
  26. width: n,
  27. height: o
  28. },this.scene,!0,BABYLON.Texture.BILINEAR_SAMPLINGMODE),
  29. this._tex.name = "decal_dy_" + this._id,
  30. this._tex.uScale = s / a,
  31. this._tex.vScale = -1,
  32. this._tex.vOffset = 1,
  33. this._tex.wrapU = 1,
  34. this._mat.emissiveColor = new BABYLON.Color3(.95,.95,.95),
  35. this._mat.diffuseTexture = this._tex,
  36. this._mat.diffuseTexture.hasAlpha = !0,
  37. this._mat.useAlphaFromDiffuseTexture = !0,
  38. this._mat.backFaceCulling = !1,
  39. this._mat.transparencyMode = BABYLON.Material.MATERIAL_ALPHATEST,
  40. c != null && c.dispose(),
  41. l(!0)) : !r && t && typeof e == "string" ? this._tex = new BABYLON.Texture(e,this.scene,!0,!1,BABYLON.Texture.BILINEAR_SAMPLINGMODE,()=>{
  42. this._tex.name = "decal_" + this._id,
  43. this._mat.emissiveTexture = this._tex,
  44. this._mat.diffuseTexture = this._tex,
  45. this._mat.diffuseTexture.hasAlpha = !0,
  46. this._mat.useAlphaFromDiffuseTexture = !0,
  47. this._mat.transparencyMode = BABYLON.Material.MATERIAL_ALPHATEST,
  48. c != null && c.dispose(),
  49. l(!0)
  50. }
  51. ,()=>{
  52. logger.error("[Engine] decal create texture error!"),
  53. u(new XDecalTextureError("[Engine] decal create texture error!"))
  54. }
  55. ,null,!0) : this._tex = new BABYLON.Texture("data:decal_" + this._id,this.scene,!0,!1,BABYLON.Texture.BILINEAR_SAMPLINGMODE,()=>{
  56. this._tex.name = "decal_" + this._id,
  57. this._mat.emissiveTexture = this._tex,
  58. this._mat.diffuseTexture = this._tex,
  59. this._mat.diffuseTexture.hasAlpha = !0,
  60. this._mat.useAlphaFromDiffuseTexture = !0,
  61. this._mat.transparencyMode = Material.MATERIAL_ALPHATEST,
  62. c != null && c.dispose(),
  63. l(!0)
  64. }
  65. ,()=>{
  66. logger.error("[Engine] decal create texture error!"),
  67. u(new XDecalTextureError("[Engine] decal create texture error!"))
  68. }
  69. ,e,!0)
  70. }
  71. ));
  72. this._id = e,
  73. this.scene = t,
  74. this._mat = new BABYLON.StandardMaterial("decalMat_" + this._id,this.scene),
  75. this._isRegisterAnimation = !1,
  76. this._uOffsetObserverable = new Observable
  77. }
  78. get uOffsetObserverable() {
  79. return this._uOffsetObserverable
  80. }
  81. getMat() {
  82. return this._mat
  83. }
  84. set speed(e) {
  85. this._speed = e
  86. }
  87. getUOffset() {
  88. return this._tex.uOffset
  89. }
  90. do_animation(e) {
  91. this._speed = e,
  92. this._isRegisterAnimation == !1 && (this._isRegisterAnimation = !0,
  93. this._animeObserver = this.scene.onBeforeRenderObservable.add(()=>{
  94. this.reg_mat_update()
  95. }
  96. ))
  97. }
  98. changeTexture(e, t=!1, r=!1, n=1, o=1, a=1, s=1) {
  99. return this._mat == null || this._tex == null ? (logger.error("[Engine] Decal Mat is null or tex is null"),
  100. Promise.reject(new XDecalTextureError("[Engine] Decal Mat is null or tex is null"))) : this.setTexture(e, t, r, n, o, a, s)
  101. }
  102. cleanTexture() {
  103. logger.info("[Engine] Decal clean Texture"),
  104. this.scene.onBeforeRenderObservable.remove(this._animeObserver),
  105. this._uOffsetObserverable.clear(),
  106. this._tex.dispose(),
  107. this._mat.dispose()
  108. }
  109. }