# XNA 4.0 Enviroment Lighting / Shadowing

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

###### Who is Participating?

Software DeveloperCommented:
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 Commented:
And here's a copy of my Effect:

``````float4x4 xWorldViewProjection;
Texture xColoredTexture;

sampler ColoredTextureSampler = sampler_state
{ texture = <xColoredTexture> ;
magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR;

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

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

{
VertexOut Output = (VertexOut)0;

Output.Position =mul(input.position, xWorldViewProjection);
Output.textureCoordinates = input.textureCoordinates;

return Output;
}

{
float4 output = tex2D(ColoredTextureSampler, input.textureCoordinates);

return output;
}

technique Textured
{
pass Pass0
{
}
}
``````
0

Software DeveloperCommented:
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 Commented:
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

Author Commented:
Thanks man.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.