From Unify Community Wiki
Revision as of 19:26, 19 October 2009 by NCarter (Talk | contribs)

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

<shaderlab>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
              #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

} </shaderlab>

Personal tools