Charactor.js 7.3 KB

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