CharactorManager.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/points.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 = common.vec3UE4ToBABYLON(data.position)
  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, () => {
  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 startPointUE4 = common.vec3BABYLONToUE4(startPoint)
  77. let endPointUE4 = common.vec3BABYLONToUE4(endPoint)
  78. let sendData = {
  79. sceneCode: settings.sceneCode,
  80. roomId: settings.roomId,
  81. userId: settings.userId,
  82. s_location: {
  83. x: startPoint.x,
  84. y: startPoint.y,
  85. z: startPoint.z,
  86. },
  87. e_location: {
  88. x: endPoint.x,
  89. y: endPoint.y,
  90. z: endPoint.z,
  91. },
  92. // s_location: {
  93. // x: startPointUE4.x,
  94. // y: startPointUE4.y,
  95. // z: startPointUE4.z,
  96. // },
  97. // e_location: {
  98. // x: endPointUE4.x,
  99. // y: endPointUE4.y,
  100. // z: endPointUE4.z,
  101. // },
  102. }
  103. window.connection.socket.emit("getRoute", sendData);
  104. console.log("[3D] send: ", sendData)
  105. // // todo
  106. // let path = data.walkPath // response
  107. // path.forEach(data => data.point = this.pointsData[data.id].position)
  108. // path = path.map((data) => {
  109. // return {
  110. // point: new BABYLON.Vector3(data.point.x, 0, data.point.z),
  111. // video: data.video && common.createVideoElement0(data.video)
  112. // }
  113. // })
  114. // // 行走时锁定camera
  115. // this.app.cameraController.lockCamera(true)
  116. // this.charactor.startWalk(path, this)
  117. }
  118. getClosestPointData(vec) {
  119. let ueVec = {x: vec.x, y: vec.z, z: vec.y}
  120. let closestPoint = { data: null, length: 99999 }
  121. this.pointsData.forEach( data => {
  122. let length = common.getLengthBetweenVec3(ueVec, data.position)
  123. if(length < closestPoint.length) {
  124. closestPoint.data = data
  125. closestPoint.length = length
  126. }
  127. })
  128. return closestPoint.data
  129. }
  130. getVideoDirecNum() {
  131. // 视频逆时针旋转
  132. let num = Math.floor(this.app.cameraController.camera.alpha % (Math.PI * 2) / (Math.PI / 4))
  133. if(num <= 0) {
  134. return -num
  135. } else {
  136. return 8 - num
  137. }
  138. }
  139. }