XBillboardManager.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const texRootDir = "https://app-asset-1258211750.file.myqcloud.com/1/textures/"
  2. class XBillboardManager {
  3. constructor(e) {
  4. E(this, "billboardMap", new Map);
  5. E(this, "sceneManager");
  6. E(this, "billboardPool");
  7. E(this, "userBackGroundBlob", new Array);
  8. E(this, "npcBackGroundBlob", new Array);
  9. E(this, "tickObserver");
  10. E(this, "tickInterval");
  11. E(this, "_updateLoopObserver");
  12. this.sceneManager = e,
  13. this.billboardPool = new Pool(this.createBillboardAsset,this.resetBillboardAsset,0,60,this.sceneManager.Scene,!1),
  14. this.tickInterval = 250;
  15. let t = 0;
  16. this.tickObserver = this.sceneManager.Scene.onAfterRenderObservable.add(()=>{
  17. t += 1,
  18. t == this.tickInterval && (this.tick(),
  19. t = 0)
  20. }
  21. ),
  22. this.launchBillboardStatusLoop()
  23. }
  24. tick() {
  25. this.billboardPool.clean(0, this.sceneManager.Scene, !1)
  26. }
  27. createBillboardAsset(e, t=!1) {
  28. const r = MeshBuilder.CreatePlane("billboard-", {
  29. height: .001,
  30. width: .001,
  31. sideOrientation: Mesh.DOUBLESIDE
  32. }, e);
  33. r.isPickable = !0,
  34. r.setEnabled(!1);
  35. const n = new DynamicTexture("billboard-tex-",{
  36. width: .001 + 1,
  37. height: .001 + 1
  38. },e,t,Texture.BILINEAR_SAMPLINGMODE);
  39. n.hasAlpha = !0;
  40. const o = new StandardMaterial("billboard-mat-",e);
  41. return o.diffuseTexture = n,
  42. o.emissiveColor = new Color3(.95,.95,.95),
  43. o.useAlphaFromDiffuseTexture = !0,
  44. r.material = o,
  45. r.billboardMode = Mesh.BILLBOARDMODE_Y,
  46. r.position.y = 0,
  47. r
  48. }
  49. resetBillboardAsset(e) {
  50. const t = e.data;
  51. return t.setEnabled(!1),
  52. t.isPickable = !1,
  53. e
  54. }
  55. async loadBackGroundTexToIDB() {
  56. ge.userBubbleUrls.forEach(r=>{
  57. this.sceneManager.urlTransformer(r).then(n=>{
  58. this.userBackGroundBlob.push(n)
  59. }
  60. )
  61. }
  62. ),
  63. ge.npcBubbleUrls.forEach(r=>{
  64. this.sceneManager.urlTransformer(r).then(n=>{
  65. this.npcBackGroundBlob.push(n)
  66. }
  67. )
  68. }
  69. )
  70. }
  71. addBillboardToMap(e, t) {
  72. this.billboardMap.set(e, t)
  73. }
  74. addBillboard(e, t, r) {
  75. let n = this.getBillboard(e);
  76. return n || (n = new XBillboard(this,t,r),
  77. this.addBillboardToMap(e, n)),
  78. n
  79. }
  80. generateStaticBillboard(e, {id: t="billboard", isUser: r, background: n, font: o="Arial", fontsize: a=40, fontcolor: s="#ffffff", fontstyle: l="600", linesize: u=16, linelimit: c, scale: h=1, width: f=.01, height: d=.01, position: _={
  81. x: 0,
  82. y: 0,
  83. z: 0
  84. }}) {
  85. const g = this.addBillboard(t, !1, !0);
  86. g.getMesh() == null && g.init(t, f, d);
  87. let m;
  88. r != null && (m = r ? ge.userBubbleUrls : ge.npcBubbleUrls),
  89. g && g.getMesh() && (g.DEFAULT_CONFIGS = {
  90. id: t,
  91. isUser: r,
  92. background: n,
  93. font: o,
  94. fontsize: a,
  95. fontcolor: s,
  96. fontstyle: l,
  97. linesize: u,
  98. linelimit: c,
  99. scale: h,
  100. width: f,
  101. height: d,
  102. position: _
  103. },
  104. g.drawBillboard({
  105. imageList: n || m
  106. }, {
  107. texts: e,
  108. font: o,
  109. fontsize: a,
  110. fontcolor: s,
  111. fontstyle: l,
  112. linesize: u,
  113. linelimit: c
  114. }, {
  115. position: _,
  116. scale: h
  117. }),
  118. t && g.setId(t),
  119. g.setStatus(BillboardStatus.SHOW))
  120. }
  121. getBillboard(e) {
  122. return this.billboardMap.get(e)
  123. }
  124. toggle(e, t) {
  125. var r;
  126. (r = this.getBillboard(e)) == null || r.setStatus(t ? BillboardStatus.SHOW : BillboardStatus.HIDE)
  127. }
  128. removeBillboard(e) {
  129. const t = this.getBillboard(e);
  130. t && (t.setStatus(BillboardStatus.DISPOSE),
  131. this.billboardMap.delete(e))
  132. }
  133. launchBillboardStatusLoop() {
  134. this._updateLoopObserver = this.sceneManager.Scene.onBeforeRenderObservable.add(()=>{
  135. this.billboardMap.size <= 0 || this.billboardMap.forEach(e=>{
  136. e.stageChanged && (e.status == BillboardStatus.SHOW ? e.show() : e.status == BillboardStatus.HIDE ? e.hide() : (e.hide(),
  137. e.dispose()),
  138. e.stageChanged = !1)
  139. }
  140. )
  141. }
  142. )
  143. }
  144. }
  145. ;
  146. E(XBillboardManager, "alphaWidthMap", new Map),
  147. E(XBillboardManager, "userBubbleUrls", [texRootDir + "bubble01.png", texRootDir + "bubble02.png", texRootDir + "bubble03.png"]),
  148. E(XBillboardManager, "npcBubbleUrls", [texRootDir + "bubble01_npc.png", texRootDir + "bubble02_npc.png", texRootDir + "bubble03_npc.png"]);