How it works...

When the Standard Shader is used from the Inspector of a material, the process behind texture mapping is completely transparent to developers. If we want to understand how it works, it's necessary to take a closer look at TexturedShader. From the Properties section, we can see that the Albedo (RGB) texture is actually referred to in the code as _MainTex:

_MainTex ("Albedo (RGB)", 2D) = "white" {} 

In the CGPROGRAM section, this texture is defined as sampler2D, the standard type for 2D textures:

sampler2D _MainTex; 

The following line shows a struct called Input. This is the input parameter for the surface function and contains a packed array called uv_MainTex:

struct Input { 
  float2 uv_MainTex; 
}; 

Every time the surf() function ...

Get Unity 2018 Shaders and Effects Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.