0
0
Unityframework~20 mins

Custom shader fundamentals in Unity - Practice Problems & Coding Challenges

Choose your learning style9 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.