TerrainTwoLayerBumped

From Unify Community Wiki
Jump to: navigation, search


Author: Aras Pranckevicius

Contents

Description

Two layer bumpmapped terrain

This shader is similar to LayerShader - it mixes two tileable textures based on the third texture, and adds bumpmapping on top. In Unity 2.x it supports shadows as well.

This shader requires a fragment program card for bumpmapping to work. Where bumpmapping is not supported, it fallbacks to a vertex lit version, which is the same as LayerShader (and this requires a 4-texture card - GeForce3 and up, Radeon 8500 and up).

Note that bumpmapping version in Unity 1.x does not work on GeForceFX, even if otherwise these cards support fragment programs (shader requires more than 4 texture units per pass). In Unity 2.x it should work on GeForceFX as well.


Usage

TerrainTwoLayerBumpedSetup.png

A typical setup is displayed: two tileable textures, an alpha texture to control the blending between them, and a bump map.

ShaderLab - TerrainTwoLayerBumped.shader for Unity 2.x

This version is for Unity 2.x only!

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/TwoLayer Bumped" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,0.5)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _MainTex2 ("Base 2 (RGB)", 2D) = "white" {}
    _Mask ("Mix Mask (A)", 2D) = "gray" {}
    _BumpMap ("Bumpmap (RGB)", 2D) = "bump" {}
}
 
Category {
    Blend AppSrcAdd AppDstAdd
    Fog { Color [_AddFog] }
 
    // ------------------------------------------------------------------
    // ARB fragment program
    
    SubShader {
        // Ambient pass
        Pass {
            Tags {"LightMode" = "PixelOrNone"}
            Color [_PPLAmbient]
            SetTexture [_MainTex] { combine texture }
            SetTexture [_Mask] { combine previous, texture }
            SetTexture [_MainTex2] { combine texture lerp (previous) previous }
            SetTexture [_MainTex2] { constantColor [_Color] combine previous * primary DOUBLE, previous * constant }
        }
        // Vertex lights
        Pass { 
            Tags {"LightMode" = "Vertex"}
            Lighting On
            Material {
                Diffuse [_Color]
                Emission [_PPLAmbient]
            }
            SetTexture [_MainTex] { combine texture }
            SetTexture [_Mask] { combine previous, texture }
            SetTexture [_MainTex2] { combine texture lerp (previous) previous }
            SetTexture [_MainTex2] { constantColor [_Color] combine previous * primary DOUBLE, previous * constant }
        }
        
        // Pixel lights
        Pass {
            Name "PPL"
            Tags { "LightMode" = "Pixel" }
                
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_builtin
 
#include "UnityCG.cginc"
#include "AutoLight.cginc"
 
struct v2f { 
    V2F_POS_FOG;
    LIGHTING_COORDS
    float2  uv[4];
    float3  lightDirT;
}; 

float4 _MainTex_ST, _MainTex2_ST, _Mask_ST, _BumpMap_ST; 
 
v2f vert (appdata_tan v)
{
    v2f o;
    PositionFog( v.vertex, o.pos, o.fog );
    o.uv[0] = TRANSFORM_TEX(v.texcoord, _MainTex);
    o.uv[1] = TRANSFORM_TEX(v.texcoord, _MainTex2);
    o.uv[2] = TRANSFORM_TEX(v.texcoord, _Mask);
    o.uv[3] = TRANSFORM_TEX(v.texcoord, _BumpMap);
    
    TANGENT_SPACE_ROTATION;
    o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );    
    
    TRANSFER_VERTEX_TO_FRAGMENT(o);
    return o;
}
 
uniform sampler2D _MainTex;
uniform sampler2D _MainTex2;
uniform sampler2D _Mask;
uniform sampler2D _BumpMap;
 
float4 frag (v2f i) : COLOR
{
    half4 col1 = tex2D(_MainTex,i.uv[0]);
    half4 col2 = tex2D(_MainTex2,i.uv[1]);
    half mix = tex2D(_Mask,i.uv[2]).a;
    half4 texcol = lerp(col1,col2,mix);
    
    // get normal from the normal map
    float3 normal = tex2D(_BumpMap, i.uv[3]).xyz * 2 - 1;
    
    return DiffuseLight( i.lightDirT, normal, texcol, LIGHT_ATTENUATION(i) );
}
ENDCG              
        }
    }
    
    // ------------------------------------------------------------------
    // Four texture cards
    
    SubShader {
        // Vertex lit
        Pass { 
            Lighting On
            Material {
                Diffuse [_Color]
                Ambient [_Color]
            }
            SetTexture [_MainTex] { combine texture }
            SetTexture [_Mask] { combine previous, texture }
            SetTexture [_MainTex2] { combine texture lerp (previous) previous }
            SetTexture [_MainTex2] { constantColor [_Color] combine previous * primary DOUBLE, previous * constant }
        }
    }
}
 
