Unity Mobile Shader Generator
Generate performance-optimized shaders for Unity mobile platforms.
Quick Start
- •Determine render pipeline: URP or Built-in
- •Choose shader type based on use case
- •Apply mobile optimization rules
- •Include appropriate fallbacks
Pipeline Selection
| Pipeline | When to Use |
|---|---|
| URP | New projects, better batching, modern features |
| Built-in | Legacy projects, specific built-in features needed |
Shader Type Templates
Unlit (Fastest)
- •UI elements, particles, skyboxes
- •No lighting calculations
- •See references/urp-patterns.md or references/builtin-patterns.md
Toon/Cel Shading
- •Stylized games, anime aesthetics
- •Banded lighting, outline support
- •See references/urp-patterns.md or references/builtin-patterns.md
PBR-Lite
- •Realistic look with mobile constraints
- •Simplified metallic/smoothness workflow
- •See references/urp-patterns.md or references/builtin-patterns.md
Vertex Lit
- •Large environments, terrain
- •Per-vertex lighting for performance
- •See references/urp-patterns.md
Core Optimization Rules
MUST follow these rules for ALL mobile shaders:
Precision
hlsl
// Use lowest precision possible half4 color; // Prefer half (16-bit) for colors fixed4 mask; // Use fixed (11-bit) for 0-1 values (Built-in only) float2 uv; // float only for UVs and positions
Texture Sampling
- •Maximum 4 texture samples per pass
- •Use texture atlases to reduce samples
- •Combine masks into single RGBA texture (R=metallic, G=AO, A=smoothness)
- •Avoid dependent texture reads
Math Operations
hlsl
// AVOID // PREFER pow(x, 5.0) x * x * x * x * x sin(), cos() Lookup textures or approximations normalize() in fragment Normalize in vertex, pass as varying length() dot(v, v) when comparing distances
Branching
hlsl
// AVOID dynamic branches
if (condition) { ... }
// PREFER
result = lerp(valueA, valueB, step(threshold, value));
Varying Count
- •Maximum 8 interpolators for broad compatibility
- •Pack data:
float4 uvAndFoginstead of separatefloat2 uv; float2 fog;
Alpha & Transparency
hlsl
// AVOID alpha testing (clip/discard) - breaks early-Z clip(alpha - 0.5); // PREFER alpha blending or opaque Blend SrcAlpha OneMinusSrcAlpha
Detailed References
- •Full optimization rules: references/optimization-rules.md
- •URP shader patterns: references/urp-patterns.md
- •Built-in shader patterns: references/builtin-patterns.md
Output Checklist
Before delivering shader, verify:
- • Correct precision qualifiers used
- • Texture samples ≤ 4
- • No unnecessary branches
- • Varyings ≤ 8
- • Fallback shader specified
- • LOD levels if complex
- • GPU Instancing support if applicable
- • SRP Batcher compatible (URP)