Anisotropic

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 NCarter)
Line 1: Line 1:
 +
Author: [[User:NeARAZ|Aras Pranckevicius]], inspired by Patrik/Metervara and NVIDIA.
  
 +
==Description==
 +
A classic anisotropic lighting shader using a lookup texture. Pretty much straight from [http://http.download.nvidia.com/developer/SDK/Individual_Samples/samples.html#HLSL_Aniso NVIDIA example].
 +
 +
It is a pixel-lit shader that requires vertex&fragment programs. Otherwise it falls back to VertexLit.
 +
 +
==Usage==
 +
 +
* Get the anisotropic lookup texture from [http://http.download.nvidia.com/developer/SDK/Individual_Samples/DEMOS/Direct3D9/HLSL_Aniso.zip NVIDIA code sample] (in Media/Textures/2d/Aniso2.tga).
 +
* Setup it's wrap mode to Clamp in Unity's texture inspector.
 +
* Create a material with this shader, assign the anisotropic lookup texture to anisotropic property.
 +
* Setup your main texture & color in the material.
 +
 +
==ShaderLab - Anisotropic.shader==
 +
<shaderlab>Shader "Anisotropic" {
 +
Properties {
 +
    _Color ("Main Color", Color) = (1,1,1,1)
 +
    _MainTex ("Base (RGB)", 2D) = "white" {}
 +
    _AnisoTex ("Aniso lookup (RGBA)", 2D) = "white" {}
 +
}
 +
 +
Category {
 +
    Lod 0
 +
    Blend AppSrcAdd AppDstAdd
 +
    Fog { Color [_AddFog] }
 +
   
 +
    SubShader {
 +
        // Ambient pass
 +
        Pass {
 +
            Name "BASE"
 +
            Tags {"LightMode" = "PixelOrNone"}
 +
            Color [_PPLAmbient]
 +
            // Make it respond less to ambient color just to make it look better - no DOUBLE part here
 +
            SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary, texture * constant}
 +
        }
 +
        // Vertex lights
 +
        Pass {
 +
            Name "BASE"
 +
            Tags {"LightMode" = "Vertex"}
 +
            Lighting On
 +
            Material {
 +
                Diffuse [_Color]
 +
                Emission [_PPLAmbient]
 +
            }
 +
            SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
 +
        }
 +
        // Pixel lights
 +
        Pass {
 +
            Name "PPL"
 +
            Tags { "LightMode" = "Pixel" }
 +
           
 +
CGPROGRAM
 +
// profiles arbfp1
 +
// vertex vert
 +
// fragment frag
 +
// autolight 7
 +
   
 +
#include "UnityCG.cginc"
 +
#include "AutoLight.cginc"
 +
 +
struct v2f {
 +
    V2F_POS_FOG;
 +
    float2 uv[2] : TEXCOORD0;
 +
    V2F_LIGHT_COORDS(TEXCOORD2);
 +
};
 +
struct v2f2 {
 +
    V2F_POS_FOG;
 +
    float2 uv[2] : TEXCOORD0;
 +
};
 +
 +
v2f vert (appdata_base v) {
 +
    v2f o;
 +
       
 +
    PositionFog( v.vertex, o.pos, o.fog );
 +
         
 +
    o.uv[0] = TRANSFORM_UV(0);
 +
   
 +
    float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
 +
    float3 lightDir = normalize(ObjSpaceLightDir(v.vertex));
 +
   
 +
    float3 halfAngle = normalize(viewDir + lightDir);
 +
    o.uv[1].x = dot(lightDir,v.normal);
 +
    o.uv[1].y = dot(halfAngle,v.normal);
 +
   
 +
    PASS_LIGHT_COORDS(2);
 +
   
 +
    return o;
 +
}
 +
 +
sampler2D _MainTex : register(s0);
 +
sampler2D _AnisoTex : register(s1);
 +
 +
float4 frag(v2f2 i, LIGHTDECL(TEXUNIT2)) : COLOR
 +
{
 +
    float4 texcol = tex2D( _MainTex, i.uv[0] );
 +
    float4 aniso = tex2D( _AnisoTex, i.uv[1] );
 +
    texcol.rgb *= _ModelLightColor[0].rgb;
 +
    texcol.rgb *= aniso.rgb * (aniso.a * LIGHTATT * 2);
 +
    return float4( texcol.rgb, 0 );
 +
}
 +
 +
ENDCG
 +
 +
            SetTexture[_MainTex] {}
 +
            SetTexture[_AnisoTex] {}
 +
            SetTexture [_LightTexture0] {}
 +
            SetTexture [_LightTextureB0] {}
 +
        }
 +
    }
 +
}
 +
 +
Fallback " VertexLit", 1
 +
 +
}</shaderlab>

Revision as of 19:27, 19 October 2009

Author: Aras Pranckevicius, inspired by Patrik/Metervara and NVIDIA.

Description

A classic anisotropic lighting shader using a lookup texture. Pretty much straight from NVIDIA example.

It is a pixel-lit shader that requires vertex&fragment programs. Otherwise it falls back to VertexLit.

Usage

  • Get the anisotropic lookup texture from NVIDIA code sample (in Media/Textures/2d/Aniso2.tga).
  • Setup it's wrap mode to Clamp in Unity's texture inspector.
  • Create a material with this shader, assign the anisotropic lookup texture to anisotropic property.
  • Setup your main texture & color in the material.

ShaderLab - Anisotropic.shader

<shaderlab>Shader "Anisotropic" { Properties {

   _Color ("Main Color", Color) = (1,1,1,1)
   _MainTex ("Base (RGB)", 2D) = "white" {}
   _AnisoTex ("Aniso lookup (RGBA)", 2D) = "white" {}

}

Category {

   Lod 0
   Blend AppSrcAdd AppDstAdd
   Fog { Color [_AddFog] }
   
   SubShader {
       // Ambient pass
       Pass {
           Name "BASE"
           Tags {"LightMode" = "PixelOrNone"}
           Color [_PPLAmbient]
           // Make it respond less to ambient color just to make it look better - no DOUBLE part here
           SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary, texture * constant}
       }
       // Vertex lights
       Pass { 
           Name "BASE"
           Tags {"LightMode" = "Vertex"}
           Lighting On
           Material {
               Diffuse [_Color]
               Emission [_PPLAmbient]
           }
           SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
       }
       // Pixel lights
       Pass {
           Name "PPL"
           Tags { "LightMode" = "Pixel" }
           

CGPROGRAM // profiles arbfp1 // vertex vert // fragment frag // autolight 7

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

struct v2f {

   V2F_POS_FOG;
   float2 uv[2] : TEXCOORD0;
   V2F_LIGHT_COORDS(TEXCOORD2);

}; struct v2f2 {

   V2F_POS_FOG;
   float2 uv[2] : TEXCOORD0;

};

v2f vert (appdata_base v) {

   v2f o;
       
   PositionFog( v.vertex, o.pos, o.fog );
         
   o.uv[0] = TRANSFORM_UV(0);
   
   float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
   float3 lightDir = normalize(ObjSpaceLightDir(v.vertex));
   
   float3 halfAngle = normalize(viewDir + lightDir);
   o.uv[1].x = dot(lightDir,v.normal);
   o.uv[1].y = dot(halfAngle,v.normal);
   
   PASS_LIGHT_COORDS(2);
   
   return o;

}

sampler2D _MainTex : register(s0); sampler2D _AnisoTex : register(s1);

float4 frag(v2f2 i, LIGHTDECL(TEXUNIT2)) : COLOR {

   float4 texcol = tex2D( _MainTex, i.uv[0] );
   float4 aniso = tex2D( _AnisoTex, i.uv[1] );
   texcol.rgb *= _ModelLightColor[0].rgb;
   texcol.rgb *= aniso.rgb * (aniso.a * LIGHTATT * 2);
   return float4( texcol.rgb, 0 );

}

ENDCG

           SetTexture[_MainTex] {}
           SetTexture[_AnisoTex] {}
           SetTexture [_LightTexture0] {}
           SetTexture [_LightTextureB0] {}
       } 
   } 

}

Fallback " VertexLit", 1

}</shaderlab>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox