Book Image

Learn Three.js - Fourth Edition

By : Jos Dirksen
Book Image

Learn Three.js - Fourth Edition

By: Jos Dirksen

Overview of this book

Three.js has become the industry standard for creating stunning 3D WebGL content. In this edition, you’ll learn about all the features of Three.js and understand how to integrate it with the newest physics engines. You'll also develop a strong grip on creating and animating immersive 3D scenes directly in your browser, reaping the full potential of WebGL and modern browsers. The book starts with the basic concepts and building blocks used in Three.js and helps you explore these essential topics in detail through extensive examples and code samples. You'll learn how to create realistic-looking 3D objects using textures and materials and how to load existing models from an external source. Next, you'll understand how to control the camera using the Three.js built-in camera controls, which will enable you to fly or walk around the 3D scene you've created. Later chapters will cover the use of HTML5 video and canvas elements as materials for your 3D objects to animate your models. You’ll learn how to use morph targets and skeleton-based animation, before understanding how to add physics, such as gravity and collision detection, to your scene. Finally, you’ll master combining Blender with Three.js and creating VR and AR scenes. By the end of this book, you'll be well-equipped to create 3D-animated graphics using Three.js.
Table of Contents (21 chapters)
Part 1: Getting Up and Running
Part 2: Working with the Three.js Core Components
Chapter 5: Learning to Work with Geometries
Part 3: Particle Clouds, Loading and Animating Models
Part 4: Post-Processing, Physics, and Sounds

Styling particles using textures

In this section, we’ll look at the following two ways of changing what the sprite looks like:

  • Use an HTML canvas to draw an image and show that for each sprite
  • Load an external image file to define what each sprite looks like

Let’s start by drawing the image ourselves.

Drawing an image on the canvas

In the attributes for THREE.PointsMaterial, we mentioned the map property. With the map property, we can load a texture for the individual points. With Three.js, this texture can also be the output from an HTML5 canvas. Before we look at the code, let’s look at an example (canvastexture.js):

Figure 7.4 – Creating sprites using a canvas-based texture

Figure 7.4 – Creating sprites using a canvas-based texture

Here, you can see that we’ve got a large set of Pac-Man-like ghosts on the screen. This uses the same approach that we saw in the Understanding points and sprites section earlier. This time, though, we aren’t showing...