XAvatarManager.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. const log$D = new Logger$1("AvatarManager");
  2. var EAvatarRelationRank = (i=>(i[i.Self = 0] = "Self",
  3. i[i.Npc = 1] = "Npc",
  4. i[i.Friend = 2] = "Friend",
  5. i[i.Stranger = 3] = "Stranger",
  6. i[i.Robot = 4] = "Robot",
  7. i[i.Unknown = 5] = "Unknown",
  8. i))(EAvatarRelationRank || {});
  9. class XAvatarManager {
  10. constructor(e) {
  11. E(this, "characterMap", new Map);
  12. E(this, "curAnimList", []);
  13. E(this, "extraComps", new Map);
  14. E(this, "_mainUser");
  15. E(this, "_scene");
  16. E(this, "_sceneManager");
  17. E(this, "_lodSettings");
  18. E(this, "maxBillBoardDist", 0);
  19. E(this, "maxAvatarNum", 0);
  20. E(this, "currentLODUsers", []);
  21. E(this, "bboxMeshPool");
  22. E(this, "_distLevels", []);
  23. E(this, "_maxLODUsers", []);
  24. E(this, "_cullingDistance", 0);
  25. E(this, "_maxDistRange");
  26. E(this, "_delayTime", 100);
  27. E(this, "_queueLength", -1);
  28. E(this, "_queue", []);
  29. E(this, "_processList", []);
  30. E(this, "_process");
  31. E(this, "_updateLoopObserver");
  32. E(this, "_avatarInDistance", []);
  33. E(this, "_renderedAvatar", []);
  34. E(this, "_enableNickname", !0);
  35. E(this, "_tickObserver");
  36. E(this, "_tickInterval");
  37. E(this, "_defaultAnims");
  38. E(this, "_tickDispose", 0);
  39. E(this, "_disposeTime", 100);
  40. E(this, "avatarLoader", avatarLoader);
  41. this._scene = e.mainScene,
  42. this._sceneManager = e,
  43. this.initAvatarMap(),
  44. this._initSettings(),
  45. this._maxDistRange = this._distLevels[this._distLevels.length - 1],
  46. this.bboxMeshPool = new Pool(this.createBboxAsset,this.resetBboxAsset,0,this.maxAvatarNum,this._sceneManager.Scene,0,0,0),
  47. this._tickInterval = 250;
  48. let t = 0;
  49. this._tickObserver = this._scene.onAfterRenderObservable.add(()=>{
  50. t += 1,
  51. t == this._tickInterval && (this.tick(),
  52. t = 0)
  53. }
  54. )
  55. }
  56. tick() {
  57. this.bboxMeshPool.clean(0)
  58. }
  59. createBboxAsset(e, t, r, n) {
  60. return MeshBuilder.CreateBox("avatarBbox", {
  61. width: t,
  62. height: r,
  63. depth: n
  64. }, e)
  65. }
  66. resetBboxAsset(e) {
  67. const t = e.data;
  68. return t.setEnabled(!1),
  69. t.isPickable = !1,
  70. e
  71. }
  72. _initSettings() {
  73. this._defaultAnims = avatarSetting.defaultIdle,
  74. this._lodSettings = avatarSetting.lod,
  75. this._distLevels = avatarSetting.lod.map(e=>e.dist),
  76. this._maxLODUsers = avatarSetting.lod.map(e=>e.quota),
  77. this.currentLODUsers = new Array(this._distLevels.length).fill(0),
  78. this.maxAvatarNum = avatarSetting.maxAvatarNum,
  79. this.maxBillBoardDist = avatarSetting.maxBillBoardDist,
  80. this._cullingDistance = avatarSetting.cullingDistance
  81. }
  82. maxRenderNum() {
  83. let e = 0;
  84. return this._maxLODUsers.forEach(t=>{
  85. e += t
  86. }
  87. ),
  88. e
  89. }
  90. curRenderNum() {
  91. let e = 0;
  92. return this.currentLODUsers.forEach(t=>{
  93. e += t
  94. }
  95. ),
  96. e
  97. }
  98. setLoDLevels(e) {
  99. this._distLevels = e
  100. }
  101. set cullingDistance(e) {
  102. this._cullingDistance = e
  103. }
  104. get cullingDistance() {
  105. return this._cullingDistance
  106. }
  107. getLoDLevels() {
  108. return this._distLevels
  109. }
  110. setLodUserLimits(e, t) {
  111. this._maxLODUsers.length > e && (this._maxLODUsers[e] = t)
  112. }
  113. setLodDist(e, t) {
  114. this._distLevels[e] = t
  115. }
  116. setMaxDistRange(e) {
  117. this._maxDistRange = e,
  118. this._distLevels[this._distLevels.length - 1] = e
  119. }
  120. get scene() {
  121. return this._scene
  122. }
  123. setMainAvatar(e) {
  124. var t;
  125. this._mainUser = (t = this.characterMap.get(0)) == null ? void 0 : t.get(e)
  126. }
  127. getMainAvatar() {
  128. return this._mainUser
  129. }
  130. enableAllNickname(e) {
  131. return this.characterMap.forEach((t,r)=>{
  132. r != 0 && t.forEach((n,o)=>{
  133. this._updateBillboardStatus(n, e ? BillboardStatus.SHOW : BillboardStatus.HIDE)
  134. }
  135. )
  136. }
  137. ),
  138. this._enableNickname = e
  139. }
  140. getAvatarById(e) {
  141. let t;
  142. return this.characterMap.forEach((r,n)=>{
  143. r.get(e) && (t = r.get(e))
  144. }
  145. ),
  146. t
  147. }
  148. getAvatarNums() {
  149. let e = 0;
  150. return this.characterMap.forEach((t,r)=>{
  151. e += t.size
  152. }
  153. ),
  154. e
  155. }
  156. registerAvatar(e) {
  157. this.characterMap.get(e.priority).set(e.id, e)
  158. }
  159. unregisterAvatar(e) {
  160. this.characterMap.get(e.priority).delete(e.id)
  161. }
  162. initAvatarMap() {
  163. this.characterMap.set(0, new Map),
  164. this.characterMap.set(1, new Map),
  165. this.characterMap.set(2, new Map),
  166. this.characterMap.set(3, new Map),
  167. this.characterMap.set(4, new Map),
  168. this.characterMap.set(5, new Map)
  169. }
  170. loadAvatar({id: e, avatarType: t, priority: r, avatarManager: n, assets: o, status: a}) {
  171. return new Promise((s,l)=>{
  172. if (this.getAvatarById(e))
  173. return l(new DuplicateAvatarIDError(`[Engine] cannot init avatar with the same id = ${e}`));
  174. if (this.getAvatarNums() > this.maxAvatarNum)
  175. return l(new ExceedMaxAvatarNumError(`[Engine] \u8D85\u51FA\u6700\u5927\u89D2\u8272\u9650\u5236 ${this.maxAvatarNum}`));
  176. const u = new XAvatar({
  177. id: e,
  178. avatarType: t,
  179. priority: r,
  180. avatarManager: n,
  181. assets: o,
  182. status: a
  183. });
  184. if (this.registerAvatar(u),
  185. r == 0)
  186. this.setMainAvatar(u.id),
  187. this.addAvatarToScene(u, 0).then(c=>(log$D.debug(`[Engine] avatar ${u.id} has been added to scene`),
  188. c ? (this._updateBillboardStatus(c, BillboardStatus.SHOW),
  189. setTimeout(()=>{
  190. this.launchProcessLoadingLoop()
  191. }
  192. , this._delayTime),
  193. s(c)) : (u.removeAvatarFromScene(),
  194. l(new AvatarAssetLoadingError)))).catch(c=>(u.removeAvatarFromScene(),
  195. l(new AvatarAssetLoadingError(c))));
  196. else
  197. return s(u)
  198. }
  199. )
  200. }
  201. deleteAvatar(e) {
  202. return e.isRender ? (e.removeAvatarFromScene(),
  203. this.currentLODUsers[e.distLevel]--) : e.bbComponent.disposeBillBoard(e),
  204. this._processList = this._processList.filter(t=>t.id !== e.id),
  205. this.unregisterAvatar(e),
  206. e.rootNode && (e.rootNode.dispose(),
  207. e.rootNode = void 0),
  208. e.bbComponent.bbox && e.bbComponent.bbox.dispose(),
  209. e.removeObserver(),
  210. e
  211. }
  212. _checkLODLevel(e) {
  213. if (e < this._distLevels[0])
  214. return 0;
  215. for (let t = 1; t < this._distLevels.length; ++t)
  216. if (e >= this._distLevels[t - 1] && e < this._distLevels[t])
  217. return t;
  218. return this._distLevels.length - 1
  219. }
  220. get sceneManager() {
  221. return this._sceneManager
  222. }
  223. launchProcessLoadingLoop() {
  224. this._updateAvatarStatus()
  225. }
  226. stopProcessLoadingLoop() {
  227. var e;
  228. this._updateLoopObserver && ((e = this._scene) == null || e.onBeforeRenderObservable.remove(this._updateLoopObserver))
  229. }
  230. _distToMain(e) {
  231. var n;
  232. const t = (n = this._mainUser) == null ? void 0 : n.position
  233. , r = e.position;
  234. if (r && t) {
  235. const o = this.sceneManager.cameraComponent.MainCamera.getFrontPosition(1).subtract(this.sceneManager.cameraComponent.MainCamera.position).normalize()
  236. , a = e.rootNode.position.subtract(this.sceneManager.cameraComponent.MainCamera.position).normalize();
  237. let s = 1;
  238. if (o && a) {
  239. const l = a.multiply(o);
  240. s = Math.acos(l.x + l.y + l.z) < this.sceneManager.cameraComponent.MainCamera.fov / 2 ? 1 : 1e11
  241. }
  242. return calcDistance3D(t, r) * s
  243. } else
  244. return log$D.warn("user position or camera position is not correct!"),
  245. 1e11
  246. }
  247. _distToCamera(e) {
  248. var n;
  249. const t = (n = this._sceneManager) == null ? void 0 : n.cameraComponent.getCameraPose().position
  250. , r = e.position;
  251. return r && t ? calcDistance3D(t, r) : (log$D.warn("user position or camera position is not correct!"),
  252. 1e11)
  253. }
  254. showAll(e) {
  255. this.characterMap.forEach((t,r)=>{
  256. e && r == 0 && t.forEach((n,o)=>{
  257. n.show()
  258. }
  259. ),
  260. r != 0 && t.forEach((n,o)=>{
  261. n.show()
  262. }
  263. )
  264. }
  265. )
  266. }
  267. hideAll(e) {
  268. this.characterMap.forEach((t,r)=>{
  269. e && r == 0 && t.forEach((n,o)=>{
  270. n.hide()
  271. }
  272. ),
  273. r != 0 && t.forEach((n,o)=>{
  274. n.hide()
  275. }
  276. )
  277. }
  278. )
  279. }
  280. _assemblyAvatar(e, t) {
  281. var n, o;
  282. const r = e.get(avatarSetting.body);
  283. if (r && !t.attachBody(r)) {
  284. t.isInLoadingList = !1,
  285. e.clear();
  286. return
  287. }
  288. for (const a of e)
  289. if (a[0] != avatarSetting.body && a[0] != avatarSetting.animations && !t.attachDecoration(a[1])) {
  290. t.isInLoadingList = !1,
  291. t.removeAvatarFromScene(),
  292. e.clear();
  293. return
  294. }
  295. t.isRender = !0,
  296. (n = t.controller) == null || n.playAnimation(t.controller.onPlay, t.controller.loop),
  297. (o = t.controller) == null || o.onPlayObservable.addOnce(()=>{
  298. var a, s;
  299. if (!this.getAvatarById(t.id)) {
  300. t.isInLoadingList = !1,
  301. t.removeAvatarFromScene(),
  302. this.currentLODUsers[t.distLevel]--;
  303. return
  304. }
  305. if (this.getAvatarById(t.id).rootNode.getChildMeshes().length < e.size) {
  306. log$D.error(`this avatar does not have complete components, render failed. current list ${(a = this.getAvatarById(t.id)) == null ? void 0 : a.clothesList},avatar: ${t.id},${t.nickName}`),
  307. t.isInLoadingList = !1,
  308. t.removeAvatarFromScene(),
  309. this.currentLODUsers[t.distLevel]--;
  310. return
  311. }
  312. t.setIsPickable(!0),
  313. t.isInLoadingList = !1,
  314. t.setAvatarVisible(!0),
  315. (s = this._sceneManager) == null || s.lightComponent.setShadow(t),
  316. t.getBbox(),
  317. t.nameBoard && t.nickName.length > 0 && t.setNickName(t.nickName, t.nameBoard.DEFAULT_CONFIGS),
  318. t.bubble && t.words.length > 0 && t.say(t.words, t.bubble.DEFAULT_CONFIGS),
  319. log$D.debug(`[Engine] avatar ${t.id} has been added to scene, current number of users : ${this.currentLODUsers}`)
  320. }
  321. )
  322. }
  323. _disposeUnusedAssets() {
  324. this._tickDispose++,
  325. this._tickDispose > this._disposeTime && (avatarLoader.disposeContainer(),
  326. this._tickDispose = 0)
  327. }
  328. _addResourcesToList(e, t) {
  329. return e.clothesList.forEach(r=>{
  330. r.lod = t,
  331. this._queue.push(r)
  332. }
  333. ),
  334. this._queue.push({
  335. type: avatarSetting.animations,
  336. id: this._defaultAnims
  337. }),
  338. this._queue.push({
  339. type: avatarSetting.body,
  340. id: e.avatarType,
  341. lod: t
  342. }),
  343. !0
  344. }
  345. _updateBillboardStatus(e, t) {
  346. e.bbComponent.updateBillboardStatus(e, t)
  347. }
  348. _processLayer(e) {
  349. const t = this.characterMap.get(e)
  350. , r = [];
  351. for (t == null || t.forEach(n=>{
  352. n.distToCam = this._distToCamera(n);
  353. const o = n.distToCam < this._cullingDistance;
  354. if (n.isRender && (!n.isHide && o ? n._hide_culling() : n._show_culling()),
  355. n.priority != 0) {
  356. n.distance = this._distToMain(n);
  357. let a = BillboardStatus.SHOW;
  358. n.distance < this._maxDistRange && (o ? a = BillboardStatus.HIDE : n._show_culling(),
  359. this._updateBillboardStatus(n, a)),
  360. n.isHide || (n.isInLoadingList ? this.currentLODUsers[n.distLevel]++ : r.push(n))
  361. }
  362. }
  363. ),
  364. r.sort((n,o)=>o.distance - n.distance); r.length > 0 && this.curRenderNum() < this.maxRenderNum(); ) {
  365. const n = r.pop();
  366. let o = this._checkLODLevel(n.distance)
  367. , a = !1;
  368. for (let s = 0; s < this._maxLODUsers.length; ++s)
  369. if (this.currentLODUsers[s] < this._maxLODUsers[s]) {
  370. o = s,
  371. a = !0;
  372. break
  373. }
  374. if (!a || n.distance > this._maxDistRange) {
  375. if (n.isRender) {
  376. n._removeAvatarFromScene();
  377. let s = BillboardStatus.HIDE;
  378. n.distance < this._maxDistRange && (s = BillboardStatus.SHOW),
  379. this._updateBillboardStatus(n, s)
  380. }
  381. break
  382. }
  383. o != n.distLevel ? (n.isRender && (n.pendingLod = !0),
  384. n.distLevel = o,
  385. this._processList.push(n),
  386. n.isInLoadingList = !0) : n.isRender || (this._processList.push(n),
  387. n.isInLoadingList = !0),
  388. this.currentLODUsers[o]++
  389. }
  390. return this.curRenderNum() >= this.maxRenderNum() && r.forEach(n=>{
  391. if (n.isRender) {
  392. n._removeAvatarFromScene();
  393. let o = BillboardStatus.HIDE;
  394. n.distance < this._maxDistRange && (o = BillboardStatus.SHOW),
  395. this._updateBillboardStatus(n, o)
  396. }
  397. }
  398. ),
  399. this.curRenderNum() < this.maxRenderNum()
  400. }
  401. _updateAvatar() {
  402. this.currentLODUsers = [0, 0, 0];
  403. const e = [5, 4, 3, 2, 1, 0];
  404. for (; e.length > 0; ) {
  405. const t = e.pop();
  406. if (!this._processLayer(t)) {
  407. e.forEach(n=>{
  408. var o;
  409. (o = this.characterMap.get(n)) == null || o.forEach(a=>{
  410. a.distance = this._distToMain(a);
  411. let s = BillboardStatus.HIDE;
  412. a.distToCam < this._maxDistRange && (s = BillboardStatus.SHOW,
  413. a.isRender && a._removeAvatarFromScene()),
  414. this._updateBillboardStatus(a, s)
  415. }
  416. )
  417. }
  418. );
  419. break
  420. }
  421. }
  422. }
  423. _updateAvatarStatus() {
  424. const e = new Map;
  425. this._updateLoopObserver = this.scene.onBeforeRenderObservable.add(()=>{
  426. var t;
  427. if (this._disposeUnusedAssets(),
  428. !(this.getAvatarNums() <= 0)) {
  429. if (!this._process && this._processList.length == 0 && this._updateAvatar(),
  430. !this._process && this._processList.length > 0) {
  431. const r = this._processList.shift();
  432. r != this._process && !r.isCulling ? this._addResourcesToList(r, r.distLevel) ? (this._process = r,
  433. this._queueLength = this._queue.length) : (this._process = void 0,
  434. this._queue = [],
  435. r.isInLoadingList = !1) : r.isInLoadingList = !1
  436. }
  437. if (e.size === this._queueLength && this._process) {
  438. this._process.pendingLod && (this._process.pendingLod = !1,
  439. this._process._removeAvatarFromScene());
  440. const r = Date.now();
  441. this._assemblyAvatar(e, this._process),
  442. (t = this._sceneManager) == null || t.engineRunTimeStats.timeArray_addAvatarToScene.add(Date.now() - r),
  443. this._updateBillboardStatus(this._process, BillboardStatus.SHOW),
  444. e.clear(),
  445. this._queue = [],
  446. this._process.isInLoadingList = !1,
  447. this._process = void 0,
  448. this._disposeUnusedAssets()
  449. }
  450. this._loadResByList(e)
  451. }
  452. }
  453. )
  454. }
  455. _loadResByList(e) {
  456. let t = 0;
  457. const r = 5;
  458. if (!this._process) {
  459. e.clear();
  460. return
  461. }
  462. for (; t < r && this._queue.length > 0; ) {
  463. const n = Date.now()
  464. , o = this._queue.pop();
  465. setTimeout(()=>{
  466. o ? o.type === avatarSetting.body ? this.loadBody(o.type, o.id, o.lod).then(a=>{
  467. a && e.set(avatarSetting.body, a),
  468. t += Date.now() - n
  469. }
  470. ).catch(a=>{
  471. this._process && (this._process.isHide = !0,
  472. this.currentLODUsers[this._process.distLevel]--,
  473. e.clear(),
  474. this._queue = [],
  475. this._process.isInLoadingList = !1,
  476. this._process = void 0,
  477. t += 100),
  478. log$D.warn(`[Engine] body ${o.id} uri error, type ${o.type}, avatar has been hided` + a)
  479. }
  480. ) : o.type === avatarSetting.animations ? this.loadAnimation(this._process.avatarType, o.id).then(a=>{
  481. a && e.set(avatarSetting.animations, a),
  482. t += Date.now() - n
  483. }
  484. ).catch(a=>{
  485. this._process && (this._process.isHide = !0,
  486. this.currentLODUsers[this._process.distLevel]--,
  487. e.clear(),
  488. this._queue = [],
  489. this._process.isInLoadingList = !1,
  490. this._process = void 0,
  491. t += 100),
  492. log$D.warn(`animation ${o.id} uri error, type ${o.type}, avatar has been hided` + a)
  493. }
  494. ) : this.loadDecoration(o.type, o.id, o.lod).then(a=>{
  495. a && e.set(a.type, a),
  496. t += Date.now() - n
  497. }
  498. ).catch(a=>{
  499. this._process && (this._process.isHide = !0,
  500. this.currentLODUsers[this._process.distLevel]--,
  501. e.clear(),
  502. this._queue = [],
  503. this._process.isInLoadingList = !1,
  504. this._process = void 0,
  505. t += 100),
  506. log$D.warn(`component ${o.id} uri error, type ${o.type}, avatar has been hided` + a)
  507. }
  508. ) : t += 100
  509. }
  510. , 0)
  511. }
  512. }
  513. _validateContainer(e) {
  514. return !e.meshes || e.meshes.length <= 1 ? (log$D.warn("import container has no valid meshes"),
  515. !1) : !e.skeletons || e.skeletons.length == 0 ? (log$D.warn("import container has no valid skeletons"),
  516. !1) : !0
  517. }
  518. _getAssetContainer(e, t) {
  519. return new Promise((r,n)=>{
  520. const o = this._getSourceKey(e, t || 0)
  521. , a = avatarLoader.containers.get(o);
  522. if (a)
  523. return r(a);
  524. avatarLoader.load(this.sceneManager, e, t).then(s=>s ? this._validateContainer(s) ? (avatarLoader.containers.set(o, s),
  525. r(s)) : n(new ContainerLoadingFailedError(`[Engine] :: cannot load body type ${e}.`)) : n(new ContainerLoadingFailedError(`[Engine] container load failed cannot load body type ${e}.`))).catch(s=>n(new ContainerLoadingFailedError(`[Engine] ${s} :: cannot load body type ${e}.`)))
  526. }
  527. )
  528. }
  529. _clipContainerRes(e) {
  530. e.transformNodes.forEach(t=>{
  531. t.dispose()
  532. }
  533. ),
  534. e.transformNodes = [],
  535. e.skeletons.forEach(t=>{
  536. t.dispose()
  537. }
  538. ),
  539. e.skeletons = []
  540. }
  541. loadBody(e, t, r) {
  542. return new Promise((n,o)=>avatarLoader.load(this.sceneManager, t, r).then(a=>{
  543. if (a) {
  544. const s = a.instantiateModelsToScene();
  545. a.xReferenceCount++;
  546. const l = {
  547. isRender: !1,
  548. uId: Math.random(),
  549. root: s.rootNodes[0],
  550. skeletonType: e,
  551. name: t,
  552. animations: s.animationGroups,
  553. skeleton: s.skeletons[0],
  554. lod: r
  555. };
  556. return s.rootNodes[0]._parentContainer = a,
  557. s.rootNodes[0].setEnabled(!1),
  558. n(l)
  559. } else
  560. return o(new ContainerLoadingFailedError("[Engine] container failed instanciates failed"))
  561. }
  562. ).catch(()=>o(new ContainerLoadingFailedError(`[Engine] body type ${e} instanciates failed`))))
  563. }
  564. updateAnimationLists(e, t) {
  565. return new Promise((r,n)=>(avatarLoader.avaliableAnimation.set(t, e),
  566. r()))
  567. }
  568. loadAnimation(e, t) {
  569. return new Promise((r,n)=>avatarLoader.loadAnimRes(this.sceneManager, t, e).then(o=>{
  570. if (o) {
  571. let a;
  572. const s = this.avatarLoader.animations;
  573. return o.animationGroups.forEach(l=>{
  574. l.stop(),
  575. l.name === t && (a = l,
  576. a.pContainer = o),
  577. s.set(getAnimationKey(l.name, e), l)
  578. }
  579. ),
  580. this._clipContainerRes(o),
  581. o.xReferenceCount++,
  582. r(a)
  583. } else
  584. return n(new ContainerLoadingFailedError("[Engine] container failed instanciates failed"))
  585. }
  586. ))
  587. }
  588. loadDecoration(e, t, r) {
  589. return new Promise((n,o)=>avatarLoader.load(this.sceneManager, t, r).then(a=>{
  590. if (a) {
  591. this._clipContainerRes(a);
  592. const s = a.meshes[1].clone(a.meshes[1].name, null);
  593. if (!s) {
  594. log$D.warn("[Engine] decoration does not exist!"),
  595. n(null);
  596. return
  597. }
  598. const l = {
  599. isRender: !1,
  600. uId: Math.random(),
  601. root: s,
  602. type: e,
  603. name: t,
  604. isSelected: !1,
  605. lod: r
  606. };
  607. if (a.xReferenceCount++,
  608. s._parentContainer = a,
  609. a.meshes.length > 1)
  610. for (let u = 2; u < a.meshes.length; u++)
  611. s.addChild(a.meshes[u].clone(a.meshes[u].name, null));
  612. s.setEnabled(!1),
  613. l.isSelected = !0,
  614. n(l)
  615. } else
  616. return o(new ContainerLoadingFailedError("[Engine] container failed, instanciates failed."))
  617. }
  618. ).catch(()=>o(new ContainerLoadingFailedError(`[Engine] body type ${e} instanciates failed.`))))
  619. }
  620. _getSourceKey(e, t) {
  621. return t && avatarSetting.lod[t] ? e + avatarSetting.lod[t].fileName.split(".")[0] : e
  622. }
  623. addAvatarToScene(e, t) {
  624. const r = Date.now();
  625. return new Promise((n,o)=>{
  626. this.loadBody(e.avatarType, e.avatarType, t).then(a=>{
  627. var s;
  628. if (!a)
  629. return e.isInLoadingList = !1,
  630. o(new ContainerLoadingFailedError(`[Engine] avatar ${e.id} instanciates failed`));
  631. if (e.attachBody(a),
  632. a.animations.length > 0)
  633. return a.animations.forEach(l=>{
  634. l.stop()
  635. }
  636. ),
  637. e.setAnimations(a.animations),
  638. (s = e.controller) == null || s.playAnimation(e.controller.onPlay, !0),
  639. e.isRender = !0,
  640. e.isInLoadingList = !1,
  641. e.setAvatarVisible(!0),
  642. n(e);
  643. this.loadAnimation(e.avatarType, this._defaultAnims).then(l=>{
  644. if (!l)
  645. return e.removeAvatarFromScene(),
  646. e.isInLoadingList = !1,
  647. o(new AvatarAnimationError);
  648. const u = [];
  649. e.clothesList.length > 0 && e.clothesList.forEach(c=>{
  650. u.push(this.loadDecoration(c.type, c.id, t))
  651. }
  652. ),
  653. Promise.all(u).then(c=>{
  654. var d, _, g, m;
  655. c.forEach(v=>{
  656. if (v && !v.isRender)
  657. e.attachDecoration(v);
  658. else
  659. return e.isInLoadingList = !1,
  660. e.removeAvatarFromScene(),
  661. o(new AvatarAssetLoadingError)
  662. }
  663. ),
  664. e.isRender = !0,
  665. (d = e.controller) == null || d.playAnimation(e.controller.onPlay, e.controller.loop),
  666. e.setAvatarVisible(!0);
  667. const h = avatarLoader.mshPath.get("meshes/ygb.glb")
  668. , f = avatarLoader.matPath.get(avatarResources.ygb.mesh);
  669. h && f ? this.loadExtra(f, h).then(v=>{
  670. var y;
  671. e.isRender = !0,
  672. e.isInLoadingList = !1,
  673. e.distLevel = t,
  674. (y = this._sceneManager) == null || y.engineRunTimeStats.timeArray_addAvatarToScene.add(Date.now() - r),
  675. n(e)
  676. }
  677. ) : (e.isRender = !0,
  678. e.isInLoadingList = !1,
  679. e.distLevel = t,
  680. (_ = this._sceneManager) == null || _.engineRunTimeStats.timeArray_addAvatarToScene.add(Date.now() - r),
  681. n(e)),
  682. (g = this._sceneManager) == null || g.lightComponent.setShadow(e),
  683. e.isInLoadingList = !1,
  684. e.distLevel = t,
  685. (m = this._sceneManager) == null || m.engineRunTimeStats.timeArray_addAvatarToScene.add(Date.now() - r),
  686. n(e)
  687. }
  688. ).catch(()=>o(new AvatarAssetLoadingError(`[Engine] avatar ${e.id} instanciates failed.`)))
  689. }
  690. ).catch(()=>o(new AvatarAssetLoadingError(`[Engine] avatar ${e.id} instanciates failed.`)))
  691. }
  692. ).catch(()=>o(new AvatarAssetLoadingError(`[Engine] avatar ${e.id} instanciates failed.`)))
  693. }
  694. )
  695. }
  696. loadExtra(e, t) {
  697. const r = avatarResources.ygb.name;
  698. return new Promise((n,o)=>{
  699. var a;
  700. (a = this.sceneManager) == null || a.urlTransformer(e).then(s=>{
  701. SceneLoader.LoadAssetContainerAsync("", s, this.scene, null, avatarSetting.fileType).then(l=>{
  702. var c;
  703. this.extraComps.set(r, l.meshes[0]);
  704. const u = new NodeMaterial(`material_${r}`,this._scene,{
  705. emitComments: !1
  706. });
  707. (c = this.sceneManager) == null || c.urlTransformer(t).then(h=>{
  708. u.loadAsync(h).then(()=>{
  709. l.meshes[2].material.dispose(!0, !0),
  710. u.build(!1),
  711. l.meshes[2].material = u,
  712. n(l.meshes[2])
  713. }
  714. )
  715. }
  716. )
  717. }
  718. )
  719. }
  720. )
  721. }
  722. )
  723. }
  724. getAvatarList() {
  725. const e = [];
  726. return this.characterMap.forEach((t,r)=>{
  727. t.forEach((n,o)=>{
  728. e.push(n)
  729. }
  730. )
  731. }
  732. ),
  733. e
  734. }
  735. _debug_avatar() {
  736. var t, r;
  737. console.error("===>currentLODUsers", this.currentLODUsers),
  738. console.error("===>maxLODUsers", this._maxLODUsers),
  739. console.error("===>Loddist", this.getLoDLevels()),
  740. console.error("===> main character loc", (r = (t = this._mainUser) == null ? void 0 : t.rootNode) == null ? void 0 : r.position);
  741. let e = 0;
  742. this.getAvatarList().forEach(n=>{
  743. n.isRender && (console.error(`avatar id : ${n.id},lod ${n.distLevel},is Hide ${n.isHide}, distance ${n.distance}, is pending ${n.isInLoadingList}`),
  744. e++)
  745. }
  746. ),
  747. console.error("========= avatar num", e),
  748. console.error("loop:", this._updateLoopObserver ? "on" : "false", "=> process", this._process, "===> comp", this._processList),
  749. console.error("===>maxLODUsers", this._maxLODUsers)
  750. }
  751. }