import Charactor from "./Charactor.js"; import common from "./utils/common.js"; import data from "./utils/data.js"; import settings from "./utils/settings.js"; export default class CharactorManager { constructor(app) { this.app = app this.frameRate = settings.video.frameRate; } readPointData() { return fetch("../textures/outputmp4/points.json", { headers: { 'content-type': 'application/json' }, method: 'GET', }) .then(response => response.json()) .then(response => { this.pointsData = response this.pointsData.forEach(data => { data.position = BABYLON.Vector3.TransformCoordinates( new BABYLON.Vector3(data.position.x, data.position.y, data.position.z), BABYLON.Matrix.FromArray([ -0.01, 0, 0, 0, 0, 0, 0.01, 0, 0, 0.01, 0, 0, 0, 0, 0, 1 ]) ) }) }) } importCharactorModel(modelPath, modelName) { let self = this BABYLON.SceneLoader.ImportMesh("", modelPath, modelName, this.app.scene, function (newMeshes, particleSystems, skeletons, animationGroups) { self.charactor = new Charactor(newMeshes, particleSystems, skeletons, animationGroups) self.charactor.mesh.scaling.scaleInPlace(1.4); // 初始人物位置 self.charactor.mesh.position = new BABYLON.Vector3( self.pointsData[0].position.x, 0, self.pointsData[0].position.z ) }); } clickHouse() { var scene = this.app.scene var pickinfo = scene.pick(scene.pointerX, scene.pointerY); if(pickinfo.pickedPoint) { // 在行走之前,首先要把人物矫正到45度的倍数(有视频的8个方向) if(this.app.camera.alpha % (Math.PI / 4) == 0 ) { // 如果已经在8个方向上了 this.charactorWalkTo(pickinfo.pickedPoint) } else { // 相机方向矫正 let cameraAlphaAmend = parseInt(this.app.camera.alpha / (Math.PI / 4)) * (Math.PI / 4) let pointData = this.getClosestPointData(this.charactor.mesh.position) let sendData = { video: [pointData.id], reverse: this.app.camera.alpha - cameraAlphaAmend < 0 } // window.connection.socket.emit("getPush", sendData) // todo this.app.rotateCamera(this.app.camera.alpha - cameraAlphaAmend, () => { this.charactorWalkTo(pickinfo.pickedPoint) }) } } } onBeforeAnimation() { this.charactor.updateAniTrans() } charactorWalkTo(endPoint) { let currentPath = this.charactor.walkData.pathArr[this.charactor.walkData.currentPoint] let startPoint = (currentPath && currentPath.point) || this.charactor.mesh.position let sendData = { startId: this.getClosestPointData(startPoint).id, // todo endId: this.getClosestPointData(endPoint).id, // todo dir: this.getVideoDirecNum() } // window.connection.socket.emit("getPush", sendData) // todo let path = data.walkPath // response path.forEach(data => data.point = this.pointsData[data.id].position) path = path.map((data) => { return { point: new BABYLON.Vector3(data.point.x, 0, data.point.z), video: data.video && common.createVideoElement0(data.video) } }) // 行走时锁定camera this.app.lockCamera(true) this.charactor.startWalk(path, this) } getClosestPointData(vec) { let ueVec = {x: vec.x, y: vec.z, z: vec.y} let closestPoint = { data: null, length: 99999 } this.pointsData.forEach( data => { let length = common.getLengthBetweenVec3(ueVec, data.position) if(length < closestPoint.length) { closestPoint.data = data closestPoint.length = length } }) return closestPoint.data } getVideoDirecNum() { // 视频逆时针旋转 let num = Math.floor(this.app.camera.alpha % (Math.PI * 2) / (Math.PI / 4)) if(num <= 0) { return -num } else { return 8 - num } } }