You are previewing Learning Three.js: The JavaScript 3D Library for WebGL.
O'Reilly logo
Learning Three.js: The JavaScript 3D Library for WebGL

Book Description

Three.js makes creating 3D computer graphics on a web browser a piece of proverbial cake, and this practical tutorial makes it easier still. All you need to know is basic JavaScript and HTML.

  • Create and animate beautiful 3D graphics directly in the browser using JavaScript without the need to learn WebGL

  • Learn how to enhance your 3D graphics with light sources, shadows, and advanced materials and textures

  • Each subject is explained using extensive examples that you can directly use and adapt for your own purposes

  • In Detail

    Three.js is a JavaScript 3D library that offers a wide range of features for creating and displaying stunning 3D computer graphics on a web browser in an intuitive manner using JavaScript without having to deal with the complexity of a WebGL low-level API. Even though WebGL makes it possible to create 3D graphics in the browser without having to use plugins, programming WebGL, however, is hard and complex. This book shows you how Three.js allows you to be independent of browser plugins.

    If you are an experienced web designer who wants to set the tone for an immersive design environment in your applications then this book is for you.

    "Learning Three.js: The JavaScript 3D Library for WebGL" is a practical, example-rich book that will help you to master all the features of Three.js. With this book, you’ll learn how to create and animate gorgeous looking 3D scenes directly in your browser utilizing the full potential of WebGL and modern browsers without having to learn WebGL.

    "Learning Three.js: The JavaScript 3D Library for WebGL" starts by going over 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. This will allow you to learn everything you need to know about Three.js in an easy and interactive manner.

    Besides the basic concepts, this book will show you how you can create realistic looking 3D objects using materials and textures as well as how to load them from externally created models. You’ll learn how to easily control the camera using the Three.js build-in camera controls so you can fly or walk around the 3D scene you have created. You will also learn how to use morph and bones-based animation and how to add physics to your scene.

    After reading Learning Three.js: The JavaScript 3D Library for WebGL and playing around with the extensive set of examples, you’ll know everything that is required to create 3D animating graphics using Three.js that run in any browser.

    Table of Contents

    1. Learning Three.js: The JavaScript 3D Library for WebGL
      1. Table of Contents
      2. Learning Three.js: The JavaScript 3D Library for WebGL
      3. Credits
      4. About the Author
      5. Acknowledgement
      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 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 approach should work on most Unix/Mac systems
            2. NPM-based approach if you've got Node.js installed
            3. Portable version of Mongoose for Mac/Windows
        3. Creating an HTML skeleton page
        4. Rendering and viewing a 3D object
        5. Adding materials, lights, and shadows
        6. Expanding your first scene with animations
          1. Introducing the requestAnimationFrame() method
          2. Animating the cube
          3. Bouncing the ball
        7. Using the dat.GUI library to make experimenting easier
        8. Using the ASCII effect
        9. Summary
      10. 2. Working with the Basic Components That Make Up a Three.js Scene
        1. Creating a scene
          1. Basic functionality of the scene
          2. Adding the fog effect to the scene
          3. Using the overrideMaterial property
        2. Working with the Geometry and Mesh objects
          1. The properties and functions of a geometry
          2. The functions and attributes for a mesh
        3. Using the available cameras for different uses
          1. The orthographic camera versus the perspective camera
          2. Focusing the camera on a specific point
        4. Summary
      11. 3. Working with the Different Light Sources Available in Three.js
        1. Exploring the lights provided by Three.js
        2. Learning about the basic lights
          1. AmbientLight – a globally applied light source
            1. Using the THREE.Color() object
          2. PointLight – the light that shines in all directions
          3. SpotLight – the light with a cone effect
          4. DirectionalLight – for a far away sun-like light source
          5. Using special lights for advanced lighting
            1. HemisphereLight
            2. AreaLight
            3. LensFlare
        3. Summary
      12. 4. Working with the Three.js Materials
        1. Understanding the common material properties
          1. Basic properties
          2. Blending properties
          3. Advanced properties
        2. Starting with the simple Mesh materials (basic, depth, and face)
          1. The MeshBasicMaterial for simple surfaces
          2. The MeshDepthMaterial for depth-based coloring
          3. Combining the materials
          4. The MeshNormalMaterial for normal-based colors
          5. The MeshFaceMaterial for assigning a material to each face
        3. Learning about the advanced materials
          1. The MeshLambertMaterial for dull, non-shiny surfaces
          2. The MeshPhongMaterial for shiny objects
          3. Creating your own shaders with the ShaderMaterial
        4. Using the materials for a line geometry
          1. The LineBasicMaterial
          2. The LineDashedMaterial
        5. Summary
      13. 5. Learning to Work with Geometries
        1. The basic geometries provided by Three.js
          1. Two-dimensional geometries
            1. PlaneGeometry
            2. CircleGeometry
            3. ShapeGeometry
          2. Three-dimensional geometries
            1. CubeGeometry
            2. SphereGeometry
            3. CylinderGeometry
            4. TorusGeometry
            5. TorusKnotGeometry
            6. PolyhedronGeometry
              1. IcosahedronGeometry
              2. TetrahedronGeometry
              3. Octahedron
        2. Summary
      14. 6. Using Advanced Geometries and Binary Operations
        1. ConvexGeometry
        2. LatheGeometry
        3. Create a geometry by extruding
          1. ExtrudeGeometry
          2. TubeGeometry
          3. Extrude from SVG
          4. ParametricGeometry
        4. Creating 3D text
          1. Rendering text
          2. Adding custom fonts
        5. Using binary operations to combine meshes
          1. The subtract function
          2. The intersect function
          3. The union function
        6. Summary
      15. 7. Particles and the Particle System
        1. Understanding particles
        2. Particles, the particle system, and the BasicParticleMaterial
        3. Styling particles with the HTML5 canvas
          1. Using HTML5 canvas with the CanvasRenderer class
          2. Using HTML5 canvas with the WebGLRenderer class
        4. Using textures to style particles
          1. Working with sprites
        5. Creating a particle system from an advanced geometry
        6. 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 a geometry
            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. OBJ and MTL format
            2. Loading a collada model
            3. Loading STL, CTM, and VTK models
            4. Showing proteins from the protein databank
            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
          6. PathControl
        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 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 mesh
          2. Using a bump map to create wrinkles
          3. Using 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 canvas as a texture
            2. Using canvas as a bump map
            3. Using the output from a video as a texture
        3. Summary
      19. 11. Custom Shaders and Render Post Processing
        1. Setting up the post processing
          1. Creating the EffectComposer object
            1. Configuring the EffectComposer object for post processing
            2. Updating the render loop
        2. Post processing passes
          1. Simple post processing passes
            1. Using the FilmPass to create a TV-like effect
            2. Adding a bloom effect to the scene with the BloomPass
            3. Outputting the scene as a set of dots with the DotScreenPass
            4. Showing the output of multiple renderers on the same screen
          2. Advanced EffectComposer flows by using masks
          3. Using the ShaderPass for custom effects
            1. Simple shaders
            2. Blurring shaders
            3. Advanced shaders
        3. Creating custom post processing shaders
          1. Custom grayscale shader
          2. Creating a custom bit shader
        4. Summary
      20. 12. Adding Physics to Your Scene with Physijs
        1. Creating a basic Three.js scene ready for Physijs
        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 a HingeConstraint
          3. Limiting movement to a single axis with a SliderConstraint
          4. Creating a ball joint-like constraint with the ConeTwist Constraint
          5. Creating detailed control with the DOFConstraint
        5. Summary
      21. Index