@Catalyst:
Well, here we go - a simple normal map example. It's fairly basic and works off a single point light, without falloff or colour, but it gets the job done, right?
What we've got is the shader itself, a preview picture and, if that's not enough, a small sample program for DB, with included media:
// Normalmap Shader - 1.0
// by Ninja Matt
//------------------------------------
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : WorldIT;
float4x4 ViewPos : ViewIT;
//------------------------------------
float4 LightPos : Position
<
string Object = "PointLight";
string Space = "World";
> = {1.0f, -1.0f, 1.0f, 0.0f};
texture TexMap
<
string name = "Fieldstone.tga";
>;
texture NormMap
<
string name = "FieldstoneNormal.tga";
>;
//------------------------------------
technique Bumpmap
{
pass p0
{
VertexShaderConstant[0] = <WorldViewProj>;
VertexShaderConstant[4] = <World>;
VertexShaderConstant[8] = <ViewPos>;
VertexShaderConstant[12] = <LightPos>;
VertexShader = asm
{
// Initilise Shader
vs.1.1
dcl_position v0
dcl_normal v1
dcl_texcoord v2
dcl_tangent v3
dcl_binormal v4
// Scale and Bias Value
def c20, 1.0f, 0.5f, 0.0f, 0.0f
// Transform Position to Clip
m4x4 oPos, v0, c0
// Output Texture Coordinates
mov oT0, v2
mov oT1, v2
// Move Light to Object Space
mov r1, c12
m4x4 r0, r1, c4
// Find Light Vector: L
add r0, r0, -v0
// Normalise Light Vector: L
dp3 r1.w, r0, r0
rsq r1.w, r1.w
mul r0, r0, r1.w
// Move Light Vector to Texture Space
dp3 r1.x, r0, v3
dp3 r1.y, r0, v4
dp3 r1.z, r0, v1
// Scale and Bias
add r1, r1, c20.x
mul r1, r1, c20.y
// Output Light Vector
mov oT2, r1
};
PixelShader = asm
{
// Initialise Shader
ps.1.4
// Read Texture Data
texld r0, t0
texld r1, t1
// Get Per-Pixel Light Vector
texcrd r2.xyz, t2
// DP3 Light Vector with Surface Normal
dp3_sat r3, r1_bx2, r2_bx2
// Multiply Diffuse with Lighting
mul r0, r0, r3
};
Texture[0] = <TexMap>;
MinFilter[0] = Linear;
MagFilter[0] = Linear;
MipFilter[0] = Linear;
Texture[1] = <NormMap>;
MinFilter[1] = Linear;
MagFilter[1] = Linear;
MipFilter[1] = Linear;
}
}
http://mysite.wanadoo-members.co.uk/wisemonkeys/darkbasic/NormalMap10.zip
@Zeal:
I've got a bit of a terrain shader for you now! It's not quite as good as I was hoping, as I couldn't get the fade effect you wanted on the detail maps. They worked fine in FXComposer but, for some reason, wouldn't show up in DB. I'll keep working at it, but this should be enough for you to get an idea of what to expect from future versions:
// Terrain Shader for Zeal - 1.0
// by Ninja Matt
//------------------------------------
float4x4 WorldViewProj : WorldViewProjection;
float4x4 ViewPos : ViewIT;
//------------------------------------
texture BaseMap
<
string name = "TerrainBase.tga";
>;
texture BlendMap
<
string name = "TerrainBlend.tga";
>;
texture DetailMap
<
string name = "TerrainDetail.tga";
>;
//------------------------------------
technique Terrain
{
pass p0
{
VertexShaderConstant[0] = <WorldViewProj>;
VertexShaderConstant[4] = <ViewPos>;
VertexShader = asm
{
// Initilise Shader
vs.1.1
dcl_position v0
dcl_normal v1
dcl_texcoord v2
// TWEAKABLE - Detail Texture Scale
def c20, 4.0f, 4.0f, 4.0f, 4.0f
// TWEAKABLE - Detail Texture Strength
def c21, 0.5f, 0.5f, 0.5f, 0.5f
// Transform Position to Clip
m4x4 oPos, v0, c0
// Base and Blend Map Tex Coords
mov oT0, v2
mov oT1, v2
// Scale and Output Detail Map Coords
mul oT2, v2, c20
mov oT3, c21
};
PixelShader = asm
{
// Initialise Shader
ps.1.4
// Load Textures
texld r0, t0
texld r1, t1
texld r2, t2
// Load Detail Fade
texcrd r3.rgb, t3
// 'Fix' the Texcrd Function
mov_sat r3.rgb, r3
+mov_sat r3.a, r3.r
// Multiply Detail Map by Fade and Strength
mul r2, r2_bias, r3
// Multiply Blend by Detail for each Channel
mad r0.rgb, r1.r, r2.r, r0
mad r0.rgb, r1.g, r2.g, r0
mad r0.rgb, r1.b, r2.b, r0
mad r0.rgb, r1.a, r2.a, r0
};
Texture[0] = <BaseMap>;
MinFilter[0] = Linear;
MagFilter[0] = Linear;
MipFilter[0] = Linear;
Texture[1] = <BlendMap>;
MinFilter[1] = Linear;
MagFilter[1] = Linear;
MipFilter[1] = Linear;
Texture[2] = <DetailMap>;
MinFilter[2] = Linear;
MagFilter[2] = Linear;
MipFilter[2] = Linear;
}
}
http://mysite.wanadoo-members.co.uk/wisemonkeys/darkbasic/Terrain10.zip
Also, if we can get something arranged, I'm quite in favour of a DB shader repository. I know just how hard it is to get samples!
As I get round to it, I'll start tidying up and posting more of my shaders. They aren't all great, but I'm sure there's people out there that would appreciate the little touch that they can add. Also, if Neo doesn't mind me hijacking his thread even more, I could even try my hand at a couple of tutorials aimed more towards learning how to program your own shaders; after all, it was only about two or three months ago that I started making them!
Edit:
Sorry, I forgot to explain how the terrain shader works, Zeal! I've got with your first idea, and so I've just done standard detail mapping - no bumpmapping or anything yet! Anyway, texture 0 is the main colour map, texture 1 the blend map, and texture 2 the detail map. The blend map and detail map use all four channels to squash data in, so you could use the red channel for a rocky terrain, the green channel for grassy, etc. Additionally, the detail map doesn't just darken the base colour, it lightens it too. Effectively, the colour value is just biased so, rather than falling into the usual 0 to 1 range, it falls under a -0.5 to 0.5 range. When you come to make the detail maps, just remember that 50% grey will not affect the base colour, while anything darker will darken it and anything lighter will lighten it. I thought you might be able to get some nicer effects if it was done this way.
There's also a couple of constants defined within the vertex shader that you might want to play around with. One is the strength of the detail mapping, so you can make it more or less prominent, and the other is an in-shader alteration to the detail map coordinates. This is actually there because I tested the shader on a plain with only one set of texture coordinates, but I still wanted to see the tiled pattern! In fact, because the shader only reads in one set of texture coordinates, this is the only way to get a tiled pattern! That's something for me to fix in the next version...
Hope you like it, anyway!