Texturing & Normal mapping

From Shader Forge Wiki
Jump to: navigation, search

Let's try out some basic texturing and normal-mapping!

Texturing in this case refers to having your shader render a texture on the surface of your model. The texture will adhere to the UV-mapping of the model being used, which you can find details about manipulating on this page: UV Scaling & Offset


This is a basic Texture2D node:


A Texture2D node has a green hue to indicate the fact that it is a Property, meaning you'll be able to modify its settings in a Material using this shader. If you hook the Texture2D node up with for example the Diffuse output of the Main node, you can see the Texture2D node appear under the properties tab to the left:


You are recommended to rename the property into something that will make sense for someone using the shader, and in this case it would be wise to follow Unity's convention and call this property MainTexture.

With this basic setup you have created a basic Blinn-Phong-shaded Diffuse shader, rejoice! In order to see the effect in the preview-window to the upper-left, you can click on the Select button inside the Texture2D node to get a selection-dialog displaying all the textures in your project. Pick one you like, and click on the Compile shader button to the upper left (which should be yellow, indicating a minor change was made). You should get something similar to the following:


(Screenshot taken with the built-in screenshot feature, click on the tiny camera icon inside the preview window)

Now while this is all fine and dandy, you might want to manipulate the output further somehow, like for example tint the texture by a color! (See also Color Mathematics)

What happens when displaying a texture like this, is that the color values are read from the pixels on the texture, a process called Texture Sampling, giving you a color output which you can modify just like any single color (as seen in Color Mathematics examples). So to make the texture somewhat red, you could multiply it by a Vector 3 with the values 1, 0.5, 0.5:


As you might know from Color Mathematics, this will result in the 3 color values Red, Green and Blue of every pixel in the texture to be multiplied with the values from the Vector 3 node, which gives us the green and blue only displaying at half their previous capacity, leaving mostly red left. In this case it results in a mostly red brick texture. In order to be able to alter the color you tint the texture by in your material, you're even better off using a Color node, which just like the Texture2D node is a Property, so it will show up in the Material inspector outside of ShaderForge.


To make things look more current-gen, you'll also probably want to have your lighting react to a normal-map. A normal-map is a relatively cheap way to give the surface of a model more definition than it actually has. The basic setup and effect looks like this:


Although it might not be easy to tell from the screenshot, the sphere now shows more detail in the shading as the light falls from grazing angles, revealing cracks and crevices more clearly! If the texture used for normal-map is marked as Normal Map in the import settings for that texture, ShaderForge will automatically tick the box that says "Normal map", making certain calculations in the background necessary to read it properly for this type of use.

Manipulating normal-maps is more complicated than diffuse-textures as it's important that the data issued for shading is mathematically correct for it to render in a way you would expect, which is why that topic is saved for later. Happy shading!