zhouenguang 3 年之前
父节点
当前提交
4447ae6847
共有 6 个文件被更改,包括 436 次插入94 次删除
  1. 50 2
      index.html
  2. 65 71
      modules/CameraController.js
  3. 5 4
      modules/CharactorManager.js
  4. 75 12
      modules/index.js
  5. 16 5
      modules/utils/common.js
  6. 225 0
      textures/outputmp4/points1.json

+ 50 - 2
index.html

@@ -4,6 +4,7 @@
     <head>
       <meta charset="UTF-8" />
       <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+
       <title>demo</title>
       <style>
         html,
@@ -23,6 +24,31 @@
 
         #videoTextureBox video {
           display: none;
+          position: fixed; left: 0px; top: 0px; z-index: 1000; width: 300px; height: 300px;
+        }
+
+        @media screen and (orientation: portrait){
+          #renderCanvas{  
+            position:absolute;
+            width: 100vh;
+            height: 100vw;
+            top: 0;
+            left: 100vw;
+            -webkit-transform:rotate(90deg);
+            -moz-transform: rotate(90deg);
+            -ms-transform: rotate(90deg);
+            transform:rotate(90deg);
+            transform-origin: 0% 0%;
+          }  
+        }
+        @media screen and (orientation: landscape) {
+          #renderCanvas {
+            position:absolute;
+            top:0;
+            left:0;
+            width: 100vw;
+            height: 100vh;
+          }
         }
       </style>
     </head>
@@ -45,9 +71,31 @@
       <canvas id="renderCanvas"></canvas>
 
       <div id="videoTextureBox">
+        <video 
+          id="houseTexture0" 
+          src="./textures/outputmp4/0/0.mp4" 
+          crossorigin="anonymous" 
+          playsinline="true" 
+          x5-playsinline="true" 
+          webkit-playsinline="true" 
+          x5-video-player-type="h5" 
+          controls="true" 
+          autoplay 
+          loop 
+          muted 
+        ></video>
+        <video
+          id="houseTexture"
+          src=""
+          crossorigin="anonymous"
+          playsinline
+          autoplay
+          muted
+          loop
+        ></video>
         <video
-          id="houseTexture0"
-          src="./textures/outputmp4/0/0.mp4"
+          id="houseTextureReverse"
+          src=""
           crossorigin="anonymous"
           playsinline
           autoplay

+ 65 - 71
modules/CameraController.js

