Babylon - Js Essentials - Sample Chapter
Babylon - Js Essentials - Sample Chapter
ee
P U B L I S H I N G
C o m m u n i t y
$ 29.99 US
19.99 UK
Julien Moreau-Mathis
pl
Babylon.js Essentials
Babylon.js Essentials
Sa
m
E x p e r i e n c e
Babylon.js Essentials
Understand, train, and be ready to develop 3D Web
applications/video games using the Babylon.js framework,
even for beginners
D i s t i l l e d
Julien Moreau-Mathis
Preface
3D development has always been something mystic. Starting from the beginning, this book
introduces the required basics of 3D development and the knowledge you require to use the
Babylon.js framework. It focuses on the simplicity provided by Babylon.js and uses a
combination of theory and practice. All the chapters are provided with example files that
are ready to run; each example file provides the previously learned features of the
framework. Finally, the developers will be ready to easily understand the new features that
are added to the framework in the future and use the more advanced features only using
the documentation.
Chapter 2, The Fundamentals of Babylon.js and Available Tools, starts with the Babylon.js
framework and creates the first 3D scene, showing the simplicity of the framework along
with the theory.
Chapter 3, Create, Load, and Draw 3D Objects on the Screen, starts with the concepts of
Chapter 2, The Fundamentals of Babylon.js and Available Tools, let's introduce the right way
materials in 3D engines. In other words, let's unleash the Babylon.js Standard Material.
Chapter 5, Create Collisions on Objects, focuses on the gameplay itself by managing the
Chapter 6, Manage Audio in Babylon.js, explains one of the added values of the Babylon.js
framework. Let's add and manage sounds, including spatialized sounds, in your scenes.
Chapter 7, Defining Actions on Objects, introduces a smart way to trigger actions on the 3D
objects themselves without to many lines of code as some tasks can be a pain for
developers.
Chapter 8, Add Rendering Effects Using Built-in Post-processes, shows the preferred part of
most of the 3D developers. This shows and explains how to easily beautify the 3D scenes
using post-processes effects, combined with the simplicity provided by Babylon.js.
Preface
Chapter 9, Create and Play Animations, allows us to play with the animations system
provided by Babylon.js. This chapter provides the final skill that you need in order to be
ready to build your own professional 3D application!
The creators
Babylon.js was created by David Catuhe (@deltakosh), David Rousset (@davrous), Pierre
Lagarde (@pierlag), and Michel Rousseau (@rousseau_michel). It's an open source
project essentially developed in their spare time. When they started Babylon.js, they wanted
it to be designed as easy-to-use and then get an accessible 3D engine for everyone. The
official web site (http://www.babylonjs.com/) contains a lot of tutorials for beginners
(even in 3D) to more advanced users with examples for each feature and scenes as
examples.
Chapter 1
cleaner and more descriptive code. It means that if a function has a lot of parameters, it's
easier to fill and understand them instead of always using the documentation as a reference.
Moreover, it allows developers to declare classes (as the ECMAScript 6 specifications do)
and interfaces for a better understandable architecture and structure of code.
[7]
3. To configure the Gulp task, just provide a JS file named gulpfile.js containing
the task description.
4. Import Gulp and Gulp-TypeScript:
var gulp = require("gulp");
var ts = require("gulp-typescript");
6. Once the default task lists all the TS files to transcompile, just call Gulp using the
following command line:
gulp
[8]
Chapter 1
var myVar = "hello !";
Here, you can write exactly the same with TS. The TS compiler will process the type
inference and guess the variable type for you:
var myVar = 1.0; // Which is a number
// or
var myVar = "hello !"; // Which is a string
To specify the type of a variable with TS, type the following command:
var myVar: type = value;
However, it's forbidden to assign a new value with a different type even if you don't
mention the type as follows:
var myVar = 1.0; // Now, myVar is a number
// and
myVar = "hello !"; // Forbidden, "hello" is a string and not a number
To get the JS flexibility with variables, let's introduce the any type. The any type allows
developers to create variables without any static type. The following is an example:
var myVar: any = 1.0; // Is a number but can be anything else
myVar = "Hello !"; // Allowed, myVar's type is "any"
Let's introduce some specific types. It's the occasion to introduce the generics using
[9]
TypeScript and enumerated types. The usage of numbers, Booleans, and strings is the same
in TypeScript and JavaScript. So, no need to learn more.
Enumerated types
Working withenumerated types (enum) is like working with numbers. The syntax is as
follows:
enum FileAccess {Read, Write};
Array
Defining an array with TS is also similar to JS. The following is an example:
// In both languages
var myArray = [];
// or
var myArray = new Array();
With TS, array is a generic class. Then, you can specify the item's type contained in the array
as follows:
var myArray = new Array<number>();
[ 10 ]
Chapter 1
myArray.push("1");
myArray.splice(0, 1);
console.log(myArray); // "[1]"
Creating a class
The syntax in TS to define a class is as follows:
class Writer {
constructor() {
// initialize some things here
}
}
Access:
[ 11 ]
With TS, you can explicitly specify the access specifier of a member (public, private, and
protected), which has been explained as follows:
Public: Any block of code can access the member to read and write
Private: Only this can access this member to read and write
Protected: External blocks of code cannot access the member; only this and
specializers (inheritance) can access this member to read and write
Let's experiment using the Writer class:
// declare class
class Writer {
// Union types. Can be a "string" or
// an array of strings "Array<string>"
public message: string|string[];
private _privateMessage: string = "private message";
protected _protectedMessage: string;
// Constructor. Called by the "new" keyword
constructor(message: string|string[]) {
this.message = message;
this._protectedMessage = "Protected message !"; // Allowed
}
// A public function accessible from everywhere.
// Returns nothing. Then, its return type is "void".
public write(): void {
console.log(this.message); // Allowed
console.log(this._privateMessage); // Allowed
console.log(this._protectedMessage); // Allowed
}
}
var writer = new Writer("My Public Message !");
console.log(writer.message); // Allowed
[ 12 ]
Chapter 1
console.log(writer._privateMessage); // Not allowed
console.log(writer._protectedMessage); // Not allowed
Using interfaces
Interfaces are used to create contracts. It means that if a class implements an interface, the
class must provide all the functions and members defined in the interface. If not, it doesn't
respect the contract, and the compiler will output an error.
All the defined functions are public and all the defined members are public.
With Babylon.js, a good example is to use the IDisposable interface. It means that the
users can call the method named dispose(). This function's job is to deactivate and/or
[ 13 ]
Summary
In this chapter, you obtained the necessary knowledge to develop programs using
TypeScript with Babylon.js. You'll see that working with TypeScript can be more productive
and secure in most cases. Additionally, some developers will be more comfortable when
using types as they are used to development with typing.
Don't hesitate to manipulate TypeScript with the attached example files. Don't forget to
install gulp and run the command lines.
You can also run the following command line:
gulp watch
This will track and recompile the TS files at each modification automatically.
In the next chapter, let's get straight to the heart of the matter with an introduction to the
Babylon.js framework, and how to create an engine and scene entities such as lights,
cameras, and meshes (3D objects). You'll build your first 3D scene with Babylon.js and
understand the architecture of the framework really quickly!
[ 14 ]
www.PacktPub.com
Stay Connected: