XAvatarComopnent.js 6.2 KB

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