123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- 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
- }
- ))
- }
- }
|