Unity Rendering Principle (11) Unity Basic Textures (gradual change textures and mask textures)
Continuing with the previous blog, let’s discuss basic textures. This time we will discuss gradual change textures and mask textures
Gradual change of texture
In the beginning, we used textures in rendering to define the color of an object, but later it was discovered that textures can actually be used to store any surface property, and one common usage is to use gradual change textures to control diffuse lighting results. When calculating diffuse illumination before, we used the dot product of the surface normal and illumination direction to multiply the reflectivity of the material to get the diffuse illumination of the surface, but sometimes we want to be able to control the illumination results more flexibly, At this time, we can use a gradual change texture to control.
1 | Shader "Unlit/RampTexture" |
In the above code, we use the half-Lambert illumination model to calculate the half-Lambert of the half-Lambert part by doing a 0.5x scaling on the dot product of normal and illumination directions and an offset of 0.5 size. In this way, the range of halfLambert we get is mapped between [0,1]. We then use halfLambert to construct a texture coordinate and use this texture coordinate to sample the _RampTex.
Since _RampTex is actually a one-dimensional texture (it doesn’t change color on the vertical axis), we use halfLambert for both the u and v directions of the texture coordinates. Then multiply the color sampled from the gradual change texture with the material color _Color to get the final diffuse emission color.
The rest of the code is to calculate the specular reflection and ambient light, and add their results.
It should be noted that we need to set the Wrap Mode of the gradual change texture to Clamp to prevent problems caused by floating point precision when sampling the texture.
If you use Repeat mode, there will be some black spots in the highlight area. This is due to floating-point precision. When we use fixed2 (halfLambert, halfLambert) to sample the gradual change texture, although in theory halfLambert will be between [0,1], it may have a value like 1.00000001. If we use Repeat mode, then the integer part will be discarded, only the fractional part will be kept, and the sampled coordinate will be 0.00000001
Mask texture
The mask texture is very useful, and it can be seen in many commercial games, so what is the mask?
Simply put, masking allows us to protect certain areas from certain modifications. For example, in the previous implementation, we applied highlight reflection to all parts of the model surface, that is, all pixels use the same size highlight intensity and highlight index. But sometimes, we want the reflected light on some areas of the model surface to be stronger and some areas to be weaker.
To get a more detailed effect, we can use a mask texture to control the lighting. Another common application is to mix multiple images when making terrain materials, such as grass textures, gravel textures, bare earth textures, etc. Using mask textures can control how these textures are mixed.
The general process of using mask textures is to obtain the texel value of the mask texture by sampling, and then use one (or several) of the channel values to multiply with a certain surface property, so that when the channel value is 0, the surface can be protected from this property.
All in all, using mask textures allows artists to control various properties of the model surface with more precision (pixel level).
1 | Shader "Unlit/MaskTexture" |
The code here is exactly the same as the code used when sampling normal textures in tangent space before, except that when calculating the specular reflection, the mask texture is sampled first. Since the rgb component of each texel of the mask texture we are using is actually exactly the same, we just use the r channel to calculate the mask value, but waste other space. In actual game production, we tend to make full use of each color channel in the mask texture to store different surface properties.