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

Introduction to the shader programming language

3.2.9. Cg / HLSL Include

This post is also available in…

The directive “.cginc” (Cg include) contains several files that can be used in our shader to bring in predefined variables and auxiliary functions. 

If we look at our USB_simple_color shader, we will find the following directives that have been declared within the pass:

  • UNITY_FOG_COORDS(texcoordindex).
  • UnityObjectToClipPos(inputVertex).
  • TRANSFORM_TEX(tex, name).
  • UNITY_TRANSFER_FOG(outputStruct, clipSpacePos).
  • UNITY_APPLY_FOG(inputCoords, colorOutput).

All these functions belong to “UnityCG.cginc”. If we remove this directive our shader will not be able to compile because it will not have its reference.

Another defined function that we can find in UnityCG.cginc is UNITY_PI which equals 3.14159265359f. The latter is not included in our default shader because it is used only in specific cases (e.g. when calculating a triangle or sphere). In case we want to review the variables and functions that come in our .cginc files we can follow the following path:

Windows → {unity install path}/Data/CGIncludes/UnityCG.cginc
Mac → /Applications/Unity/Unity.app/Contents/CGIncludes/UnityCG.cginc

In addition to the Unity default directives, we can create our directives using the extension “.cginc”, to do this we must simply create a new document, save it with that extension and then start defining our variables and functions. Later in this book, we will start using some custom directives to work with lighting, shadows, and volumes.

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.