/****************************************************************************** HLSL NormalMap Maker by Ben Cloward bsmji@hotmail.com http://www.monitorstudios.com/bcloward/ This shader is based on several shaders written by Kevin Bjorke of Nvidia and included with the Cg plugin for 3DS Max. The purpose of this shader is to generate a normal map from your high res model that you can then apply to your low res model using another shader. This shader is different from all the other shaders I've written because it's meant to be applied to a high res model as part of the process of creating your art - instead of being applied to the low res model at the end of the process. It's just a clever way of using the graphics hardware to create an instant normal map instead of having to wait for the software renderer to do it. Just follow the steps below to make it work. 1. Create a highly detailed model. It could be a brick wall, a metal panel with rivets, etc. (This shader works best on objects that can are mainly flat or that can be tiled. It doesn't work well for characters or other objects that need to be unwrapped to texture.) 2. Apply this shader to your model and take a screen shot from an orthographic viewport (top, front, etc.) 3. Paste the screen shot into an image in Photoshop or other image software. Scale and crop the image so just the object is showing and the texture is 256x256 or 512x512, etc. 4. Now you've got a normal map. Save it and apply it to your low res model using one of my other shaders that supports normal maps. It requires hardware support for DirectX 9. This shader is intended to be used by the DirectX 9 shader of 3DS Max but it could also be used in other applications that support the FX format. ******************************************************************************/ /*** automatically-tracked "tweakables" ********/ float4x4 wvp : WORLDVIEWPROJ < string UIWidget = "None"; >; float4x4 WORLDVIEWIT : WORLDVIEWIT < string UIWidget = "None"; >; /********** CG SHADER FUNCTIONS *********************/ // input from application struct a2v { float4 position : POSITION; float3 normal : NORMAL; }; // output to fragment program struct v2f { float4 position : POSITION; float3 normal : TEXCOORD0; }; /***** VERTEX SHADER ******************/ v2f v(a2v In, uniform float4x4 worldViewProj, // object to clip space uniform float4x4 WORLDVIEWIT ) { v2f Out; float4 N = mul(In.normal, -WORLDVIEWIT); //normal to camera space Out.position = mul(In.position, worldViewProj); //vertex to clip space Out.normal = N; return Out; } /***** FRAGMENT PROGRAM ***************/ float4 f(v2f In) : COLOR { float4 color = float4(In.normal,1); color.x = -color.x; color.y = -color.y; return color * 0.5 + 0.5; } /********** TECHNIQUES ******************************/ technique Complete { pass envPass { VertexShader = compile vs_1_1 v(wvp,WORLDVIEWIT); ZEnable = true; ZWriteEnable = true; CullMode = CW; PixelShader = compile ps_2_0 f(); } }