Custom shaders let you control how objects look in a game. They help create special effects like shiny surfaces or glowing lights.
0
0
Custom shader fundamentals in Unity
Introduction
You want to make a character's skin look shiny or wet.
You need a glowing effect on a magic item.
You want to create a cartoon or stylized look for your game.
You want to add special lighting that Unity's built-in shaders don't have.
You want to optimize graphics by writing simple shaders for specific effects.
Syntax
Unity
Shader "Custom/ExampleShader" { Properties { _Color ("Color", Color) = (1,1,1,1) } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag struct appdata { float4 vertex : POSITION; }; struct v2f { float4 pos : SV_POSITION; }; fixed4 _Color; v2f vert (appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); return o; } fixed4 frag (v2f i) : SV_Target { return _Color; } ENDCG } } }
The Properties block defines inputs you can change in the editor.
The SubShader contains the actual code that runs on the GPU.
Examples
This shader colors everything red by default.
Unity
Shader "Custom/SimpleColor" { Properties { _Color ("Color", Color) = (1,0,0,1) } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag struct appdata { float4 vertex : POSITION; }; struct v2f { float4 pos : SV_POSITION; }; fixed4 _Color; 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.
Unity
Shader "Custom/TextureShader" { 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 colors any object with a blue color by default. You can change the color in the Unity editor. It uses a simple vertex and fragment program to set the color.
Unity
Shader "Custom/BasicColorShader" { Properties { _Color ("Color", Color) = (0, 0.5, 1, 1) } SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag struct appdata { float4 vertex : POSITION; }; struct v2f { float4 pos : SV_POSITION; }; fixed4 _Color; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); return o; } fixed4 frag(v2f i) : SV_Target { return _Color; } ENDCG } } FallBack "Diffuse" }
OutputSuccess
Important Notes
Always test your shader on simple objects first to see how it looks.
Use the Unity editor's material inspector to tweak shader properties live.
Shaders run on the graphics card, so small mistakes can cause no rendering or errors.
Summary
Custom shaders let you control how objects look by writing small programs.
They use a Properties block for inputs and a SubShader block for GPU code.
Start simple, then add effects like textures or lighting step by step.