Unity Shaders

Tags: Unity

Learn to program the graphics pipeline in Unity for creating unique visual surfaces for game objects.

Last updated 2022-01-10 | 4.6

- Use Unity Shaderlab to create their own shader effects with CG/HLSL.
- Apply their knowledge of simple mathematics to change the visual surface of virtual objects.
- Work with mesh vertices and textures to develop unique rendering effects.

What you'll learn

Use Unity Shaderlab to create their own shader effects with CG/HLSL.
Apply their knowledge of simple mathematics to change the visual surface of virtual objects.
Work with mesh vertices and textures to develop unique rendering effects.
Demonstrate a knowledge of the graphics pipeline and apply it to problem solving shading issues.

* Requirements

* You should be familiar with the Unity Game Engine.
* You should be able to program in a procedural language such as C#.


Now Updated for Unity 2017 to 2019

This course presents a comprehensive guide to programming with Cg and High Level Shader Language in Unity's Shaderlab, to create your own visual surface effects for colouring and lighting game objects. It covers the mathematics of light and surfaces and steps you through the recreation of some of the most popular shaders. Many computer graphics concepts will be presented to help you understand the graphics pipeline and provide you with an essential toolkit of rendering knowledge, that will have you developing your own transparent, animated and texturised shaders in no time.

In this course, Penny teaches all the invaluable skills you will require to program the computer graphics pipeline in Unity from scratch using her internationally acclaimed teaching style and knowledge from over 25 years working with games and graphics.  But hold on tight as you'll be taken on a journey across the computer graphics realm as it is taught to post-graduate university students. Through detailed descriptions and hands-on workshops examining all you need to know about rendering queues, vector mathematics, graphics buffers, colour theory, 3D meshes, texture mapping, lighting models and much more.

Learn how to program and work with:

  • variables and packed arrays

  • meshes, vertices and UVs

  • the mathematics for working with objects in 3D and 2D spaces

  • a variety of lighting models from creating flat shaded objects to highly reflective shiny surfaces

  • bump maps for adding extra depth and dimension to surface textures

  • special effects such as holographic, scrolling textures and surface deformations

  • the variety of graphics buffers used in the rendering pipeline

  • forward and deferred lighting

  • surface, vertex and fragment shaders written in CG/HLSL

  • volumetric rendering

Contents and Overview

After diving right in and creating your very first shader from scratch, we will begin the lessons by examining how 3D models are structured and how that is used within shader code to colour and display the surface in computer graphics. You'll discover all the properties of a shader and how they can be controlled in code and via values fed in through Unity's Inspector.

Following this, we will examine a variety of lighting models and how lights and surface textures can influence the final look of a render. Included here, an overview of the buffers involved in the rendering queue will be given with practical examples for creating special effects that require more than one draw call. Students will also be exposed to the power of vector mathematics and especially the dot product and its role in creating beautiful effects such as outlines, rim lighting and holograms. In addition, issues surrounding transparency and blending will be discussed along with many practical hands-on workshops in which students can flex their newfound skills to interrogate the code they write for better understanding.

The next section brings together all the skills learned throughout together, to develop some of the more popular complex shaders including animated plasma and animated water with waves.

Finally volumetric shaders are covered in which you will follow along to create special effects such as fake geometry, fog and clouds.

What students are saying about this course:

  • This is the best course I've ever taken. It is perfect for me. I worked for 15 years in feature animation. A large part of my career was spent developing shaders for a proprietary renderer. This course has already helped me to bring the value of my previous experience into Unity.

  • Best shader course I've come across. There are enough and more tutorials on youtube to teach you how to develop your own shaders, but nothing falls into the class of Penny's tutorials. They are clear and to the point. Really happy about this one.

  • Wow! Thank you so much! If there were 10 stars - this course would deserve it. Going from absolutely knowing nothing about Shader writing to have a profund understanding about it.....

  • I wanted to let you know that I just finished your shader course and thought it was fantastic. It was a pleasure to take the course and finally get a grasp on a topic that seemed so foreign to me not so long ago. Your teaching style and personality really worked for me and made learning a breeze!

Who this course is for:

  • Anyone fascinated by the way computer graphics works.
  • Anyone wanting to understand how to write their own shaders.
  • Anyone who loves the visual effects that can be achieved through simple code and mathematics.

Course content

11 sections • 78 lectures

Welcome Preview 02:36

This lecture introduces the course and takes you through the content that will be covered showing demos of the effects that will be covered.

Join the H3D Student Community Preview 01:26

H3D has a bustling online student community.  Here's how to get involved.

FAQs Preview 00:14

Help! Why can't I see my shaders correctly? Preview 00:40

More often than not when you are learning something new with software you will find it difficult to tell when you've done something wrong or the software is not working. Here are a couple of common issues you might come across when you start to develop shaders. Be sure to use these as a go to when debugging your own work.

The Rendering Pipeline Preview 03:20

In this lecture we will take a look at the graphics pipeline and the stages in taking a model from virtual to an image on the screen.

Your First Surface Shader Preview 12:31

There's nothing like seeing your first shader program come to life. There's still much to discuss about the technical and programmatic specifications of shaders, but lets start with a simple example to get the ball rolling.

Challenge Preview 00:16

Crash Course in Vector Mathematics Preview 06:10

Variables and Packed Arrays Preview 14:01

Shader code has a lot in common with regular C# but can sometimes look a little strange especially with respect to data types. In this lecture we will go over these types and discuss how they are used.

The Anatomy of a Mesh and Shader Input Preview 06:01

The structure of a model's mesh is crutial in determiing how a shader will affect its surface. Understanding all the 3D components of a mesh is a fundamental you should have under your belt before attempting to manipulate the look of its surface with shaders.

Shader Properties Preview 15:55

In this lecture we will examine in detail the properties block and investigate how external values and images can be used in shaders.

Properties Challenge 1 Preview 00:11

Properties Challenge 2 Preview 00:11

Properties Challenge 3 Preview 00:10

Properties Challenge 4 Preview 00:21

Lambert & Lighting Preview 02:46

Before you can start writing more complex shaders an understanding of the way surface brightness is calculated based on the lighting model is necessary. In this lecture we will introduce the concept of lighting models and examine the simpliest: Lambert.

Normal Mapping Preview 17:01

In this lecture normal mapping will be introduced. The method is a way to add detail and depth to a model when no geometric detail exists.

Bumped Diffuse Challenge Preview 00:08

This lecture is a challenge on modifying the bumped and diffuse textures.

Visualising Normals Preview 02:57

Sometimes it can be difficult to visualise mathematical concepts without models to support them.  In this article I will provide a tool for examining normals in Unity which will visualise them and how they change as their coordinates do.

Illumination Models Preview 03:59

In this section we will examine three illumination models from the history of computer graphics. Learning how each is applied across the surface of a polygon to give a resulting visual effect will give you a better appreciation for the role of the normal in developing shaders.

Bumped Environments Preview 13:47

When you want to create an effect that involves multiple vectors, such as the world reflection and normals you need to mix these together in the shader so one affects the other before adding a texture. In this lecture you will learn how to effect a world reflection with a normal map.

Bumped Environments Challenge Preview 00:12

Buffers and Queues Preview 07:21

At some point in the rendering pipeline a pixel ends up in a buffer or two. As you create more shaders and shaders containing multiple effects it becomes critical to understand exactly whats going on otherwise it becomes difficult to debug your code. In this lecture we will examing the roles of the frame buffer, z buffer and g buffer and touch on the processes of forward and deferred lighting.

The Dot Product Preview 07:00

Being able to tell where the part of a model is facing with respect to the viewer is critical for the production of numerous shader effects. The dot product function is "need to know" information for any shader writer.


Rim Lighting Preview 10:04

Rim lighting demonstrates perfectly the use of the dot product, though it requires a little mathematical manipulation to get just the right results. In this lecture we will look at introducing other mathematical operations into the code to tweak the dot product.

Logical Cutoffs Preview 11:51

When you want to vary the effect a shader has on different parts of a surface, logical statements are your friend. In this lecture we will explore the use of logical statements embedded in the code to turn parts of an effect on and off.

Challenge Preview 00:10

Lighting Models Preview 05:44

Thus far we've only used Lambert as the lighting model for our shader. To expand the possibilities of what you can do with shaders, we now examine the other lighting models available in Unity.

Blinn-Phong Lighting & Surface Output Preview 08:51

Lambert lighting cannot produce specular highlights. For that you need BlinnPhong. In this lecture we will employ the BlinnPhong lighting model to add glossy effects to our model.

Physically-Based Rendering Preview 11:39

In this lecture we take a look at the Unity Physically-Based Lighting Shaders and use the lighting models to investigate the metallic and specular features.

Challenge 1 Preview 00:18

To the Standard PBR shader created in the last lecture add an emissive slider that makes the model glow in the same areas that are grey and white in the metallic gloss map. See the working example in this video. The solution is attached.

Challenge 2 Preview 00:20

To the Standard Specular PBR shader created in the previous lecture reverse the glossiness effect so the black areas of the map are highly reflective and the grey and white much more diffuse. See the working example in this video. The solution is attached.

Writing Your Own Lighting Model Preview 15:44

In this lecture we will explore the creation of custom lighting models through the development of our own Lambert, BlinnPhong and Toon Ramped lighting from scratch.

Challenge 3 Preview 00:09

Challenge 4 Preview 00:14

The Alpha Channel Preview 08:34

Many effects including particles and vegetation in games rely on textures which use the alpha channel. In this lecture we will explore the creation of shaders that manipulate this channel as well as discover how transparency works withing the rendering pipeline with respect to the Z buffer.

Holograms Preview 09:50

In this lecture we will use the alpha channel with rim lighting to create a holographic effect. Included in the lesson will be pointers on using mulitple passes to clear up z buffer issues arising from making models transparent.

Blending Preview 13:07

Blending provides you with control over the way incoming colours are added to colours already in the frame buffer. Through a series of very simple blend statements you can create a plethora of effects. This lecture shows you how.

Leaves and Culling Preview 05:17

By default in computer graphics the reverse side of polygons are not shown to save on processing. However, in the case of billboards you might want to be able to draw on both sides. This lecture shows you how to turn on the drawing of the backfaces.

Blending Two Images Preview 11:00

Sometimes you might want a shader that takes more than one image and then merges them together onto a single surface. In this lecture you will learn how to do this as well as dynamically turn one on and off at runtime.

Stencil Buffer Preview 10:22

In this lecture we will examine the use of another buffer called the Stencil Buffer. This is useful for masking certain pixels to create a variety of effects including XRay Vision and Drawing Holes in walls.

Advanced Stencils Preview 13:45

The best way to get to understand the stencil buffer is to try it out many different ways. In this lecture we will use the stencil buffer to create a fun optical illusion as well as a dynamic piece of stencil code to let you play with the stencil settings via the Inspector.

The Anatomy of a Vertex/Fragment Shader Preview 07:46

In this lecture we'll examine the structure of a vertex/fragment shader. I'll point out a few of the nuances in the code and then show you how to create your own.

Colouring by Vertices and Fragments Preview 17:40

Both the vertex shader and fragment shader both deal with vertices and they can both produce colours. In this lecture we will examine the differences.

Material Use Preview 11:52

V/F shaders, like surface shaders, are capable of processing materials to colour the surface of objects. In this lecture we will look at how UV values are processed in both the Vert and Frag functions. Towards the end I will show you how to take a screen grab of a scene and use that as a manipulatable material on a plane.

Lighting Preview 05:54

While lighting is best kept to surface shaders that handle them elegantly on your behalf, it is handy to know how they work in V/F shaders. This lecture examines how to add a lambert lighting model to create diffuse light.

Shadows Preview 09:47

There's no point in having light if you can't have shadows. So let's modify the lighting shader in this lecture to examine how shadows are cast and received.

Challenge 1 Preview 00:08

Challenge 2 Preview 00:07

Vertex Extruding Preview 07:10

In this lecture we will examine the use of a vertex shader with a surface shader. The vertex shader will be used to extrude the surface of a mesh for a ballooning effect.

Outlining Preview 11:21

Outline is a cool effect you can combine with toon shading to get a Borderlands rendered look. In this lecture we will examine two ways to achieve the look.

Glass Preview 10:33

In this lecture we will create everyone's favourite glass. But this glass isn't simply a transparent pane. It includes bump mapping, a stained-glass pattern and light refraction.

Waves Preview 10:41

Once you know how, modifying vertices with mathematical functions can create all sorts of interesting effects. In this lecture we will examine the sin function for producing waves on the surface of a plane.

Scrolling Textures Preview 09:09

In this lecture we will examine a simple way to change the UV values of a surface over time to create a texture that rolls over the surface.

Challenge 1 Preview 00:13

Plasma Preview 10:47

In this lecture we will use more sine functions and lots of them, plus a cosine, pow and squareroot to create some old school plasma to warp across the surface of objects.

Challenge 2 Preview 00:06

Ray Marching Preview 19:43

In this lecture we will learn about the basics of ray marching and use it to render a sphere inside a cube with shader code.

More on Ray Marching Preview 19:43

In this lecture we will explore further facets of the ray marching algorithm and also calculate per pixel lighting.

Spherical Fog Preview 18:02

In this lecture we will begin looking at another way to mathematically represent a sphere and depth test in two places.

Rendering Fog Density P1 Preview 16:40

In this lecture we will create the function that calculates the fog density.

Rendering Fog Density P2 Preview 07:03

In this lecture we will use the fog calculating function in the vertex shader to render a ball of volumetric fog.

Volumetric Clouds Value Noise Preview 17:45

In this lecture we will look at the Value Noise algorithm that will be used to create clouds as well as begin setting up the cloud environment.

Programming Value Noise P1 Preview 19:59

In this lecture we will start writing the value noise algorithm.

Programming Value Noise P2 Preview 14:23

In this lecture we begin with an overview of programming predictable random values and write some random functions for the shader.

Programming Value Noise P3 Preview 11:31

In this lecture we complete the writing of the 3d value noise and test out some of its basic functionality.

Integrating Colours and Depth Preview 15:16

In this lecture we will put the final function together that will combine lighting and cloud density into the shader for your first view of simple clouds.

Enhanced Noise Preview 13:18

In this lecture we will look at mixing and matching map functions and multiple marches to create better clouds.

Clouds On Camera P1 Preview 20:02

In this lecture we will begin modifying the clouds shader to work on the camera.

Clouds On Camera P2 Preview 13:52

In this lecture we will complete writing the Clouds.cs script that bypasses the normal Unity drawing to render the clouds on the camera.

Clouds On Camera P3 Preview 19:44

In this lecture we will finished adjusting the existing clouds shader to work when attached to a camera.

Clouds On Camera P4 Preview 12:01

In this lecture the camera direction is corrected for the cloud movement and a challenge involving plasma is presented.

References Preview 00:09

This article presents some extra reading and content on volumetric shaders.

Where to Now? Preview 11:16

This link provides further information on the courses you can look at taking based on your interests and skill level.

Appendix A - CG Function List Preview 04:25

Appendix B - Shader Cheat Sheet Preview 01:33

Appendix C - Credits and Extra Resources Preview 00:21