ch7_Multitexture.html
with your choice of HTML editor.var texture2 = null;
configure
function, add the code to load the second texture. As mentioned earlier, we're using a class to make this process easier, so the new code is as follows:texture2 = new Texture(); texture2.setImage('textures/light.png'),
draw
function, directly below the code that binds the first texture, add the following to expose the new texture to the shader:gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, texture2.tex); gl.uniform1i(Program.uSampler1, 1);
uniform sampler2D uSampler1;
uniformList
in the configure
function.gl_FragColor = texture2D(uSampler, vTextureCoord) * texture2D(uSampler1, vTextureCoord);
ch7_Multitexture_Finished.html
.We've added a second texture to the draw call and blended it with the first to create a new effect, in this case simulating a simple static spotlight.
It's important to realize that the colors sampled from a texture are treated just like any other color in the shader, that is as a generic 4-dimensional vector. As a result, we can combine textures together just like we would combine vertex and light colors, or any other color manipulation.
Multiplication is one of the most common ways to blend colors in a shader, but there's really no limit to how you can combine color values. Try experimenting with some different algorithms in the fragment shader and see what effect it has on the output. What happens when you add values instead of multiply? What if you use the red channel from one texture and the blue and green from the other? Or try out the following algorithm and see what the result is:
gl_FragColor = vec4(texture2D(uSampler2, vTextureCoord).rgb - texture2D(uSampler, vTextureCoord).rgb, 1.0);
18.217.220.114