Game Development Community

Dynamic light on translucent material...

by Tobias Niva · in Torque Game Engine Advanced · 10/17/2008 (10:22 am) · 7 replies

Hi.

I've noticed that my model (DTS with a standard material) doesn't get lit by dynamic lights (just the directional sun) as soon as I set translucency to true.

I looked up this info at TDN:
tdn.garagegames.com/wiki/TorqueShaderEngine/LightingSystem/Custom_Material_Light...

But after trying to modify the DiffuseBump-shader that comes with TGEA 1.7.1 (diffBumpV.hlsl/diffBumpP.hlsl) i can't more than realize that hlsl isn't for me... *deep sigh*
There's so much to take into account - lightmap, fog etc, etc - in the end...

Is there a way to get a standard material set to translucent = true...receive dynamic light?!

I also tried to look at the shaders procedurally generated, to get a grip of what's going on (am I right believing that my standard material actually uses one of these procedurally generated ones?!) but with no direct luck...

#1
10/17/2008 (10:38 am)
I presume you are tring to make some glass, so that something is transulcent and have a specular highlight. If so you must make a custom shader for this and or attach one of the existing shaders for your needs.
#2
10/17/2008 (10:41 am)
Hmm. Actually I'm making a wooden fence...and rather than having a couple of hundred planks modelled (performance wise) I have a texture with an alpha channel for the gaps between the planks. But it looks wierd when no dynamic lights affect it...
#3
10/17/2008 (10:53 am)
Actually putting a whole lot static shapes in as a fence, won't cause too much of a performace hit. Check out BixBy and the Isle of charms you'll see we used mesh for each post and plank
#4
10/18/2008 (10:47 am)
Yeah...of course I can model these planks...but I'll need this to work further on anyway (making fences like this one i49.photobucket.com/albums/f251/knock3xs/DSCF0086-1.jpg...and it would be quite dense polygon wise...and thus a performance sinker).

I was able to link the dynamic light shaders to my custom material;

new CustomMaterial(mat_fence)
{
mapTo = "plank";
texture[0] = "plank";
texture[1] = "barrel_1_normal";
shader = DiffuseBump;
version = 2.0;
dynamicLightingMaterial = AtlasDynamicLightingMaterial;
dynamicLightingMaskMaterial = AtlasDynamicLightingMaskMaterial;
preload = true;
translucent = true;
translucentBlendOp = LerpAlpha;
translucentZWrite = true;
blendOp = LerpAlpha;
};

..and it works as the standard material - as soon as the translucent parameters are used, the dynamic lighting vanishes!
#5
10/18/2008 (4:52 pm)
Since using the translucent parameters in both standard and custom material making the lighting from the Atlas-shaders go away...I tried to merge this dynamic lighting directly into the DiffuseBump-shader (the vertex and pixel shaders at bottom of this page tdn.garagegames.com/wiki/TorqueShaderEngine/LightingSystem/Custom_Material_Light...)...

This is what I got so far...

Vertex-shader:

#define IN_HLSL
#include "hlslStructs.h"
#include "shdrConsts.h"
#include "lightingSystem/shdrLib.h"

//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------

struct ConnectData
{
float4 hpos : POSITION;
float2 outTexCoord : TEXCOORD0;
float2 bumpCoord : TEXCOORD1;
float3 outLightVec : TEXCOORD2;
float3 lightingCoord : TEXCOORD3;
};


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
ConnectData main( VertexIn_PNTTTB IN,
uniform float4x4 modelview : register(C0),
uniform float4x4 texMat : register(C4),
uniform float3 inLightVec : register(C24),
uniform float3 eyePos : register(C20),
uniform float morphT : register(C49),
uniform float4x4 objTrans : register(C12),
uniform float4 lightPos : register(VC_LIGHT_POS1),
uniform float4x4 lightingMatrix : register(VC_LIGHT_TRANS)

)
{
ConnectData OUT;

// Apply morph calculations.
//float4 realPos = IN.pos + (IN.morphCoord * morphT);
//realPos.w = 1;

OUT.hpos = mul(modelview, IN.pos);

float4 texCoordExtend = float4( IN.uv0, 0.0, 1.0 );
OUT.outTexCoord = mul(texMat, texCoordExtend);
OUT.bumpCoord = OUT.outTexCoord;

float3x3 objToTangentSpace;
objToTangentSpace[0] = IN.T;
objToTangentSpace[1] = IN.B;
objToTangentSpace[2] = IN.normal;

OUT.outLightVec.xyz = -inLightVec;
OUT.outLightVec.xyz = mul(objToTangentSpace, OUT.outLightVec);
OUT.outLightVec = OUT.outLightVec / 2.0 + 0.5;

// Let's get some lighting calcs in here.
OUT.lightingCoord = getDynamicLightingCoord(IN.pos, lightPos, objTrans, lightingMatrix);

return OUT;
}

Pixel-shader:

#define IN_HLSL
#include "shdrConsts.h"
#include "lightingSystem/shdrLib.h"

//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------
struct ConnectData
{
float2 texCoord : TEXCOORD0;
float2 bumpCoord : TEXCOORD1;
float3 lightVec : TEXCOORD2;
float3 lightingCoord : TEXCOORD3;
};


struct Fragout
{
float4 col : COLOR0;
};


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
uniform sampler2D diffuseMap : register(S0),
uniform sampler2D bumpMap : register(S1),
uniform float4 ambient : register(C3),
uniform sampler3D dlightMap : register(S0),
uniform float4 lightColor : register(PC_DIFF_COLOR)
)
{
Fragout OUT;

OUT.col = tex2D(diffuseMap, IN.texCoord);
float4 bumpNormal = tex2D(bumpMap, IN.bumpCoord);

IN.lightVec = IN.lightVec * 2.0 - 1.0;
float4 bumpDot = saturate( dot(bumpNormal.xyz * 2.0 - 1.0, IN.lightVec.xyz) );

OUT.col *= bumpDot + ambient;

OUT.col = OUT.col * getDynamicLighting(dlightMap, IN.lightingCoord, lightColor);

// alpha is sum(r,g,b) so we can filter unlit pixels with alpha test.
OUT.col.w = OUT.col.x + OUT.col.y + OUT.col.z;

return OUT;
}

I can't get it right though...the getDynamicLighting does not seem to give me the light from the source I have next to my model...

Any thoughts?
#6
10/28/2008 (3:25 pm)
I exactly have the same problem. Tried merging the dynamic lighting into one shader, but all I get is just full ambient (lightColor).

It looks as if the dynamic lighting is in fact ambient lighting for the sun in this case. I can imagine that the use of a separate material is essential so that the correct light info can be fed the shader each time, and the result is passed to the base shader... how?

Any thought would be much appreciated, right now, I can't seem to get dynamic lights working on my TSStatic at all.

-- Konrad
#7
11/05/2008 (3:48 am)
@Konrad:
I have no problems lighting a TSStatic with dynamic light using standard material...as long as it's NOT translucent

When using a custom material - add these two lines in your material def. (as seen in previous post above also);

dynamicLightingMaterial = AtlasDynamicLightingMaterial;
dynamicLightingMaskMaterial = AtlasDynamicLightingMaskMaterial;

...or am I getting you all wrong?