XAvatarComopnent.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const log$H = new Logger$1("XAvatarComopnent");
  2. class XAvatarComopnent {
  3. constructor() {
  4. E(this, "resourceIdList", []);
  5. E(this, "skeleton");
  6. E(this, "extraProp");
  7. E(this, "extras", []);
  8. E(this, "body")
  9. }
  10. addBodyComp(e, t) {
  11. return !e.rootNode || t.root.getChildMeshes().length === 0 ? (t.isRender = !1,
  12. !1) : (this.body = t,
  13. this.body.root.parent = e.rootNode,
  14. t.isRender = !0,
  15. this.body.root.getChildMeshes()[0] && (this.body.root.getChildMeshes()[0].xtype = EMeshType.XAvatar,
  16. this.body.root.getChildMeshes()[0].xid = e.id),
  17. this.skeleton = t.skeleton,
  18. !0)
  19. }
  20. addClothesComp(e, t) {
  21. return !e.rootNode || !this.skeleton || !t.root ? (t.isRender = !1,
  22. !1) : (t.root.xtype = EMeshType.XAvatar,
  23. t.root.xid = e.id,
  24. t.isRender = !0,
  25. t.root.parent = e.rootNode.getChildMeshes()[0],
  26. this.resourceIdList.push(t),
  27. t.root.skeleton = this.skeleton,
  28. t.root.getChildMeshes().forEach(r=>{
  29. r.skeleton = this.skeleton
  30. }
  31. ),
  32. !0)
  33. }
  34. clearClothesComp(e) {
  35. e.root.getChildMeshes().forEach(t=>{
  36. t.skeleton = null,
  37. t.dispose(),
  38. t.xid = void 0
  39. }
  40. ),
  41. e.root.dispose(),
  42. this.resourceIdList = this.resourceIdList.filter(t=>t.uId != e.uId)
  43. }
  44. clearAllClothesComps() {
  45. this.resourceIdList.forEach(e=>{
  46. var t;
  47. e.root.parent = null,
  48. e.root._parentContainer.xReferenceCount && (e.root._parentContainer.xReferenceCount--,
  49. e.root._parentContainer = null),
  50. e.isRender = !1,
  51. e.isSelected = !1,
  52. e.root.getChildMeshes().forEach(r=>{
  53. r.skeleton = null,
  54. r.dispose()
  55. }
  56. ),
  57. (t = e.root.skeleton) == null || t.dispose(),
  58. e.root.dispose()
  59. }
  60. ),
  61. this.resourceIdList = []
  62. }
  63. dispose(e) {
  64. this.body ? (this.body.root._parentContainer.xReferenceCount && (this.body.root._parentContainer.xReferenceCount--,
  65. this.body.root._parentContainer = null),
  66. this.clearAllClothesComps(),
  67. this.body.isRender = !1,
  68. this.body.skeleton.dispose(),
  69. this.body.skeleton = null,
  70. this.body.root.dispose(),
  71. this.body = void 0,
  72. this.skeleton && (this.skeleton.dispose(),
  73. this.skeleton = void 0)) : log$H.warn("[Engine] no body to dispose")
  74. }
  75. changeClothesComp(e, t, r, n, o) {
  76. return new Promise(a=>{
  77. if (r === "pendant" || this.resourceIdList.some(s=>s.name === t))
  78. return a();
  79. if (e.isHide || !e.isRender)
  80. o.concat(r).forEach(l=>{
  81. e.clothesList = e.clothesList.filter(c=>c.type != l);
  82. const u = {
  83. type: r,
  84. id: t,
  85. url: n,
  86. lod: 0
  87. };
  88. e.clothesList.push(u)
  89. }
  90. ),
  91. a();
  92. else {
  93. const s = o.concat(r);
  94. e.avatarManager.loadDecoration(r, t, 0).then(l=>{
  95. if (l) {
  96. e.attachDecoration(l);
  97. const u = {
  98. type: r,
  99. id: t,
  100. url: n
  101. };
  102. e.clothesList.push(u),
  103. l.root.setEnabled(!0),
  104. s.forEach(c=>{
  105. const h = this.resourceIdList.filter(f=>f.type === c);
  106. if (h.length > 1) {
  107. const f = h.filter(d=>d.name === t);
  108. if (f.length > 1)
  109. for (let d = 1; d < f.length; ++d) {
  110. e.detachDecoration(f[d]),
  111. e.clothesList = e.clothesList.filter(g=>g.id != f[d].name);
  112. const _ = {
  113. type: r,
  114. id: t,
  115. url: n
  116. };
  117. e.clothesList.push(_)
  118. }
  119. }
  120. h[0] && h[0].name != t && this._readyToDetach(e, r) && (e.detachDecoration(h[0]),
  121. e.clothesList = e.clothesList.filter(f=>f.id != h[0].name))
  122. }
  123. )
  124. }
  125. return a()
  126. }
  127. )
  128. }
  129. }
  130. )
  131. }
  132. _readyToDetach(e, t) {
  133. return !((t == "clothes" || t == "pants") && e.clothesList.filter(n=>n.type === "suit").length == 1 && (!e.clothesList.some(n=>n.type === "pants") || !e.clothesList.some(n=>n.type === "clothes")))
  134. }
  135. addDecoComp(e, t, r, n, o) {
  136. if (e.isRender) {
  137. const a = e.avatarManager.extraComps.get(t)
  138. , s = a == null ? void 0 : a.clone(t, void 0);
  139. if (!a) {
  140. log$H.error("\u6CA1\u6709\u5BF9\u5E94\u7684\u7EC4\u4EF6");
  141. return
  142. }
  143. this.extras.push(s);
  144. const l = this.skeleton.bones.find(u=>u.name === r);
  145. s.position = n,
  146. s.rotation = o,
  147. s.attachToBone(l, e.rootNode.getChildMeshes()[0])
  148. }
  149. }
  150. showExtra(e) {
  151. this.extras.forEach(t=>{
  152. t.name.indexOf(e) > 0 && t.setEnabled(!0)
  153. }
  154. )
  155. }
  156. hideExtra(e) {
  157. this.extras.forEach(t=>{
  158. t.name.indexOf(e) > 0 && t.setEnabled(!1)
  159. }
  160. )
  161. }
  162. disposeExtra() {
  163. this.extras.forEach(e=>{
  164. e.dispose()
  165. }
  166. ),
  167. this.extras = []
  168. }
  169. }