CharactorManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. fetch("../textures/outputmp4/points.json", {
  10. headers: {
  11. 'content-type': 'application/json'
  12. },
  13. method: 'GET',
  14. })
  15. .then(response => response.json())
  16. .then(response => {
  17. this.pointsData = response
  18. })
  19. }
  20. importCharactorModel(modelPath, modelName) {
  21. let self = this
  22. BABYLON.SceneLoader.ImportMesh("", modelPath, modelName, this.app.scene, function (newMeshes, particleSystems, skeletons, animationGroups) {
  23. self.charactor = new Charactor(newMeshes, particleSystems, skeletons, animationGroups)
  24. self.charactor.mesh.scaling.scaleInPlace(1.4);
  25. // 初始人物位置
  26. self.charactor.mesh.position = new BABYLON.Vector3(
  27. self.pointsData[0].position.x,
  28. 0,
  29. self.pointsData[0].position.y
  30. )
  31. });
  32. }
  33. clickHouse() {
  34. var scene = this.app.scene
  35. var pickinfo = scene.pick(scene.pointerX, scene.pointerY);
  36. if(pickinfo.pickedPoint) {
  37. // 在行走之前,首先要把人物矫正到45度的倍数(有视频的8个方向)
  38. if(this.app.camera.alpha % (Math.PI / 4) == 0 )
  39. {
  40. // 如果已经在8个方向上了
  41. this.charactorWalkTo(pickinfo.pickedPoint)
  42. }
  43. else {
  44. // 相机方向矫正
  45. let cameraAlphaAmend = parseInt(this.app.camera.alpha / (Math.PI / 4)) * (Math.PI / 4)
  46. let pointData = this.getClosestPointData(this.charactor.mesh.position)
  47. let sendData = {
  48. video: [pointData.id],
  49. reverse: this.app.camera.alpha - cameraAlphaAmend < 0
  50. }
  51. // window.connection.socket.emit("getPush", sendData)
  52. // todo
  53. this.app.rotateCamera(this.app.camera.alpha - cameraAlphaAmend, () => {
  54. // this.charactorWalkTo(pickinfo.pickedPoint)
  55. })
  56. }
  57. }
  58. }
  59. onBeforeAnimation() {
  60. this.charactor.updateAniTrans()
  61. }
  62. charactorWalkTo(endPoint) {
  63. let endPointData = this.getClosestPointData(endPoint)
  64. let currentPath = this.charactor.walkData.pathArr[this.charactor.walkData.currentPoint]
  65. let startPoint = (currentPath && currentPath.point) || this.charactor.mesh.position
  66. let sendData = {
  67. video: [endPointData.id], // todo
  68. reverse: true // todo
  69. }
  70. // window.connection.socket.emit("getPush", sendData)
  71. // todo
  72. let path = data.walkPath // response
  73. path.forEach(data => data.point = this.pointsData[data.id].position)
  74. path = path.map(data => {
  75. return {
  76. point: new BABYLON.Vector3(data.point.x, 0, data.point.y),
  77. video: data.video && common.createVideoElement(data.video)
  78. }
  79. })
  80. // 行走时锁定camera
  81. this.app.lockCamera(true)
  82. this.charactor.startWalk(path, this)
  83. }
  84. getClosestPointData(vec) {
  85. let ueVec = {x: vec.x, y: vec.z, z: vec.y}
  86. let closestPoint = { data: null, length: 99999 }
  87. this.pointsData.forEach( data => {
  88. let length = common.getLengthBetweenVec3(ueVec, data.position)
  89. if(length < closestPoint.length) {
  90. closestPoint.data = data
  91. closestPoint.length = length
  92. }
  93. })
  94. return closestPoint.data
  95. }
  96. }