zhouenguang 3 yıl önce
ebeveyn
işleme
a72b2cdd1a
2 değiştirilmiş dosya ile 164 ekleme ve 313 silme
  1. 61 100
      src/XMaterialComponent.js
  2. 103 213
      src/Xverse_Room.js

+ 61 - 100
src/XMaterialComponent.js

@@ -99,53 +99,41 @@ export default class XMaterialComponent {
         );
     });
 
-    E(this, "updateRawYUVData", (stream, width, height, fov = -1) => {
-      var o, a;
-      fov == -1 && (fov = this.yuvInfo.fov);
-      if (this._isUpdateYUV == !0) {
-        console.log("执行:updateRawYUVData");
-        const s = {
-          width: width,
-          height: height,
-          fov: fov,
-        };
-        const videosResOriArrayIndex = this._videoRawYUVTexArray.findId(
-          width,
-          height
-        );
-        const currentVideoId = this._videoRawYUVTexArray.getCurrentVideoTexId();
+      E(this, "updateRawYUVData", (stream, width, height, fov=-1)=>{
+          fov == -1 && (fov = this.yuvInfo.fov);
 
-        if (
-          currentVideoId < 0 ||
-          videosResOriArrayIndex != currentVideoId ||
-          fov != this.yuvInfo.fov
-        ) {
-          this.yuvInfo.width = width;
-          this.yuvInfo.height = height;
-          this.yuvInfo.fov = fov;
-          this._videoRawYUVTexArray.setCurrentVideoTexId(
-            videosResOriArrayIndex
-          );
-          this._changeVideoRes(videosResOriArrayIndex);
-          this._changePureVideoLowModelShaderCanvasSize(s);
-          this._scenemanager.cameraComponent.cameraFovChange(s);
-          this._scenemanager.yuvInfo = s;
-        }
+          if (this._isUpdateYUV == !0) {
+              console.log('执行:updateRawYUVData')
+              const yuvInfo = { width, height, fov }
+              const videosResOriArrayIndex = this._videoRawYUVTexArray.findId(width, height)
+              const currentVideoId = this._videoRawYUVTexArray.getCurrentVideoTexId();
+
+              if(currentVideoId < 0 || videosResOriArrayIndex != currentVideoId || fov != this.yuvInfo.fov)
+              {
+                  this.yuvInfo.width = width;
+                  this.yuvInfo.height = height;
+                  this.yuvInfo.fov = fov;
+
+                  this._videoRawYUVTexArray.setCurrentVideoTexId(videosResOriArrayIndex);
+                  this._changeVideoRes(videosResOriArrayIndex);   // 设置texture_video
+                  this._changePureVideoLowModelShaderCanvasSize(yuvInfo); // 设置focal_width_height
+                  this._scenemanager.cameraComponent.cameraFovChange(yuvInfo);
+                  this._scenemanager.yuvInfo = yuvInfo;
+              }
+
+              let VideoTexture = this._videoRawYUVTexArray.getVideoYUVTex(videosResOriArrayIndex)
+              if(VideoTexture != null){
+                  // 更新视频流
+                  VideoTexture.update(stream)
+                  VideoTexture.updateSamplingMode(BABYLON.Texture.BILINEAR_SAMPLINGMODE)
+              }
+
+              //var o, a
+              //(o = this._videoRawYUVTexArray.getVideoYUVTex(videosResOriArrayIndex)) == null || o.update(stream),
+              //(a = this._videoRawYUVTexArray.getVideoYUVTex(videosResOriArrayIndex)) == null || a.updateSamplingMode(BABYLON.Texture.BILINEAR_SAMPLINGMODE)
+          }
+      });
 
-        let VideoTexture = this._videoRawYUVTexArray.getVideoYUVTex(
-          videosResOriArrayIndex
-        );
-        if (VideoTexture != null) {
-          console.log("stream", stream);
-          VideoTexture.update(stream);
-          VideoTexture.updateSamplingMode(
-            BABYLON.Texture.BILINEAR_SAMPLINGMODE
-          );
-        }
-        //(o = this._videoRawYUVTexArray.getVideoYUVTex(videosResOriArrayIndex)) == null || o.update(stream),
-        //(a = this._videoRawYUVTexArray.getVideoYUVTex(videosResOriArrayIndex)) == null || a.updateSamplingMode(BABYLON.Texture.BILINEAR_SAMPLINGMODE)
-      }
-    });
     E(this, "_changeVideoRes", (e) => {
       this._lowModelShader.setTexture(
         "texture_video",
@@ -567,61 +555,34 @@ export default class XMaterialComponent {
       pointLight.intensity = 0;
     }
   }
+
   _updatePanoShaderInput(e) {
-    var t, r, n, o, a, s, l, u, c, h;
-    if (this._isInDynamicRange(e))
-      if (
-        (this.scene.getLightByName("AvatarLight")
-          ? ((t = this._dynamic_shaders[e]) == null ||
-              t.setFloat("haveShadowLight", 1),
-            (o = this._dynamic_shaders[e]) == null ||
-              o.setTexture(
-                "shadowSampler",
-                (n =
-                  (r = this.scene.getLightByName("AvatarLight")) == null
-                    ? void 0
-                    : r.getShadowGenerator()) == null
-                  ? void 0
-                  : n.getShadowMapForRendering()
-              ),
-            (l = this._dynamic_shaders[e]) == null ||
-              l.setMatrix(
-                "lightSpaceMatrix",
-                (s =
-                  (a = this.scene.getLightByName("AvatarLight")) == null
-                    ? void 0
-                    : a.getShadowGenerator()) == null
-                  ? void 0
-                  : s.getTransformMatrix()
-              ))
-          : ((u = this._dynamic_shaders[e]) == null ||
-              u.setTexture("shadowSampler", null),
-            (c = this._dynamic_shaders[e]) == null ||
-              c.setMatrix("lightSpaceMatrix", new Matrix()),
-            (h = this._dynamic_shaders[e]) == null ||
-              h.setFloat("haveShadowLight", 0)),
-        this.scene.getLightByName("fireworkLight"))
-      )
-        this.scene.registerBeforeRender(() => {
-          var f;
-          this._dynamic_shaders[e].setFloat(
-            "fireworkLight",
-            this.scene.getLightByName("fireworkLight").getScaledIntensity()
-          ),
-            this._dynamic_shaders[e].setVector3(
-              "fireworkLightPosition",
-              (f = this.scene.getLightByName("fireworkLight")) == null
-                ? void 0
-                : f.position
-            );
-        });
-      else {
-        const f = new BABYLON.PointLight(
-          "fireworkLight",
-          new BABYLON.Vector3(0, 0, 0),
-          this.scene
-        );
-        f.intensity = 0;
+      var n,s;
+      if (this._isInDynamicRange(e)) 
+      {
+          let shader = this._dynamic_shaders[e]
+          let avatarLight = this.scene.getLightByName("AvatarLight")
+
+          shader == null || (avatarLight ? (
+              shader.setFloat("haveShadowLight", 1),
+              shader.setTexture("shadowSampler", (n = avatarLight == null ? void 0 : avatarLight.getShadowGenerator()) == null ? void 0 : n.getShadowMapForRendering()),
+              shader.setMatrix("lightSpaceMatrix", (s = avatarLight == null ? void 0 : avatarLight.getShadowGenerator()) == null ? void 0 : s.getTransformMatrix())
+          ) : (
+              shader.setTexture("shadowSampler", null),
+              shader.setMatrix("lightSpaceMatrix", new Matrix),
+              shader.setFloat("haveShadowLight", 0)
+          ))
+
+          let fireworkLight = this.scene.getLightByName("fireworkLight")
+          if (fireworkLight) {
+              this.scene.registerBeforeRender(()=>{
+                  shader.setFloat("fireworkLight", fireworkLight.getScaledIntensity()),
+                  shader.setVector3("fireworkLightPosition", fireworkLight == null ? void 0 : fireworkLight.position)
+              });
+          } else {
+              const f = new BABYLON.PointLight("fireworkLight", new BABYLON.Vector3(0,0,0), this.scene);
+              f.intensity = 0
+          }
       }
   }
-}
+}

+ 103 - 213
src/Xverse_Room.js

@@ -65,69 +65,64 @@ export default class Xverse_Room extends EventEmitter {
     E(this, "_currentClickingState", null);
     E(this, "signal", new Signal(this));
     E(this, "firstFrameTimestamp");
+
     E(this, "receiveRtcData", async () => {
       logger.info("Invoke receiveRtcData");
       let i = !1,
         o = !1,
         s = !1,
         c = !1;
-      return this.viewMode === "serverless"
-        ? (logger.warn("set view mode to serverless"),
-          this.setViewMode("observer").then(
-            () => this,
-            () => this
-          ))
-        : new Promise((d, _) => {
-            const b = this.networkController.rtcp.workers;
-            b.registerFunction("signal", (k) => {
-              this.signal.handleSignal(k, _);
-            }),
-              b.registerFunction("stream", (k) => {
-                var j;
-                if (
-                  (this.emit("streamTimestamp", {
-                    timestamp: Date.now(),
-                  }),
-                  o || ((o = !0), logger.warn("Invoke stream event")),
-                  k.stream)
-                ) {
-                  s || ((s = !0), logger.warn("Invoke updateRawYUVData")),
-                    (this.isUpdatedRawYUVData = !1);
-                  const $ =
-                    (j = this._currentState.skin) == null ? void 0 : j.fov;
-                  this.sceneManager.materialComponent.updateRawYUVData(
-                    k.stream,
-                    k.width,
-                    k.height,
-                    $
-                  ),
-                    (this.isUpdatedRawYUVData = !0);
+        return this.viewMode === "serverless" ? (
+            logger.warn("set view mode to serverless"),
+            this.setViewMode("observer").then(()=>this, ()=>this)
+        ) : new Promise((resolve, reject) => {
+            const workers = this.networkController.rtcp.workers;
+
+            workers.registerFunction("signal", data => {
+                // 更新坐标数据
+                this.signal.handleSignal(data, reject)
+            }), 
+
+            workers.registerFunction("stream", data => {
+                // 更新视频贴图数据
+                this.emit("streamTimestamp", {
+                    timestamp: Date.now()
+                })
+                o || (o = !0, logger.info("Invoke stream event"))
+                if (data.stream) {
+                    s || (s = !0, logger.info("Invoke updateRawYUVData"))
+                    this.isUpdatedRawYUVData = !1;
+                    const $ = this._currentState.skin == null ? void 0 : this._currentState.skin.fov;
+                    this.sceneManager.materialComponent.updateRawYUVData(data.stream, data.width, data.height, $)
+                    this.isUpdatedRawYUVData = !0
                 }
-                if (!i) {
-                  logger.warn("Invoke isAfterRenderRegistered");
-                  (i = !0),
+                if(!i){
+                    logger.info("Invoke isAfterRenderRegistered")
+                    i = !0
                     this.scene.registerAfterRender(() => {
-                      this.engineProxy.frameRenderNumber >= 2 &&
-                        (c ||
-                          ((c = !0), logger.warn("Invoke registerAfterRender")),
-                        this.isFirstDataUsed ||
-                          (logger.warn("Invoke isStreamAvailable"),
-                          (this.isFirstDataUsed = !0),
-                          (this.firstFrameTimestamp = Date.now()),
-                          d(this),
-                          this.afterJoinRoom()));
-                    });
+                        this.engineProxy.frameRenderNumber >= 2 
+                        && (c || (
+                            c = !0, 
+                            logger.info("Invoke registerAfterRender")), 
+                            this.isFirstDataUsed || (logger.info("Invoke isStreamAvailable"), 
+                            this.isFirstDataUsed = !0, 
+                            this.firstFrameTimestamp = Date.now(), 
+                            resolve(this), 
+                            this.afterJoinRoom()
+                        ))
+                    })
                 }
-              }),
-              this.panorama.bindListener(() => {
-                d(this), this.afterJoinRoom();
-              }),
-              b.registerFunction("reconnectedFrame", () => {});
-              logger.warn("Invoke decoderWorker.postMessage");
-              b.decoderWorker.postMessage({
-                t: 5,
-              });
-          });
+            }), 
+            this.panorama.bindListener(() => {
+                resolve(this)
+                this.afterJoinRoom()
+            }), 
+            workers.registerFunction("reconnectedFrame", () => {}), 
+            logger.info("Invoke decoderWorker.postMessage"), 
+            workers.decoderWorker.postMessage({
+                t: 5
+            })
+        })
     });
     E(this, "moveToExtra", "");
     (this.options = e),
@@ -200,28 +195,13 @@ export default class Xverse_Room extends EventEmitter {
   audienceViewModeHook() {}
   setViewModeToObserver() {}
   handleVehicleHook(e) {}
+  
   updateReporter() {
-    const {
-      avatarId: avatarId,
-      skinId: skinId,
-      userId: userId,
-      roomId: roomId,
-      role: role,
-      appId: appId,
-      wsServerUrl: wsServerUrl,
-    } = this.options;
-    reporter.updateHeader({
-      userId: userId,
-    }),
-      reporter.updateBody({
-        roomId: roomId,
-        role: role,
-        skinId: skinId,
-        avatarId: avatarId,
-        appId: appId,
-        wsServerUrl: wsServerUrl,
-      });
+      const {avatarId, skinId, userId, roomId, role, appId, wsServerUrl} = this.options;
+      reporter.updateHeader({ userId }),
+      reporter.updateBody({ roomId, role, skinId, avatarId, appId, wsServerUrl })
   }
+  
   async initRoom() {
     const { timeout: e = DEFAULT_JOINROOM_TIMEOUT } = this.options;
     if (util.isSupported()) {
@@ -230,113 +210,44 @@ export default class Xverse_Room extends EventEmitter {
       return Promise.reject(new UnsupportedError());
     }
   }
-  async _initRoom() {
-    const e = this.validateOptions(this.options);
-    if (e) {
-      return logger.error("initRoom param error", e), Promise.reject(e);
-    }
-    const {
-      canvas: canvas,
-      avatarId: avatarId,
-      skinId: skinId,
-      userId: userId,
-      wsServerUrl: wsServerUrl,
-      role: role,
-      token: token,
-      pageSession: pageSession,
-      rotationRenderType: rotationRenderType,
-      isAllSync: isAllSync = !1,
-      appId: f,
-      camera: d,
-      player: _,
-      avatarComponents: g,
-      nickname: nickname,
-      avatarScale: v,
-      firends: y = [],
-      syncByEvent: b = !1,
-      areaName: T,
-      attitude: C = MotionType.Walk,
-      pathName: A,
-      viewMode: S = "full",
-      person: P,
-      roomId: roomId,
-      roomTypeId: M,
-      hasAvatar: x = !1,
-      syncToOthers: I = !1,
-      prioritySync: w = !1,
-      removeWhenDisconnected: O = !0,
-      extra: D,
-    } = this.options;
 
-    this.setCurrentNetworkOptions({
-      avatarId: avatarId,
-      skinId: skinId,
-      roomId: roomId,
-      userId: userId,
-      wsServerUrl: wsServerUrl,
-      role: role,
-      token: token,
-      pageSession: pageSession,
-      rotationRenderType: rotationRenderType,
-      isAllSync: isAllSync,
-      appId: f,
-      camera: d,
-      player: _,
-      avatarComponents: g,
-      nickname: nickname,
-      avatarScale: v,
-      firends: y,
-      syncByEvent: b,
-      areaName: T,
-      attitude: C,
-      pathName: A,
-      person: P,
-      roomTypeId: M,
-      hasAvatar: x,
-      syncToOthers: I,
-      prioritySync: w,
-      extra: D,
-      removeWhenDisconnected: O,
-    });
-    this.userId = userId;
-    this.canvas = canvas;
-    T && (this.pathManager.currentArea = T);
-    this.networkController = new NetworkController(this);
-    this.setCurrentState({
-      areaName: T,
-      pathName: A,
-      attitude: C,
-      speed: 0,
-      viewMode: S,
-      state: this.networkController._state,
-      skinId: skinId,
-    });
-
-    try {
-      await Promise.all([
-        this.initNetwork(),
-        this.initConfig(),
-        this.initWasm(),
-      ]),
-        logger.info("network config wasm all ready, start to create game");
-      const F = await this.requestCreateRoom({
-          skinId: skinId,
-        }),
-        V = F.routeList.find((L) => L.areaName === T),
-        N = ((V == null ? void 0 : V.step) || 7.5) * 30;
-      this.updateCurrentState({
-        skin: F,
-        skinId: F.id,
-        versionId: F.versionId,
-        speed: N,
+  async _initRoom() {
+      const e = this.validateOptions(this.options);
+      if(e) {
+          return logger.error("initRoom param error", e),
+          Promise.reject(e);
+      }
+      const {
+          canvas, avatarId, skinId, userId, wsServerUrl, role, token, pageSession, rotationRenderType, isAllSync=!1, appId, camera, player, 
+          avatarComponents, nickname, avatarScale, firends=[], syncByEvent=!1, areaName, attitude=MotionType.Walk, pathName, viewMode="full", 
+          person, roomId, roomTypeId, hasAvatar=!1, syncToOthers=!1, prioritySync=!1, extra, removeWhenDisconnected=!0
+      } = this.options;
+      this.setCurrentNetworkOptions({
+          avatarId, skinId, roomId, userId, wsServerUrl, role, token, pageSession, rotationRenderType, isAllSync, appId, camera, player,
+          avatarComponents, nickname, avatarScale, firends, syncByEvent, areaName, attitude, pathName,
+          person, roomTypeId, hasAvatar, syncToOthers, prioritySync, extra, removeWhenDisconnected
       });
-      await this.initEngine(F);
-    } catch (F) {
-      return Promise.reject(F);
-    }
-    this.beforeJoinRoomResolve();
-    return this.receiveRtcData();
-  }
+        
+      this.userId = userId;
+      this.canvas = canvas;
+      areaName && (this.pathManager.currentArea = areaName);
+      this.networkController = new NetworkController(this);
+      this.setCurrentState({ areaName, pathName, attitude, speed: 0, viewMode, state: this.networkController._state, skinId });
+      try {
+          await Promise.all([this.initNetwork(), this.initConfig(), this.initWasm()]),
+          logger.info("network config wasm all ready, start to create game");
+          const skin = await this.requestCreateRoom({skinId})
+            , skinRoute = skin.routeList.find(route => route.areaName === areaName)
+            , speed = ((skinRoute == null ? void 0 : skinRoute.step) || 7.5) * 30;
+          this.updateCurrentState({ skin, skinId: skin.id, versionId: skin.versionId, speed }),
+          await this.initEngine(skin)
+      } catch (e) {
+          return Promise.reject(e)
+      }
+      this.beforeJoinRoomResolve();
+      return this.receiveRtcData()
+  }
+  
   beforeJoinRoomResolve() {
     this.setupStats(),
       (this.eventsController = new EventsController(this)),
@@ -376,44 +287,23 @@ export default class Xverse_Room extends EventEmitter {
     this.avatarManager.clearOtherUsers(), this.afterReconnectedHook();
   }
   leave() {
-    var e, t;
-    return (
-      logger.info("Invoke room.leave"),
-      (e = this.eventsController) == null || e.clearEvents(),
-      (t = this.networkController) == null || t.quit(),
+      return logger.info("Invoke room.leave"),
+      this.eventsController == null || this.eventsController.clearEvents(),
+      this.networkController == null || this.networkController.quit(),
       this
-    );
   }
+  
   validateOptions(e) {
-    const {
-      canvas: t,
-      avatarId: avatarId,
-      skinId: skinId,
-      userId: userId,
-      role: role,
-      roomId: roomId,
-      token: token,
-      appId: appId,
-      avatarComponents: avatarComponents,
-    } = e || {};
-    const h = [];
-    t instanceof HTMLCanvasElement ||
-      h.push(
-        new ParamError("`canvas` must be instanceof of HTMLCanvasElement")
-      );
-    (!userId || typeof userId != "string") &&
-      h.push(new ParamError("`userId` must be string"));
-    (!token || typeof token != "string") &&
-      h.push(new ParamError("`token` must be string"));
-    (!appId || typeof appId != "string") &&
-      h.push(new ParamError("`appId` must be string"));
-    role == "audience" ||
-      ((!avatarId || !skinId) &&
-        h.push(
-          new ParamError("`avatarId` and `skinId` is required when create room")
-        ));
-    return h[0];
+      const {canvas, avatarId, skinId, userId, role, roomId, token, appId, avatarComponents} = e || {}
+      const h = [];
+      canvas instanceof HTMLCanvasElement || h.push(new ParamError("`canvas` must be instanceof of HTMLCanvasElement"));
+      (!userId || typeof userId != "string") && h.push(new ParamError("`userId` must be string"));
+      (!token || typeof token != "string") && h.push(new ParamError("`token` must be string"));
+      (!appId || typeof appId != "string") && h.push(new ParamError("`appId` must be string"));
+      role == "audience" || (!avatarId || !skinId) && h.push(new ParamError("`avatarId` and `skinId` is required when create room"));
+      return h[0]
   }
+
   async initNetwork() {
     if (this.viewMode === "serverless") return Promise.resolve();
     const e = Date.now();