Charactor.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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[0].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. let { pathArr, currentPoint } = charactor.walkData
  80. // 更新房间的视频贴图
  81. let video = pathArr[currentPoint].video
  82. video.play()
  83. charactorManager.app.updateHouseVideo(video)
  84. let nextPos = pathArr[currentPoint+1].point
  85. let nextVideo = pathArr[currentPoint+1].video
  86. if(!nextVideo)
  87. {
  88. // 即将走到终点之前,获取终点旋转视频
  89. let endPointId = pathArr[currentPoint+1].id
  90. console.log("[3D] send(getRotateVideoUrl): ", endPointId + "/" + endPointId)
  91. window.connection.socket.emit("getRotateVideoUrl", {
  92. videoPath: endPointId + "/" + endPointId,
  93. sceneCode: settings.sceneCode,
  94. roomId: settings.roomId,
  95. userId: settings.userId,
  96. });
  97. window.connection.socket.emit("getRotateVideoUrl", {
  98. videoPath: endPointId + "/" + endPointId + "_rotate",
  99. sceneCode: settings.sceneCode,
  100. roomId: settings.roomId,
  101. userId: settings.userId,
  102. });
  103. }
  104. // 要跳转的位置与当前位置相同的话,就直接跳过,否则动画会出bug
  105. if(nextPos.x == this.mesh.position.x && nextPos.z == this.mesh.position.z)
  106. {
  107. console.warn("跳转点位与当前点位相同, 已跳过!")
  108. if(pathArr[currentPoint+1].video) {
  109. charactor.walkByPath(charactorManager)
  110. } else {
  111. charactor.AniTransfromTo("Idle")
  112. charactorManager.app.cameraController.lockCamera(false)
  113. }
  114. return
  115. }
  116. let startingPoint = charactor.mesh.position.clone();
  117. startingPoint.y = nextPos.y;
  118. let walkDirc = nextPos.subtract(startingPoint).normalize();
  119. // 行走动画
  120. const walkAni = new BABYLON.Animation("walk", "position", settings.video.frameRate,
  121. BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE);
  122. let walkFrameNum = settings.video.frameRate * video.duration
  123. const walkKeyFrames = [{
  124. frame: 0,
  125. value: charactor.mesh.position
  126. },{
  127. frame: walkFrameNum,
  128. value: nextPos
  129. }];
  130. walkAni.setKeys(walkKeyFrames);
  131. // 转身动画
  132. const newQuar = BABYLON.Quaternion.FromUnitVectorsToRef(new BABYLON.Vector3(0, 0, 1), walkDirc, new BABYLON.Quaternion())
  133. const turnAroundAni = new BABYLON.Animation("turnAround", "rotationQuaternion", settings.video.frameRate,
  134. BABYLON.Animation.ANIMATIONTYPE_QUATERNION, BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE);
  135. let turnAroundFrameNum = settings.video.frameRate * 0.2 // 0.2秒的帧数
  136. const turnAroundFrames = [{
  137. frame: 0,
  138. value: charactor.mesh.rotationQuaternion
  139. },{
  140. frame: turnAroundFrameNum,
  141. value: newQuar
  142. }];
  143. turnAroundAni.setKeys(turnAroundFrames);
  144. charactorManager.app.scene.beginDirectAnimation(charactor.mesh, [walkAni, turnAroundAni], 0, Math.max(walkFrameNum, turnAroundFrameNum), false, 1,
  145. () => {
  146. // 如果还有下一个点位就继续走,否则变为站立
  147. if(pathArr[++charactor.walkData.currentPoint].video) {
  148. charactor.walkByPath(charactorManager)
  149. } else {
  150. charactor.AniTransfromTo("Idle")
  151. charactorManager.app.cameraController.lockCamera(false)
  152. }
  153. });
  154. }
  155. }