Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D rendering with per-pixel lighting/normal map - directX

I'm looking for the simplest, easiest and fastest technique to render 2D textured quads with per-pixel normals. what i mean is a 3D world where the camera is fixed and all the texture quads are facing to the same direction (the camera), and between them there will be light points, and i want the textures to have per-pixel normal values so they'll lit as if they were 3D models.

most techniques i found on the net are way too complicated and heavy cause they refer to real 3D models, while all i need is simple 2d quads.

does anyone knows an appropriate technique or can post a useful tutorial? oh, and DX only pls.

thanks.

like image 748
rubi Avatar asked Mar 08 '26 00:03

rubi


1 Answers

I'm more experienced with XNA rather than directX, but the principle is the same.

You need a normal map, this tells your shader exactly what the surface normal is at a given point. You then need your quads to be texture mapped (they probably already are, so this isn't really any extra work). Then you draw the quads with a pixel shader, inside the shader you do something like:

//This shader only handles one directional light, there are various tecnhiques for handling multiple lights.
float3 LightDirection;

//Normal map, set this before rendering the quad
Texture NormalMap;
//sampler
sampler normalSampler = sampler_state
{
  texture = <NormalMap>;
}

//Diffusemap, set this before rendering the quad. This is just your normal texture you want applied to the quad
Texture DiffuseMap;
//sampler
sampler diffuseSampler = sampler_state
{
  texture = <DiffuseMap>;
}

/* you probably need a vertex shader to run all your translations etc, that's pretty bog standard stuff so I won't include one here */

float4 PixelShader(float2 texCoord : TEXCOORD0) : COLOR0
{
  //standard directional lighting equation with normals
  float3 Normal = tex2D(normalSampler, texCoord);
  float dot = dot(LightDirection, Normal);
  return tex2D(normalSampler, texCoord) * dot;
}
like image 124
Martin Avatar answered Mar 10 '26 14:03

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!