CharactorManager.js 5.5 KB

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