index.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import CharactorManager from "./CharactorManager.js";
  2. import common from "./utils/common.js";
  3. import houseShader from "./shaders/houseShader.js";
  4. import settings from "./utils/settings.js";
  5. import CameraController from "./CameraController.js";
  6. export default class App {
  7. constructor(engine) {
  8. var scene = new BABYLON.Scene(engine);
  9. this.scene = scene
  10. scene.collisionsEnabled = true;
  11. // Lights
  12. var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, -1, 0), scene);
  13. light.intensity = 1.0;
  14. light.specular = BABYLON.Color3.Black();
  15. var light2 = new BABYLON.HemisphericLight("dir01", new BABYLON.Vector3(0, 1, 0), scene);
  16. light2.intensity = 0.1;
  17. light2.position = new BABYLON.Vector3(0, 5, 5);
  18. // Skybox
  19. var skybox = BABYLON.MeshBuilder.CreateBox("skyBox", { size: 1000.0 }, scene);
  20. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  21. skyboxMaterial.backFaceCulling = false;
  22. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/environment.env", scene);
  23. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  24. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  25. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  26. skybox.material = skyboxMaterial;
  27. this.hasVideoTexture = true
  28. this.init()
  29. this.bindEvents()
  30. setTimeout(() => this.bindSocketEvents(), 1000)
  31. }
  32. init() {
  33. this.initVideo = true
  34. this.isReverse = false
  35. let self = this
  36. BABYLON.SceneLoader.ImportMesh("", "../scenes/house/", "close_to_bottom_L.glb", this.scene, async function (newMeshes, particleSystems, skeletons, animationGroups) {
  37. self.house = newMeshes
  38. // self.house[0].position = new BABYLON.Vector3(0, 0, -0.5)
  39. let houseVideo = document.getElementById("houseTexture")
  40. newMeshes.forEach(m => {
  41. // m.scaling.scaleInPlace(100);
  42. m._geometry && (m.checkCollisions = true)
  43. if(m.material)
  44. {
  45. BABYLON.Effect.ShadersStore['aFragmentShader'] = houseShader.fragment;
  46. BABYLON.Effect.ShadersStore['aVertexShader'] = houseShader.vertex;
  47. let shaderMaterial = new BABYLON.ShaderMaterial("shader", self.scene, { vertex: "a", fragment: "a", }, {
  48. attributes: houseShader.attributes,
  49. uniforms: houseShader.uniforms,
  50. defines: houseShader.defines
  51. });
  52. let videoTexture = new BABYLON.VideoTexture("", houseVideo, self.scene)
  53. // document.getElementById("houseTexture0").play()
  54. // document.getElementById("houseTexture0").loop = "loop"
  55. shaderMaterial.setTexture("texture_video", videoTexture)
  56. shaderMaterial.setVector3("focal_width_height", new BABYLON.Vector3(
  57. 864 * window.innerWidth / settings.video.width, // 1500
  58. settings.video.width * window.innerHeight / settings.video.height,
  59. window.innerHeight
  60. // 864 * window.innerHeight / settings.video.width, // 1500
  61. // settings.video.width * window.innerWidth / settings.video.height,
  62. // window.innerWidth
  63. ))
  64. shaderMaterial.setFloat("isYUV", 0)
  65. if(self.hasVideoTexture) m.material = shaderMaterial
  66. }
  67. });
  68. self.charactorManager = new CharactorManager(self)
  69. await self.charactorManager.readPointData()
  70. setTimeout(() => {
  71. window.connection.socket.emit("getRotateVideoUrl", {
  72. videoPath: "0/0",
  73. sceneCode: settings.sceneCode,
  74. roomId: settings.roomId,
  75. userId: settings.userId,
  76. });
  77. window.connection.socket.emit("getRotateVideoUrl", {
  78. videoPath: "0/0_rotate" ,
  79. sceneCode: settings.sceneCode,
  80. roomId: settings.roomId,
  81. userId: settings.userId,
  82. });
  83. self.charactorManager.importCharactorModel("../scenes/charactors/", "man_YXL.glb")
  84. }, 1500)
  85. });
  86. this.cameraController = new CameraController(self)
  87. }
  88. bindEvents() {
  89. this.scene.onPointerObservable.add((pointerInfo) => {
  90. switch (pointerInfo.type) {
  91. case BABYLON.PointerEventTypes.POINTERDOWN:
  92. this.cameraController.startMouseRotate(pointerInfo)
  93. break;
  94. case BABYLON.PointerEventTypes.POINTERUP:
  95. this.cameraController.endMouseRotate()
  96. break;
  97. case BABYLON.PointerEventTypes.POINTERMOVE:
  98. if(this.cameraController.lastFramePoint)
  99. this.cameraController.mouseRotating(pointerInfo)
  100. break;
  101. case BABYLON.PointerEventTypes.POINTERWHEEL:
  102. break;
  103. case BABYLON.PointerEventTypes.POINTERPICK:
  104. break;
  105. case BABYLON.PointerEventTypes.POINTERTAP:
  106. if(pointerInfo.pickInfo.hit && this.house.indexOf(pointerInfo.pickInfo.pickedMesh)) {
  107. this.charactorManager.clickHouse()
  108. }
  109. break;
  110. case BABYLON.PointerEventTypes.POINTERDOUBLETAP:
  111. break;
  112. }
  113. });
  114. this.scene.onBeforeAnimationsObservable.add(() => {
  115. if(this.charactorManager && this.charactorManager.charactor)
  116. this.charactorManager.onBeforeAnimation()
  117. })
  118. this.scene.onBeforeRenderObservable.add(() => {
  119. this.cameraController.updateCameraPos()
  120. })
  121. }
  122. bindSocketEvents() {
  123. let walkPointsTemp = []
  124. // 获得旋转视频
  125. window.connection.socket.on('getRotateVideoUrl', async(data) => {
  126. console.log("[3D] getRotateVideoUrl: ", data)
  127. const url = data
  128. let video = data.indexOf("_rotate") > -1
  129. ? document.getElementById("houseTextureReverse")
  130. : document.getElementById("houseTexture")
  131. video.src = url
  132. // 页面刷新后初次加载视频
  133. if(this.initVideo) {
  134. // this.updateHouseVideo(video, true)
  135. this.initVideo = false
  136. this.cameraController.camera.minZ = 0.001
  137. }
  138. })
  139. // 获得行走视频
  140. window.connection.socket.on('getVideoUrl', async(data) => {
  141. console.log("[3D] getVideoUrl: ", data)
  142. const urlArr = data
  143. let path = walkPointsTemp.map((point, i) => {
  144. return {
  145. id: point.id,
  146. point: new BABYLON.Vector3(-point.location.x, 0, point.location.z),
  147. video: urlArr[i] ? common.createVideoElement0(urlArr[i]) : null,
  148. }
  149. })
  150. // 行走时锁定camera
  151. console.log("Walk Start: ", path)
  152. this.cameraController.lockCamera(true)
  153. this.charactorManager.charactor.startWalk(path, this.charactorManager)
  154. })
  155. // 获得行走路径
  156. window.connection.socket.on("getRoute", (data) => {
  157. console.log("[3D] getRoute", data);
  158. if(!data || data.length < 2) return
  159. walkPointsTemp = data
  160. let dir = this.charactorManager.getVideoDirecNum()
  161. let videoNames = []
  162. for(let i = 0; i < data.length-1; i++) {
  163. videoNames.push(data[i].id + "/" + data[i].id + "_" + data[i+1].id + "_" + dir)
  164. }
  165. this.getSocketVideoType = "walkPath"
  166. let sendData = {
  167. videoList: videoNames,
  168. sceneCode: settings.sceneCode,
  169. roomId: settings.roomId,
  170. userId: settings.userId,
  171. }
  172. console.log("[3D] send(getVideoUrl): ", sendData);
  173. connection.socket.emit("getVideoUrl", sendData);
  174. });
  175. }
  176. updateHouseVideo(video, notPlay) {
  177. let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
  178. this.hasVideoTexture && this.house.forEach(mesh => {
  179. mesh.material && mesh.material.setTexture("texture_video", videoTexture)
  180. })
  181. !notPlay && video.play()
  182. }
  183. async updateHouseVideoBlob(blobUrl, notPlay) {
  184. let video = await common.createVideoElement(blobUrl)
  185. // video.loop = "loop"
  186. let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
  187. this.hasVideoTexture && this.house.forEach(mesh => {
  188. mesh.material && mesh.material.setTexture("texture_video", videoTexture)
  189. })
  190. !notPlay && video.play()
  191. }
  192. }