I need a help I created my Normalmapping Shader so GPU need to get from vertex tangent and binormal
but i also need 2TCoords to correct texturing of my model (ITerrainSceneNode)
HLSL Shader Code
Vertex:
Code: Select all
uniform float4x4 WorldViewProj;
uniform float3 LightPosition;
struct VS_INPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
float3 normal : NORMAL;
};
struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
float3 normal : TEXCOORD2;
float4 Position2 : TEXCOORD3;
float3 lightDir : TEXCOORD4;
};
VS_OUTPUT main(in VS_INPUT Input)
{
VS_OUTPUT Output;
float4 light = float4(LightPosition,1);
float3 lightDir = normalize(light.xyz - (Input.Position * light.w));
Output.Position = mul( Input.Position, WorldViewProj );
Output.alphamap = Input.alphamap;
Output.tex = Input.tex;
Output.normal =Input.normal;
Output.Position2= Output.Position;
Output.lightDir = lightDir;
}
Code: Select all
uniform float4x4 World;
float3 DiffuseLightDirection = float3(1, 1, 1);
float4 DiffuseColor = float4(1, 1, 1, 1);
float DiffuseIntensity = 1.0;
float Shininess = 200;
float4 SpecularColor = float4(1, 1, 1, 1);
float SpecularIntensity = 1;
float3 ViewVector = float3(1, 0, 0);
sampler2D splatMap = sampler_state
{
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler2D layer_red = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler2D layer_green = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler2D layer_blue = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler2D layer_Normal_red = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler2D layer_Normal_green = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler2D layer_Normal_blue = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
float3 normal : TEXCOORD2;
float4 Position2 : TEXCOORD3;
float3 lightDir : TEXCOORD4;
};
struct PS_OUTPUT
{
float4 diffuse : COLOR0;
};
float3x3 generateTBN(float3 vertexPosition, float2 texCoord, float3 normal)
{
float3 p_dx = ddx(vertexPosition);
float3 p_dy = ddy(vertexPosition);
float2 tc_dx = ddx(texCoord);
float2 tc_dy = ddy(texCoord);
float3 t = normalize( tc_dy.y * p_dx - tc_dx.y * p_dy );
float3 b = normalize( tc_dy.x * p_dx - tc_dx.x * p_dy );
float3 n = normalize(normal);
float3 x = cross(b, t);
t = cross(x, n);
t = normalize(t);
b = cross( t, n );
b = normalize(b);
return float3x3(t, b, n);
}
PS_OUTPUT main(in VS_OUTPUT input)
{
PS_OUTPUT output = (PS_OUTPUT)0;
float3x3 TBN = generateTBN(input.Position2,input.alphamap,input.normal);
float3 LightDir = mul(TBN, input.lightDir);
float4 splat = tex2D(splatMap, input.alphamap);
float4 red = tex2D(layer_red, input.tex);
float4 green = tex2D(layer_green, input.tex);
float4 blue = tex2D(layer_blue, input.tex);
float4 bump = float4(splat.r*tex2D(layer_Normal_red, input.tex)+splat.g*tex2D(layer_Normal_green, input.tex)+splat.b*tex2D(layer_Normal_blue, input.tex))*float4(1,1,1,splat.a);
float4 textureColor = float4(splat.r*red+splat.g*green+splat.b*blue)*float4(1,1,1,splat.a);
float a = textureColor.a;
output.diffuse = textureColor * dot(bump, LightDir);
output.diffuse.a=a;
return output;
}
http://irrlicht.sourceforge.net/forum/v ... hp?t=38676
In pixel shader I can calculate tangent and binormal, but they arn't interpolated so this don't look good
Lightning
http://zapodaj.net/1a672fd1fdad5.jpg.html
Tanget
http://zapodaj.net/59927d85816bb.jpg.html
Binormal
http://zapodaj.net/7971fcfb18208.jpg.html
Is there any way to calculate them in vertex shade,
if not, can somebody tell me how to tell engine about additional fields in S3DVertex2TCoords/S3DVertexTangent