//ThinFilm.cg //============================================================================ // define inputs from application //============================================================================ struct a2v { float4 Position : POSITION; float3 Normal : NORMAL; }; //============================================================================ // define outputs from vertex shader //============================================================================ struct v2f { float4 HPOS : POSITION; float4 diffCol : COLOR0; float4 specCol : COLOR1; float2 filmDepth : TEXCOORD0; }; //============================================================================ // The main function //============================================================================ v2f main(a2v IN, uniform float4x4 WorldViewProj, uniform float4x4 WorldViewIT, uniform float4x4 WorldView, uniform float4 LightVector, uniform float4 FilmDepth, uniform float4 EyeVector) { v2f OUT; //transform position to clip space OUT.HPOS = mul(WorldViewProj, IN.Position); float4 tempnorm = float4(IN.Normal, 0.0); // transform normal from model-space to view-space float3 normalVec = mul(WorldViewIT, tempnorm).xyz; normalVec = normalize(normalVec); // compute the eye->vertex vector float3 eyeVec = EyeVector.xyz; //infinite viewer (Best looking) // compute the view depth for the thin film float viewdepth = (1.0 / dot(normalVec, eyeVec)) * FilmDepth.x; OUT.filmDepth = viewdepth.xx; // store normalized light vector float3 lightVec = normalize((float3)LightVector); // calculate half angle vector float3 halfAngleVec = normalize(lightVec + eyeVec); // calculate diffuse component float diffuse = dot(normalVec, lightVec); // calculate specular component float specular = dot(normalVec, halfAngleVec); // use the lit instruction to calculate lighting, automatically clamp float4 lighting = lit(diffuse, specular, 32); // output final lighting results OUT.diffCol = (float4)lighting.y; OUT.specCol = (float4)lighting.z; return OUT; } //-------------------------------------------- struct v2f { float3 diffCol : COLOR0; float3 specCol : COLOR1; float2 filmDepth : TEXCOORD0; }; void main( v2f IN, out float4 color : COLOR, uniform sampler2D fringeMap, uniform sampler2D diffMap) { // diffuse material color float3 diffCol = float3(0.3, 0.3, 0.5); // lookup fringe value based on view depth float3 fringeCol = (float3)tex2D(fringeMap, IN.filmDepth); // modulate specular lighting by fringe color, combine with regular lighting color.rgb = fringeCol*IN.specCol + IN.diffCol*diffCol; color.a = 1.0; }