XDecalManager.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. class XDecalManager {
  2. constructor(e) {
  3. E(this, "scene");
  4. E(this, "_decal");
  5. E(this, "_mat");
  6. E(this, "_sharedMat");
  7. E(this, "_scenemanager");
  8. this._decal = new Map,
  9. this._mat = new Map,
  10. this._sharedMat = new Map,
  11. this._scenemanager = e,
  12. this.scene = e.Scene
  13. }
  14. get decals() {
  15. return Array.from(this._decal.values())
  16. }
  17. getMesh() {
  18. return this._decal
  19. }
  20. async addDecal(e) {
  21. const {id: t, meshPath: r, skinInfo: n="default"} = e;
  22. return this._decal.get(t) ? (log$x.warn(`[Engine] Cannot add decal with an existing id: [${t}], meshPath: ${r}, skinInfo:${n}`),
  23. Promise.resolve(!0)) : (log$x.info(`[Engine] addDecal wiht id:[${t}], meshPath: ${r}, skinInfo:${n}`),
  24. new Promise((o,a)=>this._scenemanager.urlTransformer(r).then(s=>new Promise((l,u)=>{
  25. if (this._decal.get(t))
  26. l(!0);
  27. else {
  28. const c = new XDecal({
  29. id: t,
  30. scene: this.scene,
  31. meshPath: s,
  32. skinInfo: n
  33. });
  34. this._decal.set(t, c),
  35. c.loadModel().then(()=>{
  36. l(!0)
  37. }
  38. ).catch(h=>{
  39. log$x.error(`[Engine] addDecal Error! id: [${t}], meshpath:${r}, skin: ${n}. ${h}`),
  40. u(new XDecalError(`[Engine] addDecal Error! id: [${t}], meshpath:${r}, skin: ${n}. ${h}`))
  41. }
  42. )
  43. }
  44. }
  45. )).then(s=>{
  46. s == !0 ? o(!0) : a(!1)
  47. }
  48. ).catch(s=>{
  49. log$x.error(`[Engine] Add Decal error! id: [${t}], meshpath:${r}, skin:${n}. ${s}`),
  50. a(new XDecalError(`[Engine] addDecal error! id: [${t}], meshpath:${r}, skin:${n}. ${s}`))
  51. }
  52. )))
  53. }
  54. setDecalTexture(e) {
  55. const {id: t, buffer: r, isDynamic: n=!1, width: o=1100, height: a=25, slots: s=1, visibleSlots: l=1} = e
  56. , u = !0;
  57. return log$x.info(`[Engine] setDecalTexture wiht id:[${t}]`),
  58. new Promise((c,h)=>{
  59. const f = this._decal.get(t);
  60. if (f != null)
  61. if (this._mat.get(t) != null)
  62. this.changeDecalTexture({
  63. id: t,
  64. buffer: r,
  65. isUrl: u,
  66. isDynamic: n,
  67. width: o,
  68. height: a,
  69. slots: s,
  70. visibleSlots: l
  71. }),
  72. c(!0);
  73. else {
  74. const d = new XDecalMaterial(t,this.scene);
  75. d.setTexture(r, u, n, o, a, s, l).then(()=>{
  76. f.setMat(d.getMat()),
  77. this._decal.set(t, f),
  78. this._mat.set(t, d),
  79. c(!0)
  80. }
  81. ).catch(_=>{
  82. log$x.error("[Engine] setDecalTexture Error! " + _),
  83. h(new XDecalTextureError(`[Engine] decal set texture error! ${_}`))
  84. }
  85. )
  86. }
  87. else
  88. log$x.error("[Engine] Error! decal id: [" + t + "] is not find!"),
  89. h(new XDecalTextureError(`[Engine] decal id: [${t}] is not find!`))
  90. }
  91. )
  92. }
  93. async shareDecal(e) {
  94. const {idTar: t, meshPath: r, idSrc: n, skinInfo: o="default"} = e;
  95. return this._decal.has(n) && !this._decal.has(t) && this._mat.has(n) && !this._mat.has(t) ? (log$x.info(`[Engine] shareDecal wiht idTar:[${t}], idSrc:[${n}], skinInfo: ${o}, meshPath: ${r}`),
  96. new Promise((a,s)=>this._scenemanager.urlTransformer(r).then(l=>{
  97. const u = new XDecal({
  98. id: t,
  99. scene: this.scene,
  100. meshPath: l,
  101. skinInfo: o
  102. })
  103. , c = this._mat.get(n);
  104. c != null && (u.setMat(c.getMat()),
  105. u.sourceMatId = n,
  106. this._decal.set(t, u),
  107. this.addSharedMatCount(n)),
  108. a(!0)
  109. }
  110. ).catch(l=>{
  111. s(new XDecalError(`[Engine] decal shareDecal error! ${l}`))
  112. }
  113. ))) : (log$x.error(`[Engine] shareDecal Error. idSrc: [${n}] not exist! or idTar: [${t}] exists!`),
  114. Promise.reject(`[Engine] shareDecal Error. idSrc: [${n}] not exist! or idTar: [${t}] exists!`))
  115. }
  116. async changeDecalModel(e) {
  117. const {id: t, meshPath: r} = e
  118. , n = this._decal.get(t);
  119. return new Promise((o,a)=>n != null ? (log$x.info(`[Engine] changeDecalModel id:${t}`),
  120. n.changeModel(r).then(()=>{
  121. this._decal.set(t, n),
  122. o(!0)
  123. }
  124. )) : (log$x.warn(`[Engine] changeDecalModel id:${t} is not exist`),
  125. a(`[Engine] changeDecalModel id:${t} is not exist`)))
  126. }
  127. changeDecalTexture(e) {
  128. const {id: t, buffer: r, isUrl: n=!1, isDynamic: o=!1, width: a=1110, height: s=25, slots: l=1, visibleSlots: u=1} = e
  129. , c = this._mat.get(t);
  130. c != null && this._decal.has(t) ? (c.changeTexture(r, n, o, a, s, l, u),
  131. this._mat.set(t, c)) : log$x.error(`[Engine] changeDecalTexture Error. id:${t} is not exist`)
  132. }
  133. deleteDecal(e) {
  134. var t, r;
  135. if (this._decal.has(e)) {
  136. const n = this._decal.get(e);
  137. n != null && n.cleanMesh(),
  138. this._sharedMat.get(e) != null ? this.minusSharedMatCount(e) : this._mat.get(e) != null ? ((t = this._mat.get(e)) == null || t.cleanTexture(),
  139. this._mat.delete(e)) : ((r = n.sourceMatId) == null ? void 0 : r.length) > 0 && this.minusSharedMatCount(n.sourceMatId),
  140. this._decal.delete(e)
  141. }
  142. }
  143. deleteDecalBySkinInfo(e) {
  144. for (const [t,r] of this._decal.entries())
  145. r.skinInfo == e && this.deleteDecal(t)
  146. }
  147. addSharedMatCount(e) {
  148. const t = this._sharedMat.get(e);
  149. t != null ? this._sharedMat.set(e, t + 1) : this._sharedMat.set(e, 1)
  150. }
  151. minusSharedMatCount(e) {
  152. var r;
  153. const t = this._sharedMat.get(e);
  154. t != null && (this._sharedMat.set(e, t - 1),
  155. t == 0 && (this._sharedMat.delete(e),
  156. (r = this._mat.get(e)) == null || r.cleanTexture(),
  157. this._mat.delete(e)))
  158. }
  159. toggle(e, t) {
  160. const r = this._decal.get(e);
  161. r == null || r.toggle(t)
  162. }
  163. toggleDecalBySkinInfo(e, t) {
  164. for (const [r,n] of this._decal.entries())
  165. n.skinInfo == e && n.toggle(t)
  166. }
  167. updateTexAsWords(e, t, r={}) {
  168. const {clearArea: n=!0, w: o=480, h: a=480, y: s=a / 2, fontsize: l=70, slots: u=1, visibleSlots: c=1, font: h="black-body", color: f="white", fontweight: d=100} = r;
  169. let {x: _=o / 2} = r;
  170. const g = this._mat.get(e);
  171. if (g) {
  172. _ == -1 && (_ = (g.getUOffset() + c / u) % 1 * o * u);
  173. const v = g.getMat().diffuseTexture
  174. , y = v.getContext();
  175. n && y.clearRect(_ - o / 2, s - a / 2, o, a),
  176. y.textAlign = "center",
  177. y.textBaseline = "middle",
  178. v.drawText(t, _, s, d + " " + l + "px " + h, f, "transparent", !0),
  179. v.hasAlpha = !0,
  180. v.update()
  181. }
  182. }
  183. async updateTexAsImg(e, t, r={}) {
  184. const {clearArea: n=!0, w: o=480, h: a=480, x: s=o / 2, y: l=a / 2, clearW: u=o, clearH: c=a} = r;
  185. return t == null || t == null || t == "" ? (log$x.error(`[Engine] updateTexAsImg Error. id: [${e}], newBuffer is Null or ""!`),
  186. Promise.reject(new XDecalError(`[Engine] updateTexAsImg Error. id: [${e}], newBuffer is Null or ""!`))) : new Promise((h,f)=>this._scenemanager.urlTransformer(t).then(d=>new Promise((_,g)=>{
  187. const m = this._mat.get(e);
  188. if (m) {
  189. const y = m.getMat().diffuseTexture;
  190. if (typeof t == "string") {
  191. const b = new Image;
  192. b.crossOrigin = "anonymous",
  193. b.src = d,
  194. b.onload = ()=>{
  195. const T = y.getContext();
  196. n && T.clearRect(s - u / 2, l - c / 2, u, c),
  197. T.drawImage(b, s - o / 2, l - a / 2, o, a),
  198. y.update(),
  199. _(!0)
  200. }
  201. ,
  202. b.onerror = ()=>{
  203. log$x.error(`[Engine] updateTexAsImg Error.newImg load error. id: [${e}], decalMat is Null or undefined!`),
  204. g(new XDecalError(`[Engine] updateTexAsImg Error. id: [${e}], decalMat is Null or undefined!`))
  205. }
  206. } else
  207. log$x.error(`[Engine] updateTexAsImg Error. id: [${e}], Buffer is not string!`),
  208. g(new XDecalError(`[Engine] updateTexAsImg Error. id: [${e}], Buffer is not string!`))
  209. } else
  210. log$x.error(`[Engine] updateTexAsImg Error. id: [${e}], decalMat is Null or undefined!`),
  211. g(new XDecalError(`[Engine] updateTexAsImg Error. id: [${e}], decalMat is Null or undefined!`))
  212. }
  213. ).then(_=>{
  214. _ == !0 ? h(!0) : (log$x.error(`[Engine] updateTexAsImg Error. id: [${e}] !`),
  215. f(new XDecalError(`[Engine] updateTexAsImg error! id: [${e}]`)))
  216. }
  217. ).catch(_=>{
  218. log$x.error(`[Engine] updateTexAsImg Error. id: [${e}]. ${_}`)
  219. }
  220. )))
  221. }
  222. startAnime(e, t) {
  223. log$x.info(`[Engine] Decal Start Anime. [${e}]`);
  224. const {speed: r=.001, callback: n} = t
  225. , o = this._mat.get(e);
  226. o ? (o.do_animation(r),
  227. n && o.uOffsetObserverable.add(n)) : (log$x.error(`[Engine] startAnime Error. id: [${e}] is not exist!`),
  228. new XDecalError(`[Engine] startAnime Error. id: [${e}] is not exist!`))
  229. }
  230. }