@@ -66,7 +66,7 @@ export default class CameraController {
 
     endMouseRotate() {
         this.lastFramePoint = null
-        this.lastDirc = 0
+        // this.lastDirc = 0
     }
 
     /**
@@ -101,103 +101,97 @@ export default class CameraController {
         // && dirc * this.lastDirc <= 0
         else if(Math.abs(this.initAlpha) > Math.PI / 30 && dirc != 0 && this.enable) {
 
-            let currentPath = charactor.walkData.pathArr[charactor.walkData.currentPoint]
-            let startPoint = (currentPath && currentPath.point) || charactor.mesh.position
+            // let currentPath = charactor.walkData.pathArr[charactor.walkData.currentPoint]
+            // let startPoint = (currentPath && currentPath.point) || charactor.mesh.position
 
             // let point1 = Math.floor((-this.camera.alpha + Math.PI * 2) % (Math.PI * 2) / Math.PI * 180 / 6) * 6
             // let point2 = Math.floor((-this.camera.alpha - this.initAlpha + Math.PI * 2) % (Math.PI * 2) / Math.PI * 180 / 6) * 6
             let point1 = Math.floor(this.camera.alpha / Math.PI * 180 / 6) * 6
             let point2 = Math.floor((this.camera.alpha + this.initAlpha) / Math.PI * 180 / 6) * 6
-            let sangle, eangle
+            // let sangle, eangle
             
             this.alphaOffset = (point1 - point2) / 180 * Math.PI
             
-            if(dirc < 0) {
-                sangle = point1
-                eangle = point2 == 0 ? 359 : point2
-            } else {
-                sangle = point2
-                eangle = point1 == 0 ? 359 : point1
-            }
+            // if(dirc < 0) {
+            //     sangle = point1
+            //     eangle = point2 == 0 ? 359 : point2
+            // } else {
+            //     sangle = point2
+            //     eangle = point1 == 0 ? 359 : point1
+            // }
 
-            let pointData = this.app.charactorManager.getClosestPointData(startPoint)
-            let sendData = {
-                videoPath: pointData.id + "/" + pointData.id,
-                sangle: sangle,
-                eangle: eangle,
-                reverses: dirc < 0,
-                sceneCode: settings.sceneCode,
-                roomId: settings.roomId,
-                userId: settings.userId,
-            }
+            // let pointData = this.app.charactorManager.getClosestPointData(startPoint)
+            // let sendData = {
+            //     videoPath: pointData.id + "/" + pointData.id,
+            //     sangle: sangle,
+            //     eangle: eangle,
+            //     reverses: dirc < 0,
+            //     sceneCode: settings.sceneCode,
+            //     roomId: settings.roomId,
+            //     userId: settings.userId,
+            // }
             // window.connection.socket.emit("getRotateVideo", sendData);
             // console.log("[3D] send: ", sendData)
 
-            this.rotateCamera(this.alphaOffset)
+            this.rotateCamera(this.alphaOffset, dirc)
 
             this.enable = false
             this.initAlpha = 0
         }
 
         this.lastFramePoint = currentFramePoint
-        this.lastDirc = dirc
+        if(dirc != 0 && this.lastDirc * dirc > 0) this.lastDirc = dirc
     }
 
-    // async 
-    rotateCamera(alphaOffset, func) {
+    async rotateCamera(alphaOffset, dirc, func) {
 
         console.log("alphaOffset", alphaOffset)
 
         let video0 = this.app.house[1].material._textures.texture_video.video
-        let startTime = Math.abs(this.camera.alpha) % (Math.PI * 2) / (Math.PI * 2) * video0.duration
-
-        // if(this.lastDirc * alphaOffset < 0) {
-        //     if(alphaOffset < 0) {   // 顺时针
-        //         video0 = document.getElementById("houseTexture0")
-        //         this.app.updateHouseVideo(video0)
-        //     }
-        //     if(alphaOffset > 0) {   // 逆时针
-        //         video0 = document.getElementById("houseTexture0")
-        //         this.app.updateHouseVideo(video0)
-        //     }
-        //     startTime = video0.duration - startTime
-        // }
-        // let video0 = document.getElementById("houseTexture0")
-
-        let durtime = Math.abs(alphaOffset / (Math.PI * 2) * video0.duration)
+        let startTime = (this.camera.alpha + Math.PI * 2) % (Math.PI * 2) / (Math.PI * 2) * video0.duration
 
-            // if(dirc * this.lastDirc < 0) {
-                // 清除已有动画再播新动画
-                // this.app.scene.stopAnimation(this.camera, "rotateCamera")
-            // }
+        if(this.lastDirc * dirc <= 0) {
+            if(dirc < 0) {   // 顺时针
+                video0 = document.getElementById("houseTexture")
+                this.app.house[1].material._textures.texture_video.video = video0
+                // this.app.updateHouseVideo(video0)
+            }
+            if(dirc > 0) {   // 逆时针
+                video0 = document.getElementById("houseTextureReverse")
+                this.app.house[1].material._textures.texture_video.video = video0
+                // this.app.updateHouseVideo(video0)
+            }
+            this.lastDirc = dirc
+        }
+        if(dirc < 0) {
+            startTime = video0.duration - startTime
+        }
 
-            const rotateAni = new BABYLON.Animation("rotateCamera", "alpha", settings.video.frameRate, 
-                BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE);
-
-            let rotateCameraFrameNum = settings.video.frameRate * durtime
-            const rotateFrames = [{
-                frame: 0,
-                value: this.camera.alpha
-            },{
-                frame: rotateCameraFrameNum,
-                value: this.camera.alpha + alphaOffset
-            }]; 
-
-            rotateAni.setKeys(rotateFrames);
-
-            video0.currentTime = startTime
-            // await 
-            video0.play()
-
-            this.app.scene.beginDirectAnimation(this.camera, [rotateAni], 0, rotateCameraFrameNum, false, 1, 
-                () => {
-                    this.lastDirc = 0
-                    this.enable = true
-                    video0.pause()
-                    func && func()
-                });
-            
+        let durtime = Math.abs(alphaOffset / (Math.PI * 2) * video0.duration)
 
+        const rotateAni = new BABYLON.Animation("rotateCamera", "alpha", settings.video.frameRate, 
+            BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE);
+
+        let rotateCameraFrameNum = settings.video.frameRate * durtime
+        const rotateFrames = [{
+            frame: 0,
+            value: this.camera.alpha
+        },{
+            frame: rotateCameraFrameNum,
+            value: this.camera.alpha - alphaOffset
+        }]; 
+
+        rotateAni.setKeys(rotateFrames);
+
+        video0.currentTime = startTime
+        await video0.play()
+
+        this.app.scene.beginDirectAnimation(this.camera, [rotateAni], 0, rotateCameraFrameNum, false, 1, 
+            () => {
+                this.enable = true
+                video0.pause()
+                func && func()
+            });
     }
 
     lockCamera(isTrue) {

+ 5 - 4
modules/CharactorManager.js

@@ -15,7 +15,7 @@ export default class CharactorManager {
 
     readPointData() {
         
-        return fetch("../textures/outputmp4/points.json", {
+        return fetch("../textures/outputmp4/points1.json", {
             headers: {
                 'content-type': 'application/json'
             },
@@ -25,7 +25,8 @@ export default class CharactorManager {
         .then(response => {
             this.pointsData = response
             this.pointsData.forEach(data => {
-                data.position = common.vec3UE4ToBABYLON(data.position)
+                // data.position = common.vec3UE4ToBABYLON(data.position)
+                data.position = new BABYLON.Vector3(data.position.x, data.position.y, data.position.z)
             })
         })
     }
@@ -99,8 +100,8 @@ export default class CharactorManager {
         //     dir: this.getVideoDirecNum()
         // }
 
-        let startPointUE4 = common.vec3BABYLONToUE4(startPoint)
-        let endPointUE4 = common.vec3BABYLONToUE4(endPoint)
+        // let startPointUE4 = common.vec3BABYLONToUE4(startPoint)
+        // let endPointUE4 = common.vec3BABYLONToUE4(endPoint)
 
         let sendData = {
             sceneCode: settings.sceneCode,

+ 75 - 12
modules/index.js

@@ -38,6 +38,9 @@ export default class App {
 
     init() {
 
+        this.initVideo = true
+        this.isReverse = false
+
         let self = this
         BABYLON.SceneLoader.ImportMesh("", "../scenes/house/", "close_to_bottom_L.glb", this.scene, async function (newMeshes, particleSystems, skeletons, animationGroups) {
             
@@ -47,6 +50,29 @@ export default class App {
 
             let houseVideo = document.getElementById("houseTexture0")
 
+            setTimeout(() => {
+                        
+                window.connection.socket.emit("getRotateVideo", {
+                    videoPath: "0/0",
+                    sangle: 0,
+                    eangle: 360,
+                    reverses: false,
+                    sceneCode: settings.sceneCode,
+                    roomId: settings.roomId,
+                    userId: settings.userId,
+                });
+                window.connection.socket.emit("getRotateVideo", {
+                    videoPath: "0/0" ,
+                    sangle: 0,
+                    eangle: 360,
+                    reverses: true,
+                    sceneCode: settings.sceneCode,
+                    roomId: settings.roomId,
+                    userId: settings.userId,
+                });
+        
+            }, 1500)
+
             newMeshes.forEach(m => {
                 // m.scaling.scaleInPlace(100);
                 m._geometry && (m.checkCollisions = true)
@@ -71,6 +97,9 @@ export default class App {
                         864 * window.innerWidth / settings.video.width, // 1500
                         settings.video.width * window.innerHeight / settings.video.height, 
                         window.innerHeight
+                        // 864 * window.innerHeight / settings.video.width, // 1500
+                        // settings.video.width * window.innerWidth / settings.video.height, 
+                        // window.innerWidth
                     ))
                     shaderMaterial.setFloat("isYUV", 0)
 
@@ -137,31 +166,48 @@ export default class App {
     }
 
     bindSocketEvents() {
-        
+
+        // 获得旋转视频
         window.connection.socket.on('getSocketVideo', async(data) => {
 
-            console.log("[3D] get: ", data)
+            console.log("[3D] getSocketVideo: ", data)
 
             const blob = new Blob([data], { type: 'application/video' })
             const url = URL.createObjectURL(blob)
             // this.updateHouseVideoBlob(url, true)
-            let video = common.createVideoElement0(url)
-            video.loop = "loop"
+            // let video = common.createVideoElement0(url)
+            // video.loop = "loop"
             setTimeout(function () {
-              return URL.revokeObjectURL(url)
+                return URL.revokeObjectURL(url)
             }, 3000)
 
             // this.cameraController.rotateCamera(this.cameraController.alphaOffset)
+
+            if(this.isReverse) {
+                document.getElementById("houseTextureReverse").src = url
+            } else {
+                document.getElementById("houseTexture").src = url
+            }
+
+            this.isReverse = !this.isReverse
+
+            if(this.initVideo) {
+                this.updateHouseVideoBlob(url, true)
+                this.initVideo = false
+            }
         })
 
+        // 获得行走视频
         window.connection.socket.on('getVideo', async (jsonArr) => {
             
-            let path = jsonArr.map( async (data) => { 
+            console.log("[3D] getVideo: ", data)
+
+            let path = jsonArr.map( async (data, index) => { 
 
                 const blob = new Blob([data.video], { type: 'application/video' })
                 const url = URL.createObjectURL(blob)
                 setTimeout(function () {
-                  return URL.revokeObjectURL(url)
+                    return URL.revokeObjectURL(url)
                 }, 3000)
     
                 return { 
@@ -173,14 +219,17 @@ export default class App {
             // 行走时锁定camera
             this.cameraController.lockCamera(true)
             this.charactorManager.charactor.startWalk(path, this)
-
         })
     
+        // 获得行走路径
         window.connection.socket.on("getRoute", (event) => {
-            console.log("getRoute", event);
+
+            console.log("[3D] getRoute", event);
+
+            let endPointId = 1
 
             // window.connection.socket.emit("getRotateVideo", {
-            //     videoPath: pointData.id + "/" + pointData.id,
+            //     videoPath: endPointId + "/" + endPointId,
             //     sangle: 0,
             //     eangle: 359,
             //     reverses: true,
@@ -189,7 +238,7 @@ export default class App {
             //     userId: settings.userId,
             // });
             // window.connection.socket.emit("getRotateVideo", {
-            //     videoPath: pointData.id + "/" + pointData.id,
+            //     videoPath: endPointId + "/" + endPointId,
             //     sangle: 0,
             //     eangle: 359,
             //     reverses: false,
@@ -198,6 +247,20 @@ export default class App {
             //     userId: settings.userId,
             // });
 
+            let dir = this.charactorManager.getVideoDirecNum()
+
+            let arr = []
+            let videos = []
+            for(let i = 0; i < arr.length - 1; i++) {
+                videos.push(arr[i] + "/" + arr[i] + "_" + arr[i+1] + "_" + dir)
+            }
+
+            // connection.socket.emit("getVideo", {
+            //     videoList: ["0/0_1_0", "0/0_1_1", "0/0_1_2"],
+            //     sceneCode: testConfig.sceneCode,
+            //     roomId: testConfig.roomId,
+            //     userId: testConfig.userId,
+            // });
         });
     }
 
@@ -211,7 +274,7 @@ export default class App {
 
     async updateHouseVideoBlob(blobUrl, notPlay) {
         let video = await common.createVideoElement(blobUrl)
-        video.loop = "loop"
+        // video.loop = "loop"
         let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
         this.house.forEach(mesh => {
             mesh.material && mesh.material.setTexture("texture_video", videoTexture)

+ 16 - 5
modules/utils/common.js

@@ -61,12 +61,18 @@ export default {
             } 
             else {
                 let video = document.createElement("video")
-                video.src = path
                 video.id = videoName
                 video.crossOrigin = "anonymous" 
-                video.playsinline = "playsinline" 
-                video.autoplay = "autoplay" 
+                // video.playsinline = "true" 
+                video.setAttribute("playsinline", "playsinline") 
+                video.setAttribute("x5-playsinline", "true") 
+                video.setAttribute("webkit-playsinline", "true") 
+                video.setAttribute("x5-video-player-type", "h5") 
+                video.setAttribute("controls", "true") 
+                // video.controls="true" 
                 video.muted = "muted"
+                video.autoplay = "autoplay" 
+                video.src = path
 
                 // document.getElementById("videoTextureBox").appendChild(video)
 
@@ -92,11 +98,16 @@ export default {
         video.src = path
         video.id = videoName
         video.crossOrigin = "anonymous" 
-        video.playsinline = "playsinline" 
+        // video.playsinline = "playsinline" 
         video.autoplay = "autoplay" 
         video.muted = "muted"
+        video.setAttribute("playsinline", "true") 
+        video.setAttribute("x5-playsinline", "true") 
+        video.setAttribute("webkit-playsinline", "true") 
+        video.setAttribute("x5-video-player-type", "h5") 
+        video.setAttribute("controls", "true") 
 
-        document.getElementById("videoTextureBox").appendChild(video)
+        // document.getElementById("videoTextureBox").appendChild(video)
         
         return video
     },

+ 225 - 0
textures/outputmp4/points1.json

@@ -0,0 +1,225 @@
+[{
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [1, 12, 13, 11, 14],
+	"id": 0,
+	"position": {
+		"x": 0.0,
+		"y": 0.0,
+		"z": -1.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [0, 2, 14, 13, 15],
+	"id": 1,
+	"position": {
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [1, 4, 15, 14, 3],
+	"id": 2,
+	"position": {
+		"x": 0.0,
+		"y": 0.0,
+		"z": 1.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [5, 15, 4, 2, 6],
+	"id": 3,
+	"position": {
+		"x": -1.0,
+		"y": 0.0,
+		"z": 2.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [2, 3, 15],
+	"id": 4,
+	"position": {
+		"x": 0.0,
+		"y": 0.0,
+		"z": 2.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [3, 7, 6],
+	"id": 5,
+	"position": {
+		"x": -1.0,
+		"y": 0.0,
+		"z": 3.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [5, 3],
+	"id": 6,
+	"position": {
+		"x": 0.0,
+		"y": 0.0,
+		"z": 3.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [5],
+	"id": 7,
+	"position": {
+		"x": -1.0,
+		"y": 0.0,
+		"z": 4.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [],
+	"id": 8,
+	"position": {
+		"x": 0.0,
+		"y": 0.0,
+		"z": 4.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [11, 10, 12],
+	"id": 9,
+	"position": {
+		"x": -1.0,
+		"y": 0.0,
+		"z": -3.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [9, 12, 11],
+	"id": 10,
+	"position": {
+		"x": 0.0,
+		"y": 0.0,
+		"z": -3.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [9, 13, 12, 0, 10],
+	"id": 11,
+	"position": {
+		"x": -1.0,
+		"y": 0.0,
+		"z": -2.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [0, 10, 11, 9, 13],
+	"id": 12,
+	"position": {
+		"x": 0.0,
+		"y": 0.0,
+		"z": -2.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [0, 11, 14, 1, 12],
+	"id": 13,
+	"position": {
+		"x": -1.0,
+		"y": 0.0,
+		"z": -1.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [1, 13, 15, 0, 2],
+	"id": 14,
+	"position": {
+		"x": -1.0,
+		"y": 0.0,
+		"z": 0.0
+	}
+}, {
+	"rotation": {
+		"w": 1.0,
+		"x": 0.0,
+		"y": 0.0,
+		"z": 0.0
+	},
+	"contact": [2, 3, 14, 1, 4],
+	"id": 15,
+	"position": {
+		"x": -1.0,
+		"y": 0.0,
+		"z": 1.0
+	}
+}]