123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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 = common.vec3UE4ToBABYLON(data.position)
- })
- })
- }
- 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) {
- let camera = this.app.cameraController.camera
- // 在行走之前,首先要把人物矫正到45度的倍数(有视频的8个方向)
- if(camera.alpha % (Math.PI / 4) == 0 )
- {
- // 如果已经在8个方向上了
- this.charactorWalkTo(pickinfo.pickedPoint)
- }
- else {
- // 相机方向矫正
- let cameraAlphaAmend = parseInt(camera.alpha / (Math.PI / 4)) * (Math.PI / 4)
- let pointData = this.getClosestPointData(this.charactor.mesh.position)
- let sendData = {
- video: [pointData.id],
- reverse: camera.alpha - cameraAlphaAmend < 0
- }
- // window.connection.socket.emit("getPush", sendData)
- // todo
- this.app.cameraController.rotateCamera(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()
- // }
- let startPointUE4 = common.vec3BABYLONToUE4(startPoint)
- let endPointUE4 = common.vec3BABYLONToUE4(endPoint)
- let sendData = {
- sceneCode: settings.sceneCode,
- roomId: settings.roomId,
- userId: settings.userId,
- s_location: {
- x: startPoint.x,
- y: startPoint.y,
- z: startPoint.z,
- },
- e_location: {
- x: endPoint.x,
- y: endPoint.y,
- z: endPoint.z,
- },
- // s_location: {
- // x: startPointUE4.x,
- // y: startPointUE4.y,
- // z: startPointUE4.z,
- // },
- // e_location: {
- // x: endPointUE4.x,
- // y: endPointUE4.y,
- // z: endPointUE4.z,
- // },
- }
- window.connection.socket.emit("getRoute", sendData);
- console.log("[3D] send: ", 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.cameraController.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.cameraController.camera.alpha % (Math.PI * 2) / (Math.PI / 4))
- if(num <= 0) {
- return -num
- } else {
- return 8 - num
- }
- }
- }
|