/******************************************************************************* * * Simple HLSL FX file * *******************************************************************************/ // un-tweakables // structures and shaders ///////////////////// float4x4 wvIT : WorldViewInverseTranspose; float4x4 wvp : WorldViewProjection; float4x4 wv : WorldView; float sparkScale = 16.0; void mainVS( float4 vertex : POSITION0, float3 normal : NORMAL0, float4 tex : TEXCOORD0, out float4 position: POSITION0, out float3 eyeNormal : TEXCOORD0, out float3 eyeView : TEXCOORD1, out float2 texCoord : TEXCOORD2) { // Must write gl_Position for rasterization to be defined.... // position = mul( vertex, wvp); // Transform to shading space (we are going to shade in eyespace) // // eyeNormal = mul( normal, float3x3(wvIT)); eyeView = -mul( vertex, wv).xyz; texCoord = tex.xy * sparkScale; } //sparkles texture to perturb the normals sampler2D sparkleTex; //cubic environment map samplerCUBE envMap; //parameter to control the reflection falloff, 0.33 typically looks pretty good float reflFalloff = 0.33; //how strong the spaks are at bending the normals, 0.0625 looks fairly good float sparkStrength = 0.125; //ambient lighting float3 ambient = float3( 0.2, 0.2, 0.2); float3 lightDir = float3( 0.57735, 0.57735, 0.57735); float3 lightColor = float3( 1.0, 1.0, 1.0); float3 carColor = float3(0.0, 0.5, -0.70); /* //candy apple green float3 carColor0 = float3(0.0, 0.2, 0.70); float3 carColor1 = float3(0.0, 0.5, -0.70); float3 carColor2 = float3(0.1, 0.0, -0.1); float3 sparkleColor = float3(0.7, 0.2, 0.0); */ /* //orange to red const float3 carColor0 = float3(0.40, 0.4, -0.40); const float3 carColor1 = float3(0.0, -0.43, 0.40); const float3 carColor2 = float3(1.0, 0.05, 0.18); const float3 sparkleColor = float3(-0.4, 0.5, -0.70); */ /* // cool gold const float3 carColor0 = float3(0.70, -0.09, 0.00); const float3 carColor1 = float3(0.20, 0.23, 0.00); const float3 carColor2 = float3(0.20, 0.75, 0.08); const float3 sparkleColor = float3(-0.2, 0.2, 0.20); */ /* //intense copper const float3 carColor0 = float3(0.40, 0.0, 0.00); const float3 carColor1 = float3(0.60, 0.1, 0.00); const float3 carColor2 = float3(-0.5, 0.15, -0.35); const float3 sparkleColor = float3(0.2, 0.3, 0.5); */ float clearCoatShininess = 32.0; float paintShininess = 2.0; float4 mainPS( float3 eyeNormal : TEXCOORD0, float3 eyeView : TEXCOORD1, float2 texCoord : TEXCOORD2) : COLOR0 { float3 light = normalize( lightDir); float3 normal = normalize(eyeNormal); //fetch the sparkle texture float4 sparkle1 = tex2D(sparkleTex, texCoord); //normalize the view vector float3 view=normalize(eyeView); //compute the reflection vector float3 reflection = -reflect( view, normal); float nDotV = saturate(dot(view,normal)); float3 sparkNorm = normalize( normal + float3( sparkle1*2.0 - 1.0) * sparkStrength); float3 sparkRefl = -reflect( view, sparkNorm); //compute the illumination, this is a sumation of two specular lobes float3 color = float3(pow(max(0.0,dot(sparkRefl,light)), clearCoatShininess))*lightColor.rgb; color += carColor * (float3(pow(max(0.0,dot(sparkRefl,light)), paintShininess)*pow(nDotV,reflFalloff))*lightColor.rgb+ambient.rgb); //apply the clearcoat with a fresnel term color = float3(texCUBE(envMap,reflection))*(1.0-pow(nDotV,reflFalloff))+color;//*nDotV; return float4(color,1.0); } //////// techniques //////////////////////////// technique blinn { pass p0 { VertexShader = compile vs_2_0 mainVS(); ZEnable = true; ZWriteEnable = true; ZFunc = LEqual; CullMode = Back; PixelShader = compile ps_2_0 mainPS(); } }