Game Development Community

Atlas2/Legacy Terrain Detail Shader.. What Gives?

by Bryce "Cogburn" Weiner · in Torque Game Engine Advanced · 11/27/2006 (12:00 am) · 6 replies

Sorry if this has been answered, but TGEA knowledge is a little... scattered at the moment.

I've searched all through the forums and found a few implementations for detail textures on Atlas2 and Legacy terrains.

My shader knowledge is slowly growing, but here's an implementation I found that I'm using now for my altasSurfaceP.hlsl:
//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
struct ConnectData
{
   float4 shading         : COLOR;
   float2 texCoord        : TEXCOORD0;
   float2 fogCoord        : TEXCOORD1;
   float2 detailCoord     : TEXCOORD2;
   float2 detailCoord2    : TEXCOORD3;
};


struct Fragout
{
   float4 col : COLOR0;
};


//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform sampler2D diffuseMap      : register(S0),
              uniform sampler2D fogMap          : register(S1),
              uniform sampler2D detailTex       : register(S2),
              uniform sampler2D detailMap2      : register(S3)
              )
{
   //Fragout OUT;
   
   //float4 diffuseColor = tex2D(diffuseMap, IN.texCoord) * IN.shading;
   //float4 fogCol       = tex2D(fogMap,     IN.fogCoord);
   //float4    detailCol  =  tex2D(detailMap,   IN.detailCoord);
   //          detailCol  *= tex2D(detailMap2,  IN.detailCoord2);

   //OUT.col = lerp( diffuseColor * (detailCol + 0.6), fogCol, fogCol.a );
   
   //return OUT;
  // -----------------------------------------------------------------------------
   // OUT will contain the color to be drawn at this pixel.  This bit of code's job
   // is to set that color value!
   // -----------------------------------------------------------------------------
   Fragout OUT;
   
   // -----------------------------------------------------------------------------
   // Get a single pixel (red, green, blue value) from the base TQT image that should be drawn 
   // at this point on the player's screen.
   // -----------------------------------------------------------------------------
   float4 diffuseColor = tex2D(diffuseMap, IN.texCoord) * IN.shading;

#1
11/27/2006 (12:00 am)
(Continued)
// -----------------------------------------------------------------------------
   // Get the color of the fog.  If the map were 100% foggy, this is the color you would see.
   // Normally it's some kind of grey color etc.
   // -----------------------------------------------------------------------------
   float4 fogCol       = tex2D(fogMap,     IN.fogCoord);

   // -----------------------------------------------------------------------------
   // Get a single pixel (red, green, blue value) from the detail texture.  We'll multiply
   // the diffuseColor by this value to add some "detail" noise to the ground.
   // -----------------------------------------------------------------------------
   float4 detailCol  = tex2D(detailTex,   IN.detailCoord);

   // -----------------------------------------------------------------------------
   // Combine the base texture color and the detail color
   // -----------------------------------------------------------------------------
   //float4 finalCol = diffuseColor * (detailCol + 0.6);
// -----------------------------------------------------------------------------
   // First, get the appropriate pixel from the detail texture
   // -----------------------------------------------------------------------------
   float4 bumpNormal = tex2D(detailTex, IN.detailCoord);

   // -----------------------------------------------------------------------------
   // Next, we need to set a vector that represents the DIRECTION that the light is 
   // shining.  Note that we REALLY should be grabbing this value from the "Sun"
   // entry in the mission file.  It has a light direction value.
   // -----------------------------------------------------------------------------
   float4 lightVector = { 0.6f, 0.6f, -0.5f, 1.0f };

   // -----------------------------------------------------------------------------
   // Scale the light vector so it varies from -1 to 1 instead of 0.0 to 1.0.  I am
   // I confess not sure why you do this, but I saw it in an example so I'm doing it
   // here, and it seems to work! :^)
   // -----------------------------------------------------------------------------
   lightVector = lightVector * 2.0 - 1.0;

   // -----------------------------------------------------------------------------
   // Compute how much extra light or shadow we should apply to this pixel based on the
   // angle between the light and this particular spot on the normal map.  
   // -----------------------------------------------------------------------------
   float4 bumpDot = saturate (dot (bumpNormal.xyz * 2.0 - 1.0, lightVector.xyz));

   // -----------------------------------------------------------------------------
   // Lastly, apply this extra light or shadow value to the pixel we're going
   // -----------------------------------------------------------------------------
   float4 finalCol = diffuseColor * bumpDot;
   // -----------------------------------------------------------------------------
   // Linearly interpolate between the ground color (finalCol) and the
   // full fog color (fogCol).  The value fogCol.a varies from 0.0 to
   // 1.0 depending on how far away the terrain pixel is from the viewer.
   // If the terrain is directly in front of the viewer, then the value
   // will be 0.0 (no fog).  If the point is at or beyond the maximum fog
   // distance, then the value will be 1.0 (full fog).
   // -----------------------------------------------------------------------------
   //OUT.col = lerp( finalCol, fogCol, fogCol.a );
	OUT.col = lerp( diffuseColor * (detailCol + 0.6), fogCol, fogCol.a );
   // -----------------------------------------------------------------------------
   // Note that if you want to just show the ground color and have no fog, you
   // could just use the following line instead.  Uncomment it and you will have
   // no fog.
   // OUT.col = finalCol;
   // -----------------------------------------------------------------------------
   
   // -----------------------------------------------------------------------------
   // Return the OUT structure, which contains the color to be drawn at this point
   // -----------------------------------------------------------------------------
   return OUT;
}

A few folks claimed this implementation worked for them, but in using MS4.5, I cannot get it to work on Atlas2 or legacy terrains.

Are terrain shaders simply turned off at the moment?

If not and the above shader code is correct, what is it that I'm missing in all this that is preventing this from working?

Oddly enough, of all the new features TGEA has implemented, I've found this to be the most frustrating. :(
#2
11/27/2006 (12:10 am)
Atlas2 uses shaders in the Atlas folder, not the AtlasSurfaceP/AtlasSurfaceV in the root of the Shaders folder
#3
11/27/2006 (12:11 am)
They definately don't work at all in MS4.
#4
11/27/2006 (12:58 am)
I implemented (contracted out) detail shaders for legacy and Atlas terrains in MS4. Can not spot much difference in yours than mine, the basic idea is the same - and mine works in newest TGEA just fine.

Are you sure you did not enable any of the 'disable' macros in the prefs file?

Edit: If your solution does not work anymore, check out the new legacy terrain detail shaders that Brian made. Should get you going in the right direction.
#6
12/12/2006 (3:35 pm)
Depends on your definition of "work." Currently you have to adjust them in the materials.cs file, the mission editor doesn't control detail textures.