HLSL draw selection(texture) on terrain

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Saile
Posts: 4
Joined: Mon Jul 07, 2008 5:57 pm

HLSL draw selection(texture) on terrain

Post by Saile »

Hello,

I'm new to shaders and looking to get my selections(textures) to draw on the terrain. I'm making a RTS game and used to draw selections with the 'render to texture' method, but I implemented a texture splat from an example on this forum and it works great but now I'm stuck on how to get my selections to draw. As I'm now using hlsl as texture splat, can use it to draw my selections aswell? The selections should be a circle on the terrain under a selected unit, the selection should move with the unit aswell.
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post by DavidJE13 »

Although that would be possible, it will only work for a limited number of units and be quite slow. Instead you should use flat shapes just above the terrain for selection rings - create a flat surface for each unit and texture it with a ring with transparent centre and corners.
By making it part of the unit it will automatically follow it about without any extra code, and you can show/hide it with the selection.

edit: you could also continue using render to target, and overlay the texture in the shader, but this will have a low resolution and be slower (though it will allow the texture to warp over the terrain). If you want to go that route, render the selection rings to a texture as before and supply it to the final ground texture. Then modify the shader to overlay it on the output (gl_color = (old formula) * (1.0 - rings.a) + rings * rings.a.
Note that this will remove a texture from the splatting, so you can only use 2-texture splatting with this method (unless you modify the splatting to use alpha for blending or to allow multiple textures in one, which I believe somebody did on these forums)
Saile
Posts: 4
Joined: Mon Jul 07, 2008 5:57 pm

Post by Saile »

I'd like to have it wrap over the terrain. Do the shader make it more high-res or is that dependant on how big my render-to-texture is?

however here's the texturesplat pixel shader output:
Where do I add the code you mentioned?

Code: Select all

PS_OUTPUT ps_main(in VS_OUTPUT input) 
{ 
   float texScale = 1.0; 	
   PS_OUTPUT output = (PS_OUTPUT)0; 

   vector a = tex2D(AlphaMap, input.alphamap); 
   vector i = tex2D(TextureOne, mul(input.tex, texScale));
   vector j = tex2D(TextureTwo, mul(input.tex, texScale)); 
   vector k = tex2D(TextureThree, mul(input.tex, texScale)); 

   float4 oneminusx = 1.0 - a.x; 
   float4 oneminusy = 1.0 - a.y; 
   float4 oneminusz = 1.0 - a.z; 

   vector l = a.x * i + oneminusx * i; 
   vector m = a.y * j + oneminusy * l; 
   
   vector n = a.z * k + oneminusz * m;

   output.diffuse = n ;

   return output; 
} 
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post by DavidJE13 »

the resolution of the markers is dependant on the texture size you render to, and that is limited by the hardware. You usually can't get away with more than 4096x4096, which will be low quality on large terrains. (also I wouldn't recommend using such a big texture for such a small part of the game) The shader has no effect on the quality.

You could use some special logic to only render the part near the camera and display it in the correct position with the shader. This would be an advanced technique but would have good results.

The code I mentioned can be put between "vector n ..." and "output.diffuse = n". It should look like this;

Code: Select all

n = n * (1.0-k.a) + k * k.a;
Also replace the "vector k" line with

Code: Select all

vector k = tex2D(TextureThree,input.alphamap);
and send the rings texture as the 4th texture, replacing the final detail texture.

If you still want to use 3 textures in the splatting, you'll need to adapt the rest of the code, or find a way to send a 5th texture through Irrlicht. It can be done but is more difficult. Search the forums for better splatting techniques, I know there are a couple around.
Saile
Posts: 4
Joined: Mon Jul 07, 2008 5:57 pm

Post by Saile »

Nice it works good if I use a premade texture with alpha. Is it possible to assign alpha to the RenderToTexture, I can't find any solutions.
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post by DavidJE13 »

just clear the back buffer with a transparent colour; SColor( 0u, 0u, 0u, 0u ); and draw with EMT_TRANSPARENT_ALPHA_CHANNEL
Saile
Posts: 4
Joined: Mon Jul 07, 2008 5:57 pm

Post by Saile »

Great it works, thanks for the help.
Post Reply