From Unify Community Wiki
Jump to: navigation, search

Author: [ToreTank], with some assistance and art from Seth Burnette.

Bump/spec without rim light
Bump/spec with rim light!


This is a bumped/specular shader with an artificial rim light pass that is affected by the normal map. It is ideal for situations where you may want to show off normal maps even in poor lighting conditions. Several modern video games use shaders similar to this (see Bioshock and the racing game Pure for examples).

BumpSpecRim shader setup


You will need the usual bumped/specular textures plus a ramp texture to define how "deep" the rim light illuminates the object. The rim light color can be selected from the picker labeled Rimlight Color.

  • Main texture is for color and specular highlights.
  • Normal map for bump mapping.
  • Ramp texture (set to Clamp, not Repeat) for the rim light falloff.

ShaderLab - BumpSpecRim.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 "BumpSpecRim" {

     Properties {
        _Color ("Main Color", Color) = (1,1,1,0.5)
        _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
        _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
        _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
        _BumpMap ("Bumpmap (RGB) Rim-Light Ramp (A)", 2D) = "bump" {}
        _RimLightColor ("Rimlight Color", Color) = (0.6, 0.6, 0.7, 1.0)
        _RimLightRamp ("Rimlight Ramp", 2D) = "white" {}

     SubShader {
          //Self-Illumination Depending on Facing Rotation
          Pass {
               Tags {"LightMode" = "PixelOrNone"}
            Blend AppSrcAdd AppDstAdd
               Color [_PPLAmbient]

               // profiles arbfp1
               #pragma vertex vert
               #pragma fragment frag
               #pragma fragmentoption ARB_fog_exp2

               #include "UnityCG.cginc"

               sampler2D _BumpMap;
               sampler2D _RimLightRamp;
               sampler2D _MainTex;
               float4 _RimLightColor;
               float4 _Color;

               struct v2f {
                    float2  uv;
                    float3  tangentSpaceLightDir;

               v2f vert (appdata_tan v)
                    v2f o;
                    PositionFog( v.vertex, o.pos, o.fog );
                    o.uv = TRANSFORM_UV(0);
                    float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                    float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) );
               float3x3 rotation = float3x3( v.tangent.xyz, binormal, v.normal );
               o.tangentSpaceLightDir = mul(rotation, viewDir);

                    return o;
               half4 frag (v2f i) : COLOR
               half3 tangentSpaceNormal = (tex2D(_BumpMap, i.uv).rgb * 2.0) - 1.0;
               half4 result = float4(0, 0, 0, 1);
               //You might want to normalize tangentSpaceNormal and i.tangentSpaceLightDir,
               //but for most meshes this will most likely have minimal, if any, impact on quality.
               float rampSample = dot(tangentSpaceNormal, i.tangentSpaceLightDir);
               float intensity = tex2D(_RimLightRamp, rampSample.xx).r;
               result.rgb = intensity * _RimLightColor.rgb;
               result.rgb += tex2D(_MainTex, i.uv).rgb * _PPLAmbient.rgb;
               return result;
          UsePass "Bumped Specular/PPL"
     FallBack "Bumped Specular", 1
Personal tools