Solved

XNA 4.0 Enviroment Lighting / Shadowing

Posted on 2011-09-09
5
1,091 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 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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