• Create Account

Attributing linear color variation on shader

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

#1GeneralMeliante  Members

149
Like
0Likes
Like

Posted 08 January 2013 - 09:49 AM

Hi everyone!

I'm trying, through the PixelShader on the HLSL, to define the color of the pixel based on its height taken from the heightmap texture.

The program now takes the color from a texture sampling it linearly and I would like to change that to one defined by me based on the height of that pixel.

It's a terrain so basically i get the heightmap, calculate and add to it a normal map on CPU.

Since i need this to be really fast (the user must be allowed to change the colors on real time) this logic must be held on the shaders.

Below follows a cut of the function that gets the color for a given pixel considering all the lights and information from the texture with the color.

float4 ComputeIllumination( float2 texCoord, float3 vLightTS, float3 vViewTS )
{
// Sample the normal from the normal map for the given texture sample:
float3 vNormalTS = normalize( g_nmhTexture.Sample(g_samLinear, texCoord) * 2.0 - 1.0 );

// Sample base map
float4 cBaseColor = g_baseTexture.Sample( g_samLinear, texCoord );

// Compute diffuse color component:
float4 cDiffuse = saturate( dot( vNormalTS, vLightTS ) ) * g_materialDiffuseColor;

// Compute the specular component if desired:
float4 cSpecular = 0;

// Composite the final color:
float4 cFinalColor = ( g_materialAmbientColor + cDiffuse ) * cBaseColor + cSpecular;

return cFinalColor;
}


When i try to change that logic by adding something like seen below it has no linear variation between pixels its flat that color and the normal map loses its effect for some reason.

float unit = 0.00390625;

if((vHeight.w >= (0 * 63.75 + 1) * unit)
&&   (vHeight.w < (1 * 63.75 + 1) * unit)
//&& (vHeight.w > clipThreshold)
)
{
cBaseColor.rgb = uint3(0, 0, 255);
//cBaseColor.a = 255;
}
else if((vHeight.w >= (1 * 63.75 + 1) * unit)
&&   (vHeight.w < (2 * 63.75 + 1) * unit))
{
cBaseColor.rgb = uint3(0, 255, 255);
//cBaseColor.a = 255;
}
else if((vHeight.w >= (2 * 63.75 + 1) * unit)
&&   (vHeight.w < (3 * 63.75 + 1) * unit))
{
cBaseColor.rgb = uint3(0, 255, 0);
//cBaseColor.a = 255;
}
else if((vHeight.w >= (3 * 63.75 + 1) * unit)
&&(vHeight.w < (4 * 63.75 + 1) * unit))
{
cBaseColor.rgb = uint3(255, 255, 0);
}

else if(vHeight.w == 1)
{
cBaseColor.rgb = uint3(255, 0, 0);
}

Below first is how it looks using only the provided texture and second with the added logic AFTER relating it with the texture, meaning not taking of the:

float4 cBaseColor = g_baseTexture.Sample( g_samLinear, texCoord );



float4 vHeight = g_nmhTexture.Sample( g_samLinear, texCoord );

to get information from the heightmap.

#2GeneralMeliante  Members

149
Like
0Likes
Like

Posted 09 January 2013 - 08:50 AM

I guess i forgot to make the question itself.

I would like to get the same effect as the one taking from a pre made base texture by providing in real time the range of colors.

Thank you guys very much for any input!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.