Charactor.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import settings from "./utils/settings.js"
  2. export default class Charactor {
  3. constructor(newMeshes, particleSystems, skeletons, animationGroups) {
  4. this.mesh = newMeshes[0]
  5. this.particleSystem = particleSystems[0]
  6. this.skeleton = skeletons[0]
  7. this.modelData = { newMeshes, particleSystems, skeletons, animationGroups }
  8. // 设置人物动画权重
  9. this.animation = {}
  10. animationGroups.forEach((ani, index) => {
  11. this.animation[ani.name] = ani
  12. ani.play(true)
  13. if(index == 0) {
  14. ani.setWeightForAllAnimatables(1);
  15. this.actionType = ani.name + "-" + ani.name
  16. }
  17. else ani.setWeightForAllAnimatables(0);
  18. })
  19. // 动画沙漏, 用于人物模型动画权重转换
  20. this.aniHourglass = {
  21. upper: 0,
  22. lower: 1,
  23. }
  24. // 人物行走数据,通过startWalk更新
  25. this.walkData = {
  26. pathArr: [],
  27. currentPoint: 0
  28. }
  29. }
  30. set visible(isVisible) {
  31. this.modelData.newMeshes.forEach(mesh => mesh.isVisible = isVisible)
  32. }
  33. get visible() {
  34. return this.mesh.isVisible
  35. }
  36. updateAniTrans() {
  37. // 实时更新角色模型动画
  38. if(this.aniHourglass.upper > 0) {
  39. // 10帧动画过渡
  40. this.aniHourglass.upper = (this.aniHourglass.upper * 10 - 1) / 10
  41. this.aniHourglass.lower = (this.aniHourglass.lower * 10 + 1) / 10
  42. let fromAni = this.actionType.split("-")[0]
  43. let toAni = this.actionType.split("-")[1]
  44. this.animation[fromAni].setWeightForAllAnimatables(this.aniHourglass.upper);
  45. this.animation[toAni].setWeightForAllAnimatables(this.aniHourglass.lower);
  46. }
  47. }
  48. AniTransfromTo(aniName) {
  49. let lastAniName = this.actionType.split("-")[1]
  50. if(lastAniName == aniName) return
  51. // 颠倒沙漏
  52. this.aniHourglass = {
  53. upper: 1,
  54. lower: 0,
  55. }
  56. this.actionType = lastAniName + "-" + aniName
  57. }
  58. startWalk(pathArr, charactorManager) {
  59. this.walkData = {
  60. pathArr: pathArr,
  61. currentPoint: 0
  62. }
  63. if(pathArr.length >= 2 && this.actionType.split("-")[1] != "Walking")
  64. {
  65. let video = pathArr[1].video
  66. if(video.isLoaded) {
  67. this.AniTransfromTo("Walking")
  68. this.walkByPath(charactorManager)
  69. } else {
  70. video.onloadeddata = () => {
  71. this.AniTransfromTo("Walking")
  72. this.walkByPath(charactorManager)
  73. }
  74. }
  75. }
  76. }
  77. walkByPath(charactorManager) {
  78. let charactor = this
  79. charactor.walkData.currentPoint++
  80. // 更新房间的视频贴图
  81. // let video = charactor.walkData.pathArr[charactor.walkData.currentPoint].video
  82. let video = charactor.walkData.pathArr[1].video
  83. charactor.walkData.currentPoint == 1 && charactorManager.app.updateHouseVideo(video)
  84. let newPos = charactor.walkData.pathArr[charactor.walkData.currentPoint].point
  85. // 要跳转的位置与当前位置相同的话,就直接跳过,否则动画会出bug
  86. if(newPos.x == this.mesh.position.x && newPos.z == this.mesh.position.z)
  87. {
  88. console.warn("跳转点位与当前点位相同, 已跳过!")
  89. if(charactor.walkData.pathArr[charactor.walkData.currentPoint+1]) {
  90. charactor.walkByPath(charactorManager)
  91. } else {
  92. charactor.AniTransfromTo("Idle")
  93. charactorManager.app.cameraController.lockCamera(false)
  94. }
  95. return
  96. }
  97. let startingPoint = charactor.mesh.position.clone();
  98. startingPoint.y = newPos.y;
  99. let walkDirc = newPos.subtract(startingPoint).normalize();
  100. // 行走动画
  101. const walkAni = new BABYLON.Animation("walk", "position", settings.video.frameRate,
  102. BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE);
  103. let walkFrameNum = settings.video.frameRate * video.duration / (charactorManager.app.currentPoints.length-1)
  104. const walkKeyFrames = [{
  105. frame: 0,
  106. value: charactor.mesh.position
  107. },{
  108. frame: walkFrameNum,
  109. value: newPos
  110. }];
  111. walkAni.setKeys(walkKeyFrames);
  112. // 转身动画
  113. const newQuar = BABYLON.Quaternion.FromUnitVectorsToRef(new BABYLON.Vector3(0, 0, 1), walkDirc, new BABYLON.Quaternion())
  114. const turnAroundAni = new BABYLON.Animation("turnAround", "rotationQuaternion", settings.video.frameRate,
  115. BABYLON.Animation.ANIMATIONTYPE_QUATERNION, BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE);
  116. let turnAroundFrameNum = settings.video.frameRate * 0.2 // 0.2秒的帧数
  117. const turnAroundFrames = [{
  118. frame: 0,
  119. value: charactor.mesh.rotationQuaternion
  120. },{
  121. frame: turnAroundFrameNum,
  122. value: newQuar
  123. }];
  124. turnAroundAni.setKeys(turnAroundFrames);
  125. charactorManager.app.scene.beginDirectAnimation(charactor.mesh, [walkAni, turnAroundAni], 0, Math.max(walkFrameNum, turnAroundFrameNum), false, 1,
  126. () => {
  127. // 如果还有下一个点位就继续走,否则变为站立
  128. if(charactor.walkData.pathArr[charactor.walkData.currentPoint+1]) {
  129. charactor.walkByPath(charactorManager)
  130. } else {
  131. charactor.AniTransfromTo("Idle")
  132. charactorManager.app.cameraController.lockCamera(false)
  133. }
  134. });
  135. }
  136. }