Consider this Unity shader code snippet that sets the output color. What color will the shader output on the object?
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 } } }
Look at the color values in the fragment shader's return statement. The format is (red, green, blue, alpha).
The fragment shader returns fixed4(0.0, 1.0, 0.0, 1.0), which means red=0, green=1, blue=0, alpha=1 (fully opaque). So the output color is solid green.
In a Unity shader, which stage is responsible for converting vertex positions from object space to clip space?
Think about which shader stage handles the shape and position of the object before pixels are drawn.
The vertex shader processes each vertex and transforms its position from object space to clip space, preparing it for rasterization.
Examine the following fragment shader code. Why does the rendered object appear completely black?
fixed4 frag(v2f i) : SV_Target {
fixed4 color = fixed4(1.0, 0.5, 0.0, 0.0);
return color;
}Consider if the alpha value affects the rendering. Is there a Blend mode specified in the Pass?
The fragment shader returns fixed4(1.0, 0.5, 0.0, 0.0), an orange color with alpha=0. If the shader or material uses blending, the alpha=0 causes full transparency, making the object appear black (or invisible). Without a Blend mode, opaque rendering ignores alpha, but the question implies the black screen is due to transparency.
Choose the correct syntax to declare a float4 variable named color with red=1, green=0, blue=0, alpha=1 in a Unity shader.
Remember the constructor syntax for vector types in HLSL.
In HLSL, vector types like float4 are constructed using the type name followed by parentheses enclosing the values.
Given this fragment shader code, what color will be output when i.pos.x is 0.3?
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;
}Check the condition and compare the value of i.pos.x to 0.5.
Since 0.3 is not greater than 0.5, the condition is false, so the color is fixed4(1,1,0,1), which is yellow.