- Babylon.js Essentials
- Julien Moreau-Mathis
- 286字
- 2021-07-16 12:49:11
Creating your first scene
Now, you have all the necessary elements to build your first scene. Here, the scene will be composed of a rotation camera, point light, and box. Let's create a class using TypeScript and practice with Babylon.js.
Creating a class and the scene nodes
The following class creates the Babylon.js elements directly in the constructor:
export class BasicScene { public camera: BABYLON.ArcRotateCamera; // Our camera public light: BABYLON.PointLight; // Our light public box: BABYLON.Mesh; // Our box private _engine: BABYLON.Engine; // The Babylon.js engine private _scene: BABYLON.Scene; // The scene where to add the nodes // Our constructor. The constructor provides the canvas reference // Then, we can create the Babylon.js engine constructor(canvas: HTMLCanvasElement) { // Create engine this._engine = new BABYLON.Engine(canvas); // Create the scene this._scene = new BABYLON.Scene(this._engine); // Create the camera this.camera = new BABYLON.ArcRotateCamera("camera", 0, 0, 30, BABYLON.Vector3.Zero(), this._scene); this.camera.attachControl(canvas, true); // Create the light this.light = new BABYLON.PointLight("light",new BABYLON.Vector3(20, 20, 20), this._scene); this.light.diffuse = new BABYLON.Color3(0, 1, 0); this.light.specular = new BABYLON.Color3(1, 0, 1); this.light.intensity = 1.0; // Create the box this.box = BABYLON.Mesh.CreateBox("cube", 5, this._scene); } }
Call the runRenderLoop method
Let's add a method to the class that will call the runRenderLoop
method:
public runRenderLoop(): void { this._engine.runRenderLoop(() => { this._scene.render(); }); }
This scene is exactly the same as mentioned in the previous image—there is a box and green light.
Managing the scene graph
To practice with the scene graph, let's create a method that will set the light as a child of the camera. The method will set the light's position at coordinates (x=0
,y=0
,z=0
) and set the parent of the light as the camera:
public setCameraParentOfLight(): void { this.light.parent = this.camera; }
推薦閱讀
- SQL Server 從入門到項(xiàng)目實(shí)踐(超值版)
- 計算思維與算法入門
- Mastering Zabbix(Second Edition)
- Java EE框架整合開發(fā)入門到實(shí)戰(zhàn):Spring+Spring MVC+MyBatis(微課版)
- 跟“龍哥”學(xué)C語言編程
- Apache Spark Graph Processing
- ADI DSP應(yīng)用技術(shù)集錦
- C語言程序設(shè)計學(xué)習(xí)指導(dǎo)與習(xí)題解答
- 劍指MySQL:架構(gòu)、調(diào)優(yōu)與運(yùn)維
- Procedural Content Generation for C++ Game Development
- JavaScript程序設(shè)計:基礎(chǔ)·PHP·XML
- Java編程從入門到精通
- PHP與MySQL權(quán)威指南
- Hadoop大數(shù)據(jù)分析技術(shù)
- 軟件測試(慕課版)