0
0
Unityframework~5 mins

Why shaders control visual rendering in Unity

Choose your learning style9 modes available
Introduction

Shaders decide how objects look on the screen by telling the computer how to color and light them. They control the final image you see in a game or app.

When you want to change how a character or object looks with special effects like glowing or shadows.
When you need to make surfaces look shiny, rough, or transparent.
When creating realistic lighting and shadows in a 3D scene.
When optimizing graphics to run smoothly on different devices by controlling how much detail is shown.
When adding artistic styles like cartoon or pixel effects to your visuals.
Syntax
Unity
// Example of a simple shader in Unity's ShaderLab
Shader "Custom/SimpleColor" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Color;

            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 _Color;
            }
            ENDCG
        }
    }
}

Shaders in Unity are written using ShaderLab syntax combined with HLSL code inside CGPROGRAM blocks.

The vertex function controls the position of vertices, and the fragment function controls the color of pixels.

Examples
This shader colors the whole object red using the _Color property.
Unity
// Simple color shader
Shader "Custom/SimpleColor" {
    Properties {
        _Color ("Color", Color) = (1,0,0,1)
    }
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Color;

            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 _Color;
            }
            ENDCG
        }
    }
}
This shader applies a texture image to the object instead of a solid color.
Unity
// Shader with texture
Shader "Custom/Textured" {
    Properties {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            sampler2D _MainTex;

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

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

            fixed4 frag (v2f i) : SV_Target {
                return tex2D(_MainTex, i.uv);
            }
            ENDCG
        }
    }
}
Sample Program

This shader combines a texture with a color tint. The final color is the texture color multiplied by the chosen color.

Unity
Shader "Custom/ColorAndTexture" {
    Properties {
        _Color ("Color", Color) = (0,1,0,1)
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Color;
            sampler2D _MainTex;

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

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

            fixed4 frag (v2f i) : SV_Target {
                fixed4 texColor = tex2D(_MainTex, i.uv);
                return texColor * _Color;
            }
            ENDCG
        }
    }
}
OutputSuccess
Important Notes

Shaders run on the graphics card to make rendering fast and efficient.

Changing shaders changes how light and color appear on objects.

Learning shaders helps you create unique visual styles and effects in games.

Summary

Shaders control how objects look by deciding their color and lighting.

They are used to create effects like textures, colors, and lighting in games.

Understanding shaders helps you make your visuals more interesting and realistic.