Sign in to follow this  

Spritebatch brightness?

This topic is 3310 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

Is there a way to increase a sprite brightness using spritebatch.draw()? Im guessing I have to mess with the Color parameter but Im already using Color.White for that purpose. Is there a way to increase brightness beyond this value? Thanks

Share this post


Link to post
Share on other sites
Since the Color class only accepts colours values between 0 and 1 you'll need to find another way of dong this.

I'd suggest using a pixel shader - http://msdn.microsoft.com/en-us/library/bb313868.aspx which will allow you to multiply the sprite by a colour that has more than 1 in each component (you'll want to leave alpha is-is though).

Share this post


Link to post
Share on other sites
question is it possible to pass values to shader functions?

I dont want all of my objects(sprites) to be affected by the brightness shader
so maybe I was thinking that in those cases I can pass 0 , null or whatever

in fact I wonder if its possible to pass directly the brightness value so each object can store its own value and then pass it to the shader effect

Im a little bit in the dark in this shader subject so I have no idea how I have to do to structure a .fx file , from the samples Ive seen it seems that does not have a start point , just a bunch of functions

Share this post


Link to post
Share on other sites
Yes it is possible. You can pass the params you want: Matrix, texture, color, float, etc

And try to not use if statements into the shader. Mathematical solution is often the best. So in your case, I would pass the brightness value as a float and multiply it, avoiding using if statements :)

Share this post


Link to post
Share on other sites
I think in the XNA documentation there are "How to" about that.
I did it pretty quickly the last time and I never used HLSL before.

Basically, for your case, you do not need the vertex shader. You can delete it.
And simply add a parameter in the pixel shader function: float inBrightness

and use that into your shader.

float4 color = tex2D(texture, texcoord);
color.rgb *= inBrightness;
return color;

Share this post


Link to post
Share on other sites
If you don't want to go the shader route (though it's a good thing to learn), you can modify the state the sprite system is using. Change it to use a MODULATE2X colorop. In this setting, 128 is "white", and 255 is twice as bright.

I think ID3DXSprite sets up the states when you "begin", so you can modify it right after that.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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 here
spriteBatch.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]

Share this post


Link to post
Share on other sites

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();


Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;

:)

Share this post


Link to post
Share on other sites
Im so exited about learning shaders, it feels soo hardcore to be doing this kind of work

here are a couple of shots of what I have accomplished.

Photobucket
Photobucket
Photobucket
Photobucket


man I will try to get shader-crazy for my game now that I know that these kind of effects can be made even in 2d graphics!!

Initially I had planned a spell system based solely on sprite animations but now I will change that to animations + shaders

It going to be awesome!!

Share this post


Link to post
Share on other sites
Yes! And you can do some crazy stuff. If you change to pixel shader 2.0
PixelShader = compile ps_1_1 brightness();
to
PixelShader = compile ps_2_0 brightness();

or more, you can change values of the texcoord by pixel. And do some screen distortion and more! Sky is the limit... and VRam ><

Have fun experimenting

Share this post


Link to post
Share on other sites

This topic is 3310 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this