CharactorManager.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Charactor from "./Charactor.js";
  2. import common from "./utils/common.js";
  3. import data from "./utils/data.js";
  4. import settings from "./utils/settings.js";
  5. export default class CharactorManager {
  6. constructor(app) {
  7. this.app = app
  8. this.frameRate = settings.video.frameRate;
  9. fetch("../textures/outputmp4/points.json", {
  10. headers: {
  11. 'content-type': 'application/json'
  12. },
  13. method: 'GET',
  14. })
  15. .then(response => response.json())
  16. .then(response => {
  17. this.pointsData = response
  18. })
  19. }
  20. importCharactorModel(modelPath, modelName) {
  21. let self = this
  22. BABYLON.SceneLoader.ImportMesh("", modelPath, modelName, this.app.scene, function (newMeshes, particleSystems, skeletons, animationGroups) {
  23. self.charactor = new Charactor(newMeshes, particleSystems, skeletons, animationGroups)
  24. self.charactor.mesh.scaling.scaleInPlace(1.4);
  25. // 初始人物位置
  26. self.charactor.mesh.position = new BABYLON.Vector3(
  27. self.pointsData[0].position.x,
  28. 0,
  29. self.pointsData[0].position.y
  30. )
  31. });
  32. }
  33. clickHouse() {
  34. var scene = this.app.scene
  35. var pickinfo = scene.pick(scene.pointerX, scene.pointerY);
  36. if(pickinfo.pickedPoint) {
  37. // this.charactorWalkTo(pickinfo.pickedPoint)
  38. // 在行走之前,首先要把人物矫正到45度的倍数(有视频的8个方向)
  39. if(this.charactor.actionType.split("-")[1] == "Walking")
  40. {
  41. // 如果是行走时改方向的话,相机保持之前已校正的度数,所以不用再改
  42. this.charactorWalkTo(pickinfo.pickedPoint)
  43. }
  44. else {
  45. let cameraAlphaAmend = parseInt(this.app.camera.alpha / (Math.PI / 2)) * (Math.PI / 2)
  46. let sendData = {
  47. type: "CameraRotate",
  48. point: this.charactor.mesh.position,
  49. dirc: {
  50. from: this.app.camera.alpha % (Math.PI * 2),
  51. to: cameraAlphaAmend % (Math.PI * 2)
  52. }
  53. }
  54. window.connection.socket.emit("RotateCamera", sendData)
  55. // todo 发送数据
  56. // common.postData("", sendData).then(response => {
  57. // let video = response[0].video
  58. // this.app.updateHouseVideo(video)
  59. // let rotateAni = new BABYLON.Animation("rotate", "alpha", this.frameRate,
  60. // BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE);
  61. // const rotateFrameNum = this.frameRate * video.duration
  62. // const rotateKeyFrames = [{
  63. // frame: 0,
  64. // value: this.app.camera.alpha
  65. // },{
  66. // frame: rotateFrameNum,
  67. // value: cameraAlphaAmend
  68. // }];
  69. // rotateAni.setKeys(rotateKeyFrames);
  70. // scene.beginDirectAnimation(this.app.camera, [rotateAni], 0, rotateFrameNum, false, 1, () => {
  71. // // 旋转完后开始行走
  72. // this.charactorWalkTo(pickinfo.pickedPoint)
  73. // })
  74. // })
  75. }
  76. }
  77. }
  78. onBeforeAnimation() {
  79. this.charactor.updateAniTrans()
  80. }
  81. charactorWalkTo(endPoint) {
  82. let currentPath = this.charactor.walkData.pathArr[this.charactor.walkData.currentPoint]
  83. let startPoint = (currentPath && currentPath.point) || this.charactor.mesh.position
  84. let sendData = {
  85. type: "Walk",
  86. point: {
  87. from: { x: startPoint.x, y: startPoint.z },
  88. to: { x: endPoint.x, y: endPoint.z }
  89. },
  90. dirc: this.app.camera.alpha % (Math.PI * 2)
  91. }
  92. window.connection.socket.emit("Walk", sendData)
  93. // todo 发送数据
  94. // common.postData("", sendData).then(response => {
  95. // let path = data.walkPath // response
  96. // path.forEach(data => data.point = this.pointsData[data.id].position)
  97. // path = path.map(data => {
  98. // return {
  99. // point: new BABYLON.Vector3(data.point.x, 0, data.point.y),
  100. // video: data.video && common.createVideoElement(data.video)
  101. // }
  102. // })
  103. // // 行走时锁定camera
  104. // this.app.lockCamera(true)
  105. // this.charactor.startWalk(path, this)
  106. // })
  107. }
  108. }