index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // document.getElementById("enterBtn").onclick = () => {
  32. // document.getElementById("houseTexture").play();
  33. // document.getElementById("houseTextureReverse").play();
  34. // document.getElementById("mask").style.zIndex = "-10000"
  35. // }
  36. }
  37. init() {
  38. // document.getElementById("houseTexture").onplay = () => {
  39. // console.error("video play!")
  40. // }
  41. // window.ty =()=>{
  42. // document.getElementById("houseTexture").play();
  43. // }
  44. this.initVideo = true
  45. this.isReverse = false
  46. let self = this
  47. BABYLON.SceneLoader.ImportMesh("", "../scenes/house/", "close_to_bottom_L.glb", this.scene, async function (newMeshes, particleSystems, skeletons, animationGroups) {
  48. self.house = newMeshes
  49. // self.house[0].position = new BABYLON.Vector3(0, 0, -0.5)
  50. let houseVideo = document.getElementById("houseTexture")
  51. newMeshes.forEach(m => {
  52. // m.scaling.scaleInPlace(100);
  53. m._geometry && (m.checkCollisions = true)
  54. if(m.material)
  55. {
  56. BABYLON.Effect.ShadersStore['aFragmentShader'] = houseShader.fragment;
  57. BABYLON.Effect.ShadersStore['aVertexShader'] = houseShader.vertex;
  58. let shaderMaterial = new BABYLON.ShaderMaterial("shader", self.scene, { vertex: "a", fragment: "a", }, {
  59. attributes: houseShader.attributes,
  60. uniforms: houseShader.uniforms,
  61. defines: houseShader.defines
  62. });
  63. let videoTexture = new BABYLON.VideoTexture("", houseVideo, self.scene)
  64. // document.getElementById("houseTexture0").play()
  65. // document.getElementById("houseTexture0").loop = "loop"
  66. shaderMaterial.setTexture("texture_video", videoTexture)
  67. shaderMaterial.setVector3("focal_width_height", new BABYLON.Vector3(
  68. 864 * window.innerWidth / settings.video.width, // 1500
  69. settings.video.width * window.innerHeight / settings.video.height,
  70. window.innerHeight
  71. // 864 * window.innerHeight / settings.video.width, // 1500
  72. // settings.video.width * window.innerWidth / settings.video.height,
  73. // window.innerWidth
  74. ))
  75. shaderMaterial.setFloat("isYUV", 0)
  76. if(self.hasVideoTexture) m.material = shaderMaterial
  77. }
  78. });
  79. self.charactorManager = new CharactorManager(self)
  80. await self.charactorManager.readPointData()
  81. setTimeout(() => {
  82. window.connection.socket.emit("getRotateVideoUrl", {
  83. videoPath: "0/0",
  84. sceneCode: settings.sceneCode,
  85. roomId: settings.roomId,
  86. userId: settings.userId,
  87. });
  88. window.connection.socket.emit("getRotateVideoUrl", {
  89. videoPath: "0/0_rotate" ,
  90. sceneCode: settings.sceneCode,
  91. roomId: settings.roomId,
  92. userId: settings.userId,
  93. });
  94. self.charactorManager.importCharactorModel("../scenes/charactors/", "man_YXL.glb")
  95. }, 1500)
  96. });
  97. this.cameraController = new CameraController(self)
  98. }
  99. bindEvents() {
  100. this.scene.onPointerObservable.add((pointerInfo) => {
  101. switch (pointerInfo.type) {
  102. case BABYLON.PointerEventTypes.POINTERDOWN:
  103. this.cameraController.startMouseRotate(pointerInfo)
  104. break;
  105. case BABYLON.PointerEventTypes.POINTERUP:
  106. this.cameraController.endMouseRotate()
  107. break;
  108. case BABYLON.PointerEventTypes.POINTERMOVE:
  109. if(this.cameraController.lastFramePoint)
  110. this.cameraController.mouseRotating(pointerInfo)
  111. break;
  112. case BABYLON.PointerEventTypes.POINTERWHEEL:
  113. break;
  114. case BABYLON.PointerEventTypes.POINTERPICK:
  115. break;
  116. case BABYLON.PointerEventTypes.POINTERTAP:
  117. if(pointerInfo.pickInfo.hit && this.house.indexOf(pointerInfo.pickInfo.pickedMesh)) {
  118. this.charactorManager.clickHouse()
  119. }
  120. break;
  121. case BABYLON.PointerEventTypes.POINTERDOUBLETAP:
  122. break;
  123. }
  124. });
  125. this.scene.onBeforeAnimationsObservable.add(() => {
  126. if(this.charactorManager && this.charactorManager.charactor)
  127. this.charactorManager.onBeforeAnimation()
  128. })
  129. this.scene.onBeforeRenderObservable.add(() => {
  130. this.cameraController.updateCameraPos()
  131. })
  132. }
  133. bindSocketEvents() {
  134. let walkPointsTemp = []
  135. // 获得旋转视频
  136. window.connection.socket.on('getRotateVideoUrl', async(data) => {
  137. console.log("[3D] getRotateVideoUrl: ", data)
  138. const url = data
  139. let video = data.indexOf("_rotate") > -1
  140. ? document.getElementById("houseTextureReverse")
  141. : document.getElementById("houseTexture")
  142. video.src = url
  143. // 页面刷新后初次加载视频
  144. if(this.initVideo) {
  145. // this.updateHouseVideo(video, true)
  146. this.initVideo = false
  147. this.cameraController.camera.minZ = 0.001
  148. }
  149. })
  150. // 获得行走视频
  151. window.connection.socket.on('getVideoUrl', async(data) => {
  152. console.log("[3D] getVideoUrl: ", data)
  153. const urlArr = data
  154. let path = walkPointsTemp.map((point, i) => {
  155. return {
  156. id: point.id,
  157. point: new BABYLON.Vector3(-point.location.x, 0, point.location.z),
  158. video: urlArr[i] ? common.createVideoElement0(urlArr[i]) : null,
  159. }
  160. })
  161. // 行走时锁定camera
  162. console.log("Walk Start: ", path)
  163. this.cameraController.lockCamera(true)
  164. this.charactorManager.charactor.startWalk(path, this.charactorManager)
  165. })
  166. // 获得行走路径
  167. window.connection.socket.on("getRoute", (data) => {
  168. console.log("[3D] getRoute", data);
  169. if(!data || data.length < 2) return
  170. walkPointsTemp = data
  171. let dir = this.charactorManager.getVideoDirecNum()
  172. let videoNames = []
  173. for(let i = 0; i < data.length-1; i++) {
  174. videoNames.push(data[i].id + "/" + data[i].id + "_" + data[i+1].id + "_" + dir)
  175. }
  176. this.getSocketVideoType = "walkPath"
  177. let sendData = {
  178. videoList: videoNames,
  179. sceneCode: settings.sceneCode,
  180. roomId: settings.roomId,
  181. userId: settings.userId,
  182. }
  183. console.log("[3D] send(getVideoUrl): ", sendData);
  184. connection.socket.emit("getVideoUrl", sendData);
  185. });
  186. }
  187. updateHouseVideo(video, notPlay) {
  188. let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
  189. this.hasVideoTexture && this.house.forEach(mesh => {
  190. mesh.material && mesh.material.setTexture("texture_video", videoTexture)
  191. })
  192. !notPlay && video.play()
  193. }
  194. async updateHouseVideoBlob(blobUrl, notPlay) {
  195. let video = await common.createVideoElement(blobUrl)
  196. // video.loop = "loop"
  197. let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
  198. this.hasVideoTexture && this.house.forEach(mesh => {
  199. mesh.material && mesh.material.setTexture("texture_video", videoTexture)
  200. })
  201. !notPlay && video.play()
  202. }
  203. }