Creating an aesthetically pleasing and efficient toon shader for Unity's Universal Render Pipeline (URP) involves leveraging Shader Graph or writing a custom HLSL shader. Below is a custom HLSL shader that implements a cel-shading (toon) effect optimized for URP. This shader includes features like quantized lighting for the toon effect and support for outlines to enhance the visual style.
Toon Shader for URP
Features:
- Cel Shading: Creates distinct color bands based on lighting intensity.
- Outline Support: Adds a black outline to objects for a cartoon-like appearance.
- Efficient Performance: Optimized using URP's includes and macros for better performance.
Shader Code:
Create a new shader file in your Unity project (e.g., ToonShaderURP/HLSL_ToonShader.shader) and replace its content with the following code:
Shader "Custom/URP/ToonShader"
{
Properties
{
_BaseColor ("Base Color", Color) = (1,1,1,1)
_MainTex ("Base Texture", 2D) = "white" {}
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_OutlineThickness ("Outline Thickness", Float) = 0.05
_Ramp ("Ramp Texture", 2D) = "gray" {} // Gradient texture for toon shading
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry" }
LOD 200
Pass
{
Name "ToonPass"
Tags { "LightMode"="UniversalForward" }
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float3 normalWS : NORMAL;
float2 uv : TEXCOORD0;
float3 viewDir : TEXCOORD1;
};
CBINDINGS
{
float4 _BaseColor;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _Ramp;
}
Varyings vert (Attributes IN)
{
Varyings OUT;
float4 positionWS = TransformObjectToWorld(IN.positionOS);
OUT.positionHCS = TransformWorldToHClip(positionWS);
OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS);
OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
OUT.viewDir = GetViewDirection(positionWS.xyz);
return OUT;
}
half4 frag (Varyings IN) : SV_Target
{
// Sample base texture
half4 baseColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv) * _BaseColor;
// Lighting
half3 normal = normalize(IN.normalWS);
half3 lightDir = normalize(_MainLightDirection.xyz);
half ndotl = max(dot(normal, lightDir), 0.0);
// Quantize the lighting to create toon effect
float quantized = floor(ndotl * 4.0) / 4.0; // 4 levels
half4 ramp = SAMPLE_TEXTURE2D(_Ramp, sampler_Ramp, float2(quantized, 0.0));
// Apply lighting
half4 finalColor = baseColor * ramp;
return finalColor;
}
ENDHLSL
}
// Outline Pass
Pass
{
Name "OutlinePass"
Tags { "LightMode"="UniversalForward" }
Cull Front
ZWrite Off
ZTest LessEqual
Blend SrcAlpha OneMinusSrcAlpha
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
};
CBINDINGS
{
float4 _OutlineColor;
float _OutlineThickness;
}
Varyings vert (Attributes IN)
{
Varyings OUT;
float3 normal = normalize(TransformObjectToWorldNormal(IN.normalOS));
float3 offset = normal * _OutlineThickness;
float4 positionWS = TransformObjectToWorld(IN.positionOS);
positionWS.xyz += offset;
OUT.positionHCS = TransformWorldToHClip(positionWS);
return OUT;
}
half4 frag (Varyings IN) : SV_Target
{
return half4(_OutlineColor.rgb, 1.0);
}
ENDHLSL
}
}
FallBack "Diffuse"
}
Explanation:
-
Properties:
_BaseColor: Base color of the material.
_MainTex: Main texture applied to the object.
_OutlineColor: Color of the outline.
_OutlineThickness: Thickness of the outline.
_Ramp: Gradient texture to define the quantized lighting levels for the toon effect.
-
SubShader:
- ToonPass:
- Vertex Shader (
vert): Transforms object space positions and normals to world space and calculates view direction.
- Fragment Shader (
frag): Applies the toon shading by quantizing the dot product between the surface normal and light direction using a ramp texture.
- OutlinePass:
- Adds an outline by expanding the vertices along their normals. It culls front faces to render the outline behind the object and applies the specified outline color.
-
Lighting:
- The shader uses the main directional light. The lighting is quantized into discrete steps (e.g., 4 levels) to achieve the cel-shading effect. The
_Ramp texture defines how these levels are mapped to colors.
-
Performance Considerations:
- The shader leverages URP's built-in shader libraries for core functionalities and lighting, ensuring compatibility and optimized performance.
- Outline rendering is done in a separate pass with front-face culling and minimal operations to keep it efficient.
Setting Up the Ramp Texture
To achieve effective toon shading, you need a ramp texture (_Ramp) that defines the shading levels. Here's how to set it up:
-
Create a Ramp Texture:
- Create a 256x1 grayscale texture in an image editing tool.
- Divide the texture into equal segments based on the number of quantization levels (e.g., 4 levels).
- Assign different brightness values to each segment to represent different shading intensities.
-
Import the Texture into Unity:
- Ensure the texture type is set to
Sprite (2D and UI) or Default.
- Disable sRGB if not needed.
-
Assign the Ramp Texture:
- In the material using the Toon Shader, assign the ramp texture to the
_Ramp property.
Usage Instructions
-
Create a Material:
- Right-click in the Project window and select Create > Material.
- Assign the newly created
Custom/URP/ToonShader to this material.
-
Assign Textures and Colors:
- Assign your desired base texture to
_MainTex.
- Set the
_BaseColor to your preferred color tint.
- Assign the ramp texture to
_Ramp.
- Choose an outline color and adjust the outline thickness as needed.
-
Apply the Material:
- Assign the material to the desired 3D object in your scene.
References
This shader provides a solid foundation for a toon-style aesthetic in URP. You can further customize it by adding features like multiple light sources, adjustable shading levels, or different outline styles to fit your project's needs.