import { Database, Engine, RenderingManager, Scene } from "@babylonjs/core"; import { AdvancedDynamicTexture } from "@babylonjs/gui"; import { SceneManager } from "./controller/scene-manager"; import { LoginSatus } from "./controller/status/login-status"; import { StatusManager } from "./controller/status/status-manager"; import { UIManager } from "./controller/ui-manager"; export class Game { public canvas: HTMLCanvasElement; public engine: Engine; public scene: Scene; public uiRoot: AdvancedDynamicTexture; static instance: Game; //初始化引擎和画布 public init(canvas: HTMLCanvasElement) { Game.instance = this; this.canvas = canvas; this.engine = new Engine(canvas, null, { stencil: true }); Database.IDBStorageEnabled = true;//开启本地缓存 this.scene = new Scene(this.engine); this.scene.useRightHandedSystem = true;//使用右手坐标系 RenderingManager.MIN_RENDERINGGROUPS = -1;//最小渲染序列 this.createScene(); let scene = this.scene; // canvas.translate = true; //用于设置背景透明 // scene.autoClear = true; UIManager.Instance.init(); StatusManager.enterStatus(LoginSatus); //最后,将场景渲染出来 (重要,不可缺少) this.engine.runRenderLoop(function () { scene.render(); }) // 监听浏览器改变大小的事件,通过调用engine.resize()来自适应窗口大小 window.addEventListener("resize", function () { Game.instance.engine.resize(); }); } //创建初始场景 private createScene() { let sceneManager = SceneManager.init(this.scene, this.canvas, this.engine); sceneManager.initArcRotateCamera(); sceneManager.initLight(); sceneManager.updateSceneBG(); sceneManager.initSceneEvent(); } }