• 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
cozzie

Some normal mapping questions

11 posts in this topic

Hi,
I've just implemented base normal mapping in my 3d engine. Although the basics are ok, I want to make some changes to make it look better. A few questions on this:

- objects only lit by ambient are obviously not affected by the normal map, because there's no (not yet) normal vector used in the equation. What are efficient ways to somehow do this with acceptable 80/20 results? My first thought was to take the view vector as ambient light direction (non positional) but that would only help for faces facing the camera. Or is the solution for this (similar to) ambient occlusion?
If you know any articles, always welcome

- specular highlights look "pixelized" and show artifacts on the lit textures (lines etc). Do I need to do other changes to my specular calculation, other then replacing the normal by the normalmap normal?

Thanks in advance for helping out
0

Share this post


Link to post
Share on other sites


- objects only lit by ambient are obviously not affected by the normal map, because there's no (not yet) normal vector used in the equation. What are efficient ways to somehow do this with acceptable 80/20 results? My first thought was to take the view vector as ambient light direction (non positional) but that would only help for faces facing the camera. Or is the solution for this (similar to) ambient occlusion?

 

You could use a hemispherical ambient light (a sky ambient color and a ground ambient color, and your per-pixel normals control the balance between each).

 

See here for an example: http://digitalerr0r.wordpress.com/2009/05/09/xna-shader-programming-tutorial-19-hemispheric-ambient-light/

0

Share this post


Link to post
Share on other sites
Thanks philT, the article looks clear and I understand the principle. Going to implement that for sure.

Any idea/ suggestions on the 2nd question regarding specular?
0

Share this post


Link to post
Share on other sites


Any idea/ suggestions on the 2nd question regarding specular?

 

Does your normal map have mipmaps? I suspect you might get a lot of specular "noise" if they don't.

0

Share this post


Link to post
Share on other sites

I did some tests and debugging, but it looks like there's indeed something with the mipmapping.

Below 2 screenshots one without normal mapping and one with normal mapping. Mipmapping works fine without normal mapping, but with normalmapping close up it looks bad, but further away it looks OK.

 

mip_nonormalmap.jpg

 

mip_normalmap.jpg

 

This is the 'mipmapping effect' with normal mapping:

 

mip_distantnormalmap.jpg

 

I now set the filtering of the texturesampler in my code (statemachine), assuming it goes for all textures/ materials.

I'll double check this. The normalmap texture is loaded with exact the same parameters, including mipmaps, as the diffuse textures.

Edited by cozzie
0

Share this post


Link to post
Share on other sites

Got it, I was only setting mip/min/mag filters for samplerstage 0, the normal map is in stage 1.

It actually looks amazing :)

 

The only thing I have left are some strange artifacts with specular highlight on the normalmap.

I mean the lines you can see in the middle of the screenshot below:

 

normalmap_spec_artefact.jpg

0

Share this post


Link to post
Share on other sites

I'm not sure which artifacts you're talking about. I see a pair of thin white lines just right of the top center of your pic. Not sure what those are. It also looks like your textures don't tile properly (obvious discontinuities in the bottom right).

 

Regarding your specular highlights it's hard to tell if anything's wrong without seeing what your actual normal map looks like. If I look at the full size version of your pic I can see the specular highlights are very noisy. Might want to play with your specular power value. Or maybe you've got a NaN in your shader or something.

 

Oh ok... in the large version of the pic I can see a bunch of slight lines going in slight different directions. They begin at the above/right of your texture discontinuity. Very weird. I think in order for someone to help you we'll need to see your shader code and your normal map.

0

Share this post


Link to post
Share on other sites

Hi Phil.

I've implemented the hemisphere ambient lighting, and it works great.

The only thing I did different from the article you mentioned, that I calculate the dot product of the SkyDirection and the normalMap normal and use that right away, instead of multiplying it with 0.5 and then add 0.5. Not sure what that 'hack' does/ bring me.

 

Below the normal map and the shader code. I've just implemented normal mapping basics, so at the moment 1 normal map for all textures/meshes. I've did some extra testing, and the strange lines artifact only occurs at specular highlights.

 

http://www.sierracosworth.nl/gamedev/_normalmap.png

 

I've pasted the whole shader, just to be sure.

/** IN PROGRESS - NORMAL MAPPING, 2nd SAMPLER **/

/*************************************************************/
/**	CR GENERIC SHADER		1 DIRECTIONAL, 8 POINT LIGHTS **/
/**					PER PIXEL LIGHTING		**/
/**					AMBIENT, DIFFUSE, SPECULAR 	**/
/**	TECHNIQUES:			SINGLE TEXTURE, LINEAR FOG	**/
/**	SHADER MODEL:		3.0,NO BWARDS COMPATIBILITY	**/
/*************************************************************/
	
/*******************************************************/
/**	UNIFORM INPUT, CONTROLLED BY ENGINE			**/
/**  TRANSFORMATIONS & MATERIALS				**/
/*******************************************************/

uniform extern float4x4			World			: WORLD;
uniform extern shared float4x4	ViewProj		: VIEWPROJECTION;
uniform extern shared float3		CameraPos		: CAMERAPOS;

uniform extern float3	AmbColIntSky	: AMB_COL_INT_SKY;
uniform extern float3	AmbColIntGround	: AMB_COL_INT_GROUND;
           
uniform extern float4	MatDiff		: MATERIAL_DIFFUSE;
uniform extern float4	MatSpec		: MATERIAL_SPECULAR = {0.0f, 0.0f, 0.0f, 0.0f };
uniform extern float4	MatEmi		: MATERIAL_EMISSIVE;
uniform extern float	MatSpecPower	: MATERIAL_POWER;

uniform extern float 	fogStart		: FOG_START;
uniform extern float 	fogEnd		: FOG_END;
uniform extern float4 	fogColor		: FOG_COLOR;
                            
uniform extern texture	Tex0			: TEXTURE0 < string name = "roadblock texture.tga"; >;
extern texture		NormalMap 		: NORMALMAP;

float3 			SkyDirection = float3(0.0f, 1.0f, 0.0f);

// modelfile, just for effectedit
string XFile 	= "roadblock.x";

/*******************************************************/
/**	UNIFORM INPUT, CONTROLLED BY ENGINE			**/
/**  LIGHT SOURCES AND PROPERTIES				**/
/*******************************************************/
 
#define MaxDirectionalLights 1
       
extern float3 		DirLightDir[MaxDirectionalLights];
extern float3		DirLightColInt[MaxDirectionalLights];			

#define MaxPointLights 8

extern float3		PointLightPos[MaxPointLights];
extern float2		PointLightRange[MaxPointLights];	// x = range, y = FP range
extern float3		PointLightColInt[MaxPointLights];				

/*******************************************************/
/**	SAMPLER STATES FOR TEXTURING				**/
/*******************************************************/

sampler2D textureSampler = sampler_state
{
	Texture		= (Tex0);
};

sampler2D normalMapSampler = sampler_state
{
	Texture		= (NormalMap);
};

/*******************************************************/
/**	VERTEX SHADER INPUT <= VERTEX DECLARATION		**/
/*******************************************************/

struct VS_INPUT
{
	float4 Pos		: POSITION0;
	float3 Normal	: NORMAL0;
	float2 TexCoord	: TEXCOORD0;
	float3 Binormal	: BINORMAL0;
	float3 Tangent	: TANGENT0;
};

/*******************************************************/
/**	VERTEX SHADER OUTPUT - PIXEL SHADER INPUT       **/
/*******************************************************/

struct VS_OUTPUT
{
	float4 	Pos			: POSITION0;
	float3 	Normal		: TEXCOORD1;
	float2 	TexCoord		: TEXCOORD2;
	float3 	wPos			: TEXCOORD3;
	float3 	ViewDir		: TEXCOORD4;
	float  	FogFactor		: TEXCOORD5;
	float3x3	WorldToTangent	: TEXCOORD6;
};

/*******************************************************/
/**	VERTEX SHADER PROGRAM					**/
/*******************************************************/
                    
VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	float4 worldPosition = mul(input.Pos, World);
	Out.Pos = mul(worldPosition, ViewProj);

	Out.Normal = mul(input.Normal, (float3x3)World);	// VS input = already normalized
	Out.TexCoord = input.TexCoord;
	Out.wPos = worldPosition.xyz;
	Out.ViewDir = normalize(CameraPos - Out.wPos);

	// Worldspace to Tangent space for normalMapping
	Out.WorldToTangent[0] = mul(normalize(input.Tangent), World);
	Out.WorldToTangent[1] = mul(normalize(input.Binormal), World);
	Out.WorldToTangent[2] = mul(normalize(input.Normal), World);

	// FOG
	if(fogEnd != 0)
	{
		float dist = distance(CameraPos, worldPosition);
		
		Out.FogFactor = saturate((dist - fogStart) / (fogEnd - fogStart));		
	} else Out.FogFactor = 0;

	return Out;
}

/*******************************************************/
/**	PIXEL SHADER PROGRAM					**/
/*******************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
	float4 textureColor = tex2D(textureSampler, input.TexCoord);

	float3 normalMap = normalize(2.0f * tex2D(normalMapSampler, input.TexCoord).xyz - 1.0f);
	normalMap = mul(normalMap, input.WorldToTangent);

//	float3 normalMap = normalize(input.Normal);

	float3 diffuseAcc = 0.0f;
	float3 specularAcc = 0.0f;

/** 	DIRECTIONAL LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/
	for(int i=0;i<MaxDirectionalLights;i++)
	{	
		diffuseAcc	= saturate(DirLightColInt[i] * dot(normalMap,  DirLightDir[i]) + diffuseAcc); 

		if(any(MatSpec))
		{
			float3 lightdir = normalize(DirLightDir[i] - input.wPos);
			float3 h = normalize(lightdir + input.ViewDir);
			
			specularAcc = pow(saturate(dot(h, normalMap)), MatSpecPower) * DirLightColInt[i] + specularAcc;
		}
	}

/** 	POINT LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/

	for(int i=0;i<MaxPointLights;++i)
	{
		float3 lightDir = (PointLightPos[i] - input.wPos);

		// PER PIXEL ATTENUATION
		float dist = length(lightDir);
		lightDir = lightDir / (dist + 0.001);

		float att = saturate(1 - ((dist - PointLightRange[i].y) / (PointLightRange[i].x - PointLightRange[i].y)));
		att *= att;	// optional, not correct for full power range !?!?

		// DIFFUSE

		float diffIntPoint = saturate(dot(normalMap, lightDir) * att);			
		diffuseAcc = diffIntPoint * PointLightColInt[i] + diffuseAcc;

		// SPECULAR; USING BLINN HALF ANGLE

		if(any(MatSpec))
		{
			float3 h = normalize(lightDir + input.ViewDir);
			specularAcc = pow(saturate(dot(h, normalMap)), MatSpecPower) * att * PointLightColInt[i] + specularAcc;
		}
	}

/**	AMBIENT HEMISPHERE **/
	float vecHemi = dot(normalMap, SkyDirection);
	float3 hemiAmb = lerp(AmbColIntGround, AmbColIntSky, vecHemi);

/**	FINAL PIXEL COLOR **/
	float3 cd = (MatDiff * diffuseAcc + hemiAmb); 
	float3 ci = (MatSpec * specularAcc + MatEmi);
	float3 final = (textureColor.rgb * (1 - input.FogFactor) + fogColor * input.FogFactor) * cd + ci; 
	return float4(final, textureColor.a);
}

/*******************************************************/
/**	TECHNIQUES & PASSES					**/
/*******************************************************/

technique SingleTextureTech
{
	pass P0
	{
		VertexShader = compile vs_3_0 VS_function();
		PixelShader = compile ps_3_0 PS_function();
	}
}

0

Share this post


Link to post
Share on other sites

Update;

I've tested with another normal map (http://www.sierracosworth.nl/gamedev/_normalmap2.png) and the artifacts are practically gone.

Not sure though why it happens with the 1st one.

 

Here's also the code of my specular power calculation (code, not hsl).

If you see anything strange/ to be improved..

		mMaterials[mc].power = mMaterials[mc].d3dMaterial.Power;

		/**  3DS MAX specular power (specular level & shininess) adaptation **/
		/**																	**/
		/**  Specular level controls specular color intensity				**/
		/**		100 with full RGB = Specular( 1.00, 1.00, 1.00, 1)			**/
		/**		  1 with full RGB = Specular( 0.01, 0.01, 0.01, 1)			**/
		/**																	**/
		/**  Glossiness controls specular power								**/
		/**		KW-XPORT settings:
		/**		3ds max   0 = 0.0 power			(0.0 in PS)					**/
		/**		3ds max  50 = 50 power			(0.5 in PS)					**/
		/**		3ds max 100 = 100 power			(1.0 in PS)					**/
		mMaterials[mc].power /= 100.0f;
		
		/**     PANDASOFT settings:
		/**		3ds max 100	= 32 Power			(1.0 in PS)					**/
		/**		3ds max   1	= 0.32 Power		(0.0 in PS)					**/
		//mMaterials[mc].power /= 32.0f;	

		/** Converting specular power, moved from shader to code, save cycles	**/
		mMaterials[mc].power = pow(2.0f, mMaterials[mc].power * 13.0f);
Edited by cozzie
0

Share this post


Link to post
Share on other sites


SkyDirection and the normalMap normal and use that right away, instead of multiplying it with 0.5 and then add 0.5. Not sure what that 'hack' does/ bring me.

 

The dot product produces a value between [-1,1]. That will scale/offset it so it's between [0,1], which is needed if you're lerping between two colors.

0

Share this post


Link to post
Share on other sites


I've tested with another normal map (http://www.sierracosworth.nl/gamedev/_normalmap2.png) and the artifacts are practically gone.

 

Your first normal map is extremely noisy. Looks like it's been converted to a palettized gif and back or something. Not to mention those strange lines are also there (very obviously if you load it into an image editor and up the contrast)..

0

Share this post


Link to post
Share on other sites
Thanks, I wont focus on the artifacts then. Better spend my time on finding out how to make good normal maps based on existing textures. Any suggestions/ pointers?

Clear regarding the hemisphere ambient, I'll have to dig into the "lerp" theory, the result without *0.5 + 0.5 actually isn't what I expect/ look for, compared to without 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