?
Solved

XNA 4.0 Enviroment Lighting / Shadowing

Posted on 2011-09-09
5
Medium Priority
?
1,112 Views
Last Modified: 2013-12-26
I have been working on a game as my first project with XNA 4.0. I am wanting to add a main light source (The Sun), that casts shadows on my objects. Most of the tutorials I see out there, are for 3d Models. However, I built my game on TriangleStrips to build cubes.  Below you can see the play dug a single cube.
 first.jpg

And here the player dug deep down:
 second.jpg
As you can see, theres no lighting or shading at all.  I want to be able to make it darker as you dig, making it realistic.

And here, the player places a couple of blocks that I want to shade the bottoms of the extended cubes, and cast a shadow on the cubes below.
 third.jpg


I've spent the good part of the day learning the origins of HLSL, how it works, and created my own Effect file, and have completely removed the usage of the BasicEffect. (I couldn't get the pre-built in lighting features of the BasicEffect to do what I wanted). Can someone help me out, or point me into the right direction? I have googled, and the examples I've found were either for 3d models, or for a prior version of XNA, or the code was for OpenGL. :/ Thanks in Advance


0
Comment
Question by:Iamtehbest
  • 3
  • 2
5 Comments
 

Author Comment

by:Iamtehbest
ID: 36515184
And here's a copy of my Effect:

float4x4 xWorldViewProjection;
Texture xColoredTexture;



sampler ColoredTextureSampler = sampler_state 
{ texture = <xColoredTexture> ; 
  magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; 
  AddressU = mirror; 
  AddressV = mirror;};

struct VertexIn
{
	float4 position : POSITION;
	float2 textureCoordinates : TEXCOORD0;
};

struct VertexOut
{
	float4 Position : POSITION;
	float2 textureCoordinates : TEXCOORD0;
};

VertexOut VertexShaderFunction(VertexIn input)
{
	VertexOut Output = (VertexOut)0;
	
	Output.Position =mul(input.position, xWorldViewProjection);
	Output.textureCoordinates = input.textureCoordinates;

	return Output;
}

float4 PixelShaderFunction(VertexOut input) : COLOR0
{
	float4 output = tex2D(ColoredTextureSampler, input.textureCoordinates);

	return output;
}

technique Textured
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

Open in new window

0
 
LVL 12

Expert Comment

by:satsumo
ID: 37091605
Are you still wanting an answer to this question?  I would have answered before, the site didn't show this in the area I look for questions although its in one of the branches from that area.
0
 

Author Comment

by:Iamtehbest
ID: 37116148
Absolutely.  I did manage to do some shadowing, as far as if you dig deep.  But in Image #3, the grassy area would not cast a shadow.  Any help would be deeply appreciated.
0
 
LVL 12

Accepted Solution

by:
satsumo earned 2000 total points
ID: 37117257
You're making something in the style of Minecraft, right?  Minecraft uses vertex lighting, where the light is part of the diffuse color of a vertex.  The vertex shader would pass the vertex color to the pixel shader and that would multiply the vertex color and texture color to get the final result.

In Minecraft type game, the lighting only changes when you modify the world.  There's no need to calculate it in a shader.  You store just keep color values in the vertex data and change it when the player alters something.

Your environment is drawn as a series of quads.  You need a function to calculate the color at each corner of a quad.  The simplest way to do this is to have perhaps 16 rays projecting from each vertex.  How many rays reach the sky defines how bright the vertex is.  The rays should spray out in a pyramid shape.

If your environment is a grid of cubes, you will able to intersect rays with it quickly.  Lighting always involves collision detection.
0
 

Author Closing Comment

by:Iamtehbest
ID: 37193867
Thanks man.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question