Camera.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. const log$9 = new Logger("camera");
  2. class Camera extends EventEmitter {
  3. constructor(e) {
  4. super();
  5. E(this, "initialFov", 0);
  6. E(this, "_state", CameraStates.Normal);
  7. E(this, "_person", Person.Third);
  8. E(this, "_room");
  9. E(this, "_cameraFollowing", !0);
  10. E(this, "checkPointOnLeftOrRight", e=>{
  11. const t = ue4Position2Xverse(e);
  12. if (!t || this.checkPointInView(e))
  13. return;
  14. const o = this._room.scene.activeCamera;
  15. if (!o)
  16. return;
  17. const a = [o.target.x, o.target.y, o.target.z]
  18. , s = [o.position.x, o.position.y, o.position.z]
  19. , {x: l, y: u, z: c} = t
  20. , h = calNormVector(s, a)
  21. , f = calNormVector(s, [l, u, c]);
  22. return vectorCrossMulti(h, f) < 0 ? Direction.Right : Direction.Left
  23. }
  24. );
  25. E(this, "checkPointInView", ({x: e, y: t, z: r})=>{
  26. const n = ue4Position2Xverse({
  27. x: e,
  28. y: t,
  29. z: r
  30. });
  31. if (!n)
  32. return !1;
  33. for (let o = 0; o < 6; o++)
  34. if (this._room.scene.frustumPlanes[o].dotCoordinate(n) < 0)
  35. return !1;
  36. return !0
  37. }
  38. );
  39. this._room = e
  40. }
  41. get person() {
  42. return this._person
  43. }
  44. get state() {
  45. return this._state
  46. }
  47. get pose() {
  48. return this._room.currentClickingState.camera
  49. }
  50. set cameraFollowing(e) {
  51. log$9.info("cameraFollowing setter", e),
  52. this.setCameraFollowing({
  53. isFollowHost: e
  54. })
  55. }
  56. get cameraFollowing() {
  57. return this._cameraFollowing
  58. }
  59. setCameraFollowing({isFollowHost: e}) {}
  60. handleRenderInfo(e) {
  61. const {cameraStateType: t} = e.renderInfo
  62. , r = this._room.sceneManager;
  63. if (t !== this._state && (this._state = t,
  64. log$9.debug("camera._state changed to", CameraStates[t]),
  65. t === CameraStates.CGView ? (r.cameraComponent.switchToCgCamera(),
  66. r.staticmeshComponent.getCgMesh().show()) : (r.cameraComponent.switchToMainCamera(),
  67. r.staticmeshComponent.getCgMesh().hide()),
  68. this.emit("stateChanged", {
  69. state: t
  70. })),
  71. this._room.isHost)
  72. return;
  73. const {isFollowHost: n} = e.playerState;
  74. !!n !== this._cameraFollowing && (this._cameraFollowing = !!n,
  75. this.emit("cameraFollowingChanged", {
  76. cameraFollowing: !!n
  77. }))
  78. }
  79. setCameraState({state: e}) {
  80. if (this._state === e) {
  81. log$9.warn(`You are already in ${CameraStates[e]} camera state`);
  82. return
  83. }
  84. e === CameraStates.Normal || this._state === CameraStates.ItemView && log$9.warn("CloseUp camera state can only be triggerd by room internally")
  85. }
  86. turnToFace({extra: e="", offset: t=0}) {
  87. const r = {
  88. action_type: Actions.TurnToFace,
  89. turn_to_face_action: {
  90. offset: t
  91. }
  92. };
  93. return this.emit("viewChanged", {
  94. extra: e
  95. }),
  96. this._room.actionsHandler.sendData({
  97. data: r
  98. })
  99. }
  100. isInDefaultView() {
  101. if (!this._room.isHost) {
  102. log$9.warn("It is recommended to call the function on the host side");
  103. return
  104. }
  105. if (!this._room._currentClickingState)
  106. return log$9.error("CurrentState should not be empty"),
  107. !1;
  108. const {camera: e, player: t} = this._room._currentClickingState;
  109. return Math.abs(t.angle.yaw - 180 - e.angle.yaw) % 360 <= 4
  110. }
  111. async screenShot({name: e, autoSave: t=!1}) {
  112. const r = this._room.scene.getEngine()
  113. , n = this._room.scene.activeCamera;
  114. try {
  115. this._room.sceneManager.setImageQuality(EImageQuality.high);
  116. const o = await CreateScreenshotAsync(r, n, {
  117. precision: 1
  118. });
  119. return this._room.sceneManager.setImageQuality(EImageQuality.low),
  120. t === !0 && downloadFileByBase64(o, e),
  121. Promise.resolve(o)
  122. } catch (o) {
  123. return this._room.sceneManager.setImageQuality(EImageQuality.low),
  124. Promise.reject(o)
  125. }
  126. }
  127. changeToFirstPerson(e, t, r) {
  128. const {camera: n, player: o, attitude: a, areaName: s, pathName: l} = e;
  129. return this._room.actionsHandler.requestPanorama({
  130. camera: n,
  131. player: o,
  132. attitude: a,
  133. areaName: s,
  134. pathName: l
  135. }, t, r).then(()=>{
  136. this._room.networkController.rtcp.workers.changePanoMode(!0);
  137. const {position: u, angle: c} = o || {};
  138. this._room.sceneManager.cameraComponent.changeToFirstPersonView({
  139. position: u,
  140. rotation: c
  141. })
  142. }
  143. )
  144. }
  145. setPerson(e, t={
  146. camera: this._room._currentClickingState.camera,
  147. player: this._room._currentClickingState.player
  148. }) {
  149. const r = Date.now();
  150. return this._setPerson(e, t).then(n=>(log$9.infoAndReportMeasurement({
  151. tag: Person[e],
  152. startTime: r,
  153. metric: "setPerson"
  154. }),
  155. n)).catch(n=>(log$9.infoAndReportMeasurement({
  156. tag: Person[e],
  157. startTime: r,
  158. metric: "setPerson",
  159. error: n
  160. }),
  161. Promise.reject(n)))
  162. }
  163. _setPerson(e, t={
  164. camera: this._room._currentClickingState.camera,
  165. player: this._room._currentClickingState.player
  166. }) {
  167. 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({
  168. camera: t.camera,
  169. player: t.player,
  170. tag: "setPerson"
  171. }).then(()=>{
  172. var o, a;
  173. this._person = e,
  174. (o = this._room._userAvatar) == null || o.hide();
  175. const {position: r, angle: n} = ((a = this._room.currentClickingState) == null ? void 0 : a.camera) || {};
  176. !r || !n || this._room.sceneManager.cameraComponent.changeToFirstPersonView({
  177. position: r,
  178. rotation: n
  179. })
  180. }
  181. ) : this._room.panorama.exit({
  182. camera: t.camera,
  183. player: t.player
  184. }).then(()=>{
  185. var r, n;
  186. this._person = e,
  187. (r = this._room._userAvatar) != null && r.xAvatar && ((n = this._room._userAvatar) == null || n.xAvatar.show())
  188. }
  189. )
  190. }
  191. setCameraPose(e) {
  192. this._room.sceneManager.cameraComponent.setCameraPose({
  193. position: e.position,
  194. rotation: e.angle
  195. })
  196. }
  197. setMainCameraRotationLimit(e) {
  198. const {limitAxis: t, limitRotation: r} = e;
  199. this._room.sceneManager.cameraComponent.setMainCameraRotationLimit(t, r)
  200. }
  201. }