I have been toying around with three.js these days, and may continue doing so until I have a solid collection of posts on it. You can count on at least a few posts on Materials which I have been working with today. If you are just getting started with three.js you might be familiar with at least the Basic Material, and that you use a Material with a Geometry to make a Mesh. However you might now be interested in working with lights, and having a material that will respond to a light source, if so the Lambert Material may be of interest.
In this post I will be writing about the Lambert basic material in three.js, and a basic example of it’s use.
I assume you have at least a basic working knowledge of three.js, and are now interested in learning more about the material options used in three.js, if not I have a getting started post on three.js.
There are materials in three.js that do not respond to a light source, and then there are materials that do respond to lights, the Lamber Material is one of several options that do respond to a light source.
From what I have gathered this is one of the faster solutions for having a reflective material that will respond to a light source. If you are interested in knowing why, you might choose to read more about Lambertian reflectance and Gouraud shading.
First off the Lambert material needs a light source. If you use the material without any light source shining on it, and you have a black background, you may end up staring at a black screen. So before we get into the material, lets just take a moment to touch base a lights just for a moment.
There are many lights to work with in three.js, but in this demo I will be using a spot light. This is a directional light that casts out light in the shape of a cone, it can also be used to case shadows. The spot light inherits some things from the base Light class, and the Light class inherits from Object3D which means I can move it around, and work with it just like a camera, or any other object in three.js. However the Object3D.lookAt method will not work for changing the direction of the spot light, to change that you will need to use the target property of the spot light.
I could get into spot lights more, but this post is on the Lambert Material, so that will have to wait for another day.
For a simple example I put together a scene containing a cube, and plane both of which use the Lambert material. In order to see anything though I also added a spotLight, and positioned it away from the cube, and plane.
This results in reflection in a manner that is expected with light reflecting from areas where the spotlight is striking the surfaces of the cube, and plane.
Unlike materials like the basic material, the Lambert Material does not just have a single color for filling the faces of an polygon. There is a color that is shown when it is effected by a light source, and then there is a color that it is by default regardless if there is any light or not.
To set the color value that is not effected by light you will want to set the emissve property of the Lambert material, and use the color property to set the color that is to be reflected when effected by a light source.
for example say we add the emissive property to the material that I gave in the plain to this:
This will make all the area of the plane that is not effected by the spot light a shade of gray, rather than the default which is black. In addition to being able to set the emissive color, the intensity can also be set from a 0 to one value, it is also possible to define a texture that will modulate with the emmsive color using the emmsiveMap property. To set a texture that will function as the regular color map, you will want to use the plain old map property.
The Lambert Material is a good first choice for having a material that responds to light, but depending on the project it might not be the best. The good point of it is speed, but not so much accuracy, even the Standard material seems to do a better job in that regard.