CharactorManager.js 5.6 KB

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