XBillboard.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import EMeshType from "./enum/EMeshType.js"
  2. import XBillboardManager from "./XBillboardManager.js"
  3. import Logger from "./Logger.js"
  4. import util from "./util.js"
  5. const logger = new Logger('Billboard')
  6. export default class XBillboard {
  7. constructor(e, t=!1, r=!1) {
  8. E(this, "_mesh", null);
  9. E(this, "_texture", null);
  10. E(this, "_scalingFactor", 1);
  11. E(this, "offsets", null);
  12. E(this, "_pickable");
  13. E(this, "_background", null);
  14. E(this, "_billboardManager");
  15. E(this, "poolobj", null);
  16. E(this, "_usePool");
  17. E(this, "_initMeshScale", new BABYLON.Vector3(1,1,1));
  18. E(this, "_status", -1);
  19. E(this, "_stageChanged", !1);
  20. E(this, "DEFAULT_CONFIGS", {});
  21. this._billboardManager = e,
  22. this._pickable = t,
  23. this._usePool = r
  24. }
  25. set scalingFactor(e) {
  26. this._scalingFactor = e
  27. }
  28. set background(e) {
  29. this._background = e
  30. }
  31. get size() {
  32. return -1
  33. }
  34. setStatus(e) {
  35. e != this._status && (this._stageChanged = !0,
  36. this._status = e)
  37. }
  38. get status() {
  39. return this._status
  40. }
  41. get stageChanged() {
  42. return this._stageChanged
  43. }
  44. set stageChanged(e) {
  45. this._stageChanged = e
  46. }
  47. init(e="", t=.001, r=.001, n=!1) {
  48. const o = this._billboardManager.sceneManager.Scene;
  49. if (this._usePool) {
  50. const a = this._billboardManager.billboardPool.getFree(o, t, r, n);
  51. this._mesh = a.data,
  52. this._mesh.isPickable = this._pickable,
  53. this._mesh.xid = e,
  54. this._mesh.xtype = EMeshType.XBillboard,
  55. this._texture = this._mesh.material.diffuseTexture,
  56. this.poolobj = a
  57. } else
  58. this._mesh = this._billboardManager.createBillboardAsset(o, n);
  59. this._mesh.isPickable = this._pickable,
  60. this._initMeshScale.x = t * 1e3,
  61. this._initMeshScale.y = r * 1e3,
  62. this._mesh.xid = e,
  63. this._mesh.xtype = EMeshType.XBillboard,
  64. this._texture = this._mesh.material.diffuseTexture,
  65. this.setStatus(1),
  66. this._stageChanged = !0
  67. }
  68. dispose() {
  69. this._usePool ? this.poolobj && (this._billboardManager.billboardPool.release(this.poolobj),
  70. this._mesh = null,
  71. this._texture = null,
  72. this.poolobj = null) : this._mesh && (this._mesh.dispose(!0, !0),
  73. this._mesh = null,
  74. this._texture = null),
  75. this._background = null
  76. }
  77. getMesh() {
  78. return this._mesh
  79. }
  80. updateImage(e) {
  81. return new Promise(t=>{
  82. if (this._texture == null) {
  83. logger.error("[Engine]Billboard texture not found");
  84. return
  85. }
  86. const r = this._mesh
  87. , n = this._texture
  88. , o = this._scalingFactor
  89. , a = this._initMeshScale.x
  90. , s = this._initMeshScale.y
  91. , l = this._texture.getContext()
  92. , u = this._texture.getSize();
  93. l.clearRect(0, 0, u.width, u.height);
  94. const c = new Image;
  95. c.crossOrigin = "anonymous",
  96. c.src = e,
  97. c.onload = ()=>{
  98. const h = c.width * o
  99. , f = c.height * o;
  100. r.scaling.x = h * a,
  101. r.scaling.y = f * s,
  102. n.scaleTo(h, f),
  103. l.drawImage(c, 0, 0, h, f),
  104. n.hasAlpha = !0,
  105. n.update(),
  106. t()
  107. }
  108. }
  109. )
  110. }
  111. show() {
  112. this._mesh && (this._mesh.setEnabled(!0),
  113. this._mesh.isPickable = this._pickable)
  114. }
  115. hide() {
  116. this._mesh && (this._mesh.setEnabled(!1),
  117. this._mesh.isPickable = !1)
  118. }
  119. setId(e) {
  120. this._mesh && (this._mesh.xid = e)
  121. }
  122. setPosition(e) {
  123. if (e && this._mesh) {
  124. const t = util.ue4Position2Xverse(e);
  125. this._mesh.position = t
  126. }
  127. }
  128. updateText(e, t, r=!0, n=[], o=30, a="monospace", s="black", l="bold", u) {
  129. if (this._texture == null) {
  130. logger.error("[Engine]Billboard texture not found");
  131. return
  132. }
  133. const c = this._texture
  134. , h = this._mesh
  135. , f = this._scalingFactor
  136. , d = this._initMeshScale.x
  137. , _ = this._initMeshScale.y;
  138. if (e != "") {
  139. const g = this._texture.getContext()
  140. , m = this._texture.getSize();
  141. g.clearRect(0, 0, m.width, m.height);
  142. const v = new Image;
  143. if (r) {
  144. t != null ? t ? this._background = this._billboardManager.userBackGroundBlob : this._background = this._billboardManager.npcBackGroundBlob : this._background || (this._background = this._billboardManager.userBackGroundBlob);
  145. let y = e
  146. , b = u && u < n.length - 1 ? u : n.length - 1;
  147. if (this._background) {
  148. if (b > this._background.length) {
  149. for (let T = 0; T < b - this._background.length; T++)
  150. n.pop();
  151. b = n.length - 1,
  152. y = e.slice(0, n[b] - 1) + String.fromCharCode(8230)
  153. }
  154. v.crossOrigin = "anonymous",
  155. v.src = this._background[b - 1],
  156. v.onload = function() {
  157. const T = v.width * f
  158. , C = v.height * f;
  159. h.scaling.x = T * d,
  160. h.scaling.y = C * _,
  161. c.scaleTo(T, C),
  162. g.textAlign = "center",
  163. g.textBaseline = "middle",
  164. g.drawImage(v, 0, 0, T, C);
  165. for (let A = 0; A < b; A++)
  166. c.drawText(y.slice(n[0 + A], n[1 + A]), T / 2, C * (A + 1) / (b + 1) + (A - (b - 1) / 2) * f * 10, l + " " + o * f + "px " + a, s, "transparent", !0);
  167. c.hasAlpha = !0
  168. }
  169. }
  170. } else {
  171. const y = u && u < n.length - 1 ? u : n.length - 1
  172. , b = 480 * f
  173. , T = 60 * f * y;
  174. this._mesh.scaling = new BABYLON.Vector3(b * d,T * _,1),
  175. c.scaleTo(b, T);
  176. const C = c.getContext();
  177. C.textAlign = "center",
  178. C.textBaseline = "middle";
  179. for (let A = 0; A < y; A++)
  180. c.drawText(e.slice(n[0 + A], n[1 + A]), b / 2 + 2 * f, T * (A + 1) / (y + 1) + (A - (y - 1) / 2) * f * 10 + 2 * f, l + " " + o * f + "px " + a, "#333333", "transparent", !0),
  181. c.drawText(e.slice(n[0 + A], n[1 + A]), b / 2, T * (A + 1) / (y + 1) + (A - (y - 1) / 2) * f * 10, l + " " + o * f + "px " + a, s, "transparent", !0);
  182. c.hasAlpha = !0
  183. }
  184. } else
  185. this.clearText()
  186. }
  187. drawBillboard(e, t, r) {
  188. var m;
  189. const {imageList: n} = e
  190. , {texts: o, font: a="monospace", fontsize: s=40, fontcolor: l="#ffffff", fontstyle: u="", linesize: c=20, linelimit: h} = t
  191. , {position: f, offsets: d, scale: _, compensationZ: g=0} = r;
  192. if (this.scalingFactor = _ || 1,
  193. d && (this.offsets = {
  194. x: d.x * this._scalingFactor,
  195. y: d.y * this._scalingFactor,
  196. z: d.z * this._scalingFactor
  197. }),
  198. this.offsets || (this.offsets = {
  199. x: 0,
  200. y: 0,
  201. z: 0
  202. }),
  203. this.setPosition(f),
  204. n && !o)
  205. (m = this._billboardManager.sceneManager) == null || m.urlTransformer(n[0]).then(v=>{
  206. this.updateImage(v)
  207. }
  208. );
  209. else if (o && !n) {
  210. const [v,y] = getStringBoundaries(o, c, XBillboardManager.alphaWidthMap);
  211. this.offsets.z += this._scalingFactor * g * (y.length - 1),
  212. this.updateText(v, void 0, !1, y, s, a, l, u, h)
  213. } else if (o && n) {
  214. this.background = n;
  215. const [v,y] = getStringBoundaries(o, c, XBillboardManager.alphaWidthMap);
  216. this.offsets.z += this._scalingFactor * g * (y.length - 1),
  217. this.updateText(v, void 0, !0, y, s, a, l, u, h)
  218. }
  219. this.setStatus(1)
  220. }
  221. clearText() {
  222. if (this._texture != null) {
  223. const e = this._texture.getContext()
  224. , t = this._texture.getSize();
  225. e.clearRect(0, 0, t.width, t.height),
  226. this._texture.update()
  227. }
  228. }
  229. }