Trying to port a shader from T3D to TGEA 1.8.1
by Matt Jolly · in Torque 3D Professional · 06/03/2009 (12:49 am) · 4 replies
I'll post this here in case T3D code is eventually posted:
I'm trying to implement this shader in TGEA 1.8.1, but I can't seem to find a way to fetch depth/normal data from the scene in TGEA. Does anyone know if that's possible or where I would begin to try and implement it? I've already tried porting code from depthHLSL and renderPrePassMgr, but haven't had any luck yet. T3D and TGEA differ enough that I get 10 new errors for every one I fix--so is there a quick and dirty way to find that information and pass it to a shader? Otherwise I will just have to continue blindly porting random bits of T3D, and hope I don't break my build.
P.S.
I've already implemented the fullscreen shader effects resource, I just need to pass depth/normal info along with diffuse info now.
I'm trying to implement this shader in TGEA 1.8.1, but I can't seem to find a way to fetch depth/normal data from the scene in TGEA. Does anyone know if that's possible or where I would begin to try and implement it? I've already tried porting code from depthHLSL and renderPrePassMgr, but haven't had any luck yet. T3D and TGEA differ enough that I get 10 new errors for every one I fix--so is there a quick and dirty way to find that information and pass it to a shader? Otherwise I will just have to continue blindly porting random bits of T3D, and hope I don't break my build.
P.S.
I've already implemented the fullscreen shader effects resource, I just need to pass depth/normal info along with diffuse info now.
#2
depthV.hlsl
If I use DepthOut instead of EyeSpaceDepthOut, it renders everything with a yellow-tint (as it should), but it's still all one flat color. So something must not be write with the depth calculations.
Edit: With some further tinkering I managed to get this:

That was using DepthOut, not EyeSpaceDepthOut. Still not quite right though, and the color shifts erratically when the camera is moved.
06/07/2009 (2:18 am)
I think I'm getting closer to having depthHLSL working in TGEA. As of now it is applied to all objects (except the terrain, probably because that has its own shader), and overwrites the RGB values so I can see it. Unfortunately, the RGB values, and therefore the depth information, are not correct. It renders everything black. Here are the generated depth shaders:depthV.hlsl
// TGEA -- HLSL procedural shader
//-----------------------------------------------------------------------------
//Structures
//-----------------------------------------------------------------------------
// Features:
// Vert Position
// Base Texture
// Vert Light Color
// Bumpmap
// Visibility
// Eye Space Depth (Out)
// Fog
struct VertData
{
float2 texCoord : TEXCOORD0;
float2 lmCoord : TEXCOORD1;
float3 T : TEXCOORD2;
float3 B : TEXCOORD3;
float3 normal : NORMAL;
float4 position : POSITION;
};
struct ConnectData
{
float4 hpos : POSITION;
float2 outTexCoord : TEXCOORD0;
float4 shading : COLOR;
float3 outLightVec : TEXCOORD1;
float4 wsEyeVec : TEXCOORD2;
float2 fogCoord : TEXCOORD3;
};
ConnectData main( VertData IN,
uniform float4x4 modelview : register(C0),
uniform float4 inLightColor : register(C8),
uniform float3 inLightVec : register(C9),
uniform float4x4 objTrans : register(C4),
uniform float3 eyePosWorld : register(C10),
uniform float3 fogData : register(C11)
)
{
ConnectData OUT;
OUT.hpos = mul(modelview, IN.position);
OUT.outTexCoord = IN.texCoord;
OUT.shading = inLightColor;
float3x3 objToTangentSpace;
objToTangentSpace[0] = IN.T;
objToTangentSpace[1] = IN.B;
objToTangentSpace[2] = normalize(IN.normal);
OUT.outLightVec.xyz = -inLightVec;
OUT.outLightVec.xyz = mul(objToTangentSpace, OUT.outLightVec);
OUT.wsEyeVec = mul(objTrans, float4(IN.position.xyz,1)) - float4(eyePosWorld, 0.0);
// fog setup
float3 transPos = mul( objTrans, IN.position );
OUT.fogCoord.x = 1.0 - ( distance( transPos, eyePosWorld ) / fogData.z );
OUT.fogCoord.y = (transPos.z - fogData.x) * fogData.y;
return OUT;
}depthP.hlsl// TGEA -- HLSL procedural shader
//-----------------------------------------------------------------------------
//Structures
//-----------------------------------------------------------------------------
// Features:
// Vert Position
// Base Texture
// Vert Light Color
// Bumpmap
// Visibility
// Eye Space Depth (Out)
// Fog
struct ConnectData
{
float2 texCoord : TEXCOORD0;
float4 shading : COLOR;
float3 lightVec : TEXCOORD1;
float4 wsEyeVec : TEXCOORD2;
float2 fogCoord : TEXCOORD3;
};
struct Fragout
{
float4 col : COLOR0;
};
Fragout main( ConnectData IN,
uniform sampler2D diffuseMap : register(S0),
uniform float4 ambient : register(C0),
uniform sampler2D bumpMap : register(S1),
uniform float visibility : register(C1),
uniform float3 vEye : register(C2),
uniform sampler2D fogMap : register(S2)
)
{
Fragout OUT;
OUT.col = tex2D(diffuseMap, IN.texCoord);
float shadowed = 1.0;
float4 bumpNormal = tex2D(bumpMap, IN.texCoord);
bumpNormal.xyz = bumpNormal.xyz * 2.0 - 1.0;
float4 bumpDot = saturate( dot(bumpNormal.xyz, IN.lightVec.xyz) );
OUT.col *= (IN.shading * bumpDot * shadowed) + ambient;
OUT.col.w *= visibility;
float eyeSpaceDepth = dot(vEye, (IN.wsEyeVec.xyz / IN.wsEyeVec.w));
OUT.col *= float4( eyeSpaceDepth, eyeSpaceDepth, eyeSpaceDepth, 1 );
float4 fogColor = tex2D(fogMap, IN.fogCoord);
OUT.col.rgb = lerp(OUT.col.rgb, fogColor.rgb, fogColor.a);
return OUT;
}If I use DepthOut instead of EyeSpaceDepthOut, it renders everything with a yellow-tint (as it should), but it's still all one flat color. So something must not be write with the depth calculations.
Edit: With some further tinkering I managed to get this:

That was using DepthOut, not EyeSpaceDepthOut. Still not quite right though, and the color shifts erratically when the camera is moved.
#3
You should look for one of the procedural shaders that has Eye Space Depth (Out) and GBuffer Conditioner. Those are the ones that output normals and depth that is later used to do lighting and in PostEffect.
06/07/2009 (2:53 am)
Starting from one of the ShaderGen procedural shaders isn't a bad way to do it... but you should start with the right shader. :)You should look for one of the procedural shaders that has Eye Space Depth (Out) and GBuffer Conditioner. Those are the ones that output normals and depth that is later used to do lighting and in PostEffect.
#4
06/07/2009 (4:11 am)
Will do, thanks.
Associate Tom Spilman
Sickhead Games
It would be difficult to port over as it is, but you could use the idea of it to get started.