Spritebatch brightness?

Started by
21 comments, last by Daivuk 15 years, 5 months ago
I think its working now but I end getting all white if I go overboard with the brightness value

programming shaders its soo cool but I would really like to know where is the syntax documentation for this kind of programming
Advertisement
The HLSL documentation is here. The documentation for D3D9 Effects (which XNA uses under the hood) is here.

Be sure to also have a look at the various samples available on the Creator's Club website, they're very helpful.
I've tried to apply this new shader to my current application but im having some troubles:

since I only want to draw a single sprite on screen do I still need to draw twice?

According to the example there is one draw to a hidden surface then the shader draws again using the texture retrieved from the hidden surface

cant I just draw directly to the display surface? if its not possible then how I have to do when I need to apply this shader to only a few sprites ? I dont want to apply the shader to the whole scene, the only solution Im seeing right now is to batch each sprite on its own begin-end cycle (highly ineffective)
If you're not doing any post processing, then there's no need to draw to render targets and then draw to the screen.

A very simple shader that will do what you want is something like this:

sampler mainSampler;float brightness_mult = 1;struct PS_INPUT{    float2 TexCoord : TEXCOORD0;};float4 brightness(PS_INPUT input) : COLOR0{    float4 theColor = tex2D(mainSampler, input.TexCoord) * brightness_mult;    return theColor;}technique Brightness{    pass Pass0    {        PixelShader = compile ps_1_1 brightness();    }}


What this does, is takes the current color of the current pixel it's working on and multiplies it by the value brightness_mult.

Since you're using XNA, it's really easy to use Shaders.

// when loading your shader:brightShader = Content.Load<Effect>("brightness");...// in rendering code:// Replace '2' in the SetValue parameter to the brightness you want to use. // 2 means twice as bright, 3 is 3 times as bright, .5 is half as bright, etc.brightShader.Parameters["brightness_mult"].SetValue(2);brightShader.Begin();brightShader.CurrentTechnique.Passes[0].Begin();// draw your sprites you want brighter herespriteBatch.Draw(someTexture, new Point(50, 50), Color.White);brightShader.CurrentTechnique.Passes[0].End();brightShader.End();


[Edited by - Flimflam on November 20, 2008 7:21:52 AM]

its not working :(

Im using the exact shader code as the one you have provided

here is my render code

(note that im setting the multiplier to 0 just to see a completely black sprite on screen so I know that the shader is working)

            brightness_effect.Parameters["brightness_mult"].SetValue(0);                       sprite_batch.Begin();            brightness_effect.Begin();            brightness_effect.CurrentTechnique.Passes[0].Begin();                       sprite_batch.Draw(texture_image                             , new Vector2((float)nb_width.Value / 2 * (float)nb_scale_x.Value, (float)nb_height.Value / 2 * (float)nb_scale_y.Value)                             , new Microsoft.Xna.Framework.Rectangle((int)nb_texture_u.Value, (int)nb_texture_v.Value, (int)nb_width.Value, (int)nb_height.Value)                             , Microsoft.Xna.Framework.Graphics.Color.White                             , (float)nb_rotation.Value * (float)Math.PI / 180f                             , new Vector2((float)nb_width.Value / 2, (float)nb_height.Value / 2)                             , new Vector2((float)nb_scale_x.Value, (float)nb_scale_y.Value)                             , se_horizontal | se_vertical                             , 0);            sprite_batch.End();            brightness_effect.CurrentTechnique.Passes[0].End();            brightness_effect.End();
What exactly is happening when you set it to something other than 0? Is it just not getting any brighter than if just Color.White was presented?

Edit: On a side note, I made a typo in my shader code and edited it pretty recently. So if you are using my code, you might want to try re-copying it over :)
Right now the render code just displays the image as it is

it does not matter whether the brightness value is 0 , 1 or 20 it is just rendering the very same texture
That is really bizarre. I went ahead and tested the code I wrote to make sure I wasn't just butchering the concept due to having no sleep and it already being 6:30am, but it's running exactly as I'd expect it to. I'm completely stumped.
EDIT: I got it working i rewrote the shader code

but Im afraid that the battle is far from over, because in the near future I will need to have back to front sprite ordering and since shaders only work on inmediate mode , I really dont know how what is going to work out



[Edited by - EvilNando on November 20, 2008 9:30:02 AM]
Quote:Original post by EvilNando

the alpha channel is getting multiplied as well so when I set brigh. = 0 the image disappears


Yes, and if you remember my post I knew it would happen, I wrote:
float4 color = tex2D(texture, texcoord);
color.rgb *= inBrightness; // <--- Don't multiply the alpha
return color;

:)

This topic is closed to new replies.

Advertisement