0
0
Unityframework~10 mins

Custom shader fundamentals in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom shader fundamentals
Start Shader
Define Properties
Write SubShader
Set Pass
Write Vertex Shader
Write Fragment Shader
Compile & Apply
Render Object with Shader
This flow shows the steps to create a custom shader: define properties, write subshader with vertex and fragment shaders, compile, and apply to render.
Execution Sample
Unity
Shader "Custom/SimpleColor" {
  Properties {
    _Color ("Color", Color) = (1,0,0,1)
  }
  SubShader {
    Pass {
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag
      fixed4 _Color;
      struct appdata { float4 vertex : POSITION; };
      struct v2f { float4 pos : SV_POSITION; };
      v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); return o; }
      fixed4 frag(v2f i) : SV_Target { return _Color; }
      ENDCG
    }
  }
}
This shader colors an object with a solid color defined by _Color property.
Execution Table
StepActionCode PartResult/State
1Start shader definitionShader "Custom/SimpleColor" {Shader named Custom/SimpleColor created
2Define color propertyProperties { _Color ("Color", Color) = (1,0,0,1) }_Color set to red (1,0,0,1)
3Begin SubShaderSubShader {SubShader block started
4Begin PassPass {Pass block started
5Start CGPROGRAMCGPROGRAMBegin shader program
6Set vertex and fragment shaders#pragma vertex vert #pragma fragment fragVertex shader 'vert' and fragment shader 'frag' set
7Declare _Color variablefixed4 _Color;_Color variable available in shader
8Define input structstruct appdata { float4 vertex : POSITION; };Input vertex data defined
9Define output structstruct v2f { float4 pos : SV_POSITION; };Output data for vertex shader defined
10Vertex shader codev2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); return o; }Vertex positions transformed to clip space
11Fragment shader codefixed4 frag(v2f i) : SV_Target { return _Color; }Fragment outputs solid color _Color
12End CGPROGRAMENDCGShader program ended
13End Pass}Pass block ended
14End SubShader}SubShader block ended
15End Shader}Shader definition complete
16Apply shader to objectMaterial.shader = Custom/SimpleColor;Object rendered with solid red color
💡 Shader compiled and applied; rendering uses _Color property for solid color output.
Variable Tracker
VariableStartAfter Step 2After Step 7After Step 11Final
_Colorundefined(1,0,0,1) red(1,0,0,1) red(1,0,0,1) red(1,0,0,1) red
vertexundefinedundefinedinput vertex positiontransformed clip positiontransformed clip position
posundefinedundefinedundefinedclip space positionclip space position
Key Moments - 3 Insights
Why do we define _Color in Properties and also declare it in the shader code?
Properties define what the user can change in the editor (step 2), while declaring _Color in code (step 7) makes it available inside the shader program.
What does UnityObjectToClipPos do in the vertex shader?
It transforms the object's vertex position from model space to clip space for rendering (step 10), which is necessary for the GPU to place the vertex on screen.
Why does the fragment shader just return _Color?
Because this shader colors every pixel with the same solid color defined by _Color (step 11), no lighting or texture is applied.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 2. What is the initial value of _Color?
A(1,0,0,1) red
B(0,0,0,1) black
C(1,1,1,1) white
Dundefined
💡 Hint
Check the 'Result/State' column at step 2 in the execution_table.
At which step does the vertex position get transformed to clip space?
AStep 7
BStep 10
CStep 11
DStep 15
💡 Hint
Look for the vertex shader code action in the execution_table.
If you change _Color in Properties to blue (0,0,1,1), what changes in variable_tracker?
Avertex values change to blue
B_Color remains (1,0,0,1) because code is fixed
C_Color values change to (0,0,1,1) after step 2 and remain same
Dpos values change to blue
💡 Hint
Properties set initial _Color value; see variable_tracker _Color row.
Concept Snapshot
Custom Shader Basics in Unity:
- Define Properties for user inputs
- Write SubShader with Pass
- Use CGPROGRAM for vertex and fragment shaders
- Vertex shader transforms vertices
- Fragment shader sets pixel color
- Compile and apply to objects
Full Transcript
This visual execution shows how to create a simple custom shader in Unity. We start by defining a shader and its color property. Then we write a SubShader with a Pass that includes vertex and fragment shaders in CGPROGRAM. The vertex shader transforms object vertices to clip space for rendering. The fragment shader outputs a solid color from the _Color property. We track how _Color and vertex positions change step-by-step. Key points include the difference between Properties and shader variables, the role of UnityObjectToClipPos, and how the fragment shader colors pixels. The shader compiles and applies to render objects with the chosen color.