Learn Three.js - Third Edition

Book description

Create and animate stunning 3D browser based graphics with Three.js JavaScript library

Key Features

  • Enhance your 3D graphics with light sources, shadows, advanced materials, and textures
  • Load models from external sources, and visualize and animate them directly from JavaScript
  • Create your own custom WebGL shader and explore the postprocessing feature of Three.js

Book Description

WebGL makes it possible to create 3D graphics in the browser without having to use plugins such as Flash and Java. Programming WebGL, however, is difficult and complex. With Three.js, it is possible to create stunning 3D graphics in an intuitive manner using JavaScript, without having to learn WebGL. With this book, you'll learn how to create and animate beautiful looking 3D scenes directly in your browser-utilizing the full potential of WebGL and modern browsers. It starts with the basic concepts and building blocks used in Three.js. From there on, it will expand on these subjects using extensive examples and code samples. You will learn to create, or load, from externally created models, realistic looking 3D objects using materials and textures. You'll find out how to easily control the camera using the Three.js built-in in camera controls, which will enable you to fly or walk around the 3D scene you created. You will then use the HTML5 video and canvas elements as a material for your 3D objects and to animate your models. Finally, you will learn to use morph and skeleton-based animation, and even how to add physics, such as gravity and collision detection, to your scene. After reading this book, you'll know everything that is required to create 3D animated graphics using Three.js.

What you will learn

  • Work with the different types of materials in Three.js and see how they interact with your 3D objects and the rest of the environment
  • Implement the different camera controls provided by Three.js to effortlessly navigate around your 3D scene
  • Work with vertices directly to create snow, rain, and galaxy-like effects
  • Import and animate models from external formats, such as OBJ, STL, and COLLADA
  • Create and run animations using morph targets and bones animations
  • Explore advanced textures on materials to create realistic looking 3D objects by using bump maps, normal maps, specular maps, and light maps
  • Interact directly with WebGL by creating custom vertex and fragment shaders

Who this book is for

