agentsclimarketplace

Texture packing variant stripping

Skill adevra/unity-shader-agent-skills/skills/texture-packing-variant-stripping

Curated agent skills for Unity shader development targeting mobile GPUs and WebGL browsers.

Install
npx -y skills add adevra/unity-shader-agent-skills --skill texture-packing-variant-stripping

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 8 stars8 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its author says it does

Copied from the file, not written here

Channel packing, variant reduction, shader_feature vs multi_compile, and build size optimization for Unity

SKILL.md

9.2 KB, as published. Nobody here has run it

Texture Packing and Shader Variant Stripping for Unity Mobile

TRIGGER

Use this skill when the user is dealing with texture optimization, channel packing, shader variant bloat, build size, shader compilation time, or shader keyword management in Unity. Triggers include: "channel packing", "texture packing", "mask map", "MODS map", "RMA", "shader variants", "variant stripping", "multi_compile", "shader_feature", "build size", "shader keywords", "Always Included Shaders", "IPreprocessShaders", or complaints about slow shader compilation or large build sizes.


TEXTURE CHANNEL PACKING

Channel packing stores multiple grayscale maps in a single RGBA texture. This reduces both texture sample count and memory bandwidth — the primary bottleneck on mobile.

Standard URP Packing Convention

Mask Map (single RGBA texture):
  R = Metallic
  G = Occlusion (AO)
  B = Detail mask (or custom)
  A = Smoothness

Best Practices

  1. Put the most visually important mask in the Green channel. Human vision is most sensitive to green, and most texture compression formats (ETC2, ASTC, DXT) allocate more bits to green.

  2. Pack related data together. If metallic and smoothness are always sampled together, put them in the same texture.

  3. Use linear color space (not sRGB) for packed mask textures. In Unity, uncheck "sRGB" in the texture import settings for any non-color data (roughness, AO, metallic, height).

  4. One texture sample replaces four. Instead of:

    // BAD — 4 texture samples
    half metallic = SAMPLE_TEXTURE2D(_MetallicMap, sampler_MetallicMap, uv).r;
    half ao = SAMPLE_TEXTURE2D(_AOMap, sampler_AOMap, uv).r;
    half detail = SAMPLE_TEXTURE2D(_DetailMask, sampler_DetailMask, uv).r;
    half smooth = SAMPLE_TEXTURE2D(_SmoothnessMap, sampler_SmoothnessMap, uv).r;
    

    Use:

    // GOOD — 1 texture sample
    half4 masks = SAMPLE_TEXTURE2D(_MaskMap, sampler_MaskMap, uv);
    half metallic = masks.r;
    half ao = masks.g;
    half detail = masks.b;
    half smooth = masks.a;
    

Advanced: Terrain Packing

Unity's Shader Graph 17.4 terrain samples demonstrate advanced packing that reduces 12 texture samples to 5 for a 4-layer terrain:

  • CNS packing: Color (RGB) + Normal (RG → reconstruct B) + Smoothness in a single set
  • CHNOSArray: Texture arrays packing Color, Height, Normal, Occlusion, Smoothness

HLSL Channel Packing Helper

// Pack 4 grayscale values into RGBA
half4 PackChannels(half metallic, half ao, half detail, half smoothness)
{
    return half4(metallic, ao, detail, smoothness);
}

// Reconstruct normal Z from XY (for normal map packing)
half3 UnpackNormalRG(half2 normalRG)
{
    half3 n;
    n.xy = normalRG * 2.0h - 1.0h;
    n.z = sqrt(1.0h - saturate(dot(n.xy, n.xy)));
    return n;
}

SHADER VARIANT MANAGEMENT

The Variant Explosion Problem

Each multi_compile keyword doubles variant count multiplicatively:

#pragma multi_compile _ _A        → 2 variants
#pragma multi_compile _ _A _B     → 3 variants
#pragma multi_compile _ _A
#pragma multi_compile _ _B        → 2 × 2 = 4 variants
#pragma multi_compile _ _A
#pragma multi_compile _ _B
#pragma multi_compile _ _C        → 2 × 2 × 2 = 8 variants

With URP's built-in keywords (shadows, fog, additional lights, etc.), a single shader can generate thousands of variants.

Impact

  • Build time: 20+ minutes for complex shaders
  • Build size: 100MB+ shader data (critical for WebGL)
  • Runtime memory: 20-40% extra draw calls from variant switching
  • WebGL: Shader compilation blocks the main thread — more variants = longer freeze

shader_feature vs multi_compile

shader_featuremulti_compile
CompilesOnly variants used by materials in buildALL combinations
Use forMaterial-specific togglesGlobal state (fog, shadows)
Unused variantsStripped automaticallyIncluded regardless
Local versionshader_feature_local (64 max)multi_compile_local (64 max)
Global versionshader_feature (256 max)multi_compile (256 max)

Rule: Use shader_feature for everything you control (material toggles, feature flags). Only use multi_compile for things Unity controls globally (fog, lightmaps, instancing).

Recommended URP Keyword Stripping

Strip these keywords if you're not using the features:

// In an IPreprocessShaders implementation:
static readonly ShaderKeyword[] STRIP_KEYWORDS = new[]
{
    new ShaderKeyword("_ADDITIONAL_LIGHTS"),          // If using main light only
    new ShaderKeyword("_ADDITIONAL_LIGHT_SHADOWS"),   // If no additional light shadows
    new ShaderKeyword("_SHADOWS_SOFT"),               // If using hard shadows only
    new ShaderKeyword("_MIXED_LIGHTING_SUBTRACTIVE"), // If not using mixed lighting
    new ShaderKeyword("_SCREEN_SPACE_OCCLUSION"),     // If not using SSAO
    new ShaderKeyword("LIGHTMAP_SHADOW_MIXING"),      // If not mixing lightmaps + realtime
    new ShaderKeyword("_LIGHT_LAYERS"),               // If not using light layers
    new ShaderKeyword("_REFLECTION_PROBE_BLENDING"),  // If not blending reflection probes
    new ShaderKeyword("_REFLECTION_PROBE_BOX_PROJECTION"), // If not using box projection
};

URP Asset Feature Stripping

In URP Asset settings, disable unused features to prevent their variants from being compiled:

  • Additional Lights → Off (if only using directional)
  • Shadows → Reduce cascade count, disable soft shadows
  • Post-Processing → Disable if not used
  • HDR → Disable if not needed
  • Environment Reflections → Disable if using baked reflections

Platform-Conditional Compilation

// Include features only on platforms that can handle them
#if defined(SHADER_API_MOBILE) || defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)
    // Mobile/WebGL: simplified path
    #define MOBILE_SHADER 1
#else
    // Desktop: full features
    #define MOBILE_SHADER 0
#endif

half4 frag(Varyings i) : SV_Target
{
    half4 color = SampleAlbedo(i.uv);
    
    #if !MOBILE_SHADER
        // Desktop-only: normal mapping, specular, etc.
        color.rgb += SampleNormalAndLight(i);
    #endif
    
    return color;
}

TEXTURE COMPRESSION FOR MOBILE/WEB

FormatPlatformBits/PixelQualityNotes
ASTC 4×4Android (ES 3.0+), iOS8BestPreferred for all mobile
ASTC 6×6Android (ES 3.0+), iOS3.56GoodUse for less important textures
ASTC 8×8Android (ES 3.0+), iOS2AcceptableBackground/distant textures
ETC2Android (ES 3.0+), WebGL 2.04-8GoodWebGL 2.0 default
ETC1Android (ES 2.0)4OKNo alpha channel support
PVRTCiOS (older)2-4VariableLegacy, prefer ASTC

For channel-packed textures: Use ASTC 4×4 or ETC2 with linear color space. Avoid PVRTC for mask maps — its block compression causes visible artifacts on sharp mask boundaries.


OPTIMIZATION CHECKLIST

  • All grayscale maps packed into RGBA channels of shared textures
  • Packed textures imported as Linear (not sRGB)
  • Green channel used for most important mask data
  • shader_feature used instead of multi_compile for material toggles
  • Local keywords used where possible (shader_feature_local)
  • Unused URP features disabled in URP Asset
  • Always Included Shaders list cleaned (Project Settings > Graphics)
  • IPreprocessShaders stripping script added for unused keywords
  • Build log checked for variant count (look for "Compiled N shader variants")
  • ASTC or ETC2 compression used for all mobile textures
  • Standard Shader removed from project (use Simple Lit or custom)

SOURCES

Keep looking

Skills are one crate of 328,083. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.