CharactorManager.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. self.charactor.visible = false
  48. setTimeout(() => self.charactor.visible = true, 1000)
  49. // 初始人物位置
  50. self.charactor.mesh.position = new BABYLON.Vector3(
  51. self.pointsData[0].position.x,
  52. 0,
  53. self.pointsData[0].position.z
  54. )
  55. });
  56. }
  57. clickHouse() {
  58. var scene = this.app.scene
  59. var pickinfo = scene.pick(scene.pointerX, scene.pointerY);
  60. if(pickinfo.pickedPoint) {
  61. let camera = this.app.cameraController.camera
  62. // 在行走之前,首先要把人物矫正到45度的倍数(有视频的8个方向)
  63. if(camera.alpha % (Math.PI / 4) == 0 )
  64. {
  65. // 如果已经在8个方向上了
  66. this.charactorWalkTo(pickinfo.pickedPoint)
  67. }
  68. else {
  69. // 相机方向矫正
  70. let cameraAlphaAmend = parseInt(camera.alpha / (Math.PI / 4)) * (Math.PI / 4)
  71. let pointData = this.getClosestPointData(this.charactor.mesh.position)
  72. let sendData = {
  73. video: [pointData.id],
  74. reverse: camera.alpha - cameraAlphaAmend < 0
  75. }
  76. // window.connection.socket.emit("getPush", sendData)
  77. // todo
  78. this.app.cameraController.rotateCamera(camera.alpha - cameraAlphaAmend, -Math.sign(camera.alpha - cameraAlphaAmend), () => {
  79. this.charactorWalkTo(pickinfo.pickedPoint)
  80. })
  81. }
  82. }
  83. }
  84. onBeforeAnimation() {
  85. this.charactor.updateAniTrans()
  86. }
  87. charactorWalkTo(endPoint) {
  88. let currentPath = this.charactor.walkData.pathArr[this.charactor.walkData.currentPoint]
  89. let startPoint = (currentPath && currentPath.point) || this.charactor.mesh.position
  90. // let sendData = {
  91. // startId: this.getClosestPointData(startPoint).id, // todo
  92. // endId: this.getClosestPointData(endPoint).id, // todo
  93. // dir: this.getVideoDirecNum()
  94. // }
  95. let sendData = {
  96. sceneCode: settings.sceneCode,
  97. roomId: settings.roomId,
  98. userId: settings.userId,
  99. s_location: {
  100. x: -startPoint.x,
  101. y: startPoint.y,
  102. z: startPoint.z,
  103. },
  104. e_location: {
  105. x: -endPoint.x,
  106. y: endPoint.y,
  107. z: endPoint.z,
  108. }
  109. }
  110. window.connection.socket.emit("getRoute", sendData);
  111. console.log("[3D] send(getRoute): ", sendData)
  112. }
  113. getClosestPointData(vec) {
  114. let ueVec = {x: vec.x, y: vec.z, z: vec.y}
  115. let closestPoint = { data: null, length: 99999 }
  116. this.pointsData.forEach( data => {
  117. let length = common.getLengthBetweenVec3(ueVec, data.position)
  118. if(length < closestPoint.length) {
  119. closestPoint.data = data
  120. closestPoint.length = length
  121. }
  122. })
  123. return closestPoint.data
  124. }
  125. getVideoDirecNum() {
  126. // 视频逆时针旋转
  127. let num = Math.floor(this.app.cameraController.camera.alpha % (Math.PI * 2) / (Math.PI / 4))
  128. if(num <= 0) {
  129. return -num
  130. } else {
  131. return 8 - num
  132. }
  133. }
  134. }