XAvatarComopnent.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. this.resourceIdList = [],
  7. this.skeleton = void 0,
  8. this.extraProp = void 0,
  9. this.extras = [],
  10. this.body = void 0,
  11. this.accessories = new Map
  12. }
  13. addBodyComp(e, i) {
  14. return !e.rootNode || i.root.getChildMeshes().length === 0 ? (i.isRender = !1,
  15. !1) : (this.body = i,
  16. this.body.root.parent = e.rootNode,
  17. i.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 = i.skeleton,
  21. !0)
  22. }
  23. addClothesComp(e, i) {
  24. return !e.rootNode || !this.skeleton || !i.root ? (i.isRender = !1,
  25. !1) : (i.root.xtype = EMeshType.XAvatar,
  26. i.root.xid = e.id,
  27. i.isRender = !0,
  28. i.root.parent = e.rootNode.getChildMeshes()[0],
  29. this.resourceIdList.push(i),
  30. i.root.skeleton = this.skeleton,
  31. i.root.getChildMeshes().forEach(o=>{
  32. o.skeleton = this.skeleton
  33. }
  34. ),
  35. !0)
  36. }
  37. clearClothesComp(e) {
  38. e.root.getChildMeshes().forEach(i=>{
  39. i.skeleton = null,
  40. i.dispose(),
  41. i.xid = void 0
  42. }
  43. ),
  44. e.root.dispose(),
  45. this.resourceIdList = this.resourceIdList.filter(i=>i.uId != e.uId)
  46. }
  47. clearAllClothesComps() {
  48. this.resourceIdList.forEach(e=>{
  49. var i;
  50. e.root.parent = null,
  51. e.root._parentContainer.xReferenceCount && (e.root._parentContainer.xReferenceCount--,
  52. e.root._parentContainer = null),
  53. e.isRender = !1,
  54. e.isSelected = !1,
  55. e.root.getChildMeshes().forEach(o=>{
  56. o.skeleton = null,
  57. o.dispose()
  58. }
  59. ),
  60. (i = e.root.skeleton) == null || i.dispose(),
  61. e.root.dispose()
  62. }
  63. ),
  64. this.resourceIdList = []
  65. }
  66. dispose(e) {
  67. this.body ? (this.body.root._parentContainer.xReferenceCount && (this.body.root._parentContainer.xReferenceCount--,
  68. this.body.root._parentContainer = null),
  69. this.clearAllClothesComps(),
  70. this.body.isRender = !1,
  71. this.body.skeleton.dispose(),
  72. this.body.skeleton = null,
  73. this.body.root.dispose(),
  74. this.body = void 0,
  75. this.skeleton && (this.skeleton.dispose(),
  76. this.skeleton = void 0)) : log$I.warn("[Engine] no body to dispose")
  77. }
  78. async attachPendant(e, i) {
  79. return Promise.resolve(avatarLoader.pullAndLoadXObject(e.avatarManager.sceneManager, i).then(o=>{
  80. const s = o
  81. , c = this.accessories.get(s.pointId);
  82. return c ? (c.dispose(),
  83. this.accessories.set(s.pointId, s),
  84. log$I.warn("[Engine] \u8BE5\u6302\u70B9\u5F53\u524D\u88AB\u5360\u7528\uFF0C\u5DF2\u66FF\u6362\u8BE5\u6302\u70B9")) : this.accessories.set(s.pointId, s),
  85. s.attachTo(e),
  86. s
  87. }
  88. ))
  89. }
  90. detachPendant(e, i=!0) {
  91. const o = this.accessories.get(e);
  92. o && (o.dispose(),
  93. this.accessories.delete(e))
  94. }
  95. changeClothesComp(e, i, o, s, c) {
  96. return new Promise(d=>{
  97. if (this.resourceIdList.some(_=>_.name === i))
  98. return d();
  99. if (e.isHide || !e.isRender)
  100. c.concat(o).forEach(b=>{
  101. e.clothesList = e.clothesList.filter(j=>j.type != b);
  102. const k = {
  103. type: o,
  104. id: i,
  105. url: s,
  106. lod: 0
  107. };
  108. e.clothesList.push(k)
  109. }
  110. ),
  111. d();
  112. else {
  113. const _ = c.concat(o);
  114. e.avatarManager.loadDecoration(o, i, 0).then(b=>{
  115. if (b) {
  116. e.attachDecoration(b);
  117. const k = {
  118. type: o,
  119. id: i,
  120. url: s
  121. };
  122. e.clothesList.push(k),
  123. b.root.setEnabled(!0),
  124. _.forEach(j=>{
  125. const $ = this.resourceIdList.filter(_e=>_e.type === j);
  126. if ($.length > 1) {
  127. const _e = $.filter(et=>et.name === i);
  128. if (_e.length > 1)
  129. for (let et = 1; et < _e.length; ++et) {
  130. e.detachDecoration(_e[et]),
  131. e.clothesList = e.clothesList.filter(rt=>rt.id != _e[et].name);
  132. const tt = {
  133. type: o,
  134. id: i,
  135. url: s
  136. };
  137. e.clothesList.push(tt)
  138. }
  139. }
  140. $[0] && $[0].name != i && this._readyToDetach(e, o) && (e.detachDecoration($[0]),
  141. e.clothesList = e.clothesList.filter(_e=>_e.id != $[0].name))
  142. }
  143. )
  144. }
  145. return d()
  146. }
  147. )
  148. }
  149. }
  150. )
  151. }
  152. _readyToDetach(e, i) {
  153. return !((i == "clothes" || i == "pants") && e.clothesList.filter(s=>s.type === "suit").length == 1 && (!e.clothesList.some(s=>s.type === "pants") || !e.clothesList.some(s=>s.type === "clothes")))
  154. }
  155. addDecoComp(e, i, o, s, c) {
  156. if (e.isRender) {
  157. const d = e.avatarManager.extraComps.get(i)
  158. , _ = d == null ? void 0 : d.clone(i, void 0);
  159. if (!d) {
  160. log$I.error("\u6CA1\u6709\u5BF9\u5E94\u7684\u7EC4\u4EF6");
  161. return
  162. }
  163. this.extras.push(_);
  164. const b = this.skeleton.bones.find(k=>k.name === o);
  165. _.position = s,
  166. _.rotation = c,
  167. _.attachToBone(b, e.rootNode.getChildMeshes()[0])
  168. }
  169. }
  170. showExtra(e) {
  171. this.extras.forEach(i=>{
  172. i.name.indexOf(e) > 0 && i.setEnabled(!0)
  173. }
  174. )
  175. }
  176. hideExtra(e) {
  177. this.extras.forEach(i=>{
  178. i.name.indexOf(e) > 0 && i.setEnabled(!1)
  179. }
  180. )
  181. }
  182. disposeExtra() {
  183. this.extras.forEach(e=>{
  184. e.dispose()
  185. }
  186. ),
  187. this.extras = []
  188. }
  189. }