CodeBlanks Logo Icon CodeBlanks

Vertex and fragment

Learn the roles of vertex and fragment functions.

The Role of the Vertex Shader

The Vertex Shader is the first programmable stage of the graphics pipeline. It executes once for every vertex of a 3D object, converting vertices from local model space to screen projection space (MVP transformation).

#pragma vertex vert
v2f vert (appdata v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.uv = v.uv;
    return o;
}

Waving flags, wind-bent foliage, or character mesh deformations are produced by altering positions in the vertex shader stage.

Fragment Shader and Pixel Coloring

After vertices are processed, the rasterizer stage breaks the surface into screen pixels (fragments). Then the Fragment Shader runs to compute the final RGBA color for each individual pixel.

#pragma fragment frag
fixed4 frag (v2f i) : SV_Target
{
    fixed4 col = tex2D(_MainTex, i.uv);
    return col;
}

Texture sampling, bloom effects, fog, and color grading are computed in the fragment shader stage and projected onto the display.

Practice Vertex and fragment pipeline

Define a Start method. Inside, declare int vertexSayisi = 4; and print "Kose sayisi: " + vertexSayisi to the console.

Starter code

Player.cs
using UnityEngine;

public class PipelineLogger : MonoBehaviour
{
    // Start icinde vertexSayisi tanimla ve yazdir
}

Sample solution

Player.cs
using UnityEngine;

public class PipelineLogger : MonoBehaviour
{
    void Start()
    {
        int vertexSayisi = 4;
        Debug.Log("Kose sayisi: " + vertexSayisi);
    }
}

Expected output

Kose sayisi: 4

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which shader stage is responsible for transforming vertex points of a 3D object from model space to screen projection space?

  • Fragment Shader
  • Vertex Shader Correct answer
  • Audio Mixer
  • Collider2D

Explanation: Position transformation of vertex points and geometric mesh deformations are executed in the Vertex Shader stage.

2 Fragment directive

Question
#pragma ____ frag
Answer
#pragma fragment frag

Explanation: The '#pragma fragment' directive is used to register the fragment shader function with the compiler.

3 Pipeline concepts

Term Meaning
Vertex Shader Transforms vertex positions
Rasterizer Discretizes geometry into fragments
Fragment Shader Computes pixel colors
SV_Target Semantic for final color output

All lessons in Advanced