FallBack "VertexLit"
 
}

ShaderLab - TerrainTwoLayerBumped.shader for Unity 1.x

This version is for Unity 1.x only!

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/TwoLayer Bumped" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,0.5)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _MainTex2 ("Base 2 (RGB)", 2D) = "white" {}
    _Mask ("Mix Mask (A)", 2D) = "gray" {}
    _BumpMap ("Bumpmap (RGB)", 2D) = "bump" {}
}

Category {
    Blend AppSrcAdd AppDstAdd
    Fog { Color [_AddFog] }

    // ------------------------------------------------------------------
    // ARB fragment program
    
    SubShader {
        // Ambient pass
        Pass {
            Tags {"LightMode" = "PixelOrNone"}
            Color [_PPLAmbient]
            SetTexture [_MainTex] { combine texture }
            SetTexture [_Mask] { combine previous, texture }
            SetTexture [_MainTex2] { combine texture lerp (previous) previous }
            SetTexture [_MainTex2] { constantColor [_Color] combine previous * primary DOUBLE, previous * constant }
        }
        // Vertex lights
        Pass { 
            Tags {"LightMode" = "Vertex"}
            Lighting On
            Material {
                Diffuse [_Color]
                Emission [_PPLAmbient]
            }
            SetTexture [_MainTex] { combine texture }
            SetTexture [_Mask] { combine previous, texture }
            SetTexture [_MainTex2] { combine texture lerp (previous) previous }
            SetTexture [_MainTex2] { constantColor [_Color] combine previous * primary DOUBLE, previous * constant }
        }
        
        // Pixel lights
        Pass {
            Name "PPL"
            Tags { "LightMode" = "Pixel" }
                
CGPROGRAM
// profiles arbfp1
// fragment frag
// fragmentoption ARB_fog_exp2
// fragmentoption ARB_precision_hint_fastest

// vertex vert
// autolight 7
#include "UnityCG.cginc"
#include "AutoLight.cginc"

struct v2f { 
    V2F_POS_FOG;
    float2  uv[4]       : TEXCOORD0;
    float3  lightDirT   : TEXCOORD4;
    V2F_LIGHT_COORDS(TEXCOORD5);
}; 
struct v2f2 { 
    V2F_POS_FOG;
    float2  uv[4]       : TEXCOORD0;
    float3  lightDirT   : TEXCOORD4;
};
Light l;


v2f vert (appdata_tan v)
{
    v2f o;
    PositionFog( v.vertex, o.pos, o.fog );
    o.uv[0] = TRANSFORM_UV(0);
    o.uv[1] = TRANSFORM_UV(1);
    o.uv[2] = TRANSFORM_UV(2);
    o.uv[3] = TRANSFORM_UV(3);
    
    TANGENT_SPACE_ROTATION;
    o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );    
    
    PASS_LIGHT_COORDS(4);
    return o;
}

uniform sampler2D _MainTex : register(s0);
uniform sampler2D _MainTex2 : register(s1);
uniform sampler2D _Mask : register(s2);
uniform sampler2D _BumpMap : register(s3);

float4 frag (v2f2 i, LIGHTDECL(TEXUNIT4)) : COLOR
{
    half4 col1 = tex2D(_MainTex,i.uv[0]);
    half4 col2 = tex2D(_MainTex2,i.uv[1]);
    half mix = tex2D(_Mask,i.uv[2]).a;
    half4 texcol = lerp(col1,col2,mix);
    
    // get normal from the normal map
    float3 normal = tex2D(_BumpMap, i.uv[3]).xyz * 2 - 1;
    
    return DiffuseLight( i.lightDirT, normal, texcol, LIGHTATT );
}
ENDCG  
            SetTexture [_MainTex] {}
            SetTexture [_MainTex2] {}
            SetTexture [_Mask] {}
            SetTexture [_BumpMap] {}
            SetTexture [_LightTexture0] {}
            SetTexture [_LightTextureB0] {}
        }
    }
    
    // ------------------------------------------------------------------
    // Four texture cards
    
    SubShader {
        // Vertex lit
        Pass { 
            Lighting On
            Material {
                Diffuse [_Color]
                Ambient [_Color]
            }
            SetTexture [_MainTex] { combine texture }
            SetTexture [_Mask] { combine previous, texture }
            SetTexture [_MainTex2] { combine texture lerp (previous) previous }
            SetTexture [_MainTex2] { constantColor [_Color] combine previous * primary DOUBLE, previous * constant }
        }
    }
}

FallBack " VertexLit", 1

}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools