BumpSpecModulate

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 2: Line 2:
 
[[Category: Bump Mapping]]
 
[[Category: Bump Mapping]]
 
[[Category: Alpha]]
 
[[Category: Alpha]]
=== Description ===
+
== Description ==
 
Use this shader to have a bump mapped mesh that modulates between original texture color and "alpha color" based on alpha of main texture.
 
Use this shader to have a bump mapped mesh that modulates between original texture color and "alpha color" based on alpha of main texture.
  
=== Usage ===
+
== Usage ==
 
Place this shader somewhere in your Assets folder hierarchy, and assign it to a new material with the properties of your choice.  Then assign the material to a mesh by dragging the material to the mesh.
 
Place this shader somewhere in your Assets folder hierarchy, and assign it to a new material with the properties of your choice.  Then assign the material to a mesh by dragging the material to the mesh.
  
=== ShaderLab - BumpedSpecModulate.shader ===
+
== ShaderLab - BumpedSpecModulate.shader ==
 
<shaderlab>
 
<shaderlab>
 
Shader "BumpedSpecularModulate" {
 
Shader "BumpedSpecularModulate" {

Revision as of 12:03, 21 December 2005

Description

Use this shader to have a bump mapped mesh that modulates between original texture color and "alpha color" based on alpha of main texture.

Usage

Place this shader somewhere in your Assets folder hierarchy, and assign it to a new material with the properties of your choice. Then assign the material to a mesh by dragging the material to the mesh.

ShaderLab - BumpedSpecModulate.shader

<shaderlab> Shader "BumpedSpecularModulate" {

   Properties {                                // Properties for the BumpSpec shader. These are shown in the inspector and can be changed there
       _Color ("Main Color", Color) = (.5, .5, .5, .5)
       _Color2 ("Alpha Color", Color) = (.5, .5, .5, .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", 2D) = "bump" {}
   }
   Category {                              // Stuff set here will work for all subscopes in the category
       Blend AppSrcAdd AppDstAdd
       Fog { Color [_AddFog] }
       SubShader {                         // The SubShader that works with ARB Fragment Program capable hardware 
       // ------------------------------------------------------
       // ARB_FP, Geforce3
       // ------------------------------------------------------
       UsePass " Glossy/BASE"                  // Use the pass(es) named BASE in the Glossy shader. (Can be done as they are identical, saves code)
       Pass { 
           Name "PPL"  
           Tags {
               "LightMode" = "Pixel" 
               "LightTexCount" = "012"
           }

CGPROGRAM // profiles arbfp1 // fragment frag // fragmentoption ARB_fog_exp2 // fragmentoption ARB_precision_hint_fastest // vertex vert // autolight 7

  1. include "UnityCG.cginc"
  2. include "AutoLight.cginc"

struct appdata {

   float4 vertex;
   float4 tangent;
   float3 normal;
   float4 texcoord;

};

struct v2f {

   float4 hPosition    : POSITION;  
   float fog    : FOGC;  
   float4 color  : COLOR0;   
   float4 uv : TEXCOORD0; 
   float3 viewDirectionT : TEXCOORD1;
   float3 light : COLOR1;
   float3 lightDirectionT  : TEXCOORD3;
   float4 _LightCoord[2]: TEXCOORD4;
   float4 uv2 : TEXCOORD2; 

}; struct v2f2 {

   float4 hPosition    : POSITION;  
   float fog    : FOGC;  
   float4 color  : COLOR0;   
   float4 uv : TEXCOORD0; 
   float3 viewDirectionT : TEXCOORD1;
   float3 light : COLOR1;
   float3 lightDirectionT  : TEXCOORD3;
   float4 uv2 : TEXCOORD2; 

}; Light l;


v2f vert (appdata v) {

 v2f o;
 o.hPosition = mul (glstate.matrix.mvp, v.vertex);
   o.fog = o.hPosition.z;

 // Compute the object-space light vector
 // We multiply with _ObjectSpaceLightPos[0].w to account for directional lights (which have w = 0)
 float3 lightDirection =  _ObjectSpaceLightPos[0].xyz - v.vertex.xyz * _ObjectSpaceLightPos[0].w;
 
  // Attenuation for point lights
 float dist = dot (lightDirection, lightDirection);
 float attenuate = 1.0 / (glstate.light[0].attenuation.x + dist * glstate.light[0].attenuation.z);
 o.light.rgb = attenuate;
 
 // Construct object space to texture space 3x3 matrix
 float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w;
 float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
 // Multiply uv with the uv texture matrix
 o.uv = mul(v.texcoord,glstate.matrix.texture[1]); 
 o.uv2 = mul(v.texcoord,glstate.matrix.texture[0]); 
  // Transfer light direction and view direction vectors to tangent space
  o.lightDirectionT = mul(rotation, lightDirection);
  o.viewDirectionT =  mul(rotation,_ObjectSpaceCameraPos - v.vertex.xyz);

 o._LightCoord[0] = l.TextureCoord (2, v.vertex);
 o._LightCoord[1] = l.TextureCoord (3, v.vertex);
 return o;

}

uniform sampler2D _BumpMap; uniform sampler2D _MainTex; uniform float4 _SpecColor; uniform float4 _LightColor0; uniform float _Shininess; uniform float4 _Color2; float4 frag (v2f2 i, LIGHTDECL(TEXUNIT2))  : COLOR {

 // Normalizes light vector
 float3 light = normalize (i.lightDirectionT);
 float4 color; 
 // Sample and expand the normal map texture   
float3 normal = tex2D(_BumpMap, i.uv2.xy).xyz* 2.0  - 1.0;
 normal = normalize(normal);
 // Calculate View and Half Angle vector
 float3 V = normalize(i.viewDirectionT); 
 float3 H = normalize(light + V);
 
 // Main color from texturemap
 float4 texcol = tex2D(_MainTex,i.uv.xy);
 texcol.rgb = lerp(_Color2.rgb, texcol.rgb, texcol.a); 
   
 // Diffuse contribution
 float diffuse = dot(light,normal);
 // Specular contribution
 float spec = pow( max(0, dot(H, normal)), _Shininess * 128) * texcol.a;
 spec *= max(0, dot(float3(0,0,1), light));
 // Final color
 color.rgb =  (diffuse * texcol.rgb  * _ModelLightColor[0].rgb + _SpecColor.rgb * spec * _LightColor0.rgb) * (LIGHTATT) * 2 * i.light.r; 
 
 // We populate the alpha with the spec attenuated, as this can be used for selfilumin.
 color.a = spec *  _SpecColor.a * LIGHTATT;
 return color;

} ENDCG

               SetTexture [_BumpMap]{ combine primary DOT3 texture}
               SetTexture [_MainTex]{ combine previous * texture}
               SetTexture [_LightTexture0] { combine previous * texture} 
               SetTexture [_LightTextureB0]{ combine previous * texture}
           }
       }
       // ---------------------------------------------
       // R9000 
       // ---------------------------------------------
       SubShader {
           UsePass " Glossy/BASE"
           Pass { 
               Name "PPL"  
               Tags { 
                   "LightMode" = "Pixel" 
                   "LightTexCount" = "0"
               }

CGPROGRAM // vertex

  1. include "unityCG.cginc"

struct appdata {

   float4 vertex;
   float4 tangent;
   float3 normal;
   float4 texcoord;

};

struct v2f {

   float4 pos : POSITION;
   float4 tex : TEXCOORD0;
   float4 tex2 : TEXCOORD3;
   float3 H : TEXCOORD1;
   float3 light : TEXCOORD2;
   float4 color : COLOR0;
   float fog : FOG;

}; Light l; v2f main(appdata v) {

   v2f o;
    // Construct object space to texture space 3x3 matrix
     float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w;
     float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
   o.light =_ObjectSpaceLightPos[0].xyz - v.vertex.xyz*_ObjectSpaceLightPos[0].w;  float3 V = normalize(_ObjectSpaceCameraPos - v.vertex.xyz);
   o.H = normalize(normalize(o.light) + V);
   o.light = mul(rotation, o.light);
   o.H = normalize(mul(rotation, o.H));
   o.pos = mul(glstate.matrix.mvp, v.vertex);
   o.tex = mul(v.texcoord, glstate.matrix.texture[0]); 
   o.tex2 = mul(v.texcoord, glstate.matrix.texture[3]); 
   o.fog = o.pos.z;
   return o; 

} ENDCG

               Program "" {
                   SubProgram {
                   Local 0, [_SpecularLightColor0]
                   Local 1, [_ModelLightColor0]
                   Local 2, (0,[_Shininess],0,1)
                   Local 3, [_Color2]

"!!ATIfs1.0 StartConstants;

   CONSTANT c0 = program.local[0];
   CONSTANT c1 = program.local[1];
   CONSTANT c2 = program.local[2];
   CONSTANT c3 = program.local[3];

EndConstants;

StartPrelimPass;

   SampleMap r3, t3.stq_dq;        # Lookup Normal 
   SampleMap r2, t2.str;       # Normalized light
   PassTexCoord r4, t1.str;        # Pass H
   DOT3 r5.sat, r3.bias.2x, r2.bias.2x;        # Diffuse: N.L
   DOT3 r1.sat, r3.2x.bias, r4;            # reg2 = N.H
   MUL  r1, r1, r1;                                    # reg2 = N.H * N.H = (N.H)^2
   DOT3 r1.b.sat, r4, r4;                      # reg2(blue) = H.H = |H|^2
   MUL  r1.g.half, r1.b, c2.g;             # reg2(green) = |H|^2 * 0.5 * k

EndPass;

StartOutputPass;

   SampleMap r0, t0.stq_dq;     # main texture
   SampleMap r1, r1.str_dr;
   PassTexCoord r5, r5.str;
   LERP r0.rgb, r0.a, r0, c3;
   MUL r1.rgb, r1, r5; 
   MUL r5, r5, c1;     
   MUL r1.rgb,r1,c0;
   MUL r1.a, r1.a, r0.a;
   MUL r1, r1, r0.a;
   ADD r1.rgb.2x, r5, r1;
   MUL r0.rgb, r1, r0;
   MUL r0.a.2x, r1.a, c0.r;

EndPass; "

                   }
               }
               SetTexture [_MainTex] {combine texture}
               SetTexture [_SpecFalloff]{combine previous}
               SetTexture [_CubeNormalize]{combine previous}
               SetTexture[_BumpMap]{combine previous}
           }
           Pass { 
               Name "PPL"  
               Blend AppSrcAdd AppDstAdd
               Fog { Color [_AddFog] }
               Tags { 
                   "LightMode" = "Pixel" 
                   "LightTexCount" = "1"
               }

CGPROGRAM // vertex

  1. include "unityCG.cginc"

struct appdata {

   float4 vertex;
   float4 tangent;
   float3 normal;
   float4 texcoord;

};


struct v2f {

   float4 pos : POSITION;
   float4 tex : TEXCOORD0;
   float3 H : TEXCOORD2;
   float3 light : TEXCOORD3;
   float4 tex2 : TEXCOORD4;
   float3 attenuate : TEXCOORD5;
   float4 LightCoord0   : TEXCOORD1;   
   float4 color : COLOR0;
   float fog : FOG;

}; Light l; v2f main(appdata v) {

   v2f o;
   // Construct object space to texture space 3x3 matrix
   float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w;
   float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);    o.light = _ObjectSpaceLightPos[0].xyz - v.vertex.xyz*_ObjectSpaceLightPos[0].w;
   
   float dist = dot (o.light, o.light);
   // Attenuation for point lights
   o.attenuate.rgb = 1.0 / (glstate.light[0].attenuation.x + dist * glstate.light[0].attenuation.z);
   float3 V = normalize(_ObjectSpaceCameraPos - v.vertex.xyz);
   o.H = normalize(normalize(o.light) + V);
   o.light = mul(rotation, o.light);
   o.H = mul(rotation, o.H);
   o.pos = mul(glstate.matrix.mvp, v.vertex);
   o.tex = mul(v.texcoord,glstate.matrix.texture[0]); 
   o.tex2 = mul(v.texcoord,glstate.matrix.texture[4]); 
   o.fog = o.pos.z;
   o.LightCoord0 = l.TextureCoord (1, v.vertex);
   
   return o; 

} ENDCG

       Program "" {
       SubProgram {
       Local 0, [_SpecularLightColor0]
       Local 1, [_ModelLightColor0]
       Local 2, (0,[_Shininess],0,1)
       Local 3, [_Color2]

"!!ATIfs1.0 StartConstants;

   CONSTANT c0 = program.local[0];
   CONSTANT c1 = program.local[1];
   CONSTANT c2 = program.local[2];
   CONSTANT c3 = program.local[3];

EndConstants;

StartPrelimPass;

   SampleMap r4, t4.stq_dq;        # Lookup Normal 
   SampleMap r3, t3.str;       # Normalized light
   PassTexCoord r1, t2.str;        # Pass H
   DOT3 r5.sat, r4.bias.2x, r3.bias.2x;        # Diffuse: N.L
   DOT3 r2.sat, r4.2x.bias, r1;            # reg2 = N.H
   MUL  r2, r2, r2;                                    # reg2 = N.H * N.H = (N.H)^2
   DOT3 r2.b.sat, r1, r1;                      # reg2(blue) = H.H = |H|^2
   MUL  r2.g.half, r2.b, c2.g;             # reg2(green) = |H|^2 * 0.5 * k
   MUL r3, r5, c1;     

EndPass;

StartOutputPass;

   SampleMap r0, t0.stq_dq;     # main texture
   SampleMap r1, t1.str;
   SampleMap r2, r2.str_dr;
   PassTexCoord r5, r5.str;
   PassTexCoord r4, r3.str;
   PassTexCoord r3, t5.str;
   
   LERP r0.rgb, r0.a, r0, c3;
   MUL r2.rgb, r2, r5; 
   MUL r2.rgb,r2,c0;
   MUL r2, r2, r0.a;
   MUL r2.a, r2.a, r0.a;
   ADD r2.rgb, r4, r2;
   MUL r2.rgb.2x, r2, r3;
   MUL r0.rgb, r2, r0;
   MUL r0.a, c0.a, r1.a;
   MUL r0.rgb, r0, r1.a;   
   MUL r0.a.2x, r2.a, r0.a;

EndPass; "

                   }
               }
               SetTexture [_MainTex] {combine texture}
               SetTexture [_LightTexture0] {combine texture alpha}
               SetTexture [_SpecFalloff]{combine previous}
               SetTexture [_CubeNormalize]{combine previous}
               SetTexture[_BumpMap]{combine previous}
           }
           Pass { 
               Name "PPL"  
               Blend AppSrcAdd AppDstAdd
               Fog { Color [_AddFog] }
               Tags { 
                   "LightMode" = "Pixel" 
                   "LightTexCount" = "2"
               }

CGPROGRAM // vertex

  1. include "unityCG.cginc"

struct appdata {

   float4 vertex;
   float4 tangent;
   float3 normal;
   float4 texcoord;

};

struct v2f {

   float4 pos : POSITION;
   float4 tex : TEXCOORD0;
   float4 tex2: TEXCOORD5;
   float3 light : TEXCOORD3;
   float3 H : TEXCOORD2;
   float4 LightCoord0   : TEXCOORD1;   
   float4 LightCoordB0   : TEXCOORD4;   
   float4 color : COLOR0;
   float fog : FOG;

}; Light l; v2f main(appdata v) {

   v2f o;
   // Construct object space to texture space 3x3 matrix
    // Construct object space to texture space 3x3 matrix
     float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w;
     float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
     o.pos = mul(glstate.matrix.mvp, v.vertex);
   o.light =_ObjectSpaceLightPos[0].xyz - v.vertex.xyz*_ObjectSpaceLightPos[0].w;  float3 V = normalize(_ObjectSpaceCameraPos - v.vertex.xyz);
   o.H = normalize(normalize(o.light) + V);    o.light = mul(rotation, o.light);
   o.H = mul(rotation, o.H);
   o.tex = mul(v.texcoord,glstate.matrix.texture[0]); 
   o.tex2 = mul(v.texcoord,glstate.matrix.texture[5]); 
   o.fog = o.pos.z;
   o.LightCoord0 = l.TextureCoord (1, v.vertex);
   o.LightCoordB0 = l.TextureCoord (4, v.vertex);
   return o; 

} ENDCG

               Program "" {
                   SubProgram {
                   Local 0, [_SpecularLightColor0]
                   Local 1, [_ModelLightColor0]
                   Local 2, (0,[_Shininess],0,1)
                   Local 3, [_Color2]

"!!ATIfs1.0 StartConstants;

   CONSTANT c0 = program.local[0];
   CONSTANT c1 = program.local[1];
   CONSTANT c2 = program.local[2];
   CONSTANT c3 = program.local[3];

EndConstants;

StartPrelimPass;

   SampleMap r5, t5.stq_dq;        # Lookup Normal 
   SampleMap r3, t3.str;       # Normalized light
   PassTexCoord r1, t2.str;        # Pass H
   DOT3 r3.sat, r5.bias.2x, r3.bias.2x;        # Diffuse: N.L
   DOT3 r2.sat, r5.2x.bias, r1;            # reg2 = N.H
   MUL  r2, r2, r2;                                    # reg2 = N.H * N.H = (N.H)^2
   DOT3 r2.b.sat, r1, r1;                      # reg2(blue) = H.H = |H|^2
    MUL  r2.g.half, r2.b, c2.g;            # reg2(green) = |H|^2 * 0.5 * k
   MUL r4, r3, c1;

EndPass;

StartOutputPass;

   SampleMap r0, t0.stq_dq;     # main texture
   SampleMap r1, t1.stq_dq;
   SampleMap r2, r2.str_dr;
   SampleMap r4, t4.stq_dq;
   PassTexCoord r3, r3.str;
   PassTexCoord r5, r4.str;
   
   LERP r0.rgb, r0.a, r0, c3;
   MUL r2.rgb, r2, r3;     
   MUL r2.rgb,r2,c0;
   MUL r2.a, r2.a, r4.a;
   MUL r2.rgb, r2, r0.a;
   MUL r2.a, r2.a, r1.a;
   ADD r2.rgb.2x, r5, r2;
   MUL r0.rgb, r0, r2;
   MUL r0.a, r0.a, c0.r;
   MUL r0.rgb, r0, r4.a;
   MUL r0.rgb, r0, r1.a;   
   MUL r0.a.2x, r2.a, r0.a;

EndPass; "

                   }
               }
               SetTexture [_MainTex] {combine texture}
               SetTexture [_LightTexture0] {combine texture alpha}
               SetTexture [_SpecFalloff]{combine previous}
               SetTexture [_CubeNormalize]{combine previous}
               SetTexture [_LightTextureB0] {combine texture alpha}
               SetTexture[_BumpMap]{combine previous}
           }   
       }
   }
   FallBack " VertexLit", 1

} </shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox