Guest User

Untitled

a guest
Jun 12th, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. import {
  2. Engine,
  3. Scene,
  4. ArcRotateCamera,
  5. FreeCamera,
  6. DeviceOrientationCamera,
  7. Vector3,
  8. HemisphericLight,
  9. Color3,
  10. Color4,
  11. Sound,
  12. Nullable,
  13. Mesh,
  14. AbstractMesh,
  15. VertexData,
  16. SceneLoader,
  17. Quaternion,
  18. MeshBuilder,
  19. StandardMaterial,
  20. } from "@babylonjs/core";
  21.  
  22. import {
  23. Cartesian3,
  24. Ion,
  25. createGooglePhotorealistic3DTileset,
  26. Scene as CesiumScene,
  27. } from "cesium";
  28.  
  29. import SceneManager from "../SceneManager";
  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 babylonCamera: any;
  41. private latitude: any;
  42. private longitude: any;
  43. private altitude: any;
  44. private initialCesiumPosition: any;
  45. private tileset: any;
  46.  
  47. constructor(
  48. engine: Engine,
  49. canvas: HTMLCanvasElement,
  50. sceneManager: SceneManager,
  51. ) {
  52. this.engine = engine;
  53. this.canvas = canvas;
  54. this.scene = null;
  55. this.sceneManager = sceneManager;
  56. this.longitude = -73.935242;
  57. this.latitude = 40.73061;
  58. this.altitude = 0;
  59.  
  60. Ion.defaultAccessToken = "YOUR-API-KEY";
  61.  
  62. this.cesiumScene = new CesiumScene({
  63. canvas: canvas,
  64. contextOptions: {
  65. webgl: {
  66. alpha: true,
  67. },
  68. },
  69. });
  70.  
  71. this.cesiumCamera = this.cesiumScene.camera;
  72. }
  73.  
  74. initialize() {
  75. console.log("Creating Surface level");
  76. this.scene = new Scene(this.engine);
  77. this.babylonCamera = new DeviceOrientationCamera(
  78. "deviceOrientationCamera",
  79. new Vector3(0, 1, -10),
  80. this.scene,
  81. );
  82. this.babylonCamera.attachControl(this.canvas, true);
  83. this.sphere = MeshBuilder.CreateSphere(
  84. "sphere",
  85. { diameter: 2 },
  86. this.scene,
  87. );
  88. this.sphere.parent = this.babylonCamera;
  89. this.sphere.position = new Vector3(0, -2, 10);
  90. this.scene.autoClear = false;
  91.  
  92. // Creates a light, aiming to the sky
  93. const light = new HemisphericLight(
  94. "light",
  95. new Vector3(0, 1, 0),
  96. this.scene,
  97. );
  98. light.diffuse = Color3.White();
  99. light.specular = Color3.White();
  100. light.groundColor = Color3.White();
  101. light.intensity = 0.5; //Easy
  102.  
  103. this.tileset = await createGooglePhotorealistic3DTileset();
  104. this.cesiumScene.primitives.add(this.tileset);
  105. this.cesiumCamera.setView({
  106. destination: Cartesian3.fromDegrees(
  107. this.longitude,
  108. this.latitude,
  109. this.altitude,
  110. ),
  111. });
  112.  
  113. this.initialCesiumPosition = this.cesiumCamera.position.clone();
  114. }
  115.  
  116. dispose() {
  117. this.scene?.dispose();
  118. }
  119.  
  120. render() {
  121. this.cesiumCamera.setView({
  122. orientation: {
  123. heading: -this.babylonCamera.rotation.y,
  124. pitch: -this.babylonCamera.rotation.x,
  125. roll: this.babylonCamera.rotation.z,
  126. },
  127. });
  128. this.scene?.getEngine().wipeCaches(true);
  129. this.cesiumScene.render();
  130. this.scene?.render();
  131. }
  132. }
  133.  
  134. export default SurfaceScreen;
  135.  
Advertisement
Add Comment
Please, Sign In to add comment