import BillboardStatus from "./enum/BillboardStatus.js" import {avatarLoader} from "./XAvatarLoader.js" import XAnimationController from "./XAnimationController.js" import XAvatarComopnent from "./XAvatarComopnent.js" import XAvatarBillboardComponent from "./XAvatarBillboardComponent.js" import XActor from "./XActor.js" import XMovementComponent from "./XMovementComponent.js" import Logger from "./Logger.js" const logger = new Logger('Avatar') export default class XAvatar extends XActor{ constructor({id: o, avatarType: s, priority: c, avatarManager: _, assets: b, status: k}) { var $; super(), this.id = "-1", this.priority = 0, this.isRender = !1, this.distLevel = 0, this.isInLoadingList = !1, this.isHide = !1, this.isSelected = !1, this.pendingLod = !1, this.isMoving = !1, this.rootNode = void 0, this.distToCam = 1e11, this.enableNickname = !0, this.distance = 1e11, this.isCulling = !1, this.reslevel = 0, this.isInLoadingQueue = !1, this._scene = void 0, this._transparent = 0, this.hide = ()=>(this.isHide = !0, this._hide(), !this.isRender), this._show = ()=>{ var _e; this.isHide || (this.setIsPickable(!0), this.bbComponent._attachmentObservers.forEach((et,tt)=>{ tt.setEnabled(!0) } ), this.priority == 0 && (this.rootNode.setEnabled(!0), this.isRender = !0, this.avatarManager._updateBillboardStatus(this, BillboardStatus.SHOW), this.component.accessoryMap.forEach(et=>{ et.rootComponent.setEnabled(!0) } ), (_e = this.controller) == null || _e.playAnimation(this.controller.onPlay, this.controller.loop)), this.component.accessoryMap.forEach(et=>{ et.rootComponent.setEnabled(!0) } )) } , this.show = ()=>(this.isHide = !1, this._show(), !!this.isRender), this.setAnimations = _e=>{ this.controller.animations = _e } , this.attachToAvatar = (_e,et=!1,tt={ x: 0, y: 0, z: 0 },rt=!1,it,nt)=>this.bbComponent.attachToAvatar(this, _e, et, tt, rt, nt), this.detachFromAvatar = (_e,et=!1)=>this.bbComponent.detachFromAvatar(this, _e, et), this.getBbox = (_e={})=>this.bbComponent.getBbox(this, _e), this.id = o, this._avatarManager = _, this._scene = this.avatarManager.scene, this._avatarType = s, this.priority = c || 0, this.controller = new XAnimationController(this), this.component = new XAvatarComopnent, this.component.updateAssetList(b); const j = ($ = this.assetList.find(_e=>_e.type === "ANIMATION")) == null ? void 0 : $.id; this.controller.onPlay = this.controller.defaultAnimation = j, this.bbComponent = new XAvatarBillboardComponent(this._scene), this.rootNode = new BABYLON.TransformNode(o,this._avatarManager.scene), this._avatarScale = k.avatarScale == null ? 1 : k.avatarScale, this._avatarRotation = k.avatarRotation == null ? { pitch: 0, yaw: 0, roll: 0 } : k.avatarRotation, this._avatarPosition = k.avatarPosition == null ? { x: 0, y: 0, z: 0 } : k.avatarPosition, this._isRayCastEnable = avatarSetting.isRayCastEnable, this.movementComponent = new XMovementComponent(this._avatarManager.sceneManager), this.movementComponent.registerTo(this), this._isRayCastEnable = avatarSetting.isRayCastEnable, this.movementComponent.updatePosition(this._avatarPosition, !0), this.setRotation(this._avatarRotation), this.setScale(this.scale), this._observer = this._scene.onBeforeRenderObservable.add(()=>{ this.tick() } ) } tick() { this.cullingTick() } cullingTick() { var o; this.isCulling && ((o = this.rootNode) == null || o.getChildMeshes().forEach(s=>{ this.distToCam < 50 ? s.visibility = 0 : s.visibility = this._transparent } )) } setMaterial(o) { var s; (s = this.rootNode) == null || s.getChildMeshes().forEach(c=>{ c.material instanceof BABYLON.NodeMaterial && c.material.getInputBlocks().forEach(_=>{ _._storedValue instanceof BABYLON.Color3 && (_._storedValue = new BABYLON.Color3) } ) } ) } get assetList() { return this.component.assetList } setTransParentThresh(o) { this._transparent = o } get isNameVisible() { return this.bbComponent.isNameVisible } get isBubbleVisible() { return this.bbComponent.isBubbleVisible } get isGiftButtonsVisible() { return this.bbComponent.isGiftButtonsVisible } get words() { return this.bbComponent.words } get nickName() { return this.bbComponent.nickName } get giftButtons() { return this.bbComponent.giftButtons } get bubble() { return this.bbComponent.bubble } get nameBoard() { return this.bbComponent.nameBoard } get avatarManager() { return this._avatarManager } set withinVisibleRange(o) { this.bbComponent.withinVisualRange = o } setNicknameStatus(o) { return this.bbComponent.setNicknameStatus(o) } setBubbleStatus(o) { return this.bbComponent.setBubbleStatus(o) } setButtonsStatus(o) { return this.bbComponent.setButtonsStatus(o) } setGiftButtonsVisible(o) { return this.bbComponent.setGiftButtonsVisible(o) } get avatarType() { return this._avatarType } attachBody(o) { return this.component.addBodyComp(this, o) } attachDecoration(o) { return this.component.addClothesComp(this, o) } detachDecoration(o) { return this.component.clearClothesComp(o) } detachDecorationAll() { return this.component.disposeDress() } get skeleton() { return this.component.skeleton } get position() { return this._avatarPosition } get rotation() { return this._avatarRotation } get scale() { return this._avatarScale } _hide_culling() { this.bbComponent.updateBillboardStatus(this, BillboardStatus.HIDE), this.isCulling = !0 } _show_culling() { this.isCulling && (this.rootNode && this.rootNode.getChildMeshes().forEach(o=>{ o.visibility = 1 } ), this.bbComponent.updateBillboardStatus(this, BillboardStatus.SHOW), this.isCulling = !1) } get clothesList() { return this.assetList.filter(o=>o.type != avatarSetting.animations && o.type != avatarSetting.skeleton) } _hide() { !this.isHide || (this.setIsPickable(!1), this.bbComponent._attachmentObservers.forEach((o,s)=>{ s.setEnabled(!1) } ), this.priority == 0 ? (this.rootNode.setEnabled(!1), this.isRender = !1, this.bbComponent.updateBillboardStatus(this, BillboardStatus.HIDE), this.component.accessoryMap.forEach(o=>{ o.rootComponent.setEnabled(!1) } )) : this.isRender && (this.avatarManager.currentLODUsers[this.distLevel]--, this.removeAvatarFromScene()), this.component.accessoryMap.forEach(o=>{ o.rootComponent.setEnabled(!1) } )) } rotate(o, s, c) { return this.movementComponent.roll(this, o, s, c) } set isRayCastEnable(o) { this._isRayCastEnable = o } get isRayCastEnable() { return this._isRayCastEnable } getAvatarId() { return this.id } getAvaliableAnimations() { const o = avatarLoader.avaliableAnimation.get(this.avatarType); return o || [] } setPosition(o) { var c; this._avatarPosition = o; const s = Date.now(); return this.rootNode && (this.rootNode.position = ue4Position2Xverse(this._avatarPosition)), (c = this.avatarManager.sceneManager) == null || c.engineRunTimeStats.timeArray_setPosition.add(Date.now() - s), Promise.resolve(o) } setRotation(o) { if (this._avatarRotation = o, this.rootNode) { const s = { pitch: o.pitch, yaw: o.yaw + 180, roll: o.roll } , c = ue4Rotation2Xverse(s); this.rootNode.rotation = c } } setAvatarVisible(o) { this.rootNode && (this.rootNode.setEnabled(o), this.rootNode.getChildMeshes().forEach(s=>{ s.setEnabled(o) } )) } setScale(o) { this._avatarScale = o, this.rootNode && (this.rootNode.scaling = new BABYLON.Vector3(o,o,o)), this.bbComponent.bbox && this.getBbox() } _removeAvatarFromScene(o=!1) { var s, c; this.component.accessoryMap.forEach(_=>{ _.detachFrom(!1), _.rootComponent.setEnabled(!1) } ), this.isRender = !1, this.component.dispose(o), o && ((s = this.controller) == null || s.detachAnimation(), (c = this.avatarManager.sceneManager) == null || c.lightComponent.removeShadow(this)) } removeAvatarFromScene() { var s; this.component.accessoryMap.forEach(c=>{ c.dispose() } ), this.component.accessoryMap.clear(); const o = Date.now(); this._removeAvatarFromScene(!0), this._disposeBillBoard(), (s = this.avatarManager.sceneManager) == null || s.engineRunTimeStats.timeArray_removeAvatarFromScene.add(Date.now() - o) } _disposeBillBoard() { this.bbComponent.disposeBillBoard(this) } addComponent(o, s) { switch (s) { case EDressType.PENDANT: return this.component.attachPendant(this, o); case (EDressType.BODY || EDressType.CLOTHES || EDressType.HAIR || EDressType.HEAD || EDressType.SHOES || EDressType.SUIT): return this.component.changeClothesComp(this, o, s); default: return Promise.reject("this type cannot add") } } removeComponent(o, s) { o === EDressType.PENDANT ? s ? this.component.detachPendant(s) : this.component.accessoryMap.forEach((c,_)=>{ this.component.detachPendant(_) } ) : this.component.dressMap.forEach((c,_)=>{ c.asset && c.asset.dispose() } ) } getComponentByType(o, s) { if (o === EDressType.PENDANT) if (s) { const c = this.component.accessoryMap.get(s); return c || [] } else return Array.from(this.component.accessoryMap).map(c=>c[1]); else { const c = []; return this.component.dressMap.forEach((_,b)=>{ _.type == o && c.push(_) } ), c } } setPickBoxScale(o) { return this.bbComponent.setPickBoxScale(o) } setIsPickable(o) { return this.bbComponent.setIsPickable(this, o) } createPickBoundingbox(o) { return this.bbComponent.createPickBoundingbox(this, o) } scaleBbox(o) { this.bbComponent.bbox && this.bbComponent.bbox.scale(o) } rotateTo(o, s, c) { return this.movementComponent.rotateTo(this, o, s, c) } faceTo(o, s) { return this.movementComponent.lookAt(this, o, s) } removeObserver() { this._observer && (this._observer.unregisterOnNextCall = !0), this.movementComponent.disposeObsever(), this.dispose() } moveHermite(o, s, c, _, b, k, j=!0) { return this.movementComponent.moveToHermite(this, o, s, c, _, b, k, j) } moveCardinal(o, s, c, _, b, k, j=!1, $=!0) { return this.movementComponent.moveToCardinal(this, o, s, c, _, b, k, j, $) } move(o, s, c, _, b, k=!1, j=!0) { return this.movementComponent.moveTo(this, o, s, c, _, b, k, j) } initNameboard(o=1) { return this.bbComponent.initNameboard(this, o) } initBubble(o=1) { return this.bbComponent.initBubble(this, o) } say(o, {id: s, isUser: c, background: _, font: b="Arial", fontsize: k=38, fontcolor: j="#ffffff", fontstyle: $="bold", linesize: _e=22, linelimit: et, offsets: tt={ x: 0, y: 0, z: 55 }, scale: rt=this._avatarScale, compensationZ: it=11.2, reregistAnyway: nt=!0}) { return this.bbComponent.say(this, o, { id: s, isUser: c, background: _, font: b, fontsize: k, fontcolor: j, fontstyle: $, linesize: _e, linelimit: et, offsets: tt, scale: rt, compensationZ: it, reregistAnyway: nt }) } silent() { return this.bbComponent.silent() } setNickName(o, {id: s, isUser: c, background: _, font: b="Arial", fontsize: k=40, fontcolor: j="#ffffff", fontstyle: $="bold", linesize: _e=22, linelimit: et, offsets: tt={ x: 0, y: 0, z: 30 }, scale: rt=this._avatarScale, compensationZ: it=0, reregistAnyway: nt=!1}) { return this.bbComponent.setNickName(this, o, { id: s, isUser: c, background: _, font: b, fontsize: k, fontcolor: j, fontstyle: $, linesize: _e, linelimit: et, offsets: tt, scale: rt, compensationZ: it, reregistAnyway: nt }) } generateButtons(o=null, s=this._avatarScale, c=100) { return this.bbComponent.generateButtons(this, o, s, c) } clearButtons() { return this.bbComponent.clearButtons() } getSkeletonPositionByName(o) { var s; if (this.skeleton) { const c = this.skeleton.bones.find(_=>_.name.replace("Clone of ", "") == o); if (c && c.getTransformNode() && ((s = c.getTransformNode()) == null ? void 0 : s.position)) { const _ = c.getTransformNode().position; return xversePosition2Ue4({ x: _.x, y: _.y, z: _.z }) } } } shootTo(o, s, c=2, _=10, b={ x: 0, y: 0, z: 150 }) { return this.movementComponent.sendObjectTo(this, o, s, c, _, b) } generateShowName(o) { return o.split("_")[1] } getMaterialInput() { const o = new Map; return this.rootNode && this.rootNode.getChildMeshes()[3].material instanceof BABYLON.NodeMaterial && this.rootNode.getChildMeshes()[3].material.getInputBlocks().forEach(s=>{ s.name.includes("Editable") && (s._storedValue instanceof BABYLON.Color3 || s._storedValue instanceof BABYLON.Color4 ? o.set(this.generateShowName(s.name), s._storedValue.asArray()) : o.set(this.generateShowName(s.name), s._storedValue)) } ), o } setMaterialInput(o, s) { s ? this.assetList.forEach(c=>{ c.id == s && (c.materialConfig = o) } ) : (this.component.assetList.forEach(c=>{ const _ = this.component.dressMap.get(c.id); _ && _.asset.material instanceof BABYLON.NodeMaterial && o.forEach((b,k)=>{ const j = _.asset.material.getInputBlockByPredicate($=>$.name == generateOriginalName(k)); j && (b.length == 1 ? j._storedValue = b : b.length == 3 ? j._storedValue = BABYLON.Color3.FromArray(b) : b.length == 4 && (j._storedValue = BABYLON.Color3.FromArray(b.slice(0, 3)))) } ) } ), this.assetList.forEach(c=>{ c.materialConfig = o } )) } }