Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to make a ripple effect with a HLSL shader, it works so far but I need to output negative numbers and that won't work for the calculations. Is there some way to do this with the SurfaceFormat class? By the way I am rendering 2 RenderTarget2D classes. This is how I calculate it:

float4 add = float4(0,0,0.5,0);
float4 input = tex2D( BaseSampler, Tex);
float4 cur = tex2D( RippleSampler, Tex);
float curY = 0;
float curX = cur.x;
float tempCurY = cur.y;

curY = (tex2D( RippleSampler, float2(Tex.x - pixelSizeX, Tex.y)).x + tex2D( RippleSampler, float2(Tex.x + pixelSizeX, Tex.y)).x + tex2D( RippleSampler, float2(Tex.x , Tex.y - pixelSizeY)).x +  tex2D( RippleSampler, float2(Tex.x , Tex.y + pixelSizeY)).x) * 0.5f - tempCurY;
curY *= damping;

if(curY < 0.01f){
    cur = float4(curX,curY  + input.x,0.5,1);
}else{
    cur = float4(curX,curY,0.5,1);
}

return cur;
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

The trick is to encode the negative number as positive one, by transforming it into a different positive range.

For instance if you need to store values in the [-1,1] range in the texture, you'd start by mapping that into the [0-1] range before rendering to the texture.

You can do this for instance, by taking the original number, adding one to it and then dividing by two.

float originalValue = /* some number between -1 and 1 you want to store in the texture */
float encodedValue = (originalValue + 1.0f) * 0.5f; /* now it's between 0 and 1 */

Then inside your pixel shader, after reading from the texture, convert it back into the original [-1,1] range by multiplying by two and subtracting by one.

float encodedValue = /* value in 0 to 1 range read from the texture */
float originalValue = encodedValue * 2.0f - 1.0f; /* now it's back between -1 and 1 */
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.