A Quick HLSL Question (How to modify some HLSL code)

Posted by electroflame on Game Development See other posts from Game Development or by electroflame
Published on 2012-09-07T17:59:39Z Indexed on 2012/09/07 21:51 UTC
Read the original article Hit count: 395

Filed under:
|

Thanks for wanting to help!

I'm trying to create a circular, repeating ring (that moves outward) on a texture. I've achieved this, to a degree, with the following code:

float distance = length(inTex - in_ShipCenter);
float time = in_Time;

///* Simple distance/time combination */
float2 colorIndex = float2(distance - time, .3);    

float4 shipColor = tex2D(BaseTexture, inTex);
float4 ringColor = tex2D(ringTexture, colorIndex);

float4 finalColor;
finalColor.rgb = (shipColor.rgb) + (ringColor.rgb);
finalColor.a = shipColor.a; // Use the base texture's alpha (transparency).


return finalColor; 

This works, and works how I want it to. The ring moves outward from the center of the texture at a steady rate, and is constrained to the edges of the base texture (i.e. it won't continue past an edge). However, there are a few issues with it that I would like some help on, though. They are:

  1. By combining the color additively (when I set finalColor.rgb), it makes the resulting ring color much lighter than I want (which, is pretty much the definition of additive blending, but I don't really want additive blending in this case).

  2. I would really like to be able to pass in the color that I want the ring to be. Currently, I have to pass in a texture that contains the color of the ring, but I think that doing it that way is kind of wasteful and overly-cumbersome.

I know that I'm probably being an idiot over this, so I greatly appreciate the help.

Some other (possibly relevant) information:

  • I'm using XNA.

  • I'm applying this by providing it to a SpriteBatch (as an Effect).

  • The SpriteBatch is using BlendState.NonPremultiplied.

Thanks in advance!

EDIT: Thanks for the answers thus far, as they've helped me get a better grasp of the color issue. However, I'm still unsure of how to pass a color in and not use a texture. i.e. Can I create a tex2D by using a float4 instead of a texture? Or can I make a texture from a float4 and pass the texture in to the tex2D?

DOUBLE EDIT: Here's some example pictures:

With the effect off:

Off

With the effect on:

On

With the effect on, but with the color weighting set to full:

On, With Full Color Weighting

As you can see, the color weighting makes the base texture completely black (The background is black, so it looks transparent). You can also see the red it's supposed to be, and then the white-ish it really is when blended additively.

© Game Development or respective owner

Related posts about XNA

Related posts about hlsl