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

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.

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:

With the effect on:

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

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.

• What kind of ring texture are you using right now? Could you add a picture? (If you're trying to recreate the texture, I'd be nice to know what it looks like, so we can help you better) – Msonic Sep 7 '12 at 18:48
• It's also important to note that the effect isn't using a round texture. All the texture is used for is to grab the color of the texture so the ring can be colored. – electroflame Sep 7 '12 at 21:07

You might want to set weights to the colors of the ring and the ship's texture so that they can cancel each other out and don't blend additively.

Make a white ring for the ring texture which would serve as a color mask, and use a float3 uniform variable to change it's color.

float3 newColor;

float4 ringColor = ringColorMask * newColor;

// All rgb channels have the same values for the max texture,
// so just sample the first one