CharactorManager.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import Charactor from "./Charactor.js";
  2. import common from "./common.js";
  3. import data from "./data.js";
  4. import settings from "./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. this.charactorWalkTo(pickinfo.pickedPoint)
  38. // // 在行走之前,首先要把人物矫正到45度的倍数(有视频的8个方向)
  39. // if(this.charactor.actionType.split("-")[1] == "Walking")
  40. // {
  41. // // 如果是行走时改方向的话,相机保持之前已校正的度数,所以不用再改
  42. // this.charactorWalkTo(pickinfo.pickedPoint)
  43. // }
  44. // else {
  45. // let cameraAlphaAmend = parseInt(this.app.camera.alpha / (Math.PI / 2)) * (Math.PI / 2)
  46. // let sendData = {
  47. // type: "CameraRotate",
  48. // point: this.charactor.mesh.position,
  49. // dirc: {
  50. // from: this.app.camera.alpha % (Math.PI * 2),
  51. // to: cameraAlphaAmend % (Math.PI * 2)
  52. // }
  53. // }
  54. // // todo 发送数据
  55. // // common.postData("", sendData).then(response => {
  56. // let video = response[0].video
  57. // this.app.updateHouseVideo(video)
  58. // let rotateAni = new BABYLON.Animation("rotate", "alpha", this.frameRate,
  59. // BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE);
  60. // const rotateFrameNum = this.frameRate * video.duration
  61. // const rotateKeyFrames = [{
  62. // frame: 0,
  63. // value: this.app.camera.alpha
  64. // },{
  65. // frame: rotateFrameNum,
  66. // value: cameraAlphaAmend
  67. // }];
  68. // rotateAni.setKeys(rotateKeyFrames);
  69. // scene.beginDirectAnimation(this.app.camera, [rotateAni], 0, rotateFrameNum, false, 1, () => {
  70. // // 旋转完后开始行走
  71. // this.charactorWalkTo(pickinfo.pickedPoint)
  72. // })
  73. // // })
  74. // }
  75. }
  76. }
  77. onBeforeAnimation() {
  78. this.charactor.updateAniTrans()
  79. }
  80. charactorWalkTo(endPoint) {
  81. let currentPath = this.charactor.walkData.pathArr[this.charactor.walkData.currentPoint]
  82. let startPoint = (currentPath && currentPath.point) || this.charactor.mesh.position
  83. let sendData = {
  84. type: "Walk",
  85. point: {
  86. from: { x: startPoint.x, y: startPoint.z },
  87. to: { x: endPoint.x, y: endPoint.z }
  88. },
  89. dirc: this.app.camera.alpha % (Math.PI * 2)
  90. }
  91. // todo 发送数据
  92. // common.postData("", sendData).then(response => {
  93. let path = data.walkPath // response
  94. path.forEach(data => data.point = this.pointsData[data.id].position)
  95. path = path.map(data => {
  96. return {
  97. point: new BABYLON.Vector3(data.point.x, 0, data.point.y),
  98. video: data.video && common.createVideoElement(data.video)
  99. }
  100. })
  101. // 行走时锁定camera
  102. this.app.lockCamera(true)
  103. this.charactor.startWalk(path, this)
  104. // })
  105. }
  106. }