Charactor.js 5.5 KB

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