Using Two Detail Textures On Atlas
by J.C. Smith · in Torque Game Engine Advanced · 02/15/2007 (11:35 am) · 10 replies
Someone had requested this in another thread, and had an hour or two tonight to play around and hacked it together really quick. This will allow you to use two detail textures on atlas. The second detail texture is fixed at 10% of the scale of the first one, but can easily be modified in the shader.
First you need to modify your shaders/atlas/atlas.h's VertDetailConnectData to add this to the end of it:
Change your shaders/atlas/atlasdetailp.hlsl to look like this:
And make the shaders/atlas/atlasdetailV.hlsl look like this:
First you need to modify your shaders/atlas/atlas.h's VertDetailConnectData to add this to the end of it:
float2 detCoord2 : TEXCOORD3;
Change your shaders/atlas/atlasdetailp.hlsl to look like this:
struct ConnectData
{
float4 hpos : POSITION;
float2 detCoord : TEXCOORD0;
float3 fade : TEXCOORD1;
float2 detCoord2 : TEXCOORD3;
};
struct Fragout
{
float4 col : COLOR0;
};
Fragout main( ConnectData IN,
uniform sampler2D detMap : register(S0),
uniform sampler2D detMap2 : register(S3)
)
{
Fragout OUT;
float4 diff = (tex2D(detMap, IN.detCoord) * tex2D(detMap2, IN.detCoord2)) * 0.6 + 0.2;
OUT.col = lerp(float4(0.5, 0.5, 0.5, 0.5), diff, IN.fade.x);
return OUT;
}And make the shaders/atlas/atlasdetailV.hlsl look like this:
#define IN_HLSL
#include "../shdrConsts.h"
#include "atlas.h"
//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
VertDetailConnectData main( VertData IN,
uniform float4x4 modelView : register(C0),
uniform float3 eyePos : register(VC_EYE_POS),
uniform float morphT : register(C49),
uniform float3 detData : register(C50)
)
{
VertDetailConnectData OUT;
// Apply morph calculations.
float4 realPos = IN.position + (IN.morphCoord * morphT);
realPos.w = 1;
// Transform and return.
OUT.hpos = mul(modelView, realPos);
// Do the detail map calculations.
float eyeDist = distance( IN.position, eyePos );
OUT.detCoord = IN.texCoord * detData.z; // Amplify texcoords.
OUT.detCoord2 = IN.texCoord * (detData.z * 0.1); // 10% resolution
OUT.fade.x = 1.0 - (eyeDist - detData.x) * detData.y; // And fade with distance.
OUT.fade.yz = 0.f;
return OUT;
}
#2
in atlas/atlasinstance2.h right beneath where you set up the detail textures:
in atlas/atlasinstance2.cpp do a search of detailtex and in each respective area you need to make an identical version that changes detailtex to detailtex2 in the source.
For example under the:
You would add:
Do this for all instances of detailtex.
Finally in the RenderDetail function in atlas/runtime/atlasclipmapbatcher.cpp underneath this bit of code:
add this:
The shader will do the rest of the work. Enjoy.
02/15/2007 (11:46 am)
Next you just need to make a few source code changes....in atlas/atlasinstance2.h right beneath where you set up the detail textures:
GFXTexHandle mDetailTex2; StringTableEntry mDetailTex2FileName;
in atlas/atlasinstance2.cpp do a search of detailtex and in each respective area you need to make an identical version that changes detailtex to detailtex2 in the source.
For example under the:
addField("detailTex", TypeFilename, Offset(mDetailTexFileName, AtlasInstance2));You would add:
addField("detailTex2", TypeFilename, Offset(mDetailTex2FileName, AtlasInstance2));Do this for all instances of detailtex.
Finally in the RenderDetail function in atlas/runtime/atlasclipmapbatcher.cpp underneath this bit of code:
GFX->setTexture(0, ((AtlasInstance2*)mObject)->mDetailTex); GFX->setTextureStageAddressModeU(0, GFXAddressWrap); GFX->setTextureStageAddressModeV(0, GFXAddressWrap);
add this:
GFX->setTexture(3, ((AtlasInstance2*)mObject)->mDetailTex2); GFX->setTextureStageAddressModeU(3, GFXAddressWrap); GFX->setTextureStageAddressModeV(3, GFXAddressWrap);
The shader will do the rest of the work. Enjoy.
#4
02/17/2007 (1:25 am)
This is great, and i didn't have much trouble getting it up and running. Now what i need if you can help is a way to tie this into using 2 alpha maps to regulate where what detail gets put where. That way all you have to do is paint it up in photo shop and with a little playing around, you could have some superior roads and such:)
#5
What I was thinking of trying though was to take one detail map, basically have the details as 8 bit grayscales... so you have 4 8 bit grayscale details, and then to apply that a different channel for each texture tile. Though when I took a quick glance at the code, it didn't look like it would be very easy to do.
02/17/2007 (6:46 am)
I was thinking of playing with this on blended terrains but haven't had much time yet. Have two projects I had told people I'd port to TGEA when it was released and been spending the past couple days doing that. What I was thinking of trying though was to take one detail map, basically have the details as 8 bit grayscales... so you have 4 8 bit grayscale details, and then to apply that a different channel for each texture tile. Though when I took a quick glance at the code, it didn't look like it would be very easy to do.
#6
02/17/2007 (11:48 am)
The only reason i suggested the alpha maps was because I had another shader(but for 3dgamestudio....ekkkk!!!), and it used alphas. it was just easy that all, but I'm sure by looking at what is above you know what your doing, i just try to keep ideas going because this is what I go to school for and I rally want to be proficient at writing shaders, as far as I'm concerned they are the cornerstone for most effects in games these days:)
#7
02/19/2007 (4:47 am)
I had a couple hours to play with it tonight. The previous idea will not work. I'm going to try some other things instead.
#8
Ideally, what you'd do is allow a fallback to disable detail textures entirely.
02/19/2007 (9:56 am)
If you can bind another texture, then you can easily do a blend between the two details. But as always, its a matter of number of texture units available.Ideally, what you'd do is allow a fallback to disable detail textures entirely.
#9
02/19/2007 (12:31 pm)
I was playing with the numbers in the legacy shader code, and found the value that handles the tiling for the detail. Then i got the idea to put in full color detail textures, in this case I used some grass. It looked pretty nice as far a quality was concerned of the texture, but it is one repeating texture over the entire terrain, so not ideal:) Then i realized i wasnt restricted to the 256x256 textures side cause this isn't TGE its TGEA and we use HLSL shaders, which had no problem using a 2048x2048 texture. Once I found that out i decided to paint a 1024x1024 detail texture with more than one detail texture on it, and then make it a reapeating texture. It worked pretty well, and it got me thinking. if this is possible with the detail texture, it must be able to do similar thing to the other textures. But for the life of me i cant find the values in the shader that handle the repeating of the non-detail textures and their size, I had no trouble finding it for the Atlas shaders, and if i paint the alpha's myself i can almost paint roads.
#10
Ingame it makes the terrain appear normal mapped with the right shader setting..

Thanks for this addition JC ;)
addikt
03/06/2007 (11:43 am)
In case anyones wondering, here the difference between using 1 detail texture and 2...Ingame it makes the terrain appear normal mapped with the right shader setting..

Thanks for this addition JC ;)
addikt
Torque 3D Owner Dave Young
Dave Young Games