index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. this.initVideo = true
  33. this.isReverse = false
  34. let self = this
  35. BABYLON.SceneLoader.ImportMesh("", "../scenes/house/", "close_to_bottom_L.glb", this.scene, async function (newMeshes, particleSystems, skeletons, animationGroups) {
  36. self.house = newMeshes
  37. // self.house[0].position = new BABYLON.Vector3(0.6, 2.1, 1.5)
  38. // self.house[0].position = new BABYLON.Vector3(-22, 0, 12)
  39. let houseVideo = document.getElementById("houseTexture0")
  40. setTimeout(() => {
  41. window.connection.socket.emit("getRotateVideoUrl", {
  42. videoPath: "0/0",
  43. sangle: 0,
  44. eangle: 360,
  45. reverses: false,
  46. sceneCode: settings.sceneCode,
  47. roomId: settings.roomId,
  48. userId: settings.userId,
  49. });
  50. window.connection.socket.emit("getRotateVideoUrl", {
  51. videoPath: "0/0" ,
  52. sangle: 0,
  53. eangle: 360,
  54. reverses: true,
  55. sceneCode: settings.sceneCode,
  56. roomId: settings.roomId,
  57. userId: settings.userId,
  58. });
  59. }, 1500)
  60. newMeshes.forEach(m => {
  61. // m.scaling.scaleInPlace(100);
  62. m._geometry && (m.checkCollisions = true)
  63. if(m.material)
  64. {
  65. BABYLON.Effect.ShadersStore['aFragmentShader'] = houseShader.fragment;
  66. BABYLON.Effect.ShadersStore['aVertexShader'] = houseShader.vertex;
  67. let shaderMaterial = new BABYLON.ShaderMaterial("shader", self.scene, { vertex: "a", fragment: "a", }, {
  68. attributes: houseShader.attributes,
  69. uniforms: houseShader.uniforms,
  70. defines: houseShader.defines
  71. });
  72. let videoTexture = new BABYLON.VideoTexture("", houseVideo, self.scene)
  73. // document.getElementById("houseTexture0").play()
  74. // document.getElementById("houseTexture0").loop = "loop"
  75. shaderMaterial.setTexture("texture_video", videoTexture)
  76. shaderMaterial.setVector3("focal_width_height", new BABYLON.Vector3(
  77. 864 * window.innerWidth / settings.video.width, // 1500
  78. settings.video.width * window.innerHeight / settings.video.height,
  79. window.innerHeight
  80. // 864 * window.innerHeight / settings.video.width, // 1500
  81. // settings.video.width * window.innerWidth / settings.video.height,
  82. // window.innerWidth
  83. ))
  84. shaderMaterial.setFloat("isYUV", 0)
  85. m.material = shaderMaterial
  86. }
  87. });
  88. self.charactorManager = new CharactorManager(self)
  89. await self.charactorManager.readPointData()
  90. self.charactorManager.importCharactorModel("../scenes/charactors/", "man_YXL.glb")
  91. });
  92. this.cameraController = new CameraController(self)
  93. }
  94. bindEvents() {
  95. this.scene.onPointerObservable.add((pointerInfo) => {
  96. switch (pointerInfo.type) {
  97. case BABYLON.PointerEventTypes.POINTERDOWN:
  98. this.cameraController.startMouseRotate(pointerInfo)
  99. break;
  100. case BABYLON.PointerEventTypes.POINTERUP:
  101. this.cameraController.endMouseRotate()
  102. break;
  103. case BABYLON.PointerEventTypes.POINTERMOVE:
  104. if(this.cameraController.lastFramePoint)
  105. this.cameraController.mouseRotating(pointerInfo)
  106. break;
  107. case BABYLON.PointerEventTypes.POINTERWHEEL:
  108. break;
  109. case BABYLON.PointerEventTypes.POINTERPICK:
  110. break;
  111. case BABYLON.PointerEventTypes.POINTERTAP:
  112. if(pointerInfo.pickInfo.hit && this.house.indexOf(pointerInfo.pickInfo.pickedMesh)) {
  113. this.charactorManager.clickHouse()
  114. }
  115. break;
  116. case BABYLON.PointerEventTypes.POINTERDOUBLETAP:
  117. break;
  118. }
  119. });
  120. this.scene.onBeforeAnimationsObservable.add(() => {
  121. if(this.charactorManager && this.charactorManager.charactor)
  122. this.charactorManager.onBeforeAnimation()
  123. })
  124. this.scene.onBeforeRenderObservable.add(() => {
  125. this.cameraController.updateCameraPos()
  126. })
  127. }
  128. bindSocketEvents() {
  129. let walkPointsTemp = []
  130. window.connection.socket.on('getRotateVideoUrl', async(data) => {
  131. console.log("[3D] getSocketVideo: ", data)
  132. const url = data
  133. // 获得旋转视频
  134. let video = this.isReverse ? document.getElementById("houseTextureReverse")
  135. : document.getElementById("houseTexture")
  136. video.src = url
  137. this.isReverse = !this.isReverse
  138. // 页面刷新后初次加载视频
  139. if(this.initVideo) {
  140. this.updateHouseVideo(video, true)
  141. this.initVideo = false
  142. this.cameraController.camera.minZ = 0.001
  143. }
  144. })
  145. window.connection.socket.on('getVideoUrl', async(data) => {
  146. console.log("[3D] getSocketVideo: ", data)
  147. const url = data
  148. if(this.getSocketVideoType == "walkPath") {
  149. let dir = this.charactorManager.getVideoDirecNum()
  150. // 获得行走视频
  151. let path = walkPointsTemp.map( (point, i) => {
  152. return {
  153. id: point.id,
  154. point: new BABYLON.Vector3(-point.location.x, 0, point.location.z),
  155. video: i == 0 ? common.createVideoElement0(url) : null,
  156. videoName: i != walkPointsTemp.length - 1 ? (point.id + "/" + point.id + "_" + walkPointsTemp[i+1].id + "_" + dir) : null
  157. }
  158. })
  159. this.getSocketVideoType = "nextWalk"
  160. // 行走时锁定camera
  161. this.cameraController.lockCamera(true)
  162. this.charactorManager.charactor.startWalk(path, this.charactorManager)
  163. }
  164. else if(this.getSocketVideoType == "nextWalk") {
  165. let charactor = this.charactorManager.charactor
  166. let video = common.createVideoElement0(url) //await common.createVideoElement(url)
  167. console.error(charactor.walkData.currentPoint+1)
  168. charactor.walkData.pathArr[charactor.walkData.currentPoint+1].video = video
  169. }
  170. })
  171. window.connection.socket.on('getSocketVideo', async(data) => {
  172. console.log("[3D] getSocketVideo: ", data)
  173. const blob = new Blob( [data], { type: 'application/video' })
  174. const url = URL.createObjectURL(blob)
  175. setTimeout(function () {
  176. URL.revokeObjectURL(url)
  177. }, 3000)
  178. if(this.getSocketVideoType == "walkPath") {
  179. let dir = this.charactorManager.getVideoDirecNum()
  180. // 获得行走视频
  181. let path = walkPointsTemp.map( (point, i) => {
  182. return {
  183. id: point.id,
  184. point: new BABYLON.Vector3(-point.location.x, 0, point.location.z),
  185. video: i == 0 ? common.createVideoElement0(url) : null,
  186. videoName: i != walkPointsTemp.length - 1 ? (point.id + "/" + point.id + "_" + walkPointsTemp[i+1].id + "_" + dir) : null
  187. }
  188. })
  189. this.getSocketVideoType = "nextWalk"
  190. // 行走时锁定camera
  191. this.cameraController.lockCamera(true)
  192. this.charactorManager.charactor.startWalk(path, this.charactorManager)
  193. }
  194. else if(this.getSocketVideoType == "nextWalk") {
  195. let charactor = this.charactorManager.charactor
  196. let video = common.createVideoElement0(url) //await common.createVideoElement(url)
  197. console.error(charactor.walkData.currentPoint+1)
  198. charactor.walkData.pathArr[charactor.walkData.currentPoint+1].video = video
  199. }
  200. else {
  201. // 获得旋转视频
  202. let video = this.isReverse ? document.getElementById("houseTextureReverse")
  203. : document.getElementById("houseTexture")
  204. video.src = url
  205. this.isReverse = !this.isReverse
  206. // 页面刷新后初次加载视频
  207. if(this.initVideo) {
  208. this.updateHouseVideo(video, true)
  209. this.initVideo = false
  210. this.cameraController.camera.minZ = 0.001
  211. }
  212. }
  213. })
  214. window.connection.socket.on('getVideoUrl', async (data) => {
  215. console.log("[3D] getVideoUrl: ", data)
  216. // const blob = new Blob([data], { type: 'application/video' })
  217. // const url = URL.createObjectURL(blob)
  218. // setTimeout(function () {
  219. // URL.revokeObjectURL(url)
  220. // }, 3000)
  221. // let path = this.currentPoints.map( (point, index) => {
  222. // return {
  223. // point: new BABYLON.Vector3(point.location.x, 0, point.location.z),
  224. // video: url // await common.createVideoElement0(url)
  225. // }
  226. // })
  227. // // 行走时锁定camera
  228. // this.cameraController.lockCamera(true)
  229. // this.charactorManager.charactor.startWalk(path, this.charactorManager)
  230. })
  231. // 获得行走路径
  232. window.connection.socket.on("getRoute", (data) => {
  233. console.log("[3D] getRoute", data);
  234. if(!data || data.length < 2) return
  235. walkPointsTemp = data
  236. let dir = this.charactorManager.getVideoDirecNum()
  237. let firstVideoName = data[0].id + "/" + data[0].id + "_" + data[1].id + "_" + dir
  238. this.getSocketVideoType = "walkPath"
  239. let sendData = {
  240. videoList: [firstVideoName],
  241. sceneCode: settings.sceneCode,
  242. roomId: settings.roomId,
  243. userId: settings.userId,
  244. }
  245. console.log("[3D] send(getVideoUrl): ", sendData);
  246. connection.socket.emit("getVideoUrl", sendData);
  247. });
  248. }
  249. updateHouseVideo(video, notPlay) {
  250. let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
  251. this.house.forEach(mesh => {
  252. mesh.material && mesh.material.setTexture("texture_video", videoTexture)
  253. })
  254. !notPlay && video.play()
  255. }
  256. async updateHouseVideoBlob(blobUrl, notPlay) {
  257. let video = await common.createVideoElement(blobUrl)
  258. // video.loop = "loop"
  259. let videoTexture = new BABYLON.VideoTexture("", video, this.scene)
  260. this.house.forEach(mesh => {
  261. mesh.material && mesh.material.setTexture("texture_video", videoTexture)
  262. })
  263. !notPlay && video.play()
  264. }
  265. }