Issue with alpha in 2d lighting shader in XNA 4.0 - c#

I'm currently learning HLSL with XNA, I figured the best place to start after tutorials would be some simple 2D shaders. I'm attempting to implement a simple lighting shader in 2D.
I draw the scene without shadows to a rendertarget, swap my rendertarget to a shadowmap, draw my light(each individually) onto the shadowmap via alpha channel, swap my rendertarget back to default and render the scene then the shadows on top.
The alpha of the light changes depending on the distance of the current pixel and the point of the light, this is all working fine for me except when I render the scene, if two lights overlap it causes a nasty blending issue.
I'm using alphablend when I draw on the shadowmap, and when I draw the shadowmap to the scene.
Am I just using the wrong blending settings here? I don't know much about blendstates.
Sorry if the question was vague.

I've had this happen before when doing software lighting, where your values exceed 255 (or 1.0) and you end up back in the realms of blackness. I believe the values are clamped using a modulo operation causing 1.1 to be 0.1 or 256 to be 1. I notice how the black ellipse is actually two spheres edges combined, this is what is giving me this conclusion.
Hope this gets you closer to understanding and finding out your problem. I have no idea what code you are using already but in your HLSL technique pass you could try adding:
AlphaBlendEnable = true;
SrcBlend = One;
DestBlend = One;

Related

Why does Unity's Legacy Vertex Lit disable my code that sets the color of tiles on a 2D tile map?

To start, I have a bird eye view 2d tilemap that I generate procedural. I color each tile using TileMap.SetColor( new Color(r,g,b,a));. This works very well with the default shader that does not process light on the scene. I want light in my game, so I swapped the shader, 1 by 1, to every other shader with none supporting the custom color and lighting at the same time in Legacy vertex Lit Mode. I tried swapping my rendering mode to Forward and Deffered and this did work as I had my custom colors on top of the lighting, but problems occurred. The reason I swapped to Vertex Lit in the first place was because I was getting artifacts at the boundaries of every tile with lines that showed the cross sections of tiles that overlapped and I get a way lower frame rate in forward/deffered, so I prefer legacy. I know why the shader does this and I tried writing my own, but I am very new to Unity, so I am not experience enough to dive into graphics that far.
What Is Needed: All that is needed is my custom tile colors and lighting in Legacy Vertex Lit mode or A fix to the overlapping tiles lighting up in forward and deffered modes.
Thanks again for the help! I am losing my mind trying to solve this.
EDIT: I noticed that when I changed the layer or z position of a tile in that map, the effect disappeared completely in forward and deffered of just that tile only. I don't know if this the solution I am looking for, but its a start.
EDIT #2: I set the chunk rendering mode to individual and it fixes the problem as in the first edit, but performance takes a very big hit. Any way to tell the render/shader that each tile is separated?
Ok, I eventually got it to work. What I did to get rid of the lines and get custom coloring in the game was to swap rendering modes to forward mode. Then, you can go into the shader you are using for the tilemap and add #pragma noforwardadd.

Monogame sprite alpha blending

At the moment, I am having a 2D grid of terrain textures, which I am drawing using the standard XNA/Monogame spritebatch.Draw() function. In order to have smoother transitions between adjacent but different terrain textures, I would like to blend textures within a certain area (say 3x3) around a center texture. The further a neighboring texture is away, the smaller should be its impact on the final image.
E.g., a (3x3) weighting matrix weight around a central texture at (1,1) could look something like this:
0.3 0.5 0.3
0.5 1.0 0.5
0.3 0.5 0.3
Basically, a simple problem. However, I am currently staggering with the different blending modes offered and their functionality. My initial idea was to just use BlendState.AlphaBlend together with
spriteBatch.Draw(Tex2D, TargetRec, new Color(Color.White, weight[x,y] / SumOfWeights));
The white color should give me the original texture colors, the alpha value weight[x,y] / SumOfWeights would in the end add up to 1. Instead, what I get is a very bright image plus the background shining through.
A better result can be achieved, when also setting the tinting Color to a gray with the same value as the alpha channel. Again, the background is shining through, though, when using more than 2 textures.
There must be a systematic error in my concept, but I am momentarily unable to find it. Please point out my mistake & thanks in advance.
Try this:
spriteBatch.Draw(Tex2D, TargetRec, Color.White * weight[x,y]);
This is the proper way to draw things transparently when SpriteBatch is using premultiplication. The alpha channel works differently when drawing with premultiplication. The basic change you need to make is to multiply the color by the transparency instead of setting the alpha channel of the color.
Here is some more information. http://blogs.msdn.com/b/shawnhar/archive/2009/11/06/premultiplied-alpha.aspx

Lens flare in 2D XNA 4.0 scene

I am creating realtime scene in XNA, it is 2D using sprites only (rendered on quads, standard spritebatch with alpha map on sprites). I would like to create create simply lens flare, actually only occlusion around light source (I donĀ“t need direction to center of camera to offset multiple sprites for lens flare, etc.) Only thing I basically need is to calculate how many pixels from light source sprite (small star) are rendered and according to it set scale of lens flare sprite (so scale 0 if sprite there are not visible pixels from relevant sprite).
I know how to do it in 3D, I read through this and tested few things:
http://my.safaribooksonline.com/book/programming/game-programming/9781849691987/1dot-applying-special-effects/id286698039
I would like to ask what is best and cheapest way to do it in 2D scene (counting how many pixels of sprite were rendered / occluded with per pixel precision or something comparable).
I know also stencil buffer could help but I am not sure how to applicate in this case.
Okay, two ways how to solve it, either kinda old school approach, using stencil to calculate count of occluded pixels and scale sprites of lens flare according to it.
Other way: modern approach, use screen space lens flare, isolate bright pixels (I recommend HDR rendering pipeline and use values of brightness above 1.0 to generate lens flares, but it depends on scene average and maximum) and generate ghosts, like so:
https://www.youtube.com/watch?v=_A0nKfzbs80&list=UUywPlxpmCZtuqOs6_bZEG9A

Making a lightsystem like Terraria?

I am trying to make my lighting similar to Terraria's, block-lighting. Now, I know how to make blocks darker, I can assign blocks to a certain lightlevel, but, how would I make an actual light entity, that emits light in a round shape (Can be diamond-shaped too)?
Help would be greatly appreciated, also, if I wasn't clear in my question, feel free to ask.
Basic 2D lighting is very simple. Just do a distance check from your block, to your light, and use that value to scale your light.
This is something you could do fairly simple, since Spritebatch.Draw has a nice Color tint parameter [link]
A pseudo function could be
distance = (block.position - light.position). Length();
lightPower = distance / light.MaxDistance;
finalTint = light.Color * lightPower;
Render Block, with finalTint
For more nice looking light, you could replace "distance / light.MaxDistance" with a more smooth effect.
If you also want lights to go through a few blocks like Terraria, you could count all blocks between your block and the light source. Scale your lightPower down by that amount, and you get the same effect like Terraria has.
Of course, this is a non optimized way of doing it, but should work.
The latest Terraria version however seems to have smooth per pixel lighting instead of per block [preview]. For that I assume they used a second render target and/or Pixel Shader to keep fast performance. This could be a little difficult if you are not familiar with rendering pipelines though.
Hope this helps!
I'm working on a game with a similar lighting model, and the way we do it is this:
Draw the scene, without lighting, to a render target (called the 'Scene Buffer')
Draw the scene's lights, represented as grayscale gradients of any required shape, to a second render target (called the 'Light Map')
Draw the Scene Buffer to the screen, passing in the Light Map as a parameter to the pixel shader
In the pixel shader, query the value of the Light Map at each pixel and adjust the color of the final pixel up or down as necessary.
This also gives you the ability to have colored lighting; all you have to do is tint the light gradients that you render to the Light Map. Just remember to use additive alpha blending.
The downside of this approach is that it's rather naive, and provides no easy way to occlude the lights (that is to say, they pass through walls). In our case, this isn't an issue; you might decide otherwise.

Code to extrude 2d geometry to 3d

is there any simple way to extrude a 2d geomtry (vectors ) to a 3d shape
assuming extruding parameter are lenght (double) and angle (degree)
so it should render like a cone ( all z lines going to one point )
(I'd make this a comment, but it's too big)
This isn't just an extruding problem
If it were, your original 2D image would produce either a cylinder with a series of holes in it (not really that useful unless you have a very sophisticated renderer doing either volumetrics or supporting transparency, and the polysort in that case would be very ugly) or 4 cylinders (if I extrude along the inner holes)
most extrusion algorithms don't deal with targeting a single point - that's more than extrusion, it's some form of raycasting
This looks suspiciously like a lighting issue - are you trying to do volumetric lighting, maybe show an effect where the light cone is and deal with the effect of a baffle in front of the light ? Or are you trying to compute the geometry that would define the shadow cast by the object in front of the light ?

Categories

Resources