• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
pachesantiago

Strange normal mapping problem

9 posts in this topic

Hy guys, having a little problem here, wich i think is self explanatory:
[URL=http://imgur.com/gNPmb][IMG]http://i.imgur.com/gNPmb.jpg[/IMG][/URL]
[URL=http://imgur.com/yuw46][IMG]http://i.imgur.com/yuw46.jpg[/IMG][/URL]

Im trying to implement a simple normal mapping shader, wich I allready tryed in a program i use to test shaders ( DarkShader) but when i loaded it into my engine, that happend. Got that strange black parts.
This is the shader in DarkShader
[URL=http://imgur.com/WB1Gn][IMG]http://i.imgur.com/WB1Gn.jpg[/IMG][/URL]

[CODE]
string Description = "This shader uses preview lights to produce per pixel normal mapped lighting. This shader requires lights.";
string Thumbnail = "Normal Mapping.png";
//The Extra Data map, contains :
// Red : Seams
// Blue : Specular Power
// Green : Specular Ammount
// The seams data is used to multiply the specs inverse, so the seams arent visible
//--------------
//Untweakables
//--------------
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4x4 WorldT : WorldTranspose;
float4x4 WorldIT : WorldInverseTranspose;
float4 LightPos[8] : LIGHTPOSITION;
float3 LightColor[8] : LIGHTCOLOR;
float3 AmbientColor : AMBIENTCOLOR;
float3 eyepos : CameraPosition;
float time : TIME;
int L_Count : LightCount;
//--------------
//Tweakables
//--------------
int TileRes = 512;
float detailScale
<
string UIWidget = "slider";
float UIMax = 16.0;
float UIMin = 0.1;
float UIStep = 0.01;
> = 5.0f;
float SpecularPower1
<
string UIWidget = "slider";
float UIMax = 15;
float UIMin = 0.001;
float UIStep = 0.0001;
> = 3;
float DetailInfluence
<
string UIWidget = "slider";
float UIMax = 1;
float UIMin = 0.001;
float UIStep = 0.0001;
> = 0.24;
/*
float SeamCorrection
<
string UIWidget = "slider";
float UIMax = 10;
float UIMin = 1;
float UIStep = 0.0001;
> = 2;*/
texture ExtraData
<
string ResourceName = "";
>;
sampler ExtraData_smp = sampler_state
{
Texture = <ExtraData>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
texture BaseTex
<
string ResourceName = "";
>;
sampler diffuse_smp = sampler_state
{
Texture = <BaseTex>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
texture NormalMap
<
string ResourceName = "";
>;
sampler normalmap_smp = sampler_state
{
Texture = <NormalMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
texture NormalMapTile
<
string ResourceName = "";
>;
sampler normalmapTile_smp = sampler_state
{
Texture = <NormalMapTile>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

struct app_in
{
float4 pos : POSITION;
float3 normal : NORMAL0;
float3 tangent : TANGENT0;
float3 binormal : BINORMAL0;
float2 uv : TEXCOORD0;
};
struct vs_out
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 tangent : TEXCOORD2;
float3 binormal : TEXCOORD3;
float4 wpos : TEXCOORD4;
float2 uvTile : TEXCOORD5;
};
vs_out VS( app_in IN )
{
vs_out OUT;

float4 pos = mul( IN.pos, WorldViewProj );
OUT.pos = pos;
OUT.wpos = mul( IN.pos, World );
OUT.uv = IN.uv;
OUT.uvTile = IN.uv * detailScale;
float3 normal = normalize(mul(IN.normal.xyz,(float3x3)World ));
float3 tangent = normalize(mul(IN.tangent.xyz,(float3x3)World ));
float3 binormal = normalize(mul(IN.binormal.xyz,(float3x3)World ));

//smooth out the tangents and binormals with the normals
float3 b = normalize(cross( normal,tangent ));
b *= sign(dot(b,binormal));

float3 t = normalize(cross( normal,b ));
t *= sign(dot(t,tangent));

float3 t2 = normalize(cross( normal,binormal ));
t2 *= sign(dot(t2,tangent));

float3 b2 = normalize(cross( normal,t2 ));
b2 *= sign(dot(b2,binormal));

//pass normal, tangent, and binormal to pixel shader
OUT.normal = normal;
OUT.tangent = normalize((t+t2)*0.5);
OUT.binormal = normalize((b+b2)*0.5);

return OUT;
}
float luminance ( float3 rgb )
{
return rgb.r*0.3 + rgb.g*0.59 + rgb.b*0.11;
}
float3 getTexel( float2 p, uniform sampler2D texSamp,uniform int TexRes )
{
TexRes *= 8;

p = p*TexRes + 0.5;
float2 i = floor(p);
float2 f = p - i;
f = f*f*f*(f*(f*6.0-15.0)+10.0);
p = i + f;
p = (p - 0.5)/TexRes;
float3 outValue = tex2D( texSamp, p );
return outValue;
}
float4 PS( vs_out IN, uniform int numLights ) : COLOR
{
float3 color = AmbientColor;

float3 n = normalize(IN.normal);
float3 t = normalize(IN.tangent);
float3 b = normalize(IN.binormal);

//build transpose matrix
float3x3 TangentSpace = {t,b,n};
TangentSpace = transpose(TangentSpace);

n = normalize(tex2D(normalmap_smp, IN.uv)*2 - 1);
//float3 nT = normalize(tex2D(normalmapTile_smp, IN.uvTile)*2 - 1);
float3 nT = normalize(getTexel(IN.uvTile,normalmapTile_smp,TileRes));

float3 e = normalize(eyepos - IN.wpos);
float eyeDist = length(eyepos - IN.wpos);

e = mul(e,TangentSpace);

float4 texColor = tex2D( diffuse_smp, IN.uv );

//cycle through all lights, number depending on technique chosen
for ( int i = 0; i < numLights; i++ )
{
float range = LightPos[i].w;

if ( range > 0 )
{
float3 l = (LightPos[i].xyz);// - IN.wpos.xyz);
l = mul(l,TangentSpace);

//calculate attenuation
float dist = length(l);
float att = saturate((range-dist) / range);

//calculate diffuse lighting
l = normalize(l);
float diffuse = dot(n,l)* 0.5 +0.5;

//caculate specular lighting
float3 h = normalize(l+e);
float spec = pow( saturate(dot(n,h)), SpecularPower1 )*(1-pow(1-diffuse,10));

//calculate diffuse lighting
float diffuseTile = saturate(dot(n,l));

//caculate specular lighting
float specTile = pow( saturate(dot(nT,h)), SpecularPower1 )*(1-pow(1-diffuseTile,10));

float finalSpec = (specTile+(spec-DetailInfluence)/2);

// correct seams
//finalSpec *= -(tex2D(ExtraData_smp,IN.uv).r/SeamCorrection-1);

//add to final color
//color += ((diffuse * att * LightColor[i]) + finalSpec * att * LightColor[i] * smoothstep(0.3,0.6,luminance(texColor.xyz)));
color = diffuse;
}
}

return float4(color,1.0);// * texColor;
}
//choose the technique corresponding to the number of lights in your application
technique PerPixelLighting
{
pass p0
{
VertexShader = compile vs_2_0 VS( );
PixelShader = compile ps_3_0 PS( L_Count );
}
}
[/CODE]

a lot of parts are commented, because i was trying to find where the error was. Narrowed it to the N dot L i think, but the odd part is that it worked perfect in the other program.
I ran out of ideas, if someone can help me it would be great.
0

Share this post


Link to post
Share on other sites
yes, but i dont find anything strange. i mean, i dont know assembler, but i can see the pixel history, and it says that it exited the pixel shader with that color.
0

Share this post


Link to post
Share on other sites
So you see that it exited the pixel shader with the wrong color. Now you can use PIX to step through the shader code for that pixel and figure out why.
0

Share this post


Link to post
Share on other sites
I would say your per vertex tangent space vectors are messed up (as this is an input that differs from Darkshader).

But still, in your shader you transform tangent space vectors to world space, and pass them to pixel shader, thus I think you do the dot product with a worldspace normal and a world space light direction vector.

The most effective and generall way is to

Vertex shader:
transform a light direction from world to object space (inverse world), and then from object space to texture space (tangent matrix), and, transform the object normal from Object space to texture Space(tangent matrix), pass those two , tx normal and tx light to pixel shader

Pixel Shader:
just dot the two direction vectors.

Your tangent triple should transform from object space to texture space. (byt it can also transform from texture space to even view space, this is done in deffered rendering techniques). Also, if you have smooth normals on verticies, just compute the tangent and have binormal computed just as the cross product of those two in the vertex shader, you then have smooth tangent space, tghough not perpendicular in all directions (but it does not matter, you just can invert the matrix by transponing only and so on then)
0

Share this post


Link to post
Share on other sites
[quote name='JohnnyCode' timestamp='1347228401' post='4978401']
I would say your per vertex tangent space vectors are messed up (as this is an input that differs from Darkshader).

But still, in your shader you transform tangent space vectors to world space, and pass them to pixel shader, thus I think you do the dot product with a worldspace normal and a world space light direction vector.

The most effective and generall way is to

Vertex shader:
transform a light direction from world to object space (inverse world), and then from object space to texture space (tangent matrix), and, transform the object normal from Object space to texture Space(tangent matrix), pass those two , tx normal and tx light to pixel shader

Pixel Shader:
just dot the two direction vectors.

Your tangent triple should transform from object space to texture space. (byt it can also transform from texture space to even view space, this is done in deffered rendering techniques). Also, if you have smooth normals on verticies, just compute the tangent and have binormal computed just as the cross product of those two in the vertex shader, you then have smooth tangent space, tghough not perpendicular in all directions (but it does not matter, you just can invert the matrix by transponing only and so on then)
[/quote]
wow thanks. in the vertex shader, when you say the "object normals" do you mean the normals in the normal map right?
the other thing, about smooth tangent space, I do have the normals smoothed, so i can do as you said. if is not a problem, can you point me to where i can find information on computing tangents? can you also explain a bit or point to documentacion of wath do you mean with "not perpendicular in all directions"?
thanks anyway, that's probably it, if thats it, you just saved me a lot of time
0

Share this post


Link to post
Share on other sites
[quote name='pachesantiago' timestamp='1347242289' post='4978451']

wow thanks. in the vertex shader, when you say the "object normals" do you mean the normals in the normal map right?

[/quote]
no, I have ment the per vertex normal, not the ones in the texture. Vertex normal is one of the bazic vectors for the tangent triple.

So, you have normals on your verticies, and you know how to compute binormal. tangent is a direction vector which exists in object space and points to the direction of texture coordinates x axis. It is actualy the x axis of texture coordinates expressed in 3d space (object space)

this is a great resource for u:
http://answers.unity3d.com/questions/7789/calculating-tangents-vector4.html

it laso calculates the handedness of a tangent that you need to decide with wheather cross product between tangent and normal points+ or -
so tangent data is 4 float vector [x,y,z,handedness].
0

Share this post


Link to post
Share on other sites
[quote name='JohnnyCode' timestamp='1347298546' post='4978638']
[quote name='pachesantiago' timestamp='1347242289' post='4978451']
wow thanks. in the vertex shader, when you say the "object normals" do you mean the normals in the normal map right?

[/quote]
no, I have ment the per vertex normal, not the ones in the texture. Vertex normal is one of the bazic vectors for the tangent triple.

So, you have normals on your verticies, and you know how to compute binormal. tangent is a direction vector which exists in object space and points to the direction of texture coordinates x axis. It is actualy the x axis of texture coordinates expressed in 3d space (object space)

this is a great resource for u:
[url="http://answers.unity3d.com/questions/7789/calculating-tangents-vector4.html"]http://answers.unity...ts-vector4.html[/url]

it laso calculates the handedness of a tangent that you need to decide with wheather cross product between tangent and normal points+ or -
so tangent data is 4 float vector [x,y,z,handedness].
[/quote]
thanks, gonna research a bit and try it
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0