Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Some normal mapping questions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 cozzie   Members   -  Reputation: 1758

Like
0Likes
Like

Posted 29 January 2014 - 12:50 PM

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

Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 4092

Like
0Likes
Like

Posted 29 January 2014 - 01:38 PM


- 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/



#3 cozzie   Members   -  Reputation: 1758

Like
0Likes
Like

Posted 29 January 2014 - 03:52 PM

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?

#4 phil_t   Crossbones+   -  Reputation: 4092

Like
0Likes
Like

Posted 29 January 2014 - 04:09 PM


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.



#5 cozzie   Members   -  Reputation: 1758

Like
0Likes
Like

Posted 29 January 2014 - 05:34 PM

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, 29 January 2014 - 05:34 PM.


#6 cozzie   Members   -  Reputation: 1758

Like
0Likes
Like

Posted 29 January 2014 - 05:47 PM

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



#7 phil_t   Crossbones+   -  Reputation: 4092

Like
0Likes
Like

Posted 29 January 2014 - 07:43 PM

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.



#8 cozzie   Members   -  Reputation: 1758

Like
0Likes
Like

Posted 30 January 2014 - 02:32 PM

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();
	}
}



#9 cozzie   Members   -  Reputation: 1758

Like
0Likes
Like

Posted 30 January 2014 - 04:56 PM

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, 30 January 2014 - 04:57 PM.


#10 phil_t   Crossbones+   -  Reputation: 4092

Like
0Likes
Like

Posted 30 January 2014 - 11:42 PM


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.



#11 phil_t   Crossbones+   -  Reputation: 4092

Like
0Likes
Like

Posted 30 January 2014 - 11:55 PM


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)..



#12 cozzie   Members   -  Reputation: 1758

Like
0Likes
Like

Posted 31 January 2014 - 09:32 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS