Bird
Raised Fist0
Unityframework~20 mins

Custom shader fundamentals in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Shader Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output color of this simple shader?

Consider this Unity shader code snippet that sets the output color. What color will the shader output on the object?

Unity
Shader "Custom/SimpleColor" {
  SubShader {
    Pass {
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag

      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 fixed4(0.0, 1.0, 0.0, 1.0); // green color
      }
      ENDCG
    }
  }
}
AThe object will appear solid red.
BThe object will appear solid green.
CThe object will appear solid blue.
DThe object will be fully transparent.
Attempts:
2 left
💡 Hint

Look at the color values in the fragment shader's return statement. The format is (red, green, blue, alpha).

🧠 Conceptual
intermediate
1:30remaining
Which shader stage transforms vertex positions to clip space?

In a Unity shader, which stage is responsible for converting vertex positions from object space to clip space?

ACompute shader
BFragment shader
CGeometry shader
DVertex shader
Attempts:
2 left
💡 Hint

Think about which shader stage handles the shape and position of the object before pixels are drawn.

🔧 Debug
advanced
2:00remaining
Why does this shader produce a black screen?

Examine the following fragment shader code. Why does the rendered object appear completely black?

Unity
fixed4 frag(v2f i) : SV_Target {
  fixed4 color = fixed4(1.0, 0.5, 0.0, 0.0);
  return color;
}
AThe fragment shader is missing a return statement.
BThe RGB values are invalid and cause the shader to fail.
CAlpha is zero, so the output is fully transparent and appears black.
DThe color is orange, so the object should appear orange, not black.
Attempts:
2 left
💡 Hint

Consider if the alpha value affects the rendering. Is there a Blend mode specified in the Pass?

📝 Syntax
advanced
1:30remaining
Which option correctly declares a float4 color variable in HLSL?

Choose the correct syntax to declare a float4 variable named color with red=1, green=0, blue=0, alpha=1 in a Unity shader.

Afloat4 color = float4(1, 0, 0, 1);
Bfloat4 color = (1, 0, 0, 1);
Cfloat4 color = {1, 0, 0, 1};
Dfloat4 color = [1, 0, 0, 1];
Attempts:
2 left
💡 Hint

Remember the constructor syntax for vector types in HLSL.

🚀 Application
expert
2:30remaining
What is the output of this shader code with conditional color?

Given this fragment shader code, what color will be output when i.pos.x is 0.3?

Unity
fixed4 frag(v2f i) : SV_Target {
  fixed4 color = (i.pos.x > 0.5) ? fixed4(0, 0, 1, 1) : fixed4(1, 1, 0, 1);
  return color;
}
AYellow color (1,1,0,1)
BBlue color (0,0,1,1)
CTransparent color (0,0,0,0)
DBlack color (0,0,0,1)
Attempts:
2 left
💡 Hint

Check the condition and compare the value of i.pos.x to 0.5.

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

  1. Step 1: Understand the role of shaders

    Shaders define how surfaces appear by controlling colors, lighting, and textures.
  2. Step 2: Identify what custom shaders do

    Custom shaders let you write small programs to control object appearance beyond built-in options.
  3. Final Answer:

    To control exactly how objects look by writing small programs -> Option A
  4. 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

  1. Step 1: Recall Unity shader syntax

    Unity shaders use CGPROGRAM to start the programmable shader code block.
  2. Step 2: Check options for correct keyword

    Only CGPROGRAM is the valid keyword to begin the shader code section.
  3. Final Answer:

    CGPROGRAM -> Option C
  4. 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?
fixed4 frag() : SV_Target {
    return fixed4(1, 0, 0, 1);
}
medium
A. Solid red color
B. Solid green color
C. Solid blue color
D. Transparent black color

Solution

  1. Step 1: Analyze the returned color vector

    The function returns fixed4(1, 0, 0, 1), which means red=1, green=0, blue=0, alpha=1.
  2. Step 2: Interpret color components

    Red is full intensity, green and blue are zero, alpha is fully opaque.
  3. Final Answer:

    Solid red color -> Option A
  4. Quick Check:

    fixed4(1,0,0,1) = red [OK]
Hint: RGBA (1,0,0,1) means solid red [OK]
Common Mistakes:
  • Mixing up color channel order
  • Ignoring alpha channel
  • Assuming zero means white
4. Identify the error in this shader snippet:
Shader "Custom/Simple" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader {
        Pass {
            CGPROGRAM
            fixed4 frag() : SV_Target {
                return _Color;
            }
            ENDCG
        }
    }
}
medium
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

  1. Step 1: Check Properties block syntax

    The property declaration is correct: no semicolon needed, parentheses balanced.
  2. 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.
  3. Final Answer:

    _Color cannot be used directly in fragment shader -> Option B
  4. 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); }
C. fixed4 frag(float3 pos : TEXCOORD0) : SV_Target { return pos.x > 0 ? fixed4(1,0,0,1) : fixed4(0,0,1,1); }
D. fixed4 frag(float3 pos : POSITION) : SV_Target { return pos.x > 0 ? fixed4(1,0,0,1) : fixed4(0,0,1,1); }

Solution

  1. Step 1: Understand input semantics

    To access vertex position in fragment shader, use input parameter with semantic POSITION.
  2. Step 2: Check code correctness

    fixed4 frag(float3 pos : POSITION) : SV_Target { return pos.x > 0 ? fixed4(1,0,0,1) : fixed4(0,0,1,1); } correctly declares pos with POSITION semantic and uses conditional operator to return red or blue.
  3. Step 3: Identify errors in other options

    fixed4 frag(float3 pos) : SV_Target { if (pos.x > 0) return fixed4(1,0,0,1); else return fixed4(0,0,1,1); } lacks semantic, fixed4 frag(float3 pos : TEXCOORD0) : SV_Target { return pos.x > 0 ? fixed4(1,0,0,1) : fixed4(0,0,1,1); } uses TEXCOORD0 which is wrong for position, fixed4 frag() : SV_Target { if (POSITION.x > 0) return fixed4(1,0,0,1); else return fixed4(0,0,1,1); } uses POSITION as variable incorrectly.
  4. Final Answer:

    fixed4 frag(float3 pos : POSITION) : SV_Target { return pos.x > 0 ? fixed4(1,0,0,1) : fixed4(0,0,1,1); } -> Option D
  5. Quick Check:

    Use POSITION semantic for position input [OK]
Hint: Use POSITION semantic to get vertex position [OK]
Common Mistakes:
  • Missing POSITION semantic in input
  • Using wrong semantic like TEXCOORD0
  • Referencing POSITION as variable directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard