index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.init()
  28. this.bindEvents()
  29. setTimeout(() => this.bindSocketEvents(), 1000)
  30. }
  31. init() {
  32. let self = this
  33. BABYLON.SceneLoader.ImportMesh("", "../scenes/house/", "close_to_bottom_L.glb", this.scene, async function (newMeshes, particleSystems, skeletons, animationGroups) {
  34. self.house = newMeshes
  35. // self.house[0].position = new BABYLON.Vector3(0.6, 2.1, 1.5)
  36. // self.house[0].position = new BABYLON.Vector3(-22, 0, 12)
  37. let houseVideo = document.getElementById("houseTexture0")
  38. newMeshes.forEach(m => {
  39. // m.scaling.scaleInPlace(100);
  40. m._geometry && (m.checkCollisions = true)
  41. if(m.material)
  42. {
  43. BABYLON.Effect.ShadersStore['aFragmentShader'] = houseShader.fragment;
  44. BABYLON.Effect.ShadersStore['aVertexShader'] = houseShader.vertex;
  45. let shaderMaterial = new BABYLON.ShaderMaterial("shader", self.scene, { vertex: "a", fragment: "a", }, {
  46. attributes: houseShader.attributes,
  47. uniforms: houseShader.uniforms,
  48. defines: houseShader.defines
  49. });
  50. let videoTexture = new BABYLON.VideoTexture("", houseVideo, self.scene)
  51. // document.getElementById("houseTexture0").play()
  52. // document.getElementById("houseTexture0").loop = "loop"
  53. shaderMaterial.setTexture("texture_video", videoTexture)
  54. shaderMaterial.setVector3("focal_width_height", new BABYLON.Vector3(
  55. 864 * window.innerWidth / settings.video.width, // 1500
  56. settings.video.width * window.innerHeight / settings.video.height,
  57. window.innerHeight
  58. ))
  59. shaderMaterial.setFloat("isYUV", 0)
  60. m.material = shaderMaterial
  61. }
  62. });
  63. self.charactorManager = new CharactorManager(self)
  64. await self.charactorManager.readPointData()
  65. self.charactorManager.importCharactorModel("../scenes/charactors/", "man_YXL.glb")
  66. });
  67. this.cameraController = new CameraController(self)
  68. }
  69. bindEvents() {
  70. this.scene.onPointerObservable.add((pointerInfo) => {
  71. switch (pointerInfo.type) {
  72. case BABYLON.PointerEventTypes.POINTERDOWN:
  73. this.cameraController.startMouseRotate(pointerInfo)
  74. break;
  75. case BABYLON.PointerEventTypes.POINTERUP:
  76. this.cameraController.endMouseRotate()
  77. break;
  78. case BABYLON.PointerEventTypes.POINTERMOVE:
  79. if(this.cameraController.lastFramePoint)
  80. this.cameraController.mouseRotating(pointerInfo)
  81. break;
  82. case BABYLON.PointerEventTypes.POINTERWHEEL:
  83. break;
  84. case BABYLON.PointerEventTypes.POINTERPICK:
  85. break;
  86. case BABYLON.PointerEventTypes.POINTERTAP:
  87. if(pointerInfo.pickInfo.hit && this.house.indexOf(pointerInfo.pickInfo.pickedMesh)) {
  88. this.charactorManager.clickHouse()
  89. }
  90. break;
  91. case BABYLON.PointerEventTypes.POINTERDOUBLETAP:
  92. break;
  93. }
  94. });
  95. this.scene.onBeforeAnimationsObservable.add(() => {
  96. if(this.charactorManager && this.charactorManager.charactor)
  97. this.charactorManager.onBeforeAnimation()
  98. })
  99. this.scene.onBeforeRenderObservable.add(() => {
  100. this.cameraController.updateCameraPos()
  101. })
  102. }
  103. bindSocketEvents() {
  104. window.connection.socket.on('getSocketVideo', async(data) => {
  105. console.log("[3D] get: ", data)
  106. const blob = new Blob([data], { type: 'application/video' })
  107. const url = URL.createObjectURL(blob)
  108. // this.updateHouseVideoBlob(url, true)
  109. let video = common.createVideoElement0(url)
  110. video.loop = "loop"
  111. setTimeout(function () {
  112. return URL.revokeObjectURL(url)
  113. }, 3000)
  114. // this.cameraController.rotateCamera(this.cameraController.alphaOffset)
  115. })
  116. window.connection.socket.on('getVideo', async (jsonArr) => {
  117. let path = jsonArr.map( async (data) => {
  118. const blob = new Blob([data.video], { type: 'application/video' })
  119. const url = URL.createObjectURL(blob)
  120. setTimeout(function () {
  121. return URL.revokeObjectURL(url)
  122. }, 3000)
  123. return {
  124. point: new BABYLON.Vector3(data.point.x, 0, data.point.z),
  125. video: await common.createVideoElement(url)
  126. }
  127. })
  128. // 行走时锁定camera
  129. this.cameraController.lockCamera(true)
  130. this.charactorManager.charactor.startWalk(path, this)
  131. })
  132. window.connection.socket.on("getRoute", (event) => {
  133. console.log("getRoute", event);
  134. // window.connection.socket.emit("getRotateVideo", {
  135. // videoPath: pointData.id + "/" + pointData.id,
  136. // sangle: 0,
  137. // eangle: 359,
  138. // reverses: true,
  139. // sceneCode: settings.sceneCode,
  140. // roomId: settings.roomId,
  141. // userId: settings.userId,
  142. // });
  143. // window.connection.socket.emit("getRotateVideo", {
  144. // videoPath: pointData.id + "/" + pointData.id,
  145. // sangle: 0,
  146. // eangle: 359,
  147. // reverses: false,
  148. // sceneCode: settings.sceneCode,
  149. // roomId: settings.roomId,
  150. // userId: settings.userId,
  151. // });
  152. });
  153. }
  154. updateHouseVideo(video, notPlay) {
  155. let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
  156. this.house.forEach(mesh => {
  157. mesh.material && mesh.material.setTexture("texture_video", videoTexture)
  158. })
  159. !notPlay && video.play()
  160. }
  161. async updateHouseVideoBlob(blobUrl, notPlay) {
  162. let video = await common.createVideoElement(blobUrl)
  163. video.loop = "loop"
  164. let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
  165. this.house.forEach(mesh => {
  166. mesh.material && mesh.material.setTexture("texture_video", videoTexture)
  167. })
  168. !notPlay && video.play()
  169. }
  170. }