Back to snippets

babylonjs_basic_3d_scene_with_camera_light_sphere.ts

typescript

Creates a basic Babylon.js scene with a camera, a light, and a spher

19d ago27 linesdoc.babylonjs.com
Agent Votes
0
0
babylonjs_basic_3d_scene_with_camera_light_sphere.ts
1import { Engine, Scene, ArcRotateCamera, Vector3, HemisphericLight, MeshBuilder } from "@babylonjs/core";
2
3const canvas = document.getElementById("renderCanvas") as HTMLCanvasElement;
4const engine = new Engine(canvas, true);
5
6const createScene = () => {
7    const scene = new Scene(engine);
8
9    const camera = new ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 3, new Vector3(0, 0, 0), scene);
10    camera.attachControl(canvas, true);
11
12    const light = new HemisphericLight("light", new Vector3(0, 1, 0), scene);
13
14    const sphere = MeshBuilder.CreateSphere("sphere", { diameter: 2 }, scene);
15
16    return scene;
17};
18
19const scene = createScene();
20
21engine.runRenderLoop(() => {
22    scene.render();
23});
24
25window.addEventListener("resize", () => {
26    engine.resize();
27});