|
| 1 | +import './style.css'; |
| 2 | + |
| 3 | +import * as BABYLON from '@babylonjs/core'; |
| 4 | + |
| 5 | +var createScene = function () { |
| 6 | + // This creates a basic Babylon Scene object (non-mesh) |
| 7 | + var scene = new BABYLON.Scene(engine); |
| 8 | + |
| 9 | + // This creates and positions a free camera (non-mesh) |
| 10 | + var camera = new BABYLON.FreeCamera( |
| 11 | + 'camera1', |
| 12 | + new BABYLON.Vector3(0, 5, -10), |
| 13 | + scene |
| 14 | + ); |
| 15 | + |
| 16 | + // This targets the camera to scene origin |
| 17 | + camera.setTarget(BABYLON.Vector3.Zero()); |
| 18 | + |
| 19 | + // This attaches the camera to the canvas |
| 20 | + camera.attachControl(canvas, true); |
| 21 | + |
| 22 | + // This creates a light, aiming 0,1,0 - to the sky (non-mesh) |
| 23 | + var light = new BABYLON.HemisphericLight( |
| 24 | + 'light', |
| 25 | + new BABYLON.Vector3(0, 1, 0), |
| 26 | + scene |
| 27 | + ); |
| 28 | + |
| 29 | + // Default intensity is 1. Let's dim the light a small amount |
| 30 | + light.intensity = 0.7; |
| 31 | + |
| 32 | + // Our built-in 'sphere' shape. |
| 33 | + var sphere = BABYLON.MeshBuilder.CreateSphere( |
| 34 | + 'sphere', |
| 35 | + { diameter: 2, segments: 32 }, |
| 36 | + scene |
| 37 | + ); |
| 38 | + |
| 39 | + // Move the sphere upward 1/2 its height |
| 40 | + sphere.position.y = 1; |
| 41 | + |
| 42 | + // Our built-in 'ground' shape. |
| 43 | + var ground = BABYLON.MeshBuilder.CreateGround( |
| 44 | + 'ground', |
| 45 | + { width: 6, height: 6 }, |
| 46 | + scene |
| 47 | + ); |
| 48 | + |
| 49 | + return scene; |
| 50 | +}; |
| 51 | + |
| 52 | +let engine; |
| 53 | +let canvas; |
| 54 | + |
| 55 | +const main = () => { |
| 56 | + canvas = document.getElementById('renderCanvas'); |
| 57 | + if (!canvas) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + engine = new BABYLON.Engine(canvas, true); |
| 62 | + const scene = createScene(); |
| 63 | + |
| 64 | + window.addEventListener('resize', () => { |
| 65 | + engine.resize(); |
| 66 | + }); |
| 67 | + |
| 68 | + engine.runRenderLoop(() => { |
| 69 | + scene.render(); |
| 70 | + }); |
| 71 | +}; |
| 72 | + |
| 73 | +main(); |
0 commit comments