XBillboardManager.js 5.0 KB

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