Charactor.js 6.5 KB

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