📖 Scenario: You are creating a simple visual effect in a Unity game. You want to write a custom shader that colors an object with a solid color.
🎯 Goal: Build a basic Unity shader that colors an object with a fixed color using shader code.
📋 What You'll Learn
Create a shader with a fixed color property
Write the shader code to output the fixed color
Apply the shader to an object in Unity
Print the final color output in the shader
💡 Why This Matters
🌍 Real World
Custom shaders let game developers create unique visual styles and effects for objects in games and apps.
💼 Career
Understanding shader fundamentals is important for graphics programmers, technical artists, and game developers working with Unity.
Progress0 / 4 steps
1
Create a shader with a fixed color property
Create a shader named SimpleColorShader with a _Color property set to (1, 0, 0, 1) (red). Write the shader declaration and properties block.
Unity
Hint
Start by declaring the shader name and add a Properties block with a color named _Color set to red.
2
Add a fixed color fragment shader
Inside the SubShader, add a Pass with a fragment shader named frag that returns the _Color value. Also add a vertex shader named vert that passes vertex data unchanged.
Unity
Hint
Inside SubShader, add a Pass with CGPROGRAM. Define vert and frag functions. The fragment shader should return the _Color.
3
Add a helper variable for color intensity
Add a float property named _Intensity with default value 1.0 in the Properties block. Then declare float _Intensity; inside the shader code and multiply _Color by _Intensity in the fragment shader before returning.
Unity
Hint
Add a float property _Intensity in Properties. Declare it in the shader code and multiply it with _Color in the fragment shader.
4
Print the final color output
Add a Debug.Log statement in the fragment shader to print the final color value before returning it. Then print the final color output in Unity console.
Unity
Hint
Shaders cannot use Debug.Log directly. Instead, return the final color multiplied by intensity. When applied to an object, the color should appear red with full intensity.
Practice
(1/5)
1. What is the main purpose of a custom shader in Unity?
easy
A. To control exactly how objects look by writing small programs
B. To create 3D models from scratch
C. To write game logic and rules
D. To manage user input events
Solution
Step 1: Understand the role of shaders
Shaders define how surfaces appear by controlling colors, lighting, and textures.
Step 2: Identify what custom shaders do
Custom shaders let you write small programs to control object appearance beyond built-in options.
Final Answer:
To control exactly how objects look by writing small programs -> Option A
Quick Check:
Custom shader = control object look [OK]
Hint: Shaders control object appearance with code [OK]
Common Mistakes:
Confusing shaders with 3D modeling
Thinking shaders handle game logic
Assuming shaders manage input
2. Which of the following is the correct way to start a simple Unity shader code block?
easy
A. ShaderBegin
B. StartShader
C. CGPROGRAM
D. ProgramCG
Solution
Step 1: Recall Unity shader syntax
Unity shaders use CGPROGRAM to start the programmable shader code block.
Step 2: Check options for correct keyword
Only CGPROGRAM is the valid keyword to begin the shader code section.
Final Answer:
CGPROGRAM -> Option C
Quick Check:
Shader code block starts with CGPROGRAM [OK]
Hint: Look for CGPROGRAM to start shader code [OK]
Common Mistakes:
Using made-up keywords like StartShader
Confusing CGPROGRAM with other terms
Misspelling the keyword
3. What will be the output color of this simple shader fragment code?
A. Missing semicolon after _Color property declaration
B. _Color cannot be used directly in fragment shader
C. frag function missing input parameters
D. Incorrect use of ENDCG keyword
Solution
Step 1: Check Properties block syntax
The property declaration is correct: no semicolon needed, parentheses balanced.
Step 2: Verify shader code block
Properties like _Color must be declared inside CGPROGRAM as 'uniform fixed4 _Color;' to be used; direct use causes undeclared identifier error.
Final Answer:
_Color cannot be used directly in fragment shader -> Option B
Quick Check:
Properties need uniform declaration [OK]
Hint: Declare Properties as uniforms in CGPROGRAM [OK]
Common Mistakes:
Assuming semicolons are needed in Properties
Not declaring uniform for _Color
Thinking ENDCG is incorrect
5. You want to write a custom shader that colors an object red only if its x-coordinate is positive, otherwise blue. Which fragment shader code correctly implements this?
hard
A. fixed4 frag() : SV_Target {
if (POSITION.x > 0) return fixed4(1,0,0,1);
else return fixed4(0,0,1,1);
}
B. fixed4 frag(float3 pos) : SV_Target {
if (pos.x > 0) return fixed4(1,0,0,1);
else return fixed4(0,0,1,1);
}