The ideal target audience for this book would be JavaScript developers who who want to learn how to use the Three.js library

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 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.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learn Three.jsThird Edition
  3. PacktPub.com
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Creating Your First 3D Scene with Three.js
    1. Requirements for using 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/macOS systems
        2. Npm-based web server if you've worked with Node.js
        3. Portable version Mongoose for macOS and/or Windows
        4. Running from the filesystem by 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 the browser size changes
    9. Summary
  7. The Basic Components that Make Up a Three.js Application
    1. Creating a scene
      1. The 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
  8. Working with Light Sources in Three.js
    1. The different kinds of lighting provided by Three.js
    2. Basic lights
      1. THREE.AmbientLight
        1. Using the THREE.Color object
      2. THREE.SpotLight
      3. THREE.PointLight
      4. THREE.DirectionalLight
    3. Special lights
      1. THREE.HemisphereLight
      2. THREE.AreaLight
      3. Lens flare
    4. Summary
  9. 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. Multiple materials for a single mesh
    3. Advanced materials
      1. THREE.MeshLambertMaterial
      2. THREE.MeshPhongMaterial
      3. THREE.MeshStandardMaterial
      4. THREE.MeshPhysicalMaterial
      5. Creating your own shaders with THREE.ShaderMaterial
    4. Materials you can use for a line geometry
      1. THREE.LineBasicMaterial
      2. THREE.LineDashedMaterial
    5. Summary
  10. Learning to Work with Geometries
    1. The basic geometries provided by Three.js
      1. 2D geometries
        1. THREE.PlaneGeometry
        2. THREE.CircleGeometry
      2. THREE.RingGeometry
        1. THREE.ShapeGeometry
      3. 3D geometries
        1. THREE.BoxGeometry
        2. THREE.SphereGeometry
        3. THREE.CylinderGeometry
        4. THREE.ConeGeometry
        5. THREE.TorusGeometry
        6. THREE.TorusKnotGeometry
        7. THREE.PolyhedronGeometry
          1. THREE.IcosahedronGeometry
          2. THREE.TetrahedronGeometry
          3. THREE.OctahedronGeometry
          4. THREE.DodecahedronGeometry
    2. Summary
  11. Advanced Geometries and Binary Operations
    1. THREE.ConvexGeometry
    2. THREE.LatheGeometry
    3. Creating a geometry by extruding
      1. THREE.ExtrudeGeometry
      2. THREE.TubeGeometry
      3. Extruding from SVG
    4. THREE.ParametricGeometry
    5. Creating 3D text
      1. Rendering text
      2. Adding custom fonts
    6. Using binary operations to combine meshes
      1. The subtract function
      2. The intersect function
      3. The union function
    7. Summary
  12. Points and Sprites
    1. Understanding points
    2. THREE.Points and THREE.PointsMaterial
      1. Styling particles with the HTML5 canvas
        1. Using HTML5 canvas with THREE.CanvasRenderer
        2. Using HTML5 canvas with WebGLRenderer
      2. Using textures to style particles
    3. Working with sprite maps
    4. Creating THREE.Points from an advanced geometry
    5. Summary
  13. Creating and Loading Advanced Meshes and Geometries
    1. Geometry grouping and merging
      1. Grouping objects together
      2. Merging multiple meshes into a single mesh
    2. Loading geometries from external resources
      1. Saving and loading in Three.js JSON format
      2. Saving and loading THREE.Mesh
      3. Saving and loading a scene
      4. Working with Blender
        1. Installing the Three.js exporter in Blender
        2. Loading and exporting a model from Blender
    3. Importing from 3D file formats
      1. The OBJ and MTL formats
      2. Loading a COLLADA model
      3. Loading models from other supported formats
      4. Show proteins from PDB
      5. Creating a particle system from a PLY model
    4. Summary
  14. 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. FirstPersonControls
      4. OrbitControl
    3. Morphing and skeletal animation
      1. Animation with morph targets
      2. Animation with a mixer and morph targets
      3. Using multiple THREE.AnimationClip objects
      4. 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
      4. Using the gltfLoader
      5. Visualize motions captured models using the fbxLoader
      6. Loading legacy DirectX models through the xLoader
      7. Visualizing a skeleton with the BVHLoader
      8. Reusing models from the SEA3D project
    5. Summary
  15. 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. Using a displacement map to alter the position of vertices
      5. Adding subtle shadows with an ambient occlusion map
      6. Creating fake shadows using a lightmap
      7. Metalness and roughness maps
      8. Alpha map
      9. Emissive map
      10. Specular map
      11. Creating fake reflections using an environment map
    2. Advanced usage of textures
      1. Custom UV mapping
      2. Repeat wrapping
    3. Rendering to a 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
    5. Summary
  16. Render Postprocessing
    1. Setting up Three.js for postprocessing
      1. Creating THREE.EffectComposer
      2. Configuring THREE.EffectComposer for postprocessing
      3. 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
        5. Additional simple passes
      2. Advanced EffectComposer flows using masks
      3. Advanced pass - Bokeh
      4. Advance pass - ambient occlusion
    3. Using THREE.ShaderPass for custom effects
      1. Simple shaders
      2. Blurring shaders
    4. Creating custom postprocessing shaders
      1. Custom grayscale shader
      2. Creating a custom bit shader
    5. Summary
  17. Adding Physics and Sounds to Your Scene
    1. Creating a basic Three.js scene with physics
    2. Physi.js material properties
    3. Physi.js supported shapes
    4. Using constraints to limit the movement of objects
      1. Using PointConstraint to limit movement between two points
      2. Hinge constraints
      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
    5. Add sound sources to your scene
    6. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learn Three.js - Third Edition
  • Author(s): Jos Dirksen
  • Release date: August 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788833288