Search code examples
mathtextureshlsl

Distribution Pattern (Texture) and Ramp Math?


I'm trying to achieve the ramp effect as seen here:
(source: splashdamage.com)

Blending the textures based on a distribution pattern is easy. Basically, just this (HLSL):

Result = lerp(SampleA, SampleB, DistributionPatternSample);

Which works, but without the ramp. http://aaronm.nuclearglory.com/private/stackoverflow/result1.png

My first guess was that to incorporate "Ramp Factor" I could just do this:

Result = lerp(A, B, (1.0f - Ramp)*Distribution);

However, that does not work because if Ramp is also 1.0 the result would be zero, causing just 'A' to be used. This is what I get when Ramp is 1.0f with that method: http://aaronm.nuclearglory.com/private/stackoverflow/result2.png

I've attempted to just multiply the ramp with the distribution, which is obviously incorrect. (Figured it's worth a shot to try and discover interesting effects. No interesting effect was discovered.)

I've also attempted subtracting the Ramp from the Distribution, like so:

Result = lerp(A, B, saturate(Distribution - Ramp));

But the issue with that is that the ramp is meant to control sharpness of the blend. So, that doesn't really do anything either.

I'm hoping someone can inform me what I need to do to accomplish this, mathematically. I'm trying to avoid branching because this is shader code. I can simulate branching by multiplying out results, but I'd prefer not to do this. I am also hoping someone can fill me in on why the math is formulated the way it is for the sharpness. Throwing around math without knowing how to use it can be troublesome.

For context, that top image was taken from here: http://wiki.splashdamage.com/index.php/A_Simple_First_Megatexture

I understand how MegaTextures (the clip-map approach) and Virtual Texturing (the more advanced approach) work just fine. So I don't need any explanation on that. I'm just trying to implement this particular blend in a shader.

For reference, this is the distribution pattern texture I'm using.
http://aaronm.nuclearglory.com/private/stackoverflow/distribution.png


Solution

  • Their ramp width is essentially just a contrast change on the distribution map. A brute version of this is a simple rescaling and clamp.

    Things we want to preserve are that 0.5 maps to 0.5, and that the texture goes from 0 to 1 over a region of width w.

    This gives

    x = 0.5 + (x-0.5)/w
    

    This means the final HLSL will look something like this:

    Result = lerp(A, B, clamp( 0.5 + (Distribution-0.5)/w, 0, 1) );
    

    Now if this ends up looking jaggy at the edges you can switch to using a smoothstep. In shich case you'd get

    Result = lerp(A, B, smoothstep( 0.5 + (Distribution-0.5)/w, 0, 1) );
    

    However, one thing to keep in mind here is that this type of thresholding works best with smoothish distribution patters. I'm not sure if yours is going to be smooth enough (unless that is a small version of a mega texture in which case you're probabbly OK.)