CharactorManager.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. }
  10. readPointData() {
  11. return fetch("../textures/outputmp4/points1.json", {
  12. headers: {
  13. 'content-type': 'application/json'
  14. },
  15. method: 'GET',
  16. })
  17. .then(response => response.json())
  18. .then(response => {
  19. this.pointsData = response
  20. this.pointsData.forEach(data => {
  21. data.position = new BABYLON.Vector3(-data.position.x, data.position.y, data.position.z)
  22. var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 0.2, segments: 32}, this.app.scene);
  23. sphere.position.x = data.position.x;
  24. sphere.position.y = data.position.y+1;
  25. sphere.position.z = data.position.z;
  26. })
  27. })
  28. }
  29. importCharactorModel(modelPath, modelName) {
  30. let self = this
  31. BABYLON.SceneLoader.ImportMesh("", modelPath, modelName, this.app.scene, function (newMeshes, particleSystems, skeletons, animationGroups) {
  32. self.charactor = new Charactor(newMeshes, particleSystems, skeletons, animationGroups)
  33. self.charactor.mesh.scaling.scaleInPlace(1.4);
  34. // 初始人物位置
  35. self.charactor.mesh.position = new BABYLON.Vector3(
  36. self.pointsData[0].position.x,
  37. 0,
  38. self.pointsData[0].position.z
  39. )
  40. });
  41. }
  42. clickHouse() {
  43. var scene = this.app.scene
  44. var pickinfo = scene.pick(scene.pointerX, scene.pointerY);
  45. if(pickinfo.pickedPoint) {
  46. let camera = this.app.cameraController.camera
  47. // 在行走之前,首先要把人物矫正到45度的倍数(有视频的8个方向)
  48. if(camera.alpha % (Math.PI / 4) == 0 )
  49. {
  50. // 如果已经在8个方向上了
  51. this.charactorWalkTo(pickinfo.pickedPoint)
  52. }
  53. else {
  54. // 相机方向矫正
  55. let cameraAlphaAmend = parseInt(camera.alpha / (Math.PI / 4)) * (Math.PI / 4)
  56. let pointData = this.getClosestPointData(this.charactor.mesh.position)
  57. let sendData = {
  58. video: [pointData.id],
  59. reverse: camera.alpha - cameraAlphaAmend < 0
  60. }
  61. // window.connection.socket.emit("getPush", sendData)
  62. // todo
  63. this.app.cameraController.rotateCamera(camera.alpha - cameraAlphaAmend, -Math.sign(camera.alpha - cameraAlphaAmend), () => {
  64. this.charactorWalkTo(pickinfo.pickedPoint)
  65. })
  66. }
  67. }
  68. }
  69. onBeforeAnimation() {
  70. this.charactor.updateAniTrans()
  71. }
  72. charactorWalkTo(endPoint) {
  73. let currentPath = this.charactor.walkData.pathArr[this.charactor.walkData.currentPoint]
  74. let startPoint = (currentPath && currentPath.point) || this.charactor.mesh.position
  75. // let sendData = {
  76. // startId: this.getClosestPointData(startPoint).id, // todo
  77. // endId: this.getClosestPointData(endPoint).id, // todo
  78. // dir: this.getVideoDirecNum()
  79. // }
  80. let sendData = {
  81. sceneCode: settings.sceneCode,
  82. roomId: settings.roomId,
  83. userId: settings.userId,
  84. s_location: {
  85. x: -startPoint.x,
  86. y: startPoint.y,
  87. z: startPoint.z,
  88. },
  89. e_location: {
  90. x: -endPoint.x,
  91. y: endPoint.y,
  92. z: endPoint.z,
  93. }
  94. }
  95. window.connection.socket.emit("getRoute", sendData);
  96. console.log("[3D] send(getRoute): ", sendData)
  97. }
  98. getClosestPointData(vec) {
  99. let ueVec = {x: vec.x, y: vec.z, z: vec.y}
  100. let closestPoint = { data: null, length: 99999 }
  101. this.pointsData.forEach( data => {
  102. let length = common.getLengthBetweenVec3(ueVec, data.position)
  103. if(length < closestPoint.length) {
  104. closestPoint.data = data
  105. closestPoint.length = length
  106. }
  107. })
  108. return closestPoint.data
  109. }
  110. getVideoDirecNum() {
  111. // 视频逆时针旋转
  112. let num = Math.floor(this.app.cameraController.camera.alpha % (Math.PI * 2) / (Math.PI / 4))
  113. if(num <= 0) {
  114. return -num
  115. } else {
  116. return 8 - num
  117. }
  118. }
  119. }