BumpColorSpec

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by NeARAZ)
Line 1: Line 1:
 +
[[Category: Unity 2.x shaders]]
 +
[[Category: Cg shaders]]
 +
[[Category: Pixel lit shaders]]
  
 +
Author: [[User:NeARAZ|Aras Pranckevicius]].
 +
 +
==Description==
 +
 +
[[Image:BumpColorSpec.png|thumb|Colored specular highlights]]
 +
 +
Very similar to built-in [http://unity3d.com/support/documentation/Components/shader-NormalBumpedSpecular.html 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==
 +
 +
[[Image:BumpSpecColorSetup.png|Typical setup]]
 +
 +
* Main texture is for color.
 +
* Normal map for bumpyness.
 +
* Specular map indicates color & strength of specular highlights.
 +
 +
 +
==ShaderLab - BumpColorSpec.shader==
 +
<shaderlab>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] }
 +
   
 +
    // ------------------------------------------------------------------
 +
    // ARB fragment program
 +
   
 +
    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"
 +
 +
}
 +
</shaderlab>

Revision as of 19:26, 19 October 2009


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

<shaderlab>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] }
   
   // ------------------------------------------------------------------
   // ARB fragment program
   
   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

  1. pragma fragment frag
  2. pragma fragmentoption ARB_fog_exp2
  3. pragma fragmentoption ARB_precision_hint_fastest
  1. 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

  1. pragma vertex vert
  2. pragma fragment frag
  3. pragma multi_compile_builtin
  4. pragma fragmentoption ARB_fog_exp2
  5. pragma fragmentoption ARB_precision_hint_fastest
  6. include "UnityCG.cginc"
  7. 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"

} </shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox