TerrainFourLayerDiffuseFast

From Unify Community Wiki
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 (but only on ATI cards, not nVidia apparently, or at least not the 7300 or 8600).

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

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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]
            CGPROGRAM
#pragma fragment frag
#pragma multi_compile_builtin_noshadows
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest

struct v2f {
    float4 uv[5] : TEXCOORD0;
    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 * 2;
    c.w = 0;
    return c;
} 
ENDCG

            SetTexture[_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
            SetTexture[_MainTex2] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
            SetTexture[_MainTex3] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
            SetTexture[_MainTex4] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
            SetTexture[_Mask] {}
        }
        // Vertex lights
        Pass { 
            Name "BASE"
            Tags {"LightMode" = "Vertex"}
            Lighting On
            Material {
                Diffuse [_Color]
                Ambient [_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!
#pragma fragment frag
#pragma multi_compile_builtin_noshadows
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest

#include "UnityCG.cginc"

#ifdef POINT
#define LIGHTING_COORDS float3 _LightCoord : TEXCOORD5;
uniform sampler3D _LightTexture0 : register(s5);
#ifdef SHADER_API_D3D9
#define LIGHT_ATTENUATION(a)    (tex3D(_LightTexture0, a._LightCoord).r)
#else
#define LIGHT_ATTENUATION(a)    (tex3D(_LightTexture0, a._LightCoord).w)
#endif
#endif


#ifdef SPOT
#define LIGHTING_COORDS float4 _LightCoord : TEXCOORD5; float4 _LightCoord2 : TEXCOORD6;
uniform sampler2D _LightTexture0 : register(s5);
uniform sampler2D _LightTextureB0 : register(s6);
#define LIGHT_ATTENUATION(a)    (tex2Dproj (_LightTexture0, a._LightCoord.xyz).w * tex2D(_LightTextureB0, a._LightCoord2.xy).w) 
#endif 


#ifdef DIRECTIONAL
#define LIGHTING_COORDS 
#define LIGHT_ATTENUATION(a)    1.0
#endif


#ifdef POINT_NOATT
#define LIGHTING_COORDS 
#define LIGHT_ATTENUATION(a)    1.0
#endif


#ifdef POINT_COOKIE
#define LIGHTING_COORDS float3 _LightCoord : TEXCOORD5;
uniform samplerCUBE _LightTexture0 : register(s5);
#define LIGHT_ATTENUATION(a)    (texCUBE(_LightTexture0, a._LightCoord).w)
#endif


#ifdef DIRECTIONAL_COOKIE
#define LIGHTING_COORDS float2 _LightCoord : TEXCOORD5;
uniform sampler2D _LightTexture0 : register(s5);
#define LIGHT_ATTENUATION(a)    (tex2D(_LightTexture0, a._LightCoord).w)
#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

}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools