CharactorManager.js 4.7 KB

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