HLSL tex2d sampler seemingly returning incorrect values; why?
Posted
by BlueNovember
on Stack Overflow
See other posts from Stack Overflow
or by BlueNovember
Published on 2010-04-16T14:23:13Z
Indexed on
2010/04/16
14:33 UTC
Read the original article
Hit count: 412
hlsl
|fx-composer
Hello all,
I have code that needs to render regions of my object differently depending on their location. I am trying to use a colour map to define these regions, then get a value (0-14) representing this region by sampling the texture.
The problem is when I sample from my colour map, I get collisions. Ie, two regions with different colours in the colourmap get the same value returned from the sampler.
I've tried various formats of my colour map. I set the colours for each region to be "5" apart in each case;
- Indexed colour
- RGB, RGBA: region 1 will have RGB 5,5,5. region 2 will have RGB 10,10,10 and so on.
- HSV Greyscale: region 1 will have HSV 0,0,5. region 2 will have HSV 0,0,10 and so on.
The tex2D sampler returns a value [0..1]. To get the "region number" I multiply this by 100 and divide by 5, expecting a number [0..20]. (But currently only using 0-14)
I am using Shader Model 2 and FX Composer.
//Colour map
texture gColourmapTexture <
string ResourceName = "Globe_Colourmap_Regions_Greyscale.png";
string ResourceType = "2D";
>;
sampler2D gColourmapSampler : register(s1) = sampler_state {
Texture = <gColourmapTexture>;
#if DIRECT3D_VERSION >= 0xa00
Filter = MIN_MAG_MIP_LINEAR;
#else /* DIRECT3D_VERSION < 0xa00 */
MinFilter = Linear;
MipFilter = Linear;
MagFilter = Linear;
#endif /* DIRECT3D_VERSION */
AddressU = Clamp;
AddressV = Clamp;
};
...
//Then later, in a method
float region = tex2D(gColourmapSampler,In.UV).x;
//at this point I do not think it matters which of xyz components I pick; even in HSV they're all the same for my image.
region *= 100; //Now in range [0..100]
region /= 5; //Now in range [0..20]
float3 levels[21];
//*Code populating "levels" array with what is essentially colour information *
levels[1] = ...
levels[2] = ...
//Chose which level this region has, by looking up its region number
float3 Level = levels[region];
© Stack Overflow or respective owner