123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- 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;
- this.texture = new BABYLON.Texture("https://4dkk.4dage.com/v3/img/marker.png", scene);
- }
- readPointData() {
-
- return fetch("../textures/outputmp4/points1.json", {
- headers: {
- 'content-type': 'application/json'
- },
- method: 'GET',
- })
- .then(response => response.json())
- .then(response => {
- this.pointsData = response
- this.pointsData.forEach(data => {
- data.position = new BABYLON.Vector3(-data.position.x, data.position.y, data.position.z)
- // var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 0.2, segments: 32}, this.app.scene);
- // sphere.position.x = data.position.x;
- // sphere.position.y = data.position.y+1;
- // sphere.position.z = data.position.z;
- var plane = new BABYLON.Mesh.CreatePlane("TextPlane", 0.2, scene, true);
- plane.material = new BABYLON.StandardMaterial("TextPlaneMaterial", scene);
- plane.material.alpha = 1,
- plane.material.emissiveTexture = this.texture,
- plane.material.backFaceCulling = true,
- plane.material.diffuseTexture = this.texture,
- plane.material.diffuseTexture.hasAlpha = !0,
- plane.material.useAlphaFromDiffuseTexture = !0
- plane.rotation.x = Math.PI / 2
- plane.position.x = data.position.x;
- plane.position.y = data.position.y+0.01;
- plane.position.z = data.position.z;
- })
- })
- }
- 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.visible = false
- setTimeout(() => {
- self.charactor.visible = true
- // document.getElementById("mask").style.zIndex = "-10000"
- }, 1000)
-
- // 初始人物位置
- 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 = Math.round(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)
- this.app.cameraController.rotateCamera(camera.alpha - cameraAlphaAmend, -Math.sign(camera.alpha - cameraAlphaAmend), () => {
- this.charactorWalkTo(pickinfo.pickedPoint)
- })
- }
- }
- }
- onBeforeAnimation() {
- this.charactor.updateAniTrans()
- }
- charactorWalkTo(endPoint) {
- let currentPathEnd = this.charactor.walkData.pathArr[this.charactor.walkData.currentPoint+1]
- let startPoint = (currentPathEnd && currentPathEnd.point) || this.charactor.mesh.position
- // let sendData = {
- // startId: this.getClosestPointData(startPoint).id, // todo
- // endId: this.getClosestPointData(endPoint).id, // todo
- // dir: this.getVideoDirecNum()
- // }
- 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,
- }
- }
- window.connection.socket.emit("getRoute", sendData);
- console.log("[3D] send(getRoute): ", sendData)
- }
- 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.round(this.app.cameraController.camera.alpha % (Math.PI * 2) / (Math.PI / 4))
- if(num <= 0) {
- return -num
- } else {
- return 8 - num
- }
- }
- }
|