Get an 20% OFF using the RELEASE code on your book purchase. For a limited time.

Introduction to the shader programming language

2.0.1. What is a shader?

This post is also available in…

Considering what we have seen so far, we will delve into the topic of Unity shaders. A shader is a small program with the extension “.Shader” (e.g. colour.shader) which we can be used to generate interesting effects in our projects. Inside it has mathematical calculations and lists of instructions (commands) that allow colour processing for each pixel within the area covered by an object on our computer screen. 

This program allows us to draw elements (using coordinate systems) based on the properties of a polygonal object. The shaders are executed by the GPU since they have a parallel architecture that consists of thousands of small, efficient cores designed to solve tasks simultaneously, while the CPU has been designed for sequential serial processing. 

Note that Unity has three types of files associated with shaders. Firstly, we have programs with the “.shader” extension that are capable of compiling in the different types of render pipelines. 

Secondly, we have programs with the “.shadergraph” extension that can only compile in either Universal RP or High Definition RP. In addition, we have files with the “.hlsl” extension that allow us to create customized functions; generally used within a node type called Custom Function, found in Shader Graph.

There is also another type of program with the extension “.cginc” which we will review in detail later on. For now, we will limit ourselves to making the following association: “.Cginc” is linked to “.shader” CGPROGRAM, and “.hlsl” is linked to “.Shadergraph” HLSLPROGRAM. Knowing this analogy is fundamental because each extension fulfils a different function and is used in specific contexts.

(Fig. 2.0.1a. Reference icons for shaders in Unity) 

In Unity there are at least four types of structures defined to generate shaders, among which we can find the combination of vertex shader and fragment shader then surface shader for automatic lighting calculation and compute shader for more advanced concepts. Each of these structures has previously described properties and functions that facilitate the compilation process; we can also easily define our operations since the software adds these structures automatically.

Follow us to stay informed about all the latest news, updates, and more.

Join the group to share your experiences with other developers.

Subscribe to our channel and keep learning game dev!

jettelly-logo

Jettelly Team

We are a team of indie developers with more than 9 years of experience in video games. As an independent studio, we have developed Nom Noms in which we published with Hyperbeard in 2019. We are currently developing The Unity Shader Bible.

Follow us on our social networks.