TerrainFourLayerDiffuseFast

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
m (Changed tabs to spaces)
Line 23: Line 23:
  
 
Category {
 
Category {
Blend AppSrcAdd AppDstAdd
+
    Blend AppSrcAdd AppDstAdd
Fog { Color [_AddFog] }
+
    Fog { Color [_AddFog] }
+
   
// ------------------------------------------------------------------
+
    // ------------------------------------------------------------------
// ARB fragment program
+
    // ARB fragment program
+
   
SubShader {
+
    SubShader {
// Ambient pass
+
        // Ambient pass
Pass {
+
        Pass {
Name "BASE"
+
            Name "BASE"
Tags {"LightMode" = "PixelOrNone"}
+
            Tags {"LightMode" = "PixelOrNone"}
Color [_PPLAmbient]
+
            Color [_PPLAmbient]
SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
+
            SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
}
+
        }
// Vertex lights
+
        // Vertex lights
Pass {  
+
        Pass {  
Name "BASE"
+
            Name "BASE"
Tags {"LightMode" = "Vertex"}
+
            Tags {"LightMode" = "Vertex"}
Lighting On
+
            Lighting On
Material {
+
            Material {
Diffuse [_Color]
+
                Diffuse [_Color]
Emission [_PPLAmbient]
+
                Emission [_PPLAmbient]
}
+
            }
SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
+
            SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
}
+
        }
// Pixel lights
+
        // Pixel lights
Pass {
+
        Pass {
Name "PPL"
+
            Name "PPL"
Tags { "LightMode" = "Pixel" }
+
            Tags { "LightMode" = "Pixel" }
Material { Diffuse [_Color] }
+
            Material { Diffuse [_Color] }
Lighting On
+
            Lighting On
  
 
CGPROGRAM
 
CGPROGRAM
Line 68: Line 68:
 
uniform sampler3D _LightTexture0 : register(s5);
 
uniform sampler3D _LightTexture0 : register(s5);
 
#ifdef SHADER_API_D3D9
 
#ifdef SHADER_API_D3D9
#define LIGHT_ATTENUATION(a) (tex3D(_LightTexture0, a._LightCoord).r)
+
#define LIGHT_ATTENUATION(a)   (tex3D(_LightTexture0, a._LightCoord).r)
 
#else
 
#else
#define LIGHT_ATTENUATION(a) (tex3D(_LightTexture0, a._LightCoord).w)
+
#define LIGHT_ATTENUATION(a)   (tex3D(_LightTexture0, a._LightCoord).w)
 
#endif
 
#endif
 
#endif
 
#endif
Line 79: Line 79:
 
uniform sampler2D _LightTexture0 : register(s5);
 
uniform sampler2D _LightTexture0 : register(s5);
 
uniform sampler2D _LightTextureB0 : register(s6);
 
uniform sampler2D _LightTextureB0 : register(s6);
#define LIGHT_ATTENUATION(a) (tex2Dproj (_LightTexture0, a._LightCoord.xyz).w * tex2D(_LightTextureB0, a._LightCoord2.xy).w)  
+
#define LIGHT_ATTENUATION(a)   (tex2Dproj (_LightTexture0, a._LightCoord.xyz).w * tex2D(_LightTextureB0, a._LightCoord2.xy).w)  
 
#endif  
 
#endif  
  
Line 85: Line 85:
 
#ifdef DIRECTIONAL
 
#ifdef DIRECTIONAL
 
#define LIGHTING_COORDS  
 
#define LIGHTING_COORDS  
#define LIGHT_ATTENUATION(a) 1.0
+
#define LIGHT_ATTENUATION(a)   1.0
 
#endif
 
#endif
  
Line 91: Line 91:
 
#ifdef POINT_NOATT
 
#ifdef POINT_NOATT
 
#define LIGHTING_COORDS  
 
#define LIGHTING_COORDS  
#define LIGHT_ATTENUATION(a) 1.0
+
#define LIGHT_ATTENUATION(a)   1.0
 
#endif
 
#endif
  
Line 98: Line 98:
 
#define LIGHTING_COORDS float3 _LightCoord : TEXCOORD5;
 
#define LIGHTING_COORDS float3 _LightCoord : TEXCOORD5;
 
uniform samplerCUBE _LightTexture0 : register(s5);
 
uniform samplerCUBE _LightTexture0 : register(s5);
#define LIGHT_ATTENUATION(a) (texCUBE(_LightTexture0, a._LightCoord).w)
+
#define LIGHT_ATTENUATION(a)   (texCUBE(_LightTexture0, a._LightCoord).w)
 
#endif
 
#endif
  
Line 105: Line 105:
 
#define LIGHTING_COORDS float2 _LightCoord : TEXCOORD5;
 
#define LIGHTING_COORDS float2 _LightCoord : TEXCOORD5;
 
uniform sampler2D _LightTexture0 : register(s5);
 
uniform sampler2D _LightTexture0 : register(s5);
#define LIGHT_ATTENUATION(a) (tex2D(_LightTexture0, a._LightCoord).w)
+
#define LIGHT_ATTENUATION(a)   (tex2D(_LightTexture0, a._LightCoord).w)
 
#endif
 
#endif
  
Line 111: Line 111:
 
struct v2f {
 
struct v2f {
 
     float4 uv[5] : TEXCOORD0;
 
     float4 uv[5] : TEXCOORD0;
LIGHTING_COORDS
+
    LIGHTING_COORDS
float4 diff : COLOR0;
+
    float4 diff : COLOR0;
 
};   
 
};   
  
Line 123: Line 123:
 
half4 frag (v2f i) : COLOR
 
half4 frag (v2f i) : COLOR
 
{
 
{
// get the four layer colors
+
    // get the four layer colors
half4 color1 = tex2D( _MainTex, i.uv[0].xy );
+
    half4 color1 = tex2D( _MainTex, i.uv[0].xy );
 
     half4 color2 = tex2D( _MainTex2, i.uv[1].xy );
 
     half4 color2 = tex2D( _MainTex2, i.uv[1].xy );
 
     half4 color3 = tex2D( _MainTex3, i.uv[2].xy );
 
     half4 color3 = tex2D( _MainTex3, i.uv[2].xy );
Line 131: Line 131:
 
     half4 mask = tex2D( _Mask, i.uv[4].xy );
 
     half4 mask = tex2D( _Mask, i.uv[4].xy );
 
      
 
      
half4 c;
+
    half4 c;
 
     // mix the four layers
 
     // 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.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;
+
    c.w = 0;
return c;
+
    return c;
 
}  
 
}  
 
ENDCG
 
ENDCG
  
SetTexture[_MainTex] {}
+
            SetTexture[_MainTex] {}
SetTexture[_MainTex2] {}
+
            SetTexture[_MainTex2] {}
SetTexture[_MainTex3] {}
+
            SetTexture[_MainTex3] {}
SetTexture[_MainTex4] {}
+
            SetTexture[_MainTex4] {}
SetTexture[_Mask] {}
+
            SetTexture[_Mask] {}
SetTexture[_LightTexture0] {}
+
            SetTexture[_LightTexture0] {}
SetTexture[_LightTextureB0] {}
+
            SetTexture[_LightTextureB0] {}
}
+
        }
}
+
    }
  
// ------------------------------------------------------------------
+
    // ------------------------------------------------------------------
// Radeon 7000 / 9000
+
    // Radeon 7000 / 9000
+
   
SubShader {
+
    SubShader {
Material {
+
        Material {
Diffuse [_Color]
+
            Diffuse [_Color]
Emission [_PPLAmbient]
+
            Emission [_PPLAmbient]
}
+
        }
Lighting On
+
        Lighting On
// Ambient pass
+
        // Ambient pass
Pass {
+
        Pass {
Name "BASE"
+
            Name "BASE"
Tags {"LightMode" = "PixelOrNone"}
+
            Tags {"LightMode" = "PixelOrNone"}
Color [_PPLAmbient]
+
            Color [_PPLAmbient]
Lighting Off
+
            Lighting Off
SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
+
            SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
}
+
        }
// Vertex lights
+
        // Vertex lights
Pass {
+
        Pass {
Name "BASE"
+
            Name "BASE"
Tags {"LightMode" = "Vertex"}
+
            Tags {"LightMode" = "Vertex"}
SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
+
            SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
}
+
        }
// Pixel lights with 2 light textures
+
        // Pixel lights with 2 light textures
Pass {
+
        Pass {
Name "PPL"
+
            Name "PPL"
Tags {
+
            Tags {
"LightMode" = "Pixel"
+
                "LightMode" = "Pixel"
"LightTexCount"  = "2"
+
                "LightTexCount"  = "2"
}
+
            }
ColorMask RGB
+
            ColorMask RGB
SetTexture [_LightTexture0] { combine previous * texture alpha, previous }
+
            SetTexture [_LightTexture0]     { combine previous * texture alpha, previous }
SetTexture [_LightTextureB0] { combine previous * texture alpha, previous }
+
            SetTexture [_LightTextureB0]   { combine previous * texture alpha, previous }
SetTexture [_MainTex] {combine previous * texture DOUBLE}
+
            SetTexture [_MainTex] {combine previous * texture DOUBLE}
}
+
        }
// Pixel lights with 1 light texture
+
        // Pixel lights with 1 light texture
Pass {
+
        Pass {
Name "PPL"
+
            Name "PPL"
Tags {
+
            Tags {
"LightMode" = "Pixel"
+
                "LightMode" = "Pixel"
"LightTexCount"  = "1"
+
                "LightTexCount"  = "1"
}
+
            }
ColorMask RGB
+
            ColorMask RGB
SetTexture [_LightTexture0] { combine previous * texture alpha, previous }
+
            SetTexture [_LightTexture0] { combine previous * texture alpha, previous }
SetTexture [_MainTex] { combine previous * texture DOUBLE }
+
            SetTexture [_MainTex] { combine previous * texture DOUBLE }
}
+
        }
// Pixel lights with 0 light textures
+
        // Pixel lights with 0 light textures
Pass {
+
        Pass {
Name "PPL"
+
            Name "PPL"
Tags {
+
            Tags {
"LightMode" = "Pixel"
+
                "LightMode" = "Pixel"
"LightTexCount" = "0"
+
                "LightTexCount" = "0"
}
+
            }
ColorMask RGB
+
            ColorMask RGB
SetTexture[_MainTex] { combine previous * texture DOUBLE }
+
            SetTexture[_MainTex] { combine previous * texture DOUBLE }
}
+
        }
}
+
    }
 
}
 
}
  

Revision as of 09:28, 12 January 2008

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]
           SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
       }
       // 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