Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo
Learning Three.js – the JavaScript 3D Library for WebGL - Second Edition

Book Description

Create stunning 3D graphics in your browser using the Three.js JavaScript library

In Detail

Modern browsers support WebGL, which makes it possible to create 3D graphics in a browser without having to use plugins such as Flash and Java. Programming WebGL, however, is difficult, complex, and very difficult to debug. With Three.js, it is possible to create stunning 3D graphics in an intuitive manner using JavaScript, without the need to know the details of WebGL.

Learning Three.js – the JavaScript 3D Library for WebGL, Second Edition, is a practical, example-rich book that will help you learn about all the features of Three.js. This book will show you how to create or load models from externally created models and realistic-looking 3D objects using materials and textures. You'll also learn how to use the HTML5 video and canvas elements as a material for your 3D objects, different ways of animating your models, skeleton-based animation, and how to add physics such as gravity and collision detection to your scene. By the end of the book, you'll know everything that is required to create 3D animated graphics that run in any browser using Three.js.

What You Will Learn

  • Use the different geometries that are provided by Three.js

  • Apply realistic lighting and shadows to the 3D objects you have created

  • Use the different camera controls provided by Three.js to effortlessly navigate around your 3D scene

  • Make use of advanced textures on materials to create realistic-looking 3D objects by using bump maps, normal maps, specular maps, and light maps

  • Import and animate models from external formats, such as OBJ, STL, Collada, and many more

  • Create and run animations using morph targets and bone animations

  • Explore the Physijs JavaScript library to apply physics to your 3D objects

  • Interact directly with WebGL by creating custom vertex and fragment shaders

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Learning Three.js – the JavaScript 3D Library for WebGL Second Edition
      1. Table of Contents
      2. Learning Three.js – the JavaScript 3D Library for WebGL Second Edition
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      9. 1. Creating Your First 3D Scene with Three.js
        1. Requirements to use Three.js
        2. Getting the source code
          1. Using Git to clone the repository
          2. Downloading and extracting the archive
          3. Testing the examples
            1. Python-based web servers should work on most Unix/Mac systems
            2. Npm-based web server if you've worked with Node.js
            3. Portable version Mongoose for Mac and/or Windows
            4. Disabling security exceptions in Firefox and Chrome
        3. Creating the HTML skeleton
        4. Rendering and viewing a 3D object
        5. Adding materials, lights, and shadows
        6. Expanding your first scene with animations
          1. Introducing requestAnimationFrame
          2. Animating the cube
          3. Bouncing the ball
        7. Using dat.GUI to make experimenting easier
        8. Automatically resize the output when browser size changes
        9. Summary
      10. 2. Basic Components That Make Up a Three.js Scene
        1. Creating a scene
          1. Basic functionality of a scene
          2. Adding fog to the scene
          3. Using the overrideMaterial property
        2. Geometries and meshes
          1. The properties and functions of a geometry
          2. Functions and attributes for meshes
        3. Different cameras for different uses
          1. Orthographic camera versus perspective camera
          2. Looking at specific points
        4. Summary
      11. 3. Working with the Different Light Sources Available in Three.js
        1. Different kinds of lighting provided by Three.js
        2. Basic lights
          1. THREE.AmbientLight
            1. Using the THREE.Color object
          2. THREE.PointLight
          3. THREE.SpotLight
          4. THREE.DirectionalLight
        3. Special lights
          1. THREE.HemisphereLight
          2. THREE.AreaLight
          3. LensFlare
        4. Summary
      12. 4. Working with Three.js Materials
        1. Understanding common material properties
          1. Basic properties
          2. Blending properties
          3. Advanced properties
        2. Starting with a simple mesh
          1. THREE.MeshBasicMaterial
          2. THREE.MeshDepthMaterial
          3. Combining materials
          4. THREE.MeshNormalMaterial
          5. THREE.MeshFaceMaterial
        3. Advanced materials
          1. THREE.MeshLambertMaterial
          2. THREE.MeshPhongMaterial
          3. Creating your own shaders with THREE.ShaderMaterial
        4. Materials you can use for a line geometry
          1. THREE.LineBasicMaterial
          2. THREE.LineDashedMaterial
        5. Summary
      13. 5. Learning to Work with Geometries
        1. The basic geometries provided by Three.js
          1. Two-dimensional geometries
            1. THREE.PlaneGeometry
            2. THREE.CircleGeometry
            3. THREE.RingGeometry
            4. THREE.ShapeGeometry
          2. Three-dimensional geometries
            1. THREE.BoxGeometry
            2. THREE.SphereGeometry
            3. THREE.CylinderGeometry
            4. THREE.TorusGeometry
            5. THREE.TorusKnotGeometry
            6. THREE.PolyhedronGeometry
              1. THREE.IcosahedronGeometry
              2. THREE.TetrahedronGeometry
              3. THREE.Octahedron Geometry
              4. THREE.DodecahedronGeometry
        2. Summary
      14. 6. Advanced Geometries and Binary Operations
        1. THREE.ConvexGeometry
        2. THREE.LatheGeometry
          1. Creating a geometry by extruding
            1. THREE.ExtrudeGeometry
            2. THREE.TubeGeometry
            3. Extrude from SVG
            4. THREE.ParametricGeometry
        3. Creating 3D text
          1. Rendering text
          2. Adding custom fonts
        4. Using binary operations to combine meshes
          1. The subtract function
          2. The intersect function
          3. The union function
        5. Summary
      15. 7. Particles, Sprites, and the Point Cloud
        1. Understanding particles
        2. Particles, THREE.PointCloud, and THREE.PointCloudMaterial
        3. Styling particles with the HTML5 canvas
          1. Using HTML5 canvas with THREE.CanvasRenderer
          2. Using HTML5 canvas with WebGLRenderer
        4. Using textures to style particles
        5. Working with sprite maps
        6. Creating THREE.PointCloud from an advanced geometry
        7. Summary
      16. 8. Creating and Loading Advanced Meshes and Geometries
        1. Geometry grouping and merging
          1. Grouping objects together
          2. Merging multiple meshes into a single mesh
          3. Loading geometries from external resources
          4. Saving and loading in Three.js JSON format
            1. Saving and loading THREE.Mesh
            2. Saving and loading a scene
          5. Working with Blender
            1. Installing the Three.js exporter in Blender
            2. Loading and exporting a model from Blender
          6. Importing from 3D file formats
            1. The OBJ and MTL formats
            2. Loading a Collada model
            3. Loading the STL, CTM, VTK, AWD, Assimp, VRML, and Babylon models
            4. Show proteins from Protein Data Bank
            5. Creating a particle system from a PLY model
        2. Summary
      17. 9. Animations and Moving the Camera
        1. Basic animations
          1. Simple animations
          2. Selecting objects
          3. Animating with Tween.js
        2. Working with the camera
          1. TrackballControls
          2. FlyControls
          3. RollControls
          4. FirstPersonControls
          5. OrbitControl
        3. Morphing and skeletal animation
          1. Animation with morph targets
            1. Animation with MorphAnimMesh
            2. Creating an animation by setting the morphTargetInfluence property
          2. Animation using bones and skinning
        4. Creating animations using external models
          1. Creating a bones animation using Blender
          2. Loading an animation from a Collada model
          3. Animation loaded from a Quake model
        5. Summary
      18. 10. Loading and Working with Textures
        1. Using textures in materials
          1. Loading a texture and applying it to a mesh
          2. Using a bump map to create wrinkles
          3. Achieving more detailed bumps and wrinkles with a normal map
          4. Creating fake shadows using a light map
          5. Creating fake reflections using an environment map
          6. Specular map
        2. Advanced usage of textures
          1. Custom UV mapping
          2. Repeat wrapping
          3. Rendering to canvas and using it as a texture
            1. Using the canvas as a texture
            2. Using the canvas as a bump map
          4. Using the output from a video as a texture
        3. Summary
      19. 11. Custom Shaders and Render Postprocessing
        1. Setting up Three.js for postprocessing
          1. Creating THREE.EffectComposer
            1. Configuring THREE.EffectComposer for postprocessing
            2. Updating the render loop
        2. Postprocessing passes
          1. Simple postprocessing passes
            1. Using THREE.FilmPass to create a TV-like effect
            2. Adding a bloom effect to the scene with THREE.BloomPass
            3. Output the scene as a set of dots
            4. Showing the output of multiple renderers on the same screen
          2. Advanced EffectComposer flows using masks
          3. Using THREE.ShaderPass for custom effects
            1. Simple shaders
            2. Blurring shaders
            3. Advanced shaders
        3. Creating custom postprocessing shaders
          1. Custom grayscale shader
          2. Creating a custom bit shader
        4. Summary
      20. 12. Adding Physics and Sounds to Your Scene
        1. Creating a basic Three.js scene
        2. Material properties
        3. Basic supported shapes
        4. Using constraints to limit movement of objects
          1. Using PointConstraint to limit movement between two points
          2. Creating door-like constraints with HingeConstraint
          3. Limiting movement to a single axis with SliderConstraint
          4. Creating a ball-and-socket-joint-like constraint with ConeTwistConstraint
          5. Creating detailed control with DOFConstraint
          6. Add sound sources to your scene
        5. Summary
      21. Index