CharactorManager.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. this.texture = new BABYLON.Texture("https://4dkk.4dage.com/v3/img/marker.png", scene);
  10. }
  11. readPointData() {
  12. return fetch("../textures/outputmp4/points1.json", {
  13. headers: {
  14. 'content-type': 'application/json'
  15. },
  16. method: 'GET',
  17. })
  18. .then(response => response.json())
  19. .then(response => {
  20. this.pointsData = response
  21. this.pointsData.forEach(data => {
  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. var plane = new BABYLON.Mesh.CreatePlane("TextPlane", 0.2, scene, true);
  28. plane.material = new BABYLON.StandardMaterial("TextPlaneMaterial", scene);
  29. plane.material.alpha = 1,
  30. plane.material.emissiveTexture = this.texture,
  31. plane.material.backFaceCulling = true,
  32. plane.material.diffuseTexture = this.texture,
  33. plane.material.diffuseTexture.hasAlpha = !0,
  34. plane.material.useAlphaFromDiffuseTexture = !0
  35. plane.rotation.x = Math.PI / 2
  36. plane.position.x = data.position.x;
  37. plane.position.y = data.position.y+0.01;
  38. plane.position.z = data.position.z;
  39. })
  40. })
  41. }
  42. importCharactorModel(modelPath, modelName) {
  43. let self = this
  44. BABYLON.SceneLoader.ImportMesh("", modelPath, modelName, this.app.scene, function (newMeshes, particleSystems, skeletons, animationGroups) {
  45. self.charactor = new Charactor(newMeshes, particleSystems, skeletons, animationGroups)
  46. self.charactor.mesh.scaling.scaleInPlace(1.4);
  47. // 初始人物位置
  48. self.charactor.mesh.position = new BABYLON.Vector3(
  49. self.pointsData[0].position.x,
  50. 0,
  51. self.pointsData[0].position.z
  52. )
  53. });
  54. }
  55. clickHouse() {
  56. var scene = this.app.scene
  57. var pickinfo = scene.pick(scene.pointerX, scene.pointerY);
  58. if(pickinfo.pickedPoint) {
  59. let camera = this.app.cameraController.camera
  60. // 在行走之前,首先要把人物矫正到45度的倍数(有视频的8个方向)
  61. if(camera.alpha % (Math.PI / 4) == 0 )
  62. {
  63. // 如果已经在8个方向上了
  64. this.charactorWalkTo(pickinfo.pickedPoint)
  65. }
  66. else {
  67. // 相机方向矫正
  68. let cameraAlphaAmend = parseInt(camera.alpha / (Math.PI / 4)) * (Math.PI / 4)
  69. let pointData = this.getClosestPointData(this.charactor.mesh.position)
  70. let sendData = {
  71. video: [pointData.id],
  72. reverse: camera.alpha - cameraAlphaAmend < 0
  73. }
  74. // window.connection.socket.emit("getPush", sendData)
  75. // todo
  76. this.app.cameraController.rotateCamera(camera.alpha - cameraAlphaAmend, -Math.sign(camera.alpha - cameraAlphaAmend), () => {
  77. this.charactorWalkTo(pickinfo.pickedPoint)
  78. })
  79. }
  80. }
  81. }
  82. onBeforeAnimation() {
  83. this.charactor.updateAniTrans()
  84. }
  85. charactorWalkTo(endPoint) {
  86. let currentPath = this.charactor.walkData.pathArr[this.charactor.walkData.currentPoint]
  87. let startPoint = (currentPath && currentPath.point) || this.charactor.mesh.position
  88. // let sendData = {
  89. // startId: this.getClosestPointData(startPoint).id, // todo
  90. // endId: this.getClosestPointData(endPoint).id, // todo
  91. // dir: this.getVideoDirecNum()
  92. // }
  93. let sendData = {
  94. sceneCode: settings.sceneCode,
  95. roomId: settings.roomId,
  96. userId: settings.userId,
  97. s_location: {
  98. x: -startPoint.x,
  99. y: startPoint.y,
  100. z: startPoint.z,
  101. },
  102. e_location: {
  103. x: -endPoint.x,
  104. y: endPoint.y,
  105. z: endPoint.z,
  106. }
  107. }
  108. window.connection.socket.emit("getRoute", sendData);
  109. console.log("[3D] send(getRoute): ", sendData)
  110. }
  111. getClosestPointData(vec) {
  112. let ueVec = {x: vec.x, y: vec.z, z: vec.y}
  113. let closestPoint = { data: null, length: 99999 }
  114. this.pointsData.forEach( data => {
  115. let length = common.getLengthBetweenVec3(ueVec, data.position)
  116. if(length < closestPoint.length) {
  117. closestPoint.data = data
  118. closestPoint.length = length
  119. }
  120. })
  121. return closestPoint.data
  122. }
  123. getVideoDirecNum() {
  124. // 视频逆时针旋转
  125. let num = Math.floor(this.app.cameraController.camera.alpha % (Math.PI * 2) / (Math.PI / 4))
  126. if(num <= 0) {
  127. return -num
  128. } else {
  129. return 8 - num
  130. }
  131. }
  132. }