From Unify Community Wiki
Revision as of 19:16, 19 October 2009 by NCarter (Talk | contribs)

Jump to: navigation, search

This shader shows helpes you to visualize the loss of detail to a texture from any particular compression option, in order to allow you to select the option that best matches your priorities for any given texture.

It does this by subtracting a compressed texture from an uncompressed version of the same texture, taking the absolute value of the difference, magnifying it by a specified amount to help highlight the difference.

If there's no differences, the result would be black. More color means more difference. If magnification is set to 1, subtle differences may be harder to spot, but if magnification is larger, the perceptual importance of differences will tend to be overstated.


The same object, compressed as RGB16, DXT1, PVRTC 2BPP RGB, and PVRTC 4BPP RGB with magnification turned up to 10x.


To use this, find a texture you're interested in. Make a duplicate of it. Change the texture compression settings of one to be either RGB24 or ARGB32 if it has an alpha channel. Change the other to be compressed. Make sure mip-map settings, filtering, and wrap-mode are identical. Apply each image to one of the slots provided by the shader, and apply the material to a plane mesh. Set the view to orthographic projection. Zoom in/out to see how mip-levels fare.

To view changes in the alpha channel, change the scene view from color to alpha.

The slider labeled "White, Red, Green, Blue, Alpha" will let you visualize the error on a per-channel basis, which can be useful since human color discrimination is less precise for some colors (red, blue) than others (green). High-intensity spots in the green channel are thus worse than such spots in the red channel, which is worse than the blue channel.

NOTE: By default, Unity will NOT compress the texture immediately and you'll just see black. In preferences, set "Compress Textures when Importing", and ensure that you do NOT see "(not compressed yet)" on the Format line when inspecting the texture. If you don't want to enable this option, perform a build after setting compression options, to actually compress the textures.

<shaderlab> Shader "Debug/DeltaShader" { Properties { _Magnifier ("Magnifier", Range(1.0,10.0)) = 1.0

       _Mask ("White, Red, Green, Blue, Alpha", Range(0, 4)) = 0

_Uncompressed ("Uncompressed (RGBA)", 2D) = "black" {} _Compressed ("Compressed (RGBA)", 2D) = "black" {} } SubShader { Pass { Blend Off

           #pragma vertex vert
           #pragma fragment frag
           #pragma multi_compile_builtin_noshadows
           #pragma fragmentoption ARB_fog_exp2
           #pragma fragmentoption ARB_precision_hint_fastest
           #include "UnityCG.cginc"
           struct appdata_simple {
               float4 vertex : POSITION;
               float4 texcoord : TEXCOORD0;
           struct v2f {
           	float2	uv;
           uniform float4 _Compressed_ST, _Uncompressed_ST;
           v2f vert (appdata_simple v)
           	v2f o;
           	o.pos = mul( glstate.matrix.mvp, v.vertex );
           	o.uv = TRANSFORM_TEX(v.texcoord,_Compressed);
           	return o;
           uniform sampler2D _Compressed;
           uniform sampler2D _Uncompressed;
           uniform float _Magnifier;
           uniform float _Mask;
           float4 frag (v2f i) : COLOR
           	float4 texcolA = tex2D(_Compressed,i.uv);
           	float4 texcolB = tex2D(_Uncompressed,i.uv);
               half4 dT = abs(texcolA - texcolB) * _Magnifier;
               int tmp = round(_Mask);
               half4 colorMask = half4(
                   ((tmp == 0) || (tmp == 1)) ? 1.0 : 0.0,
                   ((tmp == 0) || (tmp == 2)) ? 1.0 : 0.0,
                   ((tmp == 0) || (tmp == 3)) ? 1.0 : 0.0,
                   ((tmp == 0) || (tmp == 4)) ? 1.0 : 0.0
               dT *= colorMask;
           	return dT;

} } } </shaderlab>

Personal tools