Skip to main content
gamedev.net
🔒 Locked

Height based texturing problem

Started by pulo 1 reply 1.1k views
Original Post
pulo
pulo
Hey there,

first of all i have to say that i am fairly new to the whole shader programing or 3D programming in general, so this problem might seem pretty obvious to you.
I want to texture my terrain based on slope and on height. The slope part is working fine but i am having trouble getting the height part working. What i am wanting to accomplish is texturing the terrain near ground level (height <= 10) with a rock texture.
So im using the pixel shader like this:


grassColor = grassTexture.Sample(SampleType, input.tex);
// Sample the slope color from the texture using the sampler at this texture coordinate location.
slopeColor = slopeTexture.Sample(SampleType, input.tex);
// Sample the rock color from the texture using the sampler at this texture coordinate location.
rockColor = rockTexture.Sample(SampleType, input.tex);
// Calculate the slope of this point.
slope = 1.0f - input.normal.y;
// Determine which texture to use based on height.
if(slope < 0.2)
{
blendAmount = slope / 0.2f;
textureColor = lerp(grassColor, slopeColor, blendAmount);
}

if((slope < 0.7) && (slope >= 0.2f))
{
blendAmount = (slope - 0.2f) * (1.0f / (0.7f - 0.2f));
textureColor = lerp(slopeColor, rockColor, blendAmount);
}
if((slope >= 0.7) || (input.position.y <= 10))
{
textureColor = rockColor;
}


But if i do it like this only a small area at the top of my screen is getting textured with the rock texture. not based on the terrain. If i rotate my camera around i can see the texture of my terrain changing in this little part of my screen.
This seems kinda odd to me, because i am pretty sure that i am transforming the position coordinates to 3D space like this:


input.position.w = 1.0f;
// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);


What am i doing wrong?`Thanks to anyone willing to help!
BCullis
BCullis
Add another element to your vertexShader output called something like "worldHeight" set equal to mul(input.position, worldMatrix).y. Then check against worldHeight for your height constraint.

The problem is that you're converting the position of the vertex into view-contextual screen-space coordinates. (You're multiplying by world, then view, then projection, all before you hand the position to your pixel shader, which is fine for the "output position" of the eventual pixel, but not for keeping your y value in the right coordinate space) You're only interested in the world position of the vertex, which shouldn't change based on your view.
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
pulo
pulo
Ohh. Right! Thanks a lot!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.