Reader small image

You're reading from  Learning Three.js - the JavaScript 3D Library for WebGL

Product typeBook
Published inMar 2015
Reading LevelIntermediate
Publisher
ISBN-139781784392215
Edition1st Edition
Languages
Right arrow
Author (1)
Jos Dirksen
Jos Dirksen
author image
Jos Dirksen

Jos Dirksen has worked as a software developer and architect for almost two decades. He has a lot of experience in many technologies, ranging from backend technologies, such as Java and Scala, to frontend development using HTML5, CSS, JavaScript, and Typescript. Besides working with these technologies, Jos regularly speaks at conferences and likes to write about new and interesting technologies on his blog. He also likes to experiment with new technologies and see how they can best be used to create beautiful data visualizations. Previously, Jos has worked in many different roles in the private and public sectors, ranging from private companies such as ING, ASML, Malmberg, and Philips to organizations in the public sector, such as the Department of Defense and the Port of Rotterdam.
Read more about Jos Dirksen

Right arrow

Chapter 3. Working with the Different Light Sources Available in Three.js

In the first chapter, you learned about the basics of Three.js, and in the previous chapter, we looked a bit deeper at the most important parts of the scene: the geometries, meshes, and cameras. You might have noticed that we skipped lights in that chapter even though they make up an important part of every Three.js scene. Without lights, we won't see anything rendered. Since Three.js contains a large number of lights, each of which has a specific use, we'll use this whole chapter to explain the various details of the lights and prepare you for the next chapter on material usage.

Note

WebGL itself doesn't have inherent support for lighting. Without Three.js, you would have to write specific WebGL shader programs to simulate these kinds of lights. A good introduction on simulating lighting in WebGL from scratch can be found at https://developer.mozilla.org/en-US/docs/Web/WebGL/Lighting_in_WebGL.

In this chapter, you'll...

Different kinds of lighting provided by Three.js


There are a number of different lights available in Three.js that all have specific behavior and usages. In this chapter, we'll discuss the following set of lights:

Basic lights


We'll start with the most basic of the lights: THREE.AmbientLight.

THREE.AmbientLight

When you create THREE.AmbientLight, the color is applied globally. There isn't a specific direction this light comes from, and THREE.AmbientLight doesn't contribute to any shadows. You would normally not use THREE.AmbientLight as the single source of light in a scene since it colors all the objects in the same color, regardless of shape. You use it together with other lighting sources, such as THREE.SpotLight or THREE.DirectionalLight to soften the shadows or add some additional color to the scene. The easiest way to understand this is by looking at the 01-ambient-light.html example in the chapter-03 folder. With this example, you get a simple user interface that can be used to modify THREE.AmbientLight that is available in this scene. Note that in this scene, we also have THREE.SpotLight, which adds additional lighting and provides shadows.

In the following screenshot, you can see that we used...

Special lights


In this section on special lights, we'll discuss two additional lights provided by Three.js. First, we'll discuss THREE.HemisphereLight, which helps in creating more natural lighting for outdoor scenes, then we'll look at THREE.AreaLight, which emits lights from a large area instead of a single point, and finally, we'll show you how you can add a lens flare effect to your scene.

THREE.HemisphereLight

The first special light we're going to look at is THREE.HemisphereLight. With THREE.HemisphereLight, we can create more natural-looking outdoor lighting. Without this light, we could simulate the outdoors by creating THREE.DirectionalLight, which emulates the sun, and maybe add additional THREE.AmbientLight to provide some general color to the scene. This, however, won't look really natural. When you're outdoors, not all the light comes directly from above: much is diffused by the atmosphere and reflected by the ground and other objects. THREE.HemisphereLight in Three.js is created...

Summary


In this chapter, we covered a lot of information about the different kinds of lights that are available in Three.js. In this chapter, you learned that configuring lights, colors, and shadows is not an exact science. To get to the correct result, you should experiment with the different settings and use a dat.GUI control to fine-tune your configuration. The different lights behave in different manners. A THREE.AmbientLight color is added to each and every color in the scene and is often used to smooth hard colors and shadows. THREE.PointLight emits light in all directions but can't be used to create shadows. THREE.SpotLight is a light that resembles a flashlight. It has a conical shape, can be configured to fade over distance, and is able to cast shadows. We also looked at THREE.DirectionalLight. This light can be compared with a faraway light, such as the sun, whose light rays travel parallel to each other, the intensity of which doesn't decrease the farther away it gets from the...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Three.js - the JavaScript 3D Library for WebGL
Published in: Mar 2015Publisher: ISBN-13: 9781784392215
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jos Dirksen

Jos Dirksen has worked as a software developer and architect for almost two decades. He has a lot of experience in many technologies, ranging from backend technologies, such as Java and Scala, to frontend development using HTML5, CSS, JavaScript, and Typescript. Besides working with these technologies, Jos regularly speaks at conferences and likes to write about new and interesting technologies on his blog. He also likes to experiment with new technologies and see how they can best be used to create beautiful data visualizations. Previously, Jos has worked in many different roles in the private and public sectors, ranging from private companies such as ING, ASML, Malmberg, and Philips to organizations in the public sector, such as the Department of Defense and the Port of Rotterdam.
Read more about Jos Dirksen

Name

Description

THREE.AmbientLight

This is a basic light, the color of which is added to the current color of the objects in the scene.

THREE.PointLight

This is a single point in space from which light spreads in all directions. This light can't be used to create shadows.

THREE.SpotLight

This light source has a cone-like effect like that of a desk lamp, a spot in the ceiling, or a torch. This light can cast shadows.

THREE.DirectionalLight

This is also called infinite light. The light rays from this light can be seen as parallel, like, for instance, the light from the sun. This light can also be used to create shadows.

THREE.HemisphereLight

This is a special light and can be used to create more natural-looking outdoors lighting by simulating a reflective surface...