Jump to content
  • Advertisement
Sign in to follow this  
MatsK

HLSL colormapping textures

This topic is 2568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have a heightmap that I am texturing using a colormap. I followed a tutorial to create an HLSL-script that did this for me.
I'm wondering if there's any way I can identify if the colormap contains the color yellow and apply a sand-texture if it does?

Here's what I tried (didn't work);

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
//NOTE: Changed red to sample from the water-texture,
// and blue to sample from the rock-texture, because
// it seems to fit the colorcomposition of the colormap.
float4 red = tex2D(WaterSampler, input.UV * UVScaling);
float4 green = tex2D(GrassSampler, input.UV * UVScaling);
float4 blue = tex2D(RockSampler, input.UV * UVScaling);
float4 yellow = tex2D(SandSampler, input.UV * UVScaling);
float4 alpha = tex2D(SnowSampler, input.UV * UVScaling);

float4 map = tex2D(TextureMapSampler, input.UV);

float a = map.a;
float4 final = (map.r * a * red) + (map.g * a * green) + (map.b * a * blue) + ((1.0f - a) * alpha);

if(map.r = 255 && map.g == 255 && map.b == 0)
final = (map.r * a * yellow) + (map.g * a * yellow) + (map.b * a * blue) + ((1.0f - a) + alpha);

return final;
}

Share this post


Link to post
Share on other sites
Advertisement
Hidden
Hello smile.gif

First I'd like to say that a sampler provides color for which r,g,b,a are expressed in the range [0 , 1]
Concerning uv values, the range [0,1]x[0,1] covers the entire map whatever are its dimensions, no need to scale the uv for that

so, to check if the sampled color is yellow, instead of this :

[color=#000000] [color=#000088]if[color=#666600]([color=#000000]map[color=#666600].[color=#000000]r [color=#666600]=[color=#000000] [color=#006666]255[color=#000000] [color=#666600]&&[color=#000000] map[color=#666600].[color=#000000]g [color=#666600]==[color=#000000] [color=#006666]255[color=#000000] [color=#666600]&&[color=#000000] map[color=#666600].[color=#000000]b [color=#666600]==[color=#000000] [color=#006666]0[color=#666600]) ...



something like :

if (map.r == 1.0f && map.g==1.0f && map.b==0.0f)
final=[font=CourierNew, monospace][size=2]

tex2D

[/font][color=#666600][font=CourierNew, monospace][size=2]

(

[/font][color=#660066][font=CourierNew, monospace][size=2]

SandSampler

[/font][color=#666600][font=CourierNew, monospace][size=2]

,

[/font][font=CourierNew, monospace][size=2]

input

[/font][color=#666600][font=CourierNew, monospace][size=2]

.

[/font][font=CourierNew, monospace][size=2]

UV)

[/font]


.... if I understood what you want correctly.

I see you're doing some alpha blending 'manually', maybe you could think about using alpha blend states for that ?

Nicosmile.gif

Share this post


Link to post
Hidden
Hello smile.gif

First I'd like to say that a sampler provides color for which r,g,b,a are expressed in the range [0 , 1]
Concerning uv values, the range [0,1]x[0,1] covers the entire map whatever are its dimensions, no need to scale the uv for that

so, to check if the sampled color is yellow, instead of this :

[color="#000088"]if[color="#666600"]([color="#000000"]map[color="#666600"].[color="#000000"]r [color="#666600"]= [color="#006666"]255 [color="#666600"]&&[color="#000000"] map[color="#666600"].[color="#000000"]g [color="#666600"]== [color="#006666"]255 [color="#666600"]&&[color="#000000"] map[color="#666600"].[color="#000000"]b [color="#666600"]== [color="#006666"]0[color="#666600"])


something like :

if (map.r == 1.0f && map.g==1.0f && map.b==0.0f)
final=[font=CourierNew, monospace][size=2]

tex2D

[/font][color=#666600][font=CourierNew, monospace][size=2]

(

[/font][color=#660066][font=CourierNew, monospace][size=2]

SandSampler

[/font][color=#666600][font=CourierNew, monospace][size=2]

,

[/font][font=CourierNew, monospace][size=2]

input

[/font][color=#666600][font=CourierNew, monospace][size=2]

.

[/font][font=CourierNew, monospace][size=2]

UV)

[/font]


.... if I understood what you want correctly.

I see you're doing some alpha blending 'manually', maybe you could think about using alpha blend states for that ?

Nicosmile.gif

Share this post


Link to post

So I have a heightmap that I am texturing using a colormap. I followed a tutorial to create an HLSL-script that did this for me.
I'm wondering if there's any way I can identify if the colormap contains the color yellow and apply a sand-texture if it does?

Here's what I tried (didn't work);

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
//NOTE: Changed red to sample from the water-texture,
// and blue to sample from the rock-texture, because
// it seems to fit the colorcomposition of the colormap.
float4 red = tex2D(WaterSampler, input.UV * UVScaling);
float4 green = tex2D(GrassSampler, input.UV * UVScaling);
float4 blue = tex2D(RockSampler, input.UV * UVScaling);
float4 yellow = tex2D(SandSampler, input.UV * UVScaling);
float4 alpha = tex2D(SnowSampler, input.UV * UVScaling);

float4 map = tex2D(TextureMapSampler, input.UV);

float a = map.a;
float4 final = (map.r * a * red) + (map.g * a * green) + (map.b * a * blue) + ((1.0f - a) * alpha);

if(map.r = 255 && map.g == 255 && map.b == 0)
final = (map.r * a * yellow) + (map.g * a * yellow) + (map.b * a * blue) + ((1.0f - a) + alpha);

return final;
}



Hello smile.gif

First I'd like to say that a sampler provides color for which r,g,b,a are expressed in the range [0 , 1]
Concerning uv values, the range [0,1]x[0,1] covers the entire map whatever are its dimensions, no need to scale the uv for that

so, to check if the sampled color is yellow, instead of this :

if(map.r = 255 && map.g == 255 && map.b == 0)


something like :

if(map.r ==1.0f && map.g == 1.0f && map.b == 0.0f)
final=tex2D(SandSampler, input.UV);

... If I understood correctly what you want.

I see you do some 'manual' alpha blending, maybe you could use blend states ?

Nico

Share this post


Link to post
Share on other sites
Hi,

I think that result won't be what you are looking for. If you use a formula such as if(color == yellow) the texture will have hard edge. I mean, the sand texture won't blend to it's surroundings.

You may always add another alpha texture to get more and more textures to blend, but in the same time you are wasting a lot of texture reads since they won't affect the final image.

Best regards!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!