/////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// // // File: bumpreflect.fx // // string ParamID = "0x002"; // string Category = "Effects/Cg/Bump"; // string keywords = "bumpmap,texture"; // string description = "Bumpy-shiny"; #include "../Data/Shaders/ShaderInterfaces/DirectionalLight.shader" #include "../Data/Shaders/ShaderInterfaces/AmbientLight.shader" float4x4 Local2World : World; // World or Model matrix float4x4 World2Proj : WorldViewProjection; // Model*View*Projection float4x4 Camera2World : ViewInverse; /////////////////////////////////////////////////////////////// // tweakables ///////////////////////////////////////////////// /////////////////////////////////////////////////////////////// float BumpHeight; sampler2D SamplerNormal; samplerCUBE envMapSampler; /////////////////////////////////////////////////////////////// // structures and shaders ///////////////////// /////////////////////////////////////////////////////////////// struct VertexInput { float4 Position : POSITION; // in object space float2 TexCoord : TEXCOORD0; float3 Tangent : TANGENT; // TANGENT; // ATTR14; // in object space float3 Binormal : BINORMAL; // BINORMAL; // ATTR15; // in object space float3 Normal : NORMAL; // in object space }; struct PixelInput { float4 Position : POSITION; // in projection space float2 TexCoord : TEXCOORD0; float3 TexCoord1 : TEXCOORD1; // first row of the 3x3 transform from tangent to cube space float3 TexCoord2 : TEXCOORD2; // second row of the 3x3 transform from tangent to cube space float3 TexCoord3 : TEXCOORD3; // third row of the 3x3 transform from tangent to cube space float3 View : TEXCOORD4; }; /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// PixelInput BumpReflectVS( VertexInput _Input ) { PixelInput OUT; // Position in screen space. OUT.Position = mul( _Input.Position, World2Proj ); // pass texture coordinates for fetching the normal map OUT.TexCoord.xy = _Input.TexCoord; // compute the 4x4 tranform from tangent space to object space float3x3 Tangent2Local; Tangent2Local[0] = -_Input.Tangent; Tangent2Local[1] = -_Input.Binormal; Tangent2Local[2] = -_Input.Normal; Tangent2Local = mul( Tangent2Local, Local2World ); OUT.TexCoord1 = Tangent2Local[0]; OUT.TexCoord2 = Tangent2Local[1]; OUT.TexCoord3 = Tangent2Local[2]; // compute the eye vector (going from shaded point to eye) in cube space float4 worldPos = mul( _Input.Position, Local2World ); OUT.View = normalize( worldPos.xyz - Camera2World[3].xyz ); return OUT; } float4 BumpReflectPS( PixelInput _Input ) : COLOR { float3 Normal = tex2D( SamplerNormal, _Input.TexCoord.xy ).xyz * 2.0 - 1.0; Normal = normalize( float3( BumpHeight * Normal.xy, Normal.z ) ); // Transform the bump normal float3 WorldNormal = float3( dot( _Input.TexCoord1, Normal ), dot( _Input.TexCoord2, Normal ), dot( _Input.TexCoord3, Normal ) ); float3 LookUp = reflect( _Input.View, WorldNormal ); return LightColor * texCUBE( envMapSampler, LookUp ); } //////// techniques //////////////////////////// technique BumpReflect { pass p0 { ZEnable = true; ZWriteEnable = true; CullMode = None; VertexShader = compile vs_1_1 BumpReflectVS(); PixelShader = compile ps_2_0 BumpReflectPS(); } } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////