Unity Shaders2024-01-15Sivabalan

Getting Started with Shader Graph in Unity

A beginner's guide to creating your first visual shader in Unity using Shader Graph.

Shaders are the secret sauce of game rendering. They determine how light interacts with surfaces, creating everything from realistic water to stylized toon effects. In this post, we'll explore Unity Shader Graph, a visual tool that makes shader programming accessible to artists and developers alike.

What is Shader Graph?

Shader Graph enables you to build shaders visually. Instead of writing code in HLSL (High-Level Shading Language), you create and connect nodes in a graph framework. It gives instant feedback on the changes, and it's simple enough for users who are new to shader creation.

Setting Up

To get started, ensure you are using a Scriptable Render Pipeline (SRP) like URP (Universal Render Pipeline) or HDRP (High Definition Render Pipeline).

  1. Right-click in the Project window.
  2. Select Create > Shader Graph > URP > Lit Shader Graph.
  3. Name your shader (e.g., MyFirstShader).

Creating a Simple Fresnel Effect

One of the most common effects in games is the Fresnel effect, which highlights the edges of an object. Here's how to create it:

  1. Fresnel Node: Add a Fresnel Effect node.
  2. Color Node: Add a Color node and pick a nice emissive color (like cyan or neon pink).
  3. Multiply: Connect both the Fresnel output and the Color output to a Multiply node.
  4. Master Stack: Connect the result to the Emission port of the Master Stack.
// Ideally, this is what goes on under the hood!
float3 fresnelColor = pow(1.0 - saturate(dot(normal, viewDir)), power) * emissionColor;

Conclusion

This is just the tip of the iceberg! Shader Graph allows for complex mathematics, texture manipulation, and even vertex displacement without writing a single line of code.

Stay tuned for more tutorials where we will dive deeper into:

  • Vertex Displacement
  • Custom Lighting Models
  • Interactive Water Shaders

Happy Coding!