• 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

"Convert" multiple lighting shader to 'per vertex lighting'

6 posts in this topic

Hi,

I've managed to create working effect/ shader which has a flexible number of directional and point lights, based on the given scene and available shader model.

 

Next thing I want to do is go to per pixel lighting instead of per vertex.

At the moment I have the normal of the vertex available in the pixel shader, but I don't use it..

 

Can someone give me some pointers on how to go to work to achieve per pixel lighting in my effect?

 

The code:

VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

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

	float4 normal = mul(input.Normal, WorldInvTransp);

	Out.Normal = normal;
	Out.TexCoord = input.TexCoord;
	Out.wPos = worldPosition;

//	DIRECTIONAL LIGHT
	float dirIntensity[MaxDirectionalLights];
	float dirTotal = 0.0f;

	for(int i=0;i<MaxDirectionalLights;i++)
	{	
		dirIntensity[i] 	= dot(normal, DirLightDir[i]);
		dirTotal 		+= saturate(DirLightCol[i] * DirLightInt[i] * dirIntensity[i]);
	}

	Out.Color = dirTotal;
	return Out;
}

float4 PS_function(VS_OUTPUT input): COLOR0
{
	float4 textureColor = tex2D(textureSampler, input.TexCoord);
	float4 amb = AmbientColor * AmbientIntensity * MatAmb;
	float4 diff = input.Color * MatDiff;

	float distt[MaxPointLights];
	float att[MaxPointLights];

	float4 att_total = 0.0f;
	float4 attcolored;

	for(int i=0;i<MaxPointLights;i++)
	{
		distt[i] 	= distance(PointLightPos[i], input.wPos);
		att[i]	= 1 - saturate((distt[i] - PointLightFPRange[i]) / PointLightRange[i]);
		att[i]	= (pow(att[i], 2)) * PointLightInt[i];
		
		attcolored	= att[i] * PointLightCol[i];
		att_total	+= attcolored;
	}

	return saturate((diff + amb + att_total) * textureColor);
}

I've read several tutorials on per pixel lighting, but up till now can't manage to implement it in my existing effect/shader.

Here are two screenshots of the current result, which by the way looks ok even on low poly (the ground):

 

pointlight.jpg

 

pointlight-wireframe.jpg

Edited by cozzie
0

Share this post


Link to post
Share on other sites

If you look closely at that image it appears that the right hand end of that wall is facing away from the light and should therefore not be lit by it.

 

The standard way to handle that is to multiply the light intensity by N dot L: saturate(dot(normalize(light_position - pixel_position), normalize(normal)))

 

If you want a specular highlight too take a look at: http://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model

0

Share this post


Link to post
Share on other sites

I checked if I pass all the shader constants, which is the case

(ambientcolor, ambientintensity, matamb, matdiff).

 

The right wall is lit up by another lightsource, in this case directional.

I've disabled it, here's the result of only the point light (with a little ambient light to see the effect):

 

pointlight2.jpg

 

pointlight2-wireframe.jpg

 

@Adam; thanks, I'll play around with multiplying light intensity by N dot L and see what it does.

0

Share this post


Link to post
Share on other sites

It works and shows a clear difference, clearly visible on the corners where normals are/ aren't applied.

Not sure though which looks better or more realistic.

 

pointwithoutnormal.jpg

 

pointwithnormal.jpg

 

First impression is that with normals is more realistic since the corners of the building 'break' the light.

What do you think?

 

I actually only changed the pixel shader for the point light calculations, not the directional lighting calculation in the vertex shader:

 

	float4 perpixel;

	for(int i=0;i<MaxPointLights;i++)
	{
		distt[i] 	= distance(PointLightPos[i], input.wPos);
		att[i]	= 1 - saturate((distt[i] - PointLightFPRange[i]) / PointLightRange[i]);
		att[i]	= (pow(att[i], 2)) * PointLightInt[i];
		
		attcolored	= att[i] * PointLightCol[i];

		perpixel	= saturate(dot(normalize(PointLightPos[i] - input.wPos), normalize(input.Normal)));
		att_total	+= (attcolored * perpixel);
	}

Edited by cozzie
0

Share this post


Link to post
Share on other sites

That appears correct. The lighted area of the point light is barely grazing the side wall of the building, and with the light attenuation together with the wall normals, the wall is receiving very little light. The normals reduce it a lot here because its resulting dot product is very low.

0

Share this post


Link to post
Share on other sites

thanks, on the 'non-fact' side it also just looks better.

 

by accident I did notice though, when rendering wireframes, I have some culling improvement possibilities :)

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