Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Three.js: The JavaScript 3D Library for WebGL - Second Edition

You're reading from   Learning Three.js: The JavaScript 3D Library for WebGL - Second Edition Create stunning 3D graphics in your browser using the Three.js JavaScript library

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781784392215
Length 422 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jos Dirksen Jos Dirksen
Author Profile Icon Jos Dirksen
Jos Dirksen
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Creating Your First 3D Scene with Three.js 2. Basic Components That Make Up a Three.js Scene FREE CHAPTER 3. Working with the Different Light Sources Available in Three.js 4. Working with Three.js Materials 5. Learning to Work with Geometries 6. Advanced Geometries and Binary Operations 7. Particles, Sprites, and the Point Cloud 8. Creating and Loading Advanced Meshes and Geometries 9. Animations and Moving the Camera 10. Loading and Working with Textures 11. Custom Shaders and Render Postprocessing 12. Adding Physics and Sounds to Your Scene Index

Chapter 1. Creating Your First 3D Scene with Three.js

Modern browsers are slowly getting more powerful features that can be accessed directly from JavaScript. You can easily add video and audio with the new HTML5 tags and create interactive components through the use of the HTML5 canvas. Together with HTML5, modern browsers also started supporting WebGL. With WebGL, you can directly make use of the processing resources of your graphics card and create high-performance 2D and 3D computer graphics. Programming WebGL directly from JavaScript to create and animate 3D scenes is a very complex and error-prone process. Three.js is a library that makes this a lot easier. The following list shows some of the things that Three.js makes easy:

  • Creating simple and complex 3D geometries
  • Animating and moving objects through a 3D scene
  • Applying textures and materials to your objects
  • Making use of different light sources to illuminate the scene
  • Loading objects from 3D-modeling software
  • Adding advanced postprocessing effects to your 3D scene
  • Working with your own custom shaders
  • Creating point clouds

With a couple of lines of JavaScript, you can create anything, from simple 3D models to photorealistic real-time scenes, as shown in the following screenshot (see it yourself by opening http://www.vill.ee/eye/ in your browser):

Creating Your First 3D Scene with Three.js

In this chapter, we'll directly dive into Three.js and create a couple of examples that show you how Three.js works, and which you can use to play around with. We won't dive into all the technical details yet; that's something you'll learn in the following chapters. In this chapter, we'll cover the following points:

  • Tools required to work with Three.js
  • Downloading the source code and examples used in this book
  • Creating your first Three.js scene
  • Improving the first scene with materials, lights, and animations
  • Introducing a couple of helper libraries for statistics and controlling the scene

We'll start this book with a short introduction to Three.js and then quickly move on to the first examples and code samples. Before we get started, let's quickly look at the most important browsers out there and their support for WebGL.

At the time of writing this, WebGL works with the following desktop browsers:

Browser

Support

Mozilla Firefox

This browser has supported WebGL since version 4.0.

Google Chrome

This browser has supported WebGL since version 9.

Safari

Safari Version 5.1 and newer installed on Mac OS X Mountain Lion, Lion, or Snow Leopard supports WebGL. Make sure you enable WebGL in Safari. You can do this by going to Preferences | Advanced and checking Show develop menu in menu bar. After that, go to Develop | Enable WebGL.

Opera

This browser has supported WebGL since version 12.00. You still have to enable this by opening opera:config and setting the values of WebGL and Enable Hardware Acceleration to 1. After that, restart the browser.

Internet Explorer

Internet Explorer was for a long time the only major player that didn't support WebGL. Starting with IE11, Microsoft has added WebGL support.

Basically, Three.js runs on any of the modern browsers except older versions of IE. So, if you want to use an older version of IE, you've got to take an additional step. For IE 10 and older, there is the iewebgl plugin, which you can get from https://github.com/iewebgl/iewebgl. This plugin is installed inside IE 10 and older versions and enables WebGL support for those browsers.

It is also possible to run Three.js on mobile devices; the support for WebGL and the performance you'll get will vary, but both are quickly improving:

Device

Support

Android

The native browser for Android doesn't have WebGL support and is generally also lacking in support for modern HTML5 features. If you want to use WebGL on Android, you can use the latest Chrome, Firefox, or Opera mobile versions.

IOS

With IOS 8, there is also support for WebGL on IOS devices. IOS Safari version 8 has great WebGL support.

Windows mobile

Windows mobile supports WebGL since version 8.1.

With WebGL, you can create interactive 3D visualizations that run very well on desktops and on mobile devices.

Tip

In this book, we'll focus mostly on the WebGL-based renderer provided by Three.js. There is, however, also a CSS 3D-based renderer, which provides an easy API to create CSS 3D-based 3D scenes. A big advantage of using a CSS 3D-based approach is that this standard is supported on almost all mobile and desktop browsers and allows you to render HTML elements in a 3D space. We'll show how to use the CSS 3D browser in Chapter 7, Particles, Sprites, and the Point Cloud.

In this first chapter, you'll directly create your first 3D scene and will be able to run this in any of the previously mentioned browsers. We won't introduce too many complex Three.js features yet, but at the end of this chapter, you'll have created the Three.js scene you can see in the following screenshot:

Creating Your First 3D Scene with Three.js

For this first scene, you'll learn about the basics of Three.js and also create your first animation. Before you start your work on this example, in the next couple of sections, we'll first look at the tools you need to easily work with Three.js and how you can download the examples shown in this book.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image