Solved

XNA 4.0 Enviroment Lighting / Shadowing

Posted on 2011-09-09
5
1,085 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks man.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now