Book Image

Game Development with Three.js

By : Isaac Sukin
Book Image

Game Development with Three.js

By: Isaac Sukin

Overview of this book

The advent of WebGL and its inclusion in many browsers enabled JavaScript programs running in a web browser to access the GPU without a plugin or extension. Three.js is a next generation high-level library that makes it possible to author complex 3D computer animations that display in the browser using nothing more than a simple text editor. The development of these new tools has opened up the world of real-time 3D computer animations to a far broader spectrum of developers. Starting with how to build 3D games on the web using the Three.js graphics library, you will learn how to build 3D worlds with meshes, lighting, user interaction, physics, and more. Along the way, you'll learn how to build great online games through fun examples. Use this book as a guide to embrace the next generation of game development! Moving on from the basics, you will learn how to use Three.js to build game worlds using its core components, including renderers, geometries, materials, lighting, cameras, and scenes. Following on from this, you will learn how to work with mouse and keyboard interactions, incorporate game physics, and import custom models and animations. You will also learn how to include effects like particles, sounds, and post-processing. You will start by building a 3D world, and then create a first person shooter game using it. You will then be shown how to imbue this FPS game with a “capture the flag” gameplay objective. With Game Development with Three.js, you will be able to build 3D games on the Web using the Three.js graphics library.
Table of Contents (12 chapters)

Preface

Three.js is an easy-to-learn 3D graphics library for the web. This book explains the Three.js API and how to use it to build immersive online games. By the time you finish this book, you'll be able to reach millions of gamers through their web browsers, and you'll build exciting projects such as a first-person shooter along the way.

I've been building games for more than a decade. When I discovered Three.js, the first project I built was very similar to the first-person shooter game you'll build in Chapter 3, Exploring and Interacting. I was hooked by how quickly I could create fun games with no prior exposure to the library.

In Game Development with Three.js, I've tried to stay true to that exploratory spirit. I hope you have as much fun reading it as I had writing it.

What this book covers

Chapter 1, Hello, Three.js, describes what Three.js is and what it does, how to start writing code with it, and a basic scene.

Chapter 2, Building a World, explains the components of a Three.js scene including renderers, geometries, materials, and lighting for building a procedurally generated city.

Chapter 3, Exploring and Interacting, explains mouse and keyboard interaction, basic physics, and creating a first-person shooter game.

Chapter 4, Adding Detail, explains particle systems, sound, graphic effects, and managing external assets such as 3D models in addition to building a capture-the-flag game.

Chapter 5, Design and Development, describes game design for the web, including development processes, performance considerations, and the basics of networking.

What you need for this book

You will need a web browser. To fully experience all the features discussed in this book, use Chrome 22 or later or Firefox 22 or later. Internet Explorer 11 or later should also work. A text editor is also recommended, especially if you are not using Chrome, as discussed in Chapter 1, Hello, Three.js. You will need an Internet connection at certain points in the book such as when downloading the Three.js library (these points will be identified in the text).

Who this book is for

This book is for people interested in programming 3D games for the web. Basic familiarity with JavaScript syntax and a basic understanding of HTML and CSS is assumed. No prior exposure to Three.js is assumed. This book should be useful regardless of prior experience with game programming, whether you intend to build casual side projects or large-scale professional titles.

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: "The THREE variable is global."

A block of code is set as follows:

renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

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

renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

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: "If you want to experiment with WebGL features that are still in development, you can enable some of them in Canary's about:flags page."

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 via 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 for all Packt books you have purchased from your account at http://www.packtpub.com. 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 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 http://www.packtpub.com/sites/default/files/downloads/8539OS_Images.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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.