CharactorManager.js 5.7 KB

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