Jump to content
  • Advertisement
Sign in to follow this  

More textures in pixel shader?

This topic is 3422 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

I've been doing a tutorial about terrain generation that has an example of how to do texture splatting with using a pixel shader. It currently supports only 3 textures (grass, mountain, snow etc) however I want to add some more. The tutorial doesn't go into much detail about the ps code so I'm a little stuck at the moment. I've been doing some reading to try and understand HLSL but I still can't figure it out. Here is the pixel shader code sampler a1; sampler t1; sampler t2; sampler t3; float4 Main(float2 alphaUV : TEXCOORD0, float2 colorUV : TEXCOORD1) : COLOR { float4 a = tex2D(a1, alphaUV); float4 c1 = tex2D(t1, colorUV); float4 c2 = tex2D(t2, colorUV); float4 c3 = tex2D(t3, colorUV); float inverse = 1.0f / (a.r + a.g + a.b); c1 *= a.b * inverse; c2 *= a.g * inverse; c3 *= a.r * inverse; return c1 + c2 + c3; } Could anyone show me how to modify this to allow extra textures? Whats the max number of textures that can be used with ps_2?

Share this post


Link to post
Share on other sites
Advertisement
You can have up to 16 texture samplers in ps_2_0. However as you add more, you'll probably start to have a noticeable decrease in performance. To use more, just add more sampler declarations to your HLSL and then sample from them with the tex2D intrinsic. You'll also of course need to bind textures to your samplers in your application code.

Share this post


Link to post
Share on other sites
Thanks for your reply. Sorry for my late response but I've been away on holidays :)

Ok so I've modified my code to support an extra texture like so

sampler a1;
sampler t1;
sampler t2;
sampler t3;
sampler t4;

float4 Main(float2 alphaUV : TEXCOORD0, float2 colorUV : TEXCOORD1) : COLOR
{
float4 a = tex2D(a1, alphaUV);
float4 c1 = tex2D(t1, colorUV);
float4 c2 = tex2D(t2, colorUV);
float4 c3 = tex2D(t3, colorUV);
float4 c4 = tex2D(t4, colorUV);

float inverse = 1.0f / (a.r + a.g + a.b + a.a);

c1 *= a.b * inverse;
c2 *= a.g * inverse;
c3 *= a.r * inverse;
c4 *= a.a * inverse;

return c1 + c2 + c3 + c4;
}

This works as expected however if i want to add more textures do I need to pass in extra alphaUV texcoord's?

Since performance will decrease as I add more textures, should I be looking at another method if I wiish to support quite a few texture types? What other options do I have?

Share this post


Link to post
Share on other sites
Quote:
Original post by plebon
Since performance will decrease as I add more textures, should I be looking at another method if I wiish to support quite a few texture types? What other options do I have?
One good option is a texture atlas: pack all your tile textures into a single, larger texture. You have to get a bit cute with the texture coordinates because of mipmapping, but it keeps the number of textures down nicely. However, your approach of using an alpha map to blend the four textures together doesn't scale well to a larger number of textures, so you would need to switch to another heuristic to determine which texture belongs where.

Share this post


Link to post
Share on other sites
One option is to split the terrain up into chunks, where each chunk uses only four textures, but different chunks could use a different set of four textures. For example you could have a mountain with rock, snow, ice and grass and a hill with grass, mud, rock and sand. You just need to be careful to make the joins match up.

For a bit of extra speed it might also be worth having a separate shader for large areas of terrain that just use one texture.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!