Guest User

Untitled

a guest
Jun 8th, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. import {
  2. Engine,
  3. Scene,
  4. ArcRotateCamera,
  5. Vector3,
  6. HemisphericLight,
  7. Color3,
  8. Color4,
  9. Sound,
  10. Nullable,
  11. Mesh,
  12. VertexData,
  13. SceneLoader,
  14. } from "@babylonjs/core";
  15.  
  16. import "@babylonjs/core/Debug/debugLayer";
  17. import "@babylonjs/inspector";
  18.  
  19. import "@babylonjs/loaders";
  20.  
  21. import {
  22. Cartesian3,
  23. Ion,
  24. createGooglePhotorealistic3DTileset,
  25. Scene as CesiumScene,
  26. } from "cesium";
  27.  
  28. import SceneManager from "../SceneManager";
  29.  
  30. import GameLevel from "../IGameLevel";
  31.  
  32. // Set up scene
  33. class SurfaceScreen implements GameLevel {
  34. private engine: Engine;
  35. private canvas: HTMLCanvasElement;
  36. private scene: Nullable<Scene>;
  37. private sceneManager: SceneManager;
  38. private cesiumScene: CesiumScene;
  39. private cesiumCamera: any;
  40. private latitude: any;
  41. private longitude: any;
  42. private altitude: any;
  43.  
  44. constructor(
  45. engine: Engine,
  46. canvas: HTMLCanvasElement,
  47. sceneManager: SceneManager,
  48. ) {
  49. this.engine = engine;
  50. this.canvas = canvas;
  51. this.scene = null;
  52. this.sceneManager = sceneManager;
  53. this.longitude = -73.935242;
  54. this.latitude = 40.73061;
  55. this.altitude = 48;
  56.  
  57. Ion.defaultAccessToken =
  58. "YOUR_API_KEY";
  59.  
  60. //Dont actually attach this div to the DOM...
  61. let container = document.createElement("DIV2");
  62. let cesiumCanvas = document.createElement("canvas");
  63. container.appendChild(cesiumCanvas);
  64.  
  65. this.cesiumScene = new CesiumScene({
  66. canvas: cesiumCanvas,
  67. contextOptions: {
  68. webgl: {
  69. alpha: true,
  70. depth: true,
  71. stencil: false,
  72. antialias: true,
  73. powerPreference: "high-performance",
  74. failIfMajorPerformanceCaveat: true,
  75. },
  76. },
  77. });
  78. this.cesiumCamera = this.cesiumScene.camera;
  79. }
  80.  
  81. initialize() {
  82. console.log("Creating Surface level");
  83. this.scene = new Scene(this.engine);
  84. //this.scene.debugLayer.show();
  85.  
  86. let camera = new ArcRotateCamera(
  87. "Camera",
  88. 0,
  89. 0.8,
  90. 14460639.453, //These models are huge....
  91. Vector3.Zero(),
  92. this.scene,
  93. );
  94. camera.maxZ = 16893714734; //Make far wall very far
  95. camera.attachControl(this.canvas, true);
  96.  
  97. // Function to update the radius (zoom) based on scroll wheel
  98. function updateWheelPrecision() {
  99. var radius = camera.radius;
  100. // Adjust this factor based on how you want the zoom speed to behave
  101. var factor = 200;
  102. camera.wheelPrecision = factor / radius;
  103. }
  104.  
  105. // Attach event listeners for zooming
  106. camera.onViewMatrixChangedObservable.add(updateWheelPrecision);
  107. updateWheelPrecision(); // Initial call to set the correct wheel precision
  108.  
  109. this.scene.clearColor = new Color4(0, 0, 0, 0);
  110.  
  111. // Creates a light, aiming 0,1,0 - to the sky
  112. const light = new HemisphericLight(
  113. "light",
  114. new Vector3(0, 1, 0),
  115. this.scene,
  116. );
  117. light.diffuse = Color3.White();
  118. light.specular = Color3.White();
  119. light.groundColor = Color3.White();
  120. light.intensity = 0.5; //Easy
  121.  
  122. this.loadCesium();
  123. }
  124.  
  125. convertCesiumMeshToBabylon(cesiumMesh: any) {
  126. console.log("CONVERT", cesiumMesh.content._model);
  127. const vertices = [];
  128. const indices = [];
  129.  
  130. // Extract vertex data from Cesium mesh
  131. const positions =
  132. cesiumMesh._root._content._content._model._drawCommands[0]._vertexArray
  133. ._vertexBufferTypedArray;
  134. const indicesArray =
  135. cesiumMesh._root._content._content._model._drawCommands[0]._vertexArray
  136. ._indexBufferTypedArray;
  137.  
  138. for (let i = 0; i < positions.length; i += 3) {
  139. vertices.push(positions[i], positions[i + 1], positions[i + 2]);
  140. }
  141.  
  142. for (let i = 0; i < indicesArray.length; i++) {
  143. indices.push(indicesArray[i]);
  144. }
  145.  
  146. // Create a new Babylon.js mesh
  147. const babylonMesh = new Mesh("convertedMesh", this.scene);
  148. const vertexData = new VertexData();
  149. vertexData.positions = vertices;
  150. vertexData.indices = indices;
  151. vertexData.applyToMesh(babylonMesh);
  152.  
  153. return babylonMesh;
  154. }
  155.  
  156. async loadCesium() {
  157. console.log("LOADING CESIUM");
  158.  
  159. //Set location, hopefully pulling in tiles and automatically managing them if we sync with babyoncamera later...
  160. this.cesiumScene.camera.setView({
  161. destination: Cartesian3.fromDegrees(
  162. this.longitude,
  163. this.latitude,
  164. this.altitude,
  165. ),
  166. });
  167.  
  168. try {
  169. const tileset = await createGooglePhotorealistic3DTileset();
  170. console.log("tileset", tileset);
  171. //DONT forget about request content to maybe avoid the render loop...
  172. tileset.tileVisible.addEventListener((tile) => {
  173. //console.log("VISILE", tile.content);
  174. });
  175. tileset.tileLoad.addEventListener((tile) => {
  176. const content = tile.content;
  177. const gltfUrl = content.url;
  178. console.log(tile);
  179.  
  180. console.log("gltf", gltfUrl);
  181. SceneLoader.ImportMesh("", gltfUrl, "", this.scene, (meshes) => {
  182. //Hook up visible and such hooks
  183. });
  184. });
  185. tileset.tileUnload.addEventListener((tile) => {
  186. console.log("UNLOADED", tile);
  187. });
  188. tileset.tileFailed.addEventListener((tile) => {
  189. console.log("FAILED", tile);
  190. });
  191. tileset.allTilesLoaded.addEventListener((tile) => {
  192. console.log("ALL LOADED", tile);
  193. });
  194. tileset.initialTilesLoaded.addEventListener((tile) => {
  195. console.log("INITIAL LOADEDS", tile);
  196. });
  197. tileset.loadProgress.addEventListener((tile) => {
  198. console.log("PROGRESS", tile);
  199. });
  200. this.cesiumScene.primitives.add(tileset);
  201. } catch (error) {
  202. console.log(`Failed to load tileset: ${error}`);
  203. }
  204. }
  205.  
  206. // Sync Babylon.js camera with Cesium data
  207. syncCamera(camera: any) {
  208. const cesiumCamera = this.cesiumScene.camera;
  209. const babylonCamera = camera;
  210.  
  211. const position = babylonCamera.position;
  212. const direction = babylonCamera.getDirection(Vector3.Forward());
  213.  
  214. const cesiumPosition = new Cartesian3(position.x, position.y, position.z);
  215. const cesiumDirection = new Cartesian3(
  216. direction.x,
  217. direction.y,
  218. direction.z,
  219. );
  220.  
  221. cesiumCamera.setView({
  222. destination: cesiumPosition,
  223. orientation: {
  224. direction: cesiumDirection,
  225. up: Cartesian3.UNIT_Z,
  226. },
  227. });
  228. }
  229.  
  230. dispose() {
  231. this.scene?.dispose();
  232. }
  233.  
  234. render() {
  235. this.cesiumScene.render();
  236. this.scene?.render();
  237. //this.syncCamera(this.scene?.activeCamera);
  238. }
  239. }
  240.  
  241. export default SurfaceScreen;
  242.  
Advertisement
Add Comment
Please, Sign In to add comment