TerrainFourLayerDiffuseFast

From Unify Community Wiki
Revision as of 05:49, 14 January 2008 by Eric5h5 (Talk | contribs)

Jump to: navigation, search

Author: Eric Haines (Eric5h5) - Based on Aras's shader and the built-in Diffuse Fast shader; any mistakes are mine...though it does work....

Description

This shader is a combination of the TerrainFourLayer shader and the built-in Diffuse Fast shader, so it can use fog and light cookies. According to the graphics emulation, it needs an R9500 class card or newer to work.

Usage

Place this shader somewhere in your Assets folder hierarchy, create a material which uses it and apply it to the relevant objects.

ShaderLab - FourLayerDiffuseFast.shader

<shaderlab>Shader "Terrain/Four Layer Diffuse Fast" { Properties {

   _Color ("Main Color", Color) = (1,1,1,1)
   _MainTex ("Color 1 (RGB)", 2D) = "white" {}
   _MainTex2 ("Color 2 (RGB)", 2D) = "white" {}
   _MainTex3 ("Color 3 (RGB)", 2D) = "white" {}
   _MainTex4 ("Color 4 (RGB)", 2D) = "white" {}
   _Mask ("Mixing Mask (RGBA)", 2D) = "gray" {}

}

// Calculates lighting per vertex, but applies // light attenuation maps or spot cookies per pixel. // Quite fine for tesselated geometry.

Category {

   Blend AppSrcAdd AppDstAdd
   Fog { Color [_AddFog] }
   
   // ------------------------------------------------------------------
   // ARB fragment program
   
   SubShader {
       // Ambient pass
       Pass {
           Name "BASE"
           Tags {"LightMode" = "PixelOrNone"}
           Color [_PPLAmbient]
       }
       // Vertex lights
       Pass { 
           Name "BASE"
           Tags {"LightMode" = "Vertex"}
           Lighting On
           Material {
               Diffuse [_Color]
               Emission [_PPLAmbient]
           }
           SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
       }
       // Pixel lights
       Pass {
           Name "PPL"
           Tags { "LightMode" = "Pixel" }
           Material { Diffuse [_Color] }
           Lighting On

CGPROGRAM // TODO: need vertex-lighting emulation here!

  1. pragma fragment frag
  2. pragma multi_compile_builtin_noshadows
  3. pragma fragmentoption ARB_fog_exp2
  4. pragma fragmentoption ARB_precision_hint_fastest
  1. include "UnityCG.cginc"
  1. ifdef POINT
  2. define LIGHTING_COORDS float3 _LightCoord : TEXCOORD5;

uniform sampler3D _LightTexture0 : register(s5);

  1. ifdef SHADER_API_D3D9
  2. define LIGHT_ATTENUATION(a) (tex3D(_LightTexture0, a._LightCoord).r)
  3. else
  4. define LIGHT_ATTENUATION(a) (tex3D(_LightTexture0, a._LightCoord).w)
  5. endif
  6. endif


  1. ifdef SPOT
  2. define LIGHTING_COORDS float4 _LightCoord : TEXCOORD5; float4 _LightCoord2 : TEXCOORD6;

uniform sampler2D _LightTexture0 : register(s5); uniform sampler2D _LightTextureB0 : register(s6);

  1. define LIGHT_ATTENUATION(a) (tex2Dproj (_LightTexture0, a._LightCoord.xyz).w * tex2D(_LightTextureB0, a._LightCoord2.xy).w)
  2. endif


  1. ifdef DIRECTIONAL
  2. define LIGHTING_COORDS
  3. define LIGHT_ATTENUATION(a) 1.0
  4. endif


  1. ifdef POINT_NOATT
  2. define LIGHTING_COORDS
  3. define LIGHT_ATTENUATION(a) 1.0
  4. endif


  1. ifdef POINT_COOKIE
  2. define LIGHTING_COORDS float3 _LightCoord : TEXCOORD5;

uniform samplerCUBE _LightTexture0 : register(s5);

  1. define LIGHT_ATTENUATION(a) (texCUBE(_LightTexture0, a._LightCoord).w)
  2. endif


  1. ifdef DIRECTIONAL_COOKIE
  2. define LIGHTING_COORDS float2 _LightCoord : TEXCOORD5;

uniform sampler2D _LightTexture0 : register(s5);

  1. define LIGHT_ATTENUATION(a) (tex2D(_LightTexture0, a._LightCoord).w)
  2. endif


struct v2f {

   float4 uv[5] : TEXCOORD0;
   LIGHTING_COORDS
   float4 diff : COLOR0;

};

uniform sampler2D _MainTex : register(s0); uniform sampler2D _MainTex2 : register(s1); uniform sampler2D _MainTex3 : register(s2); uniform sampler2D _MainTex4 : register(s3); uniform sampler2D _Mask : register(s4);

half4 frag (v2f i) : COLOR {

   // get the four layer colors
   half4 color1 = tex2D( _MainTex, i.uv[0].xy );
   half4 color2 = tex2D( _MainTex2, i.uv[1].xy );
   half4 color3 = tex2D( _MainTex3, i.uv[2].xy );
   half4 color4 = tex2D( _MainTex4, i.uv[3].xy );
   // get the mixing mask texture
   half4 mask = tex2D( _Mask, i.uv[4].xy );
   
   half4 c;
   // mix the four layers
   c.xyz = (color1.xyz * mask.r + color2.xyz * mask.g + color3.xyz * mask.b + color4.xyz * mask.a) * i.diff.xyz * (LIGHT_ATTENUATION(i) * 2);
   c.w = 0;
   return c;

} ENDCG

           SetTexture[_MainTex] {}
           SetTexture[_MainTex2] {}
           SetTexture[_MainTex3] {}
           SetTexture[_MainTex4] {}
           SetTexture[_Mask] {}
           SetTexture[_LightTexture0] {}
           SetTexture[_LightTextureB0] {}
       }
   }
   // ------------------------------------------------------------------
   // Radeon 7000 / 9000
   
   SubShader {
       Material {
           Diffuse [_Color]
           Emission [_PPLAmbient]
       }
       Lighting On
       // Ambient pass
       Pass {
           Name "BASE"
           Tags {"LightMode" = "PixelOrNone"}
           Color [_PPLAmbient]
           Lighting Off
           SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
       }
       // Vertex lights
       Pass {
           Name "BASE"
           Tags {"LightMode" = "Vertex"}
           SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
       }
       // Pixel lights with 2 light textures
       Pass {
           Name "PPL"
           Tags {
               "LightMode" = "Pixel"
               "LightTexCount"  = "2"
           }
           ColorMask RGB
           SetTexture [_LightTexture0]     { combine previous * texture alpha, previous }
           SetTexture [_LightTextureB0]    { combine previous * texture alpha, previous }
           SetTexture [_MainTex] {combine previous * texture DOUBLE}
       }
       // Pixel lights with 1 light texture
       Pass {
           Name "PPL"
           Tags {
               "LightMode" = "Pixel"
               "LightTexCount"  = "1"
           }
           ColorMask RGB
           SetTexture [_LightTexture0] { combine previous * texture alpha, previous }
           SetTexture [_MainTex] { combine previous * texture DOUBLE }
       }
       // Pixel lights with 0 light textures
       Pass {
           Name "PPL"
           Tags {
               "LightMode" = "Pixel"
               "LightTexCount" = "0"
           }
           ColorMask RGB
           SetTexture[_MainTex] { combine previous * texture DOUBLE }
       }
   }

}

Fallback "VertexLit", 2

}</shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox