Jump to content

  • Log In with Google      Sign In   
  • Create Account


Shaderbased Dynamic 2D Shadows


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 bonus.2113   Members   -  Reputation: 630

Like
0Likes
Like

Posted 20 December 2011 - 09:38 AM

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:

Posted Image

Distances:

Posted Image

Distortion:

Posted Image

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

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

(after first pass)
Posted Image



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?



Sponsor:

#2 Syranide   Members   -  Reputation: 375

Like
0Likes
Like

Posted 20 December 2011 - 03:27 PM

I actually played with something similar a few weeks back.
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.

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.



#3 bonus.2113   Members   -  Reputation: 630

Like
0Likes
Like

Posted 20 December 2011 - 03:44 PM

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...

#4 Syranide   Members   -  Reputation: 375

Like
0Likes
Like

Posted 20 December 2011 - 03:50 PM


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...


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

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?



#5 bonus.2113   Members   -  Reputation: 630

Like
0Likes
Like

Posted 20 December 2011 - 04:00 PM

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 :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS