Jump to content
  • Advertisement
Sign in to follow this  
bonus.2113

Shaderbased Dynamic 2D Shadows

This topic is 2528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,
I've started to implement dynamic shadows in 2D using the technique descriped in this blog post.

http://www.catalinzi...mic-2d-shadows/

The method seems good, but I ran into a problem. All the steps work until it gets to the reduction.

Map render from the light points perspective:

maprender.png

Distances:

distance.png

Distortion:

distort.png

Those look all like they are supposed to, but then the reduce algorithm outputs something like that (scaled to square resolution):
reduced0.png

After Checking the steps the of reduction it seems like the shader is just cropping the right half over and over.

(after first pass)
[size="1"]reduced6.png



The shader is exactly the same as the one in blogpost and the source files.

float4 HorizontalReductionPS(float2 TexCoord : TEXCOORD0) : COLOR0
{
float2 color = tex2D(TextureSampler, TexCoord);
float2 colorR = tex2D(TextureSampler, TexCoord + float2(TextureDimensions.x,0));

float2 result = min(colorR,color);

return float4(result, 0, 1);
}


The C# sourcecode looks like this:

private RenderTarget2D Reduce(RenderTarget2D _distorted, int _index)
{

int step = reductionChainCount - 1;
RenderTarget2D s = _distorted;
RenderTarget2D d = rtReduction[_index][step]; //Array of RenderRaget2D's in the dimensions needed for the current step


while (step >= steps)
{
d = rtReduction[_index][step];

Device.SetRenderTarget(d);
Device.Clear(Color.White);

reductionEffect.CurrentTechnique = reductionEffect.Techniques["HorizontalReduction"];
Vector2 textureDim = new Vector2((1f / (float)s.Width), 1f / (float)s.Height);
reductionEffect.Parameters["TextureDimensions"].SetValue(textureDim);
reductionEffect.Parameters["sourceTexture"].SetValue(s);

spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.Opaque,
SamplerState.PointClamp,
null,
null,
reductionEffect);



spriteBatch.Draw(s, Vector2.Zero, Color.White);

spriteBatch.End();
Device.SetRenderTarget(null);

s = d;
step--;
}

return s;
}



It's pretty simple and I understand what it is supposed to do. I triple checked the TextureDimension and I'm certain
I pass the correct value.
I tried to find a way around this problem or come up with my own solution, but it didn't work out and I'm sure the sample
in the blog post should work.

Has anyone else got an idea, what I might be doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
I actually played with something similar a few weeks back.
[s]Looking at your distortion map, it seems that to the left and right you have walls, meaning, everything ends up in shadows... which also seems more evident as it becomes wider in the second pass, as the algorithm propagates the shadows.

Try a different picture without the center walls and see what happens.

EDIT: Wait a bit, I'm looking at it the wrong way I think, those are the outer walls of course.
[/s]
EDIT: From looking at the results from the first pass, it seems like it's scaling it down incorrectly, rather than scaling it down, it's cropping it.

Share this post


Link to post
Share on other sites

EDIT: From looking at the results from the first pass, it seems like it's scaling it down incorrectly, rather than scaling it down, it's cropping it.


That's what I think, too, but why would it do that? The shader is straight forward, it compares the current pixel with the next pixel on the right and writes the one with the
lowest distance to the render target. The only thing I could imagine is, that the UV-Coordinates are messed up. But even that would give different results than just cropping the right half, wouldn't it?
Well, I'll keep trying, maybe it's just one of those tiny stupid mistakes...

Share this post


Link to post
Share on other sites

[quote name='Syranide' timestamp='1324416448' post='4895881']
EDIT: From looking at the results from the first pass, it seems like it's scaling it down incorrectly, rather than scaling it down, it's cropping it.


That's what I think, too, but why would it do that? The shader is straight forward, it compares the current pixel with the next pixel on the right and writes the one with the
lowest distance to the render target. The only thing I could imagine is, that the UV-Coordinates are messed up. But even that would give different results than just cropping the right half, wouldn't it?
Well, I'll keep trying, maybe it's just one of those tiny stupid mistakes...
[/quote]

[s]From what I can tell, it's cropping it to half the size, but fixed to the left.

[/s]EDIT: Wrong again! Opened it in photoshop, the vertical size is correct, but it's stretched horizontally by a factor of two, so the right half is missing (basically, it's never scaled down in X).

Is your "s" variable perhaps using the wrong dimensions?

Share this post


Link to post
Share on other sites
hmm, I got it working now, but I used alot of the sample code. That's not optimal as I wanted to learn something from it
and I don't know what was wrong before. But it will do for now. When I get back to it and find a solution I'll post it here.
Thanks for your help anyway, I appreciate it :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!