XAvatarBillboardComponent.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. const log$F = new Logger$1("XAvatarBillboardComponent");
  2. class XAvatarBillboardComponent {
  3. constructor(e) {
  4. E(this, "_nickName", "");
  5. E(this, "_words", "");
  6. E(this, "_isNameVisible", !0);
  7. E(this, "_isBubbleVisible", !0);
  8. E(this, "_isGiftButtonsVisible", !1);
  9. E(this, "withinVisualRange", !1);
  10. E(this, "_bubble");
  11. E(this, "_nameBoard");
  12. E(this, "_giftButtons", new Map);
  13. E(this, "_buttonTex", new Map);
  14. E(this, "_nameLinesLimit", 2);
  15. E(this, "_nameLengthPerLine", 16);
  16. E(this, "_scene");
  17. E(this, "_pickBbox", null);
  18. E(this, "bbox");
  19. E(this, "_height", .26);
  20. E(this, "_attachmentObservers", new Map);
  21. E(this, "attachToAvatar", (e,t,r=!1,n={
  22. x: 0,
  23. y: 0,
  24. z: 0
  25. },o=!1,a,s)=>{
  26. const l = e.rootNode;
  27. if (this.bbox || e.getBbox(),
  28. t && l) {
  29. const u = a || t.uniqueId;
  30. let c = this._attachmentObservers.get(u);
  31. if (c)
  32. if (o)
  33. this._scene.onBeforeRenderObservable.remove(c),
  34. this._attachmentObservers.delete(u);
  35. else
  36. return;
  37. const h = ue4Position2Xverse(n);
  38. r ? (t.setParent(l),
  39. t.position = h) : (c = this._scene.onBeforeRenderObservable.add(()=>{
  40. let f = 0;
  41. s ? (f = e.rootNode.rotation.y / Math.PI * 180 + 90,
  42. e.rootNode.rotation.y && (t.rotation.y = e.rootNode.rotation.y)) : f = e.avatarManager.sceneManager.cameraComponent.getCameraPose().rotation.yaw,
  43. f || (f = 0);
  44. const d = new Vector3(0,this._height,0);
  45. e.controller && e.controller.activeAnimation() && e.controller.activeAnimation().animatables[0] && (this._height = d.y = (e.controller.activeAnimation().animatables[0].target.position.y * .01 - .66) * e.scale),
  46. d.y < .07 * e.scale && (d.y = 0),
  47. t.position.x = l.position.x + h.x * Math.sin(f * Math.PI / 180) + h.z * Math.cos(f * Math.PI / 180),
  48. t.position.z = l.position.z + h.x * Math.cos(f * Math.PI / 180) - h.z * Math.sin(f * Math.PI / 180),
  49. t.position.y = l.position.y + this.bbox.maximum.y + h.y + d.y
  50. }
  51. ),
  52. this._attachmentObservers.set(u, c))
  53. } else
  54. log$F.error("avatar or attachment not found!")
  55. }
  56. );
  57. E(this, "detachFromAvatar", (e,t,r=!1)=>{
  58. const n = this._attachmentObservers.get(t.uniqueId);
  59. n && this._scene.onBeforeRenderObservable.remove(n),
  60. e.rootNode ? (t.setEnabled(!1),
  61. t.parent = null,
  62. r && t.dispose()) : log$F.error("avatar not found!")
  63. }
  64. );
  65. E(this, "getBbox", (e,t={})=>{
  66. const {isConst: r=!1, changeWithAvatar: n=!1} = t;
  67. let {localCenter: o={
  68. x: 0,
  69. y: 0,
  70. z: 75
  71. }, width: a=1.32, height: s=1.5, depth: l=.44} = t;
  72. if (n) {
  73. const u = e.scale;
  74. o = {
  75. x: o.x * u,
  76. y: o.y * u,
  77. z: o.z * u
  78. },
  79. a *= u,
  80. s *= u,
  81. l *= u
  82. }
  83. if (e.rootNode) {
  84. let u = new Vector3(0,0,0)
  85. , c = new Vector3(0,0,0);
  86. if (r) {
  87. const f = ue4Position2Xverse(o);
  88. u = u.add(f.add(new Vector3(-a / 2,-s / 2,-l / 2))),
  89. c = c.add(f.add(new Vector3(a / 2,s / 2,l / 2)))
  90. } else if (u = u.add(new Vector3(Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY)),
  91. c = c.add(new Vector3(Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY)),
  92. e.isRender) {
  93. e.rootNode.getChildMeshes().forEach(_=>{
  94. const g = _.getBoundingInfo().boundingBox.minimum
  95. , m = _.getBoundingInfo().boundingBox.maximum;
  96. u.x = Math.min(u.x, g.x),
  97. c.x = Math.max(c.x, m.x),
  98. u.y = Math.min(u.y, g.y),
  99. c.y = Math.max(c.y, m.y),
  100. u.z = Math.min(u.z, g.z),
  101. c.z = Math.max(c.z, m.z)
  102. }
  103. );
  104. const f = c.x - u.x
  105. , d = c.z - u.z;
  106. u.x -= e.scale * f / 2,
  107. c.x += e.scale * f / 2,
  108. c.y *= e.scale,
  109. u.z -= e.scale * d / 2,
  110. c.z += e.scale * d / 2
  111. } else {
  112. const f = e.avatarManager.getMainAvatar();
  113. f && f.bbComponent.bbox && (u.x = f.bbComponent.bbox.minimum.x,
  114. c.x = f.bbComponent.bbox.maximum.x,
  115. u.y = f.bbComponent.bbox.minimum.y,
  116. c.y = f.bbComponent.bbox.maximum.y,
  117. u.z = f.bbComponent.bbox.minimum.z,
  118. c.z = f.bbComponent.bbox.maximum.z)
  119. }
  120. const h = e.rootNode.computeWorldMatrix(!0);
  121. if (this.bbox ? this.bbox.reConstruct(u, c, h) : this.bbox = new BoundingBox(u,c,h),
  122. this._pickBbox == null) {
  123. const f = this.createPickBoundingbox(e, this.bbox);
  124. this.attachToAvatar(e, f.data, !1, {
  125. x: 0,
  126. y: 0,
  127. z: 0
  128. }, !1, "pickbox"),
  129. this._pickBbox = f
  130. }
  131. } else
  132. log$F.error("avatar not found!")
  133. }
  134. );
  135. this._scene = e
  136. }
  137. get isNameVisible() {
  138. return this._isNameVisible
  139. }
  140. get isBubbleVisible() {
  141. return this._isBubbleVisible
  142. }
  143. get isGiftButtonsVisible() {
  144. return this._isGiftButtonsVisible
  145. }
  146. get words() {
  147. return this._words
  148. }
  149. get nickName() {
  150. return this._nickName
  151. }
  152. get giftButtons() {
  153. return this._giftButtons
  154. }
  155. get bubble() {
  156. return this._bubble
  157. }
  158. get nameBoard() {
  159. return this._nameBoard
  160. }
  161. setNicknameStatus(e) {
  162. if (this.nameBoard && this.nameBoard.setStatus(e),
  163. e == BillboardStatus.DISPOSE) {
  164. const t = this._attachmentObservers.get("nickname");
  165. t && (this._scene.onBeforeRenderObservable.remove(t),
  166. this._attachmentObservers.delete("nickname"))
  167. }
  168. }
  169. setBubbleStatus(e) {
  170. if (this.bubble && this.bubble.setStatus(e),
  171. e == BillboardStatus.DISPOSE) {
  172. const t = this._attachmentObservers.get("bubble");
  173. t && (this._scene.onBeforeRenderObservable.remove(t),
  174. this._attachmentObservers.delete("bubble"))
  175. }
  176. }
  177. setButtonsStatus(e) {
  178. this.giftButtons && this.giftButtons.size != 0 && this.giftButtons.forEach(t=>{
  179. if (t.setStatus(e),
  180. e == BillboardStatus.DISPOSE && t.getMesh()) {
  181. const r = "button_" + t.getMesh().xid
  182. , n = this._attachmentObservers.get(r);
  183. n && (this._scene.onBeforeRenderObservable.remove(n),
  184. this._attachmentObservers.delete(r))
  185. }
  186. }
  187. )
  188. }
  189. setGiftButtonsVisible(e) {
  190. this.setButtonsStatus(e ? BillboardStatus.SHOW : BillboardStatus.DISPOSE)
  191. }
  192. dispose(e) {
  193. this._attachmentObservers.forEach(t=>{
  194. this._scene.onBeforeRenderObservable.remove(t)
  195. }
  196. ),
  197. this._attachmentObservers.clear(),
  198. this.updateBillboardStatus(e, BillboardStatus.DISPOSE),
  199. this._buttonTex.clear(),
  200. this._pickBbox && (e.avatarManager.bboxMeshPool.release(this._pickBbox),
  201. this._pickBbox = null)
  202. }
  203. updateBillboardStatus(e, t) {
  204. this.bbox || e.getBbox(),
  205. e.isRender ? (e.setBubbleStatus(t),
  206. e.setButtonsStatus(t),
  207. e.setNicknameStatus(t)) : (e.setBubbleStatus(BillboardStatus.DISPOSE),
  208. e.setButtonsStatus(BillboardStatus.DISPOSE),
  209. e.enableNickname ? e.setNicknameStatus(t) : e.setNicknameStatus(BillboardStatus.DISPOSE))
  210. }
  211. disposeBillBoard(e) {
  212. this._attachmentObservers.forEach(t=>{
  213. this._scene.onBeforeRenderObservable.remove(t)
  214. }
  215. ),
  216. this._attachmentObservers.clear(),
  217. this.updateBillboardStatus(e, BillboardStatus.DISPOSE),
  218. this._buttonTex.clear(),
  219. this._pickBbox && (e.avatarManager.bboxMeshPool.release(this._pickBbox),
  220. this._pickBbox = null)
  221. }
  222. setPickBoxScale(e) {
  223. this._pickBbox && this._pickBbox.data && (this._pickBbox.data.scaling = new Vector3(e,e,e))
  224. }
  225. setIsPickable(e, t) {
  226. e.rootNode && e.rootNode.getChildMeshes().forEach(r=>{
  227. r.isPickable = t
  228. }
  229. ),
  230. this._pickBbox && this._pickBbox.data && (this._pickBbox.data.isPickable = t)
  231. }
  232. initNameboard(e, t=1) {
  233. this._nameBoard == null && (this._nameBoard = e.avatarManager.sceneManager.billboardComponent.addBillboard("name-" + e.id, !1, !0)),
  234. this._nameBoard.init("nickname", t / 300, t / 300)
  235. }
  236. initBubble(e, t=1) {
  237. this._bubble == null && (this._bubble = e.avatarManager.sceneManager.billboardComponent.addBillboard("bubble-" + e.id, !1, !0)),
  238. e.isRender && this._bubble.init("bubble", t / 250, t / 250)
  239. }
  240. say(e, t=this._words, {id: r, isUser: n, background: o, font: a="Arial", fontsize: s=38, fontcolor: l="#ffffff", fontstyle: u="bold", linesize: c=22, linelimit: h, offsets: f={
  241. x: 0,
  242. y: 0,
  243. z: 40
  244. }, scale: d, compensationZ: _=11.2, reregistAnyway: g=!0}) {
  245. (!this.bubble || this.bubble.getMesh() == null) && e.initBubble(),
  246. this._words = t;
  247. let m;
  248. n != null && (m = n ? XBillboardManager.userBubbleUrls : XBillboardManager.npcBubbleUrls),
  249. this._bubble && (this._bubble.DEFAULT_CONFIGS = {
  250. id: r,
  251. isUser: n,
  252. background: o || m,
  253. font: a,
  254. fontsize: s,
  255. fontcolor: l,
  256. fontstyle: u,
  257. linesize: c,
  258. linelimit: h,
  259. offsets: f,
  260. scale: d,
  261. compensationZ: _,
  262. reregistAnyway: g
  263. },
  264. this._bubble.getMesh() && (this._bubble.drawBillboard({
  265. imageList: o || m
  266. }, {
  267. texts: this._words,
  268. font: a,
  269. fontsize: s,
  270. fontcolor: l,
  271. fontstyle: u,
  272. linesize: c
  273. }, {
  274. offsets: f,
  275. scale: d,
  276. compensationZ: _
  277. }),
  278. this.attachToAvatar(e, this._bubble.getMesh(), !1, this._bubble.offsets, g, "bubble"),
  279. r && this._bubble.setId(r))),
  280. this.setButtonsStatus(BillboardStatus.DISPOSE)
  281. }
  282. silent() {
  283. this.setBubbleStatus(BillboardStatus.DISPOSE),
  284. this._words = ""
  285. }
  286. setNickName(e, t, {id: r, isUser: n, background: o, font: a="Arial", fontsize: s=40, fontcolor: l="#ffffff", fontstyle: u="bold", linesize: c=22, linelimit: h, offsets: f={
  287. x: 0,
  288. y: 0,
  289. z: 15
  290. }, scale: d, compensationZ: _=0, reregistAnyway: g=!1}) {
  291. this._nickName = t,
  292. (!this.nameBoard || this.nameBoard.getMesh() == null) && this.initNameboard(e),
  293. this._nameBoard && this._nameBoard.getMesh() && (this._nameBoard.DEFAULT_CONFIGS = {
  294. id: r,
  295. isUser: n,
  296. background: o,
  297. font: a,
  298. fontsize: s,
  299. fontcolor: l,
  300. fontstyle: u,
  301. linesize: c,
  302. linelimit: h,
  303. offsets: f,
  304. scale: d,
  305. compensationZ: _,
  306. reregistAnyway: g
  307. },
  308. this._nameBoard.drawBillboard({}, {
  309. texts: this._nickName,
  310. font: a,
  311. fontsize: s,
  312. fontcolor: l,
  313. fontstyle: u,
  314. linesize: c,
  315. linelimit: h
  316. }, {
  317. offsets: f,
  318. scale: d,
  319. compensationZ: 0
  320. }),
  321. this.attachToAvatar(e, this._nameBoard.getMesh(), !1, this._nameBoard.offsets, g, "nickname"),
  322. r && this._nameBoard.setId(r))
  323. }
  324. generateButtons(e, t=null, r, n=85) {
  325. if (t && (this._buttonTex = t,
  326. this.clearButtons()),
  327. this._buttonTex.size == 0)
  328. return;
  329. let o = (this._buttonTex.size - 1) / 2;
  330. this._buttonTex.forEach((a,s)=>{
  331. let l = this._giftButtons.get(s);
  332. l || (l = e.avatarManager.sceneManager.billboardComponent.addBillboard("button-" + s + e.id, !0, !1),
  333. l.init(s, r / 240, r / 240));
  334. const u = {
  335. x: r * o * 70,
  336. y: 0,
  337. z: r * (n - 20 * (o * o))
  338. };
  339. l.drawBillboard({
  340. imageList: [a]
  341. }, {}, {
  342. offsets: u,
  343. scale: r
  344. }),
  345. this.attachToAvatar(e, l.getMesh(), !1, l.offsets, !0, "button_" + s),
  346. this._giftButtons.set(s, l),
  347. o -= 1
  348. }
  349. ),
  350. this.setBubbleStatus(BillboardStatus.DISPOSE)
  351. }
  352. clearButtons() {
  353. this._giftButtons.forEach(e=>{
  354. e.dispose()
  355. }
  356. ),
  357. this._giftButtons.clear()
  358. }
  359. createPickBoundingbox(e, t) {
  360. const r = t.extendSize.x * 2
  361. , n = t.extendSize.y * 2
  362. , o = t.extendSize.z * 2
  363. , a = this._scene
  364. , s = Math.max(r, o)
  365. , l = e.avatarManager.bboxMeshPool.getFree(a, s, n, s)
  366. , u = l.data;
  367. return u && (u.position = t.centerWorld,
  368. u.setEnabled(!1),
  369. u.isPickable = !0,
  370. u.xtype = EMeshType.XAvatar,
  371. u.xid = e.id),
  372. l
  373. }
  374. }