DesaturatedDarks2

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

Jump to: navigation, search

Original Author: Aras Pranckevicius

Updated for V2: Joe Schultz

Description

Desaturated Darks shader

This shader desaturates unlit areas (whereas in traditional shaders unlit areas are black, with this one they are only grayscale). This is a pixel lit shader.

This shader requires a fragment program card to work (GeForce FX and up, Radeon 9500 and up, Intel 9xx and up). On older cards it will fallback to Diffuse.

ShaderLab - DesaturatedDarks.shader

<shaderlab>Shader "Desaturated Darks" { Properties {

   _Color ("Main Color", Color) = (1,1,1,1)
   _MainTex ("Base (RGB)", 2D) = "white" {}
   _Darkening ("Darkening", Range (0,1)) = 0.5

} Category {

   Blend AppSrcAdd AppDstAdd
   Fog { Color [_AddFog] }
   
   SubShader {
       
       // Ambient pass
       Pass {
           Name "BASE"
           Tags {"LightMode" = "PixelOrNone"}

CGPROGRAM

  1. pragma fragment frag
  2. pragma fragmentoption ARB_fog_exp2
  3. pragma fragmentoption ARB_precision_hint_fastest
  1. include "UnityCG.cginc"

uniform sampler2D _MainTex; uniform float _Darkening; uniform float4 _Color;

half4 frag( v2f_vertex_lit i ) : COLOR {

   // texture
   half4 texcol = tex2D( _MainTex, i.uv ) * _Color;
   half unlit = Luminance( texcol.xyz ) * (1.0 - _Darkening);
   texcol.xyz = unlit;
   return texcol;

} ENDCG

           SetTexture [_MainTex] {}
       }
       
       // Vertex lit pass
       Pass {
           Name "BASE"
           Tags {"LightMode" = "Vertex"}
           Material {
               Diffuse [_Color]
               Ambient (0,0,0,0)
           }
           Lighting On

CGPROGRAM

  1. pragma fragment frag
  2. pragma fragmentoption ARB_fog_exp2
  3. pragma fragmentoption ARB_precision_hint_fastest
  1. include "UnityCG.cginc"

uniform sampler2D _MainTex; uniform float _Darkening; uniform float4 _Color;

half4 frag( v2f_vertex_lit i ) : COLOR {

   // texture
   half4 texcol = tex2D( _MainTex, i.uv );

   // intensity of the lighting
   half brightness = saturate(Luminance( i.diff.xyz ) * 2);
   
   // color of completely unlit & desaturated areas
   half unlit = Luminance(
       lerp( texcol.xyz, texcol.xyz * i.diff.xyz, _Darkening ) );
   
   // final color based on lighting intensity
   half4 c;
   c.xyz = lerp( half3(unlit), texcol.xyz * i.diff.xyz * 2.0, brightness );
   
   c.w = texcol.w * i.diff.w;
   return c;

} ENDCG

           SetTexture [_MainTex] {}
       }
       
       // Pixel lit pass
       Pass {
           Name "PPL"
           Tags { "LightMode" = "Pixel" }
           // This lighting is not additive, instead it must
           // blend over existing grayscale into saturated.
           // So we output light's intensity into alpha
           // and blend.
           Blend SrcAlpha OneMinusSrcAlpha
           ColorMask RGB
           

CGPROGRAM

  1. pragma vertex vert
  2. pragma fragment frag
  3. pragma multi_compile_builtin
  4. pragma fragmentoption ARB_fog_exp2
  5. pragma fragmentoption ARB_precision_hint_fastest
  6. include "UnityCG.cginc"
  7. include "AutoLight.cginc"

uniform float4 _MainTex_ST; // for _MainTex uniform sampler2D _MainTex; uniform float _Darkening; uniform float4 _Color;


struct v2f {

   V2F_POS_FOG;
   LIGHTING_COORDS
   float3  uv;
   float3  normal;
   float3  lightDir;

};

v2f vert (appdata_base v) {

   v2f o;
   PositionFog( v.vertex, o.pos, o.fog );
   o.normal = v.normal;

TRANSFORM_TEX(v.vertex, _MainTex);

   o.lightDir = ObjSpaceLightDir( v.vertex );    

TRANSFER_VERTEX_TO_FRAGMENT(o);

   return o;

}

float4 frag (v2f i) : COLOR {

   // texture
   half4 texcol = tex2D( _MainTex, i.uv );
   
   // intensity of the lighting
   half brightness = Luminance( DiffuseLight( i.lightDir, i.normal, _ModelLightColor0, LIGHT_ATTENUATION(i) ).rgb );
   
   half4 c;
   c.rgb = texcol.rgb * _ModelLightColor0.rgb;
   c.a = brightness;
   return c;

} ENDCG

       }
   }

}

Fallback " Diffuse", 1 } </shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox