123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- const log$9 = new Logger("camera");
- class Camera extends EventEmitter {
- constructor(e) {
- super();
- E(this, "initialFov", 0);
- E(this, "_state", CameraStates.Normal);
- E(this, "_person", Person.Third);
- E(this, "_room");
- E(this, "_cameraFollowing", !0);
- E(this, "checkPointOnLeftOrRight", e=>{
- const t = ue4Position2Xverse(e);
- if (!t || this.checkPointInView(e))
- return;
- const o = this._room.scene.activeCamera;
- if (!o)
- return;
- const a = [o.target.x, o.target.y, o.target.z]
- , s = [o.position.x, o.position.y, o.position.z]
- , {x: l, y: u, z: c} = t
- , h = calNormVector(s, a)
- , f = calNormVector(s, [l, u, c]);
- return vectorCrossMulti(h, f) < 0 ? Direction.Right : Direction.Left
- }
- );
- E(this, "checkPointInView", ({x: e, y: t, z: r})=>{
- const n = ue4Position2Xverse({
- x: e,
- y: t,
- z: r
- });
- if (!n)
- return !1;
- for (let o = 0; o < 6; o++)
- if (this._room.scene.frustumPlanes[o].dotCoordinate(n) < 0)
- return !1;
- return !0
- }
- );
- this._room = e
- }
- get person() {
- return this._person
- }
- get state() {
- return this._state
- }
- get pose() {
- return this._room.currentClickingState.camera
- }
- set cameraFollowing(e) {
- log$9.info("cameraFollowing setter", e),
- this.setCameraFollowing({
- isFollowHost: e
- })
- }
- get cameraFollowing() {
- return this._cameraFollowing
- }
- setCameraFollowing({isFollowHost: e}) {}
- handleRenderInfo(e) {
- const {cameraStateType: t} = e.renderInfo
- , r = this._room.sceneManager;
- if (t !== this._state && (this._state = t,
- log$9.debug("camera._state changed to", CameraStates[t]),
- t === CameraStates.CGView ? (r.cameraComponent.switchToCgCamera(),
- r.staticmeshComponent.getCgMesh().show()) : (r.cameraComponent.switchToMainCamera(),
- r.staticmeshComponent.getCgMesh().hide()),
- this.emit("stateChanged", {
- state: t
- })),
- this._room.isHost)
- return;
- const {isFollowHost: n} = e.playerState;
- !!n !== this._cameraFollowing && (this._cameraFollowing = !!n,
- this.emit("cameraFollowingChanged", {
- cameraFollowing: !!n
- }))
- }
- setCameraState({state: e}) {
- if (this._state === e) {
- log$9.warn(`You are already in ${CameraStates[e]} camera state`);
- return
- }
- e === CameraStates.Normal || this._state === CameraStates.ItemView && log$9.warn("CloseUp camera state can only be triggerd by room internally")
- }
- turnToFace({extra: e="", offset: t=0}) {
- const r = {
- action_type: Actions.TurnToFace,
- turn_to_face_action: {
- offset: t
- }
- };
- return this.emit("viewChanged", {
- extra: e
- }),
- this._room.actionsHandler.sendData({
- data: r
- })
- }
- isInDefaultView() {
- if (!this._room.isHost) {
- log$9.warn("It is recommended to call the function on the host side");
- return
- }
- if (!this._room._currentClickingState)
- return log$9.error("CurrentState should not be empty"),
- !1;
- const {camera: e, player: t} = this._room._currentClickingState;
- return Math.abs(t.angle.yaw - 180 - e.angle.yaw) % 360 <= 4
- }
- async screenShot({name: e, autoSave: t=!1}) {
- const r = this._room.scene.getEngine()
- , n = this._room.scene.activeCamera;
- try {
- this._room.sceneManager.setImageQuality(EImageQuality.high);
- const o = await CreateScreenshotAsync(r, n, {
- precision: 1
- });
- return this._room.sceneManager.setImageQuality(EImageQuality.low),
- t === !0 && downloadFileByBase64(o, e),
- Promise.resolve(o)
- } catch (o) {
- return this._room.sceneManager.setImageQuality(EImageQuality.low),
- Promise.reject(o)
- }
- }
- changeToFirstPerson(e, t, r) {
- const {camera: n, player: o, attitude: a, areaName: s, pathName: l} = e;
- return this._room.actionsHandler.requestPanorama({
- camera: n,
- player: o,
- attitude: a,
- areaName: s,
- pathName: l
- }, t, r).then(()=>{
- this._room.networkController.rtcp.workers.changePanoMode(!0);
- const {position: u, angle: c} = o || {};
- this._room.sceneManager.cameraComponent.changeToFirstPersonView({
- position: u,
- rotation: c
- })
- }
- )
- }
- setPerson(e, t={
- camera: this._room._currentClickingState.camera,
- player: this._room._currentClickingState.player
- }) {
- const r = Date.now();
- return this._setPerson(e, t).then(n=>(log$9.infoAndReportMeasurement({
- tag: Person[e],
- startTime: r,
- metric: "setPerson"
- }),
- n)).catch(n=>(log$9.infoAndReportMeasurement({
- tag: Person[e],
- startTime: r,
- metric: "setPerson",
- error: n
- }),
- Promise.reject(n)))
- }
- _setPerson(e, t={
- camera: this._room._currentClickingState.camera,
- player: this._room._currentClickingState.player
- }) {
- return e !== Person.First && e !== Person.Third ? Promise.reject("invalid person " + e) : !t.camera || !t.player ? Promise.reject(new ParamError("wrong camera or player")) : e === Person.First ? this._room.panorama.access({
- camera: t.camera,
- player: t.player,
- tag: "setPerson"
- }).then(()=>{
- var o, a;
- this._person = e,
- (o = this._room._userAvatar) == null || o.hide();
- const {position: r, angle: n} = ((a = this._room.currentClickingState) == null ? void 0 : a.camera) || {};
- !r || !n || this._room.sceneManager.cameraComponent.changeToFirstPersonView({
- position: r,
- rotation: n
- })
- }
- ) : this._room.panorama.exit({
- camera: t.camera,
- player: t.player
- }).then(()=>{
- var r, n;
- this._person = e,
- (r = this._room._userAvatar) != null && r.xAvatar && ((n = this._room._userAvatar) == null || n.xAvatar.show())
- }
- )
- }
- setCameraPose(e) {
- this._room.sceneManager.cameraComponent.setCameraPose({
- position: e.position,
- rotation: e.angle
- })
- }
- setMainCameraRotationLimit(e) {
- const {limitAxis: t, limitRotation: r} = e;
- this._room.sceneManager.cameraComponent.setMainCameraRotationLimit(t, r)
- }
- }
|