CharactorManager.js 4.4 KB

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