BumpColorSpec

From Unify Community Wiki
Jump to: navigation, search


Author: Aras Pranckevicius.

Description

Colored specular highlights

Very similar to built-in Bumped Specular, just this one uses separate colored specular map (as opposed to grayscale gloss map in alpha of the main texture).

Works on fragment program capable cards (Radeon 9500+, GeForce FX+, Intel 9xx). Supports shadows.

Usage

Typical setup

  • Main texture is for color.
  • Normal map for bumpyness.
  • Specular map indicates color & strength of specular highlights.


ShaderLab - BumpColorSpec.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 "Bumped Color Specular" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    _BumpMap ("Bumpmap (RGB)", 2D) = "bump" {}
    _SpecMap ("Spec map (RGB)", 2D) = "white" {}
}

Category {
    Blend AppSrcAdd AppDstAdd
    Fog { Color [_AddFog] }
    Tags { "RenderType"="Opaque" }
    
    SubShader { 
        // Ambient pass
        Pass {
            Name "BASE"
            Tags {"LightMode" = "PixelOrNone"}
            Blend AppSrcAdd AppDstAdd
            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]
            }

CGPROGRAM
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest

#include "UnityCG.cginc"

uniform sampler2D _MainTex;

half4 frag (v2f_vertex_lit i) : COLOR {
    return VertexLight( i, _MainTex );
} 
ENDCG
        }
        
        // Pixel lights
        Pass { 
            Name "PPL"    
            Tags { "LightMode" = "Pixel" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest 
#include "UnityCG.cginc"
#include "AutoLight.cginc" 

struct v2f {
    V2F_POS_FOG;
    LIGHTING_COORDS
    float3    uvK; // xy = UV, z = specular K
    float4    uv2; // bumpmap UV, specmap UV
    float3    viewDirT;
    float3    lightDirT;
}; 

uniform float4 _MainTex_ST, _BumpMap_ST, _SpecMap_ST;
uniform float _Shininess;


v2f vert (appdata_tan v)
{    
    v2f o;
    PositionFog( v.vertex, o.pos, o.fog );
    o.uvK.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    o.uvK.z = _Shininess * 128;
    o.uv2.xy = TRANSFORM_TEX(v.texcoord, _BumpMap);
    o.uv2.zw = TRANSFORM_TEX(v.texcoord, _SpecMap);

    TANGENT_SPACE_ROTATION;
    o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );    
    o.viewDirT = mul( rotation, ObjSpaceViewDir( v.vertex ) );    

    TRANSFER_VERTEX_TO_FRAGMENT(o);    
    return o;
}

uniform sampler2D _BumpMap;
uniform sampler2D _MainTex;
uniform sampler2D _SpecMap;
uniform float4 _LightColor0;

// Calculates Blinn-Phong (specular) lighting model
inline half4 SpecularColorLight( half3 lightDir, half3 viewDir, half3 normal, half4 color, half4 specColor, float specK, half atten )
{
    #ifndef USING_DIRECTIONAL_LIGHT
    lightDir = normalize(lightDir);
    #endif
    viewDir = normalize(viewDir);
    half3 h = normalize( lightDir + viewDir );
    
    half diffuse = dot( normal, lightDir );
    
    float nh = saturate( dot( h, normal ) );
    float spec = pow( nh, specK ) * color.a;
    
    half4 c;
    c.rgb = (color.rgb * _ModelLightColor0.rgb * diffuse + _LightColor0.rgb * specColor.rgb * spec) * (atten * 2);
    c.a = _LightColor0.a * specColor.a * spec * atten; // specular passes by default put highlights to overbright
    return c;
}


half4 frag (v2f i) : COLOR
{        
    half4 texcol = tex2D( _MainTex, i.uvK.xy );
    half4 speccol = tex2D( _SpecMap, i.uv2.zw );
    float3 normal = tex2D(_BumpMap, i.uv2.xy).xyz * 2.0 - 1.0;
    
    half4 c = SpecularColorLight( i.lightDirT, i.viewDirT, normal, texcol, speccol, i.uvK.z, LIGHT_ATTENUATION(i) );
    return c;
}
ENDCG  
        }
    }    
}

FallBack "Diffuse"

}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools