Version: Unity 6 (6000.0)
Language : English
Writing shaders in code
Shader object fundamentals

Introduction to writing shaders in code

How you write custom shadersA program that runs on the GPU. More info
See in Glossary
in Unity depends on the render pipelineA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary
you use:

Languages

When you write shaders for Unity, you use the following languages:

  • A programming language called HLSL. Use this to write the shader programs themselves. For more information on HLSL, see HLSL in Unity.
  • A Unity-specific language called ShaderLab. Use this to define a Shader objectAn instance of the Shader class, a Shader object is container for shader programs and GPU instructions, and information that tells Unity how to use them. Use them with materials to determine the appearance of your scene. More info
    See in Glossary
    , which acts as a container for your shader programs. For more information on ShaderLab, see ShaderLabUnity’s language for defining the structure of Shader objects. More info
    See in Glossary
    .

You do not need to use different languages for different platforms; Unity compiles your HLSL and ShaderLab code into different languages for different graphics APIs. For more information, see Shader compilation.

Note: You can also directly write your shader programs in GLSL and Metal if you want. This is not recommended or needed as part of a normal workflow. For more information on using GLSL, see GLSL in Unity.

ShaderLab

ShaderLab is a declarative language that you use in shader source files. It uses a nested-braces syntax to describe a Shader object.

There are many things that you can define in ShaderLab, but the most common are:

Different ways of writing shaders

There are different ways of writing shaders:

  • The most common way is to write vertex and fragment shaders in HLSL. For more information, see Writing vertex and fragment shaders.
  • In the Built-in Render Pipeline, you can also write Surface Shaders. They are a simplified way of writing shaders that interact with lighting. For more information, see Surface ShadersA streamlined way of writing shaders for the Built-in Render Pipeline. More info
    See in Glossary
    .
  • For backwards compatibility reasons, Unity also supports “fixed function style” ShaderLab commands. These let you write shaders in ShaderLab, without using HLSL. This is no longer recommended, but it is documented on the page ShaderLab legacy functionality.

Writing shaders for different graphics APIs

In some cases, you must write your shader code differently depending on the graphics API that you are targeting. For information on this, see Writing shaders for different graphics APIs.

Writing shaders in code
Shader object fundamentals