test pc 3 years ago
parent
commit
22c7749d4e
3 changed files with 279 additions and 12 deletions
  1. 107 0
      src/get-router/get-router.service.ts
  2. 153 11
      src/move/move.service.ts
  3. 19 1
      src/scene/scene.service.ts

+ 107 - 0
src/get-router/get-router.service.ts

@@ -214,6 +214,113 @@ export class GetRouterService implements OnModuleInit {
     }
   }
 
+
+  searchRoad2(startPointId, endPointId) {
+    try {
+      if (startPointId == endPointId) {
+        console.log('点击的点和当前点是同一个点');
+        return null;
+      }
+
+      const startPoint = this.breakPointInfo[startPointId];
+      if (startPoint.contact.indexOf(endPointId) > -1) {
+        return [startPointId, endPointId];
+      }
+
+      const endPoint = this.breakPointInfo[endPointId];
+      const openList = [], //开启列表
+        closeList = []; //关闭列表
+      let result = []; //结果数组
+      let result_index: number; //结果数组在开启列表中的序号
+
+      //把当前点加入到开启列表中,并且G是0
+      openList.push({
+        breakPointId: startPointId,
+        G: 0,
+        position: startPoint.position,
+        contact: startPoint.contact,
+      }); //把当前点加入到开启列表中,并且G是0
+
+      do {
+        const currentPoint = openList.pop();
+        closeList.push(currentPoint);
+        if (closeList.length > 10000) {
+          console.log('错误过渡路径:', closeList.length);
+          return null;
+        }
+        let surroundPoint = [];
+        const _breakPoint = this.breakPointInfo[currentPoint.breakPointId];
+        surroundPoint = _breakPoint.contact;
+        for (let i = 0; i < surroundPoint.length; ++i) {
+          const neighPointId = surroundPoint[i];
+          const item = this.breakPointInfo[neighPointId];
+          if (this.existList(item, closeList) != -1) {
+            continue;
+          }
+          item.breakPointId = neighPointId;
+          //g 到父节点的位置
+          const g =
+            currentPoint.G +
+            this.getDistance(currentPoint.position, item.position);
+          if (this.existList(item, openList) == -1) {
+            //如果不在开启列表中
+            item['H'] = this.getDistance(endPoint.position, item.position);
+            item['G'] = g;
+            item['F'] = item.H + item.G;
+            item['parent'] = currentPoint;
+            openList.push(item);
+          } else {
+            //存在在开启列表中,比较目前的g值和之前的g的大小
+            const index = this.existList(item, openList);
+            //如果当前点的g更小
+            if (g < openList[index].G) {
+              openList[index].parent = currentPoint;
+              openList[index].G = g;
+              openList[index].F = g + openList[index].H;
+            }
+          }
+        }
+        //如果开启列表空了,没有通路,结果为空
+        if (openList.length == 0) {
+          break;
+        }
+
+        openList.sort(this.sortF); //这一步是为了循环回去的时候,找出 F 值最小的, 将它从 "开启列表" 中移掉
+      } while ((result_index = this.existList(endPoint, openList)) == -1);
+
+      //判断结果列表是否为空
+      let currentObj;
+      if (result_index == -1) {
+        result = [];
+      } else {
+        currentObj = openList[result_index];
+        do {
+          //把路劲节点添加到result当中
+          result.unshift(currentObj.breakPointId);
+          currentObj = currentObj.parent;
+          if (!currentObj) {
+            break;
+          }
+        } while (
+          result.length < 50 &&
+          currentObj.contact.indexOf(startPointId) < 0
+        );
+      }
+      if (result.length > 50) {
+        console.log('错误过渡路径:' + result);
+        // debugger;
+        return null;
+      }
+      result.unshift(currentObj.breakPointId);
+      result.unshift(startPointId);
+      console.log('path-end' + result);
+      return result;
+    } catch (error) {
+      console.error('searchRoad', error);
+      return [];
+    }
+  }
+
   //用F值对数组排序
   sortF(a, b) {
     return b.F - a.F;

+ 153 - 11
src/move/move.service.ts

@@ -802,10 +802,10 @@ export class MoveService implements OnModuleInit {
         //   console.log('20220708_cameraInfos不取值');
         //   return this.reply;
         // }
-        if(user.lastJoyStickMediaSrc){
-          user.lastJoyStickMediaSrc = false;
-          return this.reply;
-        }
+        // if(user.lastJoyStickMediaSrc){
+        //   user.lastJoyStickMediaSrc = false;
+        //   return this.reply;
+        // }
         const cameraInfo = this.getCameraInfo();
         if (cameraInfo != null) {
           console.log(
@@ -820,6 +820,7 @@ export class MoveService implements OnModuleInit {
             this.reply.mediaSrc = cameraInfo.mediaSrc;
             this.reply.isIDR = cameraInfo.isIDR;
             user.lastJoyStickMediaSrc = true;
+            console.log('20220708_mediaSrc1:'+cameraInfo.mediaSrc);
           }
 
           user.camera.position = JSON.parse(
@@ -1313,12 +1314,13 @@ export class MoveService implements OnModuleInit {
     // if(this.cameraInfos.length>MaxBufferCameraInfo){
     //   moveInterval = 1;
     // }
-    let moveInterval = 4;
+    let moveInterval = 2;
     let flag = false;
     for (let i = 0; i < moveFrames.length; ++i) {
-      if(i!=0 && i%moveInterval == 0){
+      if(i!= 0 && i!= moveFrames.length-1 && i%moveInterval == 0){
         continue;
       }
+      moveFrames[i].startBreakPointId = startBreakPointId;
       moveFrames[i].endBreakPointId = endBreakPointId;
       moveFrames[i].mediaSrc =
         '/' +
@@ -1351,6 +1353,7 @@ export class MoveService implements OnModuleInit {
           '?m=' +
           new Date().getTime();
       }
+      console.log('20220708_mediaSrc3:'+moveFrames[i].mediaSrc);
       if (i == 0 || i == moveFrames.length - 1) {
         moveFrames[i].isIDR = true;
         if(i == moveFrames.length - 1){
@@ -1397,6 +1400,7 @@ export class MoveService implements OnModuleInit {
       }
       cameraInfo.isIDR = true;
       this.cameraInfos.push(cameraInfo);
+      console.log('20220708_mediaSrc4:'+cameraInfo.mediaSrc);
     }
   }
 
@@ -1421,7 +1425,6 @@ export class MoveService implements OnModuleInit {
 
   complementFrame(userId) {
     if (this.cameraInfos.length > 0) {
-      this.updateCameraInfoForDely();
       console.log('20220627test-执行complementFrame,' + new Date().getTime());
       const user = this.users[userId];
       const cameraInfo = this.cameraInfos.shift();
@@ -1469,11 +1472,150 @@ export class MoveService implements OnModuleInit {
     }
   }
 
-  updateCameraInfoForDely(){
-    if (this.cameraInfos.length > 20) {
-      let _interval = Math.floor(this.cameraInfos.length/12);
-      this.cameraInfos = this.cameraInfos.filter((itme,index)=>index%_interval === 0);
+  // createDefaultReply(userId){
+  //   const user = this.users[userId];
+  //   this.reply.traceIds = [];
+  //   this.reply['newUserStates'][0].userId = userId;
+  //   this.reply['actionResponses'][0].traceId = null;
+  //   this.reply['newUserStates'][0].playerState.player.angle.yaw =
+  //     user.player.angle.yaw;
+  //   this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
+  //     JSON.stringify(user.player.position),
+  //   );
+  //   this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
+  //     JSON.stringify(user.camera.angle),
+  //   );
+  //   this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
+  //     JSON.stringify(user.camera.position),
+  //   );
+
+  //   this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
+  //     JSON.stringify(user.player.position),
+  //   );
+  //   this.reply['newUserStates'][0].renderInfo.isMoving = 0;
+  //   this.reply['actionResponses'][0].traceId = null;
+  //   return JSON.parse(JSON.stringify(this.reply));
+  // }
+
+  async updateCameraInfoForDely(appId,userId,path){
+
+    if(path == null){
+      this.cameraInfos = [];
+      return;
+    }
+    const user = this.users[userId];
+    const index = this.getMoveIndex(user.camera.angle.yaw);
+    let frames = [];
+
+    let pointIds = [];
+
+    for (let i = 0; i < path.length - 1; ++i) {
+      const start_break_point_id = path[i];
+      const end_break_point_id = path[i + 1];
+      const moveFrames = await this.getMoveFrames(
+        appId,
+        start_break_point_id,
+        end_break_point_id,
+        index,
+      );
+      if (!moveFrames) {
+        return null;
+      }
+      frames = frames.concat(moveFrames)
+      pointIds.push({
+        index:frames.length,
+        startBreakPointId:start_break_point_id,
+        endBreakPointId:end_break_point_id
+      });
     }
+    this.cameraInfos = [];
+    const interval = Math.floor(frames.length/12);
+    for (let i = 0; i < frames.length-1; i+=interval) {
+      const frame = frames[i];
+      let isIDR = false;
+      if(i == 0){
+        isIDR = true;
+      }
+
+      let startBreakPointId,endBreakPointId;
+      for(let j=0;j<pointIds.length;++j){
+        if(i<pointIds[j].index+1){
+          startBreakPointId = pointIds[j].startBreakPointId;
+          endBreakPointId = pointIds[j].endBreakPointId;
+          break;
+        }
+      }
+
+        let mediaSrc =
+        '/' +
+        appId +
+        '/' +
+        startBreakPointId +
+        '/' +
+        frame.file_name.substring(0, frame.file_name.indexOf('.')) +
+        '/' +
+        frame.file_name +
+        '?m=' +
+        new Date().getTime();
+      if (startBreakPointId > endBreakPointId) {
+        mediaSrc =
+          '/' +
+          appId +
+          '/' +
+          endBreakPointId +
+          '/' +
+          frame.file_name.substring(0, frame.file_name.indexOf('.')) +
+          '/' +
+          frame.file_name +
+          '?m=' +
+          new Date().getTime();
+      }
+      console.log('20220708_mediaSrc2:'+mediaSrc);
+      this.cameraInfos.push({
+        startBreakPointId:startBreakPointId,
+        endBreakPointId:endBreakPointId,
+        camera_position: frame.camera_position,
+        camera_angle: frame.camera_angle,
+        mediaSrc: mediaSrc,
+        isIDR: isIDR,
+      });
+    }
+
+    const startBreakPointId = pointIds[pointIds.length-1].startBreakPointId;
+    const endBreakPointId = pointIds[pointIds.length-1].endBreakPointId;
+    const frame = frames[frames.length-1];
+    let mediaSrc =
+        '/' +
+        appId +
+        '/' +
+        startBreakPointId +
+        '/' +
+        frame.file_name.substring(0, frame.file_name.indexOf('.')) +
+        '/' +
+        frame.file_name +
+        '?m=' +
+        new Date().getTime();
+      if (startBreakPointId > endBreakPointId) {
+        frame.mediaSrc =
+          '/' +
+          appId +
+          '/' +
+          endBreakPointId +
+          '/' +
+          frame.file_name.substring(0, frame.file_name.indexOf('.')) +
+          '/' +
+          frame.file_name +
+          '?m=' +
+          new Date().getTime();
+      }
+      this.cameraInfos.push({
+        startBreakPointId:startBreakPointId,
+        endBreakPointId:endBreakPointId,
+        camera_position: frame.camera_position,
+        camera_angle: frame.camera_angle,
+        mediaSrc: mediaSrc,
+        isIDR: true,
+      });
   }
 
   stopJoystick(userId) {

+ 19 - 1
src/scene/scene.service.ts

@@ -547,6 +547,22 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     }
   }
 
+  async getSimplestCameraInfo(appId,userId){
+    if(this.moveService.cameraInfos.length>20){
+      const startBreakPointId = this.moveService.cameraInfos[0].startBreakPointId;
+      const endBreakPointId = this.moveService.cameraInfos[this.moveService.cameraInfos.length-1].endBreakPointId;
+
+      const path = this.getRouterService.searchRoad2(startBreakPointId,endBreakPointId);
+      if(path == null){
+        //可以清空cameraInfos
+        this.moveService.cameraInfos = [];
+      }
+      else{
+        await this.moveService.updateCameraInfoForDely(appId,userId,path);
+      }
+    }
+  }
+
   /**
    * 行走动作
    *
@@ -913,11 +929,13 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   /**
    * Joystick Stop function
    */
-  handleJoystickStop(hasPush: StreamPushResponse) {
+  async handleJoystickStop(hasPush: StreamPushResponse) {
     // 最后一帧200ms
     clearTimeout(this._JoyStickingSteamTimeout);
     this._JoyStickingSteamTimeout = setTimeout(async () => {
       console.log('20220627test-complementFrame handleJoystickStop 200ms之后');
+      const user = this.moveService.users[this.user_id];
+      await this.getSimplestCameraInfo(user.appId,this.user_id);
       const complementFrame = this.moveService.complementFrame(
         this.user_id,
       ) as StreamReplyType;