Book Image

Three.js Cookbook

By : Jos Dirksen
Book Image

Three.js Cookbook

By: Jos Dirksen

Overview of this book

Table of Contents (15 chapters)
Three.js Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Each year, web browsers become more powerful, increase in features, and improve in performance. Throughout the last couple of years, browsers have emerged as a great platform to create immersive, complex, and beautiful applications. Most of the current applications that are being built use modern HTML5 features such as web sockets, local storage, and advanced CSS techniques for styling.

Most modern browsers, however, also have support for a technology that can be used to create beautiful, 3D graphics and animations that use the GPU to achieve maximal performance. This technology is called WebGL and is supported by the latest versions of Firefox, Chrome, Safari, and Internet Explorer. With WebGL, you can create 3D scenes that run directly in your browser, without the need for any plugins. Support on the desktop for this standard is great, and most modern devices and mobile browsers fully support this standard.

To create WebGL applications, however, you need to learn a new language (called GLSL) and understand how vertex and fragment shaders can be used to render your 3D geometries. Luckily, though, there are a number of JavaScript libraries available that wrap the WebGL internals and provide a JavaScript API that you can use without having to understand the most complex features of WebGL. One of the most mature and feature-rich of those libraries is Three.js.

Three.js started in 2010 and provides a large number of easy-to-use APIs that expose all the features of Three.js and allows you to quickly create complex 3D scenes and animations in your browser.

Through its APIs, you can do pretty much everything that you want with Three.js. However, because it has so many features, it's sometimes difficult to find the right way to accomplish something. Throughout the years, Three.js has been under heavy development, but it is stabilizing now. So many examples and tutorials that you find online are outdated and don't work anymore. In this book, we'll provide you with a large number of recipes that you can follow to accomplish some common tasks with Three.js. Each example is accompanied with a runnable example that you can examine to better understand the recipe or adapt for your own purposes.

What this book covers

Chapter 1, Getting Started, covers the basic recipes that you can use when you create new Three.js-based applications. We'll show you how you can set up a basic Three.js skeleton using any of the available Three.js renderers. We'll further show you WebGL detection, loading resources, setting up an animation loop, adding drag and drop support, and controlling your scene through the keyboard.

Chapter 2, Geometries and Meshes, shows you a number of recipes that focus on creating, working with and manipulating geometries and meshes. We'll go into detail on how to rotate meshes in different ways, manipulate them using matrix transformations, generate geometries programmatically, and load models from Blender and in other formats.

Chapter 3, Working with the Camera, focuses on recipes that manipulate the cameras available in Three.js. It shows you how to work with the perspective and the orthogonal camera. This chapter also shows you recipes that explain how to rotate a camera, center a camera, and follow objects around.

Chapter 4, Materials and Textures, contains recipes that explain how to get good results working with the materials provided by Three.js. It has recipes on transparency, reflections, UV mapping, face materials, bump and normal maps and also explains how the various blend modes work.

Chapter 5, Lights and Custom Shaders, has recipes that deal with the workings of the different light sources in Three.js and shows you how to work with WebGL shaders. It shows you how to correctly set up shadows, create a sun-like lighting source, and goes into the differences between spot lights, point lights, and directional lights. In this chapter, we'll also provide you with a couple of recipes that explain how to create a custom vertex shader and a custom fragment shader.

Chapter 6, Point Clouds and Postprocessing, provides you with recipes that show you how to set up postprocessing. With postprocessing, you can enhance your scene with blurring, coloring, or other types of effects. This chapter also contains recipes that explain features of a particle system, such as animation and particle materials.

Chapter 7, Animation and Physics, shows you a number of recipes that help you in animating the objects in your scene and show you how to add physics (such as gravity and collision detection) to your scene.

What you need for this book

The only thing that you need for this book is a simple text editor to experiment with the provided recipes and a modern web browser to run the examples. For some of the advanced recipes, it is preferred to have a locally installed web server or disable some security settings in your browser. In Chapter 1, Getting Started, recipes are provided that explain how to set up such a server and disable the relevant security settings.

Who this book is for

This book is for everyone who has a basic understanding of JavaScript and Three.js but wants to learn how to use more advanced features of Three.js You don't need to understand advanced math concepts or have in depth knowledge of WebGL. The recipes in this book will explain the various features of Three.js step-by-step and we also provide all the recipes as ready to use HTML sources.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: " One interesting thing to note is the addition of texture.needsUpdate = true to the ondrop event handler."

A block of code is set as follows:

step1(function (value1) {
  step2(value1, function(value2) {
    step3(value2, function(value3) {
      step4(value3, function(value4) {
        // Do something with value4
      });
    });
  });
});

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    var y = camera.position.y;
    camera.position.y = y * Math.cos(control.rotSpeed) +z * Math.sin(control.rotSpeed);
    camera.position.z = z * Math.cos(control.rotSpeed) –y * Math.sin(control.rotSpeed);

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "On this screen, just click on the I'll be careful, I promise! button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/1182OS.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.