Three js alpha maps
When working with materials in threejs many of the materials support one or more types of maps for skinning a geometry. One such texture map option is a alpha map that is a gray scale texture that will be used to define the alpha transparency values for the material. So then the white areas of the texture will result in a face being fully opaque, black areas will result in the face being fully transparent, and values between the two will be used to set any alpha value between the two.
Alpha maps will then come into play when it comes to working things out with transparency in a threejs project. An alpha map is something more to add after working out the very basics of this with the transparency and opacity properties of a material. With that said the transparency option is used to turn transparency off and on, opacity is used to set an over all global alpha value, and alpha maps can the be used to set opacity values on a texture level.
Alpha maps and what to know before hand
You might want to brush up on textures
There are many other ways to control visibility in threejs.
Alpha maps are a way to go about adjusting opacity of a material with a texture rather than setting the opacity of the material over all. When it comes to just setting the opacity of a material over all there is the transparent and opacity properties of a material. Also there are a number of features when it comes to just turning the visibility of an object on and off completely also. There is the visible Boolean of the object3d base class of Mesh objects, and there is also getting into learning a thing or two about layers.
You might want to read up more on what the options are with materials
It is a good idea to really look into what the options are when it comes to materials. I often go with the basic material if I am not going to do anything involving light, but when I do get into using light I have found that I like the standard material, and now also the Phong material as well. However you should not just use that as a way to make a decision when it comes to materials there are many other options with materials that also support the alpha mo feature along with many other features.
Source code is up on Github
The source code examples in this post are up on Github in my test threejs repo. This is also where I park the source code for my many other blog posts on threejs.
Version Numbers matter with three.js
When I first wrote this post I was using threejs version r104, and the last time I edited this post I was using r146. Threejs is still being developed and is moving pretty fast, in the future there might come another time where this code might break. So if things are not working out for you with this example, and many other examples on the open Internet the first thing you should check is the version of threejs that you are using.
1 - Alpha map example in three js
So for a basic example of an alpha map in threejs I have this example that makes used of a texture that is created from a canvas element. To do so I just create a canvas element, get a refernce to the 2d drawing context, and then draw gray scale areas in the canvas. When drawing to the canvas any area that I draw as black will end up being totally transparent, and any area that is white will be fully opaque, shads of gray then set values between the two extremes.
I then used the THREE.CanvasTexture constructor to create a texture that I can then use with the alpha map property of a material that supports alpha maps such as the Mesh basic Material.
The transparent property of the material also needs to be set to true, and a renderer that supports transparency also needs to be used. The usual WebGl renderer worked just fine for me in this example, but other renderer options may not support this feature. Still that is the basic idea to create a texture that is in gray scale and then just add that texture to the alpha map property of the material to which I want to add an alpha map for.
2 - Animation loop example of an alpha map
So now that I have a basic static example out of the way maybe the next step is to do something involving an animation loop using request animation frame. When I made the first video for this post I took the basic example and reworked what I was doing to update the canvas texture. So then I made another example based off of that that I now have for this new section on alpha maps and using canvas textures for such maps.
When it comes to updating a canvas texture all I need to do is just update the state of the canvas element that was used for the texture object, and then just make sure that the needs update property of the texture is set to true.
There are a whole bunch of different maps to be aware of when it comes to skinning faces of a geometry with a material in threejs. There is the plain old color map that can also be used, and with materials that respond to light sources there are many other maps of interest as well such as an emissive map that will be a color that will always show up regardless of the lighting situation. Still alpha maps are one of the many types of maps to be aware of, and they can be fun to play around with when working out details with the materials that are going to be used with objects in a scene.