Game Development Community

Custom Shader on T3D?

by Alfio Saitta · in Torque 3D Professional · 04/19/2011 (10:02 am) · 37 replies

I'm trying since yesterday to import a shader into the T3D. But i always get an unexpected result. The shader is maded on RenderMonkey, and edited to adapt to the T3D. But i just can not get it to work.

I think the problem is relates to the matrixes. But i did not find documentation on how to import them.

The working shader:


The result:
img194.imageshack.us/img194/1145/screenshot00100000a.th.png

Material Definition:
singleton CustomMaterial( SkinShaderMaterial )
{   
   mapTo = "Skin_defaultMat_Map-COL.jp";
   
   sampler["DiffSampler"] = "art/shapes/SkinShader/Map-1024_COLOR";
   sampler["NormalSampler"] = "art/shapes/SkinShader/Map-1024_NRM";
   sampler["SpecSampler"] = "art/shapes/SkinShader/Map-1024_SPEC";
   sampler["MicroSampler"] = "art/shapes/SkinShader/Default_MicroDetail_NRM";
   sampler["TransSampler"] = "art/shapes/SkinShader/Map-1024_OCC";
   
   shader = SkinShader;
   //stateBlock = SkinStateBlock;
   version = 3.0;
};

Shader Definiton:
singleton ShaderData( SkinShader )
{
   DXVertexShaderFile     = "shaders/common/SkinV.hlsl";
   DXPixelShaderFile      = "shaders/common/SkinP.hlsl";   
   
   //OGLVertexShaderFile     = "shaders/common/gl/SkinV.glsl";
   //OGLPixelShaderFile      = "shaders/common/gl/SkinP.glsl";
   
   pixVersion = 3.0;
};

// DEFINED BUT DON'T USED ->
singleton GFXStateBlockData( SkinStateBlock )
{
   samplersDefined = true;
   samplerStates[0] = SamplerClampLinear;
   samplerStates[1] = SamplerClampLinear;
   samplerStates[2] = SamplerClampLinear;
   samplerStates[3] = SamplerClampLinear;
   samplerStates[4] = SamplerClampLinear;

   //cullDefined = true;
   //cullMode = "GFXCullNone";
};

Part of the PS:
#define IN_HLSL
#include "shdrConsts.h"
#include "shadergen:/autogenConditioners.h"
#include "./torque.hlsl"

uniform sampler2D DiffSampler: register( S0 );
uniform sampler2D NormalSampler: register( S1 );
uniform sampler2D SpecSampler: register( S2 );
uniform sampler2D MicroSampler: register( S3 );
uniform sampler2D TransSampler: register( S4 );
 
uniform int      UseMicroDetail = 1;
uniform int      UseSpecColour = 0;

uniform float    MicroScale = 55.0;
uniform float    MicroFactor = 5.0;

uniform float    SpecPower = 0.28;
uniform float    SpecGloss = 4.5;
uniform float    SpecFresnel = 3.4;

uniform float    FresnelPower = 10.0;
uniform float    FresnelGloss = 2.3;

uniform float    TransMultiplier = 0.9;
uniform float    TransRampOff = 0.4;

uniform float4   SpecColour = {0.45, 0.65, 1.00, 1.0};
uniform float4   TransColIn = {0.81, 0.91, 0.96, 1.0};
uniform float4   TransColOut = {1.0, 0.71, 0.32, 1.0};
uniform float4   TransColBack = {0.58, 0.20, 0.24, 1.0};

uniform float4   LightColour = {0.89, 0.89, 0.89, 1.0};

struct ConnectData
{
   float4 Position      : POSITION0;
   float2 TexCoord      : TEXCOORD0;
   float3 WorldNormal   : TEXCOORD1;
   float3 EyeVec        : TEXCOORD2;
   float3 LightVec      : TEXCOORD3;
   float3 WorldTangent  : TEXCOORD4;
   float3 WorldBinormal : TEXCOORD5;
};

....

float4 main( ConnectData IN ) : COLOR
{
   ....
}

Part of the VS:
#include "shdrConsts.h"
#include "shadergen:/autogenConditioners.h"

uniform float4x4 matWorld;
uniform float4x4 matWorldInverseTranspose;
uniform float4x4 matWorldViewProjection;
uniform float4x4 matViewInverse;

uniform float DisplacementScale;
uniform float DisplacementBias;

uniform float3 LightPosition; 

struct VertData
{
   float4 Position : POSITION0;
   float2 UV      :   TEXCOORD0;
   float3 Normal   :   NORMAL;
   float3 Tangent   :    TANGENT;
   float3 Binormal   :    BINORMAL;
};

struct ConnectData
{
   float4 Position : POSITION;
   float2 TexCoord      :   TEXCOORD0;
   float3 WorldNormal   :   TEXCOORD1;
   float3 EyeVec           :   TEXCOORD2;
   float3 LightVec      :   TEXCOORD3;
   float3 WorldTangent   :    TEXCOORD4;
   float3 WorldBinormal   :    TEXCOORD5;
};

ConnectData main( VertData Input )
{
   ConnectData Output;
  
   ....
   
   return Output;
}
Page «Previous 1 2
#1
04/19/2011 (11:29 am)
I would like to know that, too.
#2
04/19/2011 (11:49 am)
GG devs,
are all new shaders for Torque engines written by hand only?

if so, please could we get a few "templates"?

but if you are using a program, like render monkey or whatever, please can we get a few video tutorials?

shaders are one of the least understood, yet very powerful tools torque artists have to use...

this is an official request I guess...
#3
04/19/2011 (11:57 am)
Basically this is your transformation stuff:

matWorldViewProjection = modelview
matView = eyeMat = eyeWorld ^ -1
matWorld = objTrans
matViewInverse = eyeMat ^ -1
matWorldInverseTranspose = (objTrans . T) ^ -1

I think the last two transforms are not exposed (sent) to VS,you have to do it manually.
Also pay attention on InverseTranspose operations: firstly you must transpose ,then inverse the result.

Additional notes: T3D does not send the binormal(bitangent), you have to cross T and N on the GPU.
#4
04/21/2011 (5:46 am)
@Ivan: Thanks for the clarification.

I fixed a couple of arrays, but now i still can not find the vectors of the camera and lights.

Then, surprisingly, i noticed the texture of another model has been applied to my model ingame. But this does not occurs in ShapeEditor.

EDIT: If i remove the other objects in the scene, my model acquire the texture of the terrain.

img577.imageshack.us/img577/3006/screenshot00200000.th.png img836.imageshack.us/img836/1940/screenshot00200001.th.png

The new VS:
#define IN_HLSL
#include "hlslStructs.h"

//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
struct ConnectData
{
   float4 Position      : POSITION;
   float2 TexCoord      : TEXCOORD0;
   float3 EyeVec        : TEXCOORD1;
   float3 LightVec      : TEXCOORD2;
   float3 WorldNormal   : NORMAL;
   float3 WorldTangent  : TANGENT;
   float3 WorldBinormal : BINORMAL;
};

//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
ConnectData main( VertexIn_PNTTTB IN,
	uniform float4x4 modelview : register(C0),
	uniform float4x4 objTrans : register(C4)
)
{
   ConnectData OUT;
   

   OUT.WorldNormal = mul( transpose(objTrans), IN.normal ).xyz;
   OUT.WorldTangent = mul( transpose(objTrans), IN.T ).xyz;
   OUT.WorldBinormal = mul( transpose(objTrans), IN.B ).xyz;

   float3 WorldSpacePos = mul(IN.pos, objTrans).xyz;

   // WRONG VECTORS ->
   OUT.EyeVec = WorldSpacePos;
   OUT.LightVec = WorldSpacePos; // inLightPos.xyz - WorldSpacePos;
   // <- WRONG VECTORS

   OUT.TexCoord.xy = IN.uv0;

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

   return OUT;
}

Part of the new PS:
uniform sampler2D DiffSampler: register( S0 );
uniform sampler2D NormalSampler: register( S1 );
uniform sampler2D SpecSampler: register( S2 );
uniform sampler2D MicroSampler: register( S3 );
uniform sampler2D TransSampler: register( S4 );

static const int      UseMicroDetail = 1;
static const int      UseSpecColour = 0;
static const float    MicroScale = 55.0;
static const float    MicroFactor = 0.5;
static const float    SpecPower = 1;
static const float    SpecGloss = 4.5;
static const float    SpecFresnel = 3.4;
static const float    FresnelPower = 10.0;
static const float    FresnelGloss = 2.3;
static const float    TransMultiplier = 0.9;
static const float    TransRampOff = 0.4;
static const float4   SpecColour = {0.45, 0.65, 1.00, 1.0};
static const float4   TransColIn = {0.81, 0.91, 0.96, 1.0};
static const float4   TransColOut = {1.0, 0.71, 0.32, 1.0};
static const float4   TransColBack = {0.59, 0.20, 0.24, 1.0};
static const float4   LightColour = {0.89, 0.89, 0.89, 0.0};

//-----------------------------------------------------------------------------  
// Structures                                                                    
//-----------------------------------------------------------------------------  
struct ConnectData
{
   float4 Position     : POSITION;
   float3 TexCoord     : TEXCOORD0;
   float3 EyeVec       : TEXCOORD1;
   float3 LightVec     : TEXCOORD2;
   float3 WorldNormal  : NORMAL;
   float3 WorldTangent : TANGENT;
   float3 WorldBinormal: BINORMAL;
};

//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
float4 main( ConnectData IN ) : COLOR
{
   float2 TexUV = IN.TexCoord.xy;
               
   float3 normal = normalize( tex2D(NormalSampler, TexUV ).xyz * 2.0 - 1.0 );
   
   float3 WN = IN.WorldNormal;
   float3 TN = IN.WorldTangent;
   float3 BN = IN.WorldBinormal;

   float3 N = (WN * normal.z * 2) + (normal.x * BN + normal.y * -TN);
   N = normalize(N);
   
   // WRONG VECTORS ->
   float3 EV = normalize(IN.EyeVec);
   float3 LV = normalize(IN.LightVec);
   // <- WRONG VECTORS

   float4 DotLN = dot(N, LV);

   ....

}
#5
04/21/2011 (6:59 am)
@Alfio - The second picture with only the head in it, it looks like the head itself is projected on it. Does the material/texture move when you move the camera around it?
I have problems when using the material selector, that it doesn't get applied and sometimes another model changes the material by doing so.

@deepscratch - Isn't the liman3D shader pack something like that?
#6
04/21/2011 (8:04 am)
@Ivan, thank for the BN tip. Now i'v crossed.

On VS:
ConnectData main( VertexIn_PNTTTB IN,
	uniform float4x4 modelview : register(C0),
	uniform float4x4 objTrans : register(C4)
)
{
   ConnectData OUT;

   OUT.WorldNormal = mul( transpose(objTrans), IN.normal ).xyz;
   OUT.WorldTangent = mul( transpose(objTrans), IN.T ).xyz;

   // NEW CROSSED BN ->
   //OUT.WorldBinormal = mul( transpose(objTrans), IN.B ).xyz;
   OUT.WorldBinormal = cross(OUT.WorldTangent, OUT.WorldNormal);
   // <- NEW CROSSED BN

   ....

}

On PS:
//================================Pixel shader - Complete================================
float4 main( ConnectData IN ) : COLOR
{

   float2 TexUV = IN.TexCoord.xy;
               
   float3 normal = normalize( tex2D(NormalSampler, TexUV ).xyz * 2.0 - 1.0 );  
   
   float3 WN = IN.WorldNormal;
   float3 TN = IN.WorldTangent;

   // NEW CROSSED BN ->
   //float3 BN = IN.WorldBinormal;
   float3 BN = cross(IN.WorldTangent,IN.WorldNormal); 
   // <- NEW CROSSED BN
  
   ....

}
#7
04/21/2011 (8:06 am)
Does it work now?
#8
04/21/2011 (9:03 am)
@Alexander: Only in part.
When i set manually the light and cam vector, work nearly great. But unfortunately still do not understand how to have some vectors in input.

img135.imageshack.us/img135/1540/screenshot00300000.th.png

At this point, instead of writing the shader inside RenderMonkey, i'll try to write directly into T3D. At least until i understand how to make a shader portable too complex shader as this.

It would be useful if the developers of the GG, gave guidelines and a list of variables (vectors and arrays) that are exposed by the engine.
#9
04/21/2011 (9:44 am)
Too bad. I wanna have awsomeness in my game as well.
Looks more difficult than I thought now.

Ivan has one shader in his pack that simulates subsurface scattering. Maybe that can be extended to a nice skin-shader?

Quote:...variables (vectors and arrays) that are exposed by the engine.

So that's what deepscratch meant?
#10
04/21/2011 (10:02 am)
- I've been complaining about the lack of Torque specific shader docs since TGEA when their Advertisement page clearly read :
[Courtesy of WayBackMachine]
"You can modify the shaders in TGEA's system, drop in shaders from 3rd party apps (e.g. FX Composer or RenderMonkey) or start from scratch. With TGEA, you can do it all".

Yeah, I know that TGEA was long ago and that was old GG 3 companies ago..and here's my point: if o9ld GG and TP never cared to bother with this, I wonder how much of this, anyone ever understood.
- Sickhead is clearly pushing forth on the shaders end of T3D...I hope that we can get them to write out some docs on this at some point.
-> Oh and BTW...Nothing EVER "dropped" into TGEA and worked. A little work was needed and that was never properly documented. You had to sort it with your own brain and fiddlestick.
---------
@(New)GG; can we have someone write a few pages on it ? Pulling apart the engine to write basic shaders is a bit of a stretch.
P.S. @ anyone reading and wanting to learn HLSL the easiest way I know how; CG vids
#11
04/21/2011 (11:27 am)
That would be like a dream to just drop FX Composer produced shader in my game. I could browse the nvidia shader library, that would be like:
.
.

But I hope documentation will follow the final 1.1
#12
04/21/2011 (11:36 am)
uniform float3 eyePos;

..
OUT.EyeVec = normalize(eyePos - IN.pos);
OUT.LightVec = normalize(sunPos - IN.pos);

As a start use a fixed vector for LightVec in order to get some results.
I think inLightPos is the sun position in T3D, refer to ShaderGenVars.cpp

Afaik these values are in SoA order:

- include lighting.hlsl

uniform float4 inLightPos[3];
..
float3 sunPos = float3(inLightPos[0].x, inLightPos[1].x, inLightPos[2].x);
#13
04/21/2011 (12:34 pm)
After some changes, and manually setting some vectors, i was able to render almost decently with the shader. Surely i still have not understand much, but they are on right road to understand the meanders of the hardware shaders inside the T3D.

As you can see from the pictures, if i use IN.WorldNormal instead of IN.TexCoord, i lost the bump mapping and the smoothing.

img29.imageshack.us/img29/2203/immagine001o.th.png img688.imageshack.us/img688/4062/immagine002.th.png

img856.imageshack.us/img856/4204/screenshot00400003.th.png img148.imageshack.us/img148/9824/screenshot00400004.th.png

The VS:
#define IN_HLSL
#include "hlslStructs.h"

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


struct ConnectData
{
   float4 Position      : POSITION;
   float2 TexCoord      : TEXCOORD0;
   float3 EyeVec        : TEXCOORD1;
   float3 LightVec      : TEXCOORD2;
   float3 WorldNormal   : NORMAL;
   float3 WorldTangent  : TANGENT;
   float3 WorldBinormal : BINORMAL;
};

//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
ConnectData main( VertexIn_PNTTTB IN,
	uniform float4x4 modelview : register(C0),
	uniform float4x4 objTrans : register(C4),
	uniform float3 sunPos,
	uniform float3 eyePos
)
{
   ConnectData OUT;

   OUT.WorldNormal = mul( transpose(objTrans), IN.normal).xyz;
   OUT.WorldTangent = mul( transpose(objTrans), IN.T ).xyz;
   //OUT.WorldBinormal = mul( transpose(objTrans), IN.B ).xyz;
   OUT.WorldBinormal = cross(OUT.WorldTangent, OUT.WorldNormal);

   float3 WorldSpacePos = mul(modelview, IN.pos).xyz;

   OUT.EyeVec = WorldSpacePos;
   OUT.LightVec = WorldSpacePos; // inLightPos.xyz - WorldSpacePos;

   //OUT.EyeVec = normalize(eyePos - IN.pos);
   //OUT.LightVec = normalize(sunPos - IN.pos);

   OUT.TexCoord.xy = IN.uv0;

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

   return OUT;
}

Part of the PS:
uniform sampler2D DiffSampler: register( S0 );
uniform sampler2D NormalSampler: register( S1 );
uniform sampler2D SpecSampler: register( S2 );
uniform sampler2D MicroSampler: register( S3 );
uniform sampler2D TransSampler: register( S4 );

static const int      UseMicroDetail = 1;
static const int      UseSpecColour = 0;
static const float    MicroScale = 1.0;
static const float    MicroFactor = 10;
static const float    SpecPower = 0.8;
static const float    SpecGloss = 4.5;
static const float    SpecFresnel = 3.4;
static const float    FresnelPower = 10.0;
static const float    FresnelGloss = 2.3;
static const float    TransMultiplier = 0.9;
static const float    TransRampOff = 0.1;
static const float4   SpecColour = {0.45, 0.65, 1.00, 1.0};
static const float4   TransColIn = {0.81, 0.91, 0.96, 1.0};
static const float4   TransColOut = {1.0, 0.71, 0.32, 1.0};
static const float4   TransColBack = {0.59, 0.20, 0.24, 1.0};
static const float4   LightColour = {0.89, 0.89, 0.89, 0.0};

struct ConnectData
{
   float4 Position     : POSITION;
   float3 TexCoord     : TEXCOORD0;
   float3 EyeVec       : TEXCOORD1;
   float3 LightVec     : TEXCOORD2;
   float3 WorldNormal  : NORMAL;
   float3 WorldTangent : TANGENT;
   float3 WorldBinormal: BINORMAL;
};

....

//================================Pixel shader - Complete================================
float4 main( ConnectData IN ) : COLOR
{

   float2 TexUV = IN.TexCoord.xy;
             
   float3 normal = tex2D(NormalSampler, TexUV ).xyz * 2.0 - 1.0;
   normal = normalize( normal );
   
   float3 WN = IN.TexCoord; //IN.WorldNormal;
   float3 TN = IN.TexCoord; //IN.WorldTangent;
   float3 BN = IN.WorldBinormal;

   float3 N = (WN * normal.z * 2) + (normal.x * BN + normal.y * -TN);
   N = normalize(N);
   
   float3 EV = normalize(IN.EyeVec);
   //float3 LV = normalize(IN.LightVec);

   // MANUALLY ->
   float3 LV = {1.0, 1.0, 1.0};
   LV = normalize(LV);
   // <- MANUALLY

   ....

}
#14
04/21/2011 (2:33 pm)
So the in-game lighting does not affect the model with the working method?
#15
04/21/2011 (3:57 pm)
No. I can not find the right vectors.

img829.imageshack.us/img829/9568/screenshot00500000.th.png

The new VS:
#define IN_HLSL
#include "hlslStructs.h"
#include "lighting.hlsl"

//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
struct ConnectData
{
   float4 Position      : POSITION;
   float2 TexCoord      : TEXCOORD0;
   float3 EyeVec        : TEXCOORD1;
   float3 LightVec      : TEXCOORD2;
   float3 WorldNormal   : NORMAL;
   float3 WorldTangent  : TANGENT;
   float3 WorldBinormal : BINORMAL;
};

//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
ConnectData main( VertexIn_PNTTTB IN,
	uniform float4x4 modelview : register(C0),
	uniform float4x4 objTrans : register(C4),
	uniform float4 inLightPos[3],
	uniform float4 viewToObj[3]
)
{
   ConnectData OUT;

   float4x4 a = transpose(objTrans);
   
   OUT.WorldNormal = mul(IN.uv0, a); //mul( IN.normal, a).xyz;
   OUT.WorldTangent = mul( IN.T, a).xyz;
   OUT.WorldBinormal = cross(OUT.WorldTangent, OUT.WorldNormal);

   // Calculate the EyeVector
   float3 EV = float3(viewToObj[0].x, viewToObj[1].x, viewToObj[2].x);
   OUT.EyeVec = normalize(EV);

   // Calculate the LightVector
   float3 sunPos = float3(inLightPos[0].x, inLightPos[1].x, inLightPos[2].x);
   OUT.LightVec = normalize(sunPos - IN.pos);

   OUT.TexCoord.xy = IN.uv0;

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

   return OUT;
}

The new PS:
#include "shadergen:/autogenConditioners.h"

uniform sampler2D DiffSampler: register( S0 );
uniform sampler2D NormalSampler: register( S1 );
uniform sampler2D SpecSampler: register( S2 );
uniform sampler2D MicroSampler: register( S3 );
uniform sampler2D TransSampler: register( S4 );

static const int      UseMicroDetail = 1;
static const int      UseSpecColour = 1;
static const float    MicroScale = 55.0;
static const float    MicroFactor = 2.0;
static const float    SpecPower = 0.4;
static const float    SpecGloss = 4.5;
static const float    SpecFresnel = 3.4;
static const float    FresnelPower = 1.0;
static const float    FresnelGloss = 2.3;
static const float    TransMultiplier = 1.0;
static const float    TransRampOff = 1;
static const float4   SpecColour = {0.45, 0.65, 1.00, 1.0};
static const float4   TransColIn = {0.81, 0.91, 0.96, 1.0};
static const float4   TransColOut = {1.0, 0.71, 0.32, 1.0};
static const float4   TransColBack = {0.59, 0.20, 0.24, 1.0};
static const float4   LightColour = {0.89, 0.89, 0.89, 0.0};

struct ConnectData
{
   float4 Position     : POSITION;
   float3 TexCoord     : TEXCOORD0;
   float3 EyeVec       : TEXCOORD1;
   float3 LightVec     : TEXCOORD2;
   float3 WorldNormal  : NORMAL;
   float3 WorldTangent : TANGENT;
   float3 WorldBinormal: BINORMAL;
};

....

//================================Pixel shader - Complete================================
float4 main( ConnectData IN ) : COLOR
{

   float2 TexUV = IN.TexCoord.xy;
             
   float3 normal = tex2D(NormalSampler, TexUV ).xyz;// * 2.0 - 1.0;
   normal = normalize( normal );
   
   float3 WN = IN.WorldNormal;
   float3 TN = IN.WorldTangent;
   float3 BN = IN.WorldBinormal;

   //float3 N = (WN * normal.z * 2) + (normal.x * BN + normal.y * -TN);
   //N = normalize(N);
   float3 N = normalize( normal );
   
   float3 EV = IN.EyeVec;
   float3 LV = IN.LightVec;

   float4 DotLN = dot(N, LV);

   ....
            
   return BaseLighting * LightColour + Spec;
}
#16
04/21/2011 (4:17 pm)
It might be slightly out of date but here is an example of how to get Advanced Lighting working with your custom shaders:

www.garagegames.com/community/forums/viewthread/101821/1#comment-677325

farm3.static.flickr.com/2487/3941899889_921d7fd07d.jpg
#17
04/21/2011 (6:14 pm)
This is the one area where T3D truly falls on its face. Nearly every other modern engine has a relatively pain free method of either importing, or directly creating complex shaders. I hope beyond hope that the next feature polish is on the material system, and getting it up to par with the competition. (Terrain still doesn't support specular! Seriously this needs to at least be integrated with the standard materials)
#18
04/21/2011 (7:28 pm)
Using the #lightinfo partly solve the problem, even if i lose the smoothing and the bumping.

img694.imageshack.us/img694/3923/screenshot00600000.th.png img90.imageshack.us/img90/840/screenshot00600001.th.png

VS:
#define IN_HLSL
#include "shdrConsts.h"
#include "hlslStructs.h"
#include "shadergen:/autogenConditioners.h"

//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------
struct ConnectData
{
   float4 Position      : POSITION;
   float2 TexCoord      : TEXCOORD0;
   float3 EyeVec        : TEXCOORD1;
   float3 LightVec      : TEXCOORD2;
   float4 screenspacePos: TEXCOORD3;
   float3 WorldNormal   : NORMAL;
   float3 WorldTangent  : TANGENT;
   float3 WorldBinormal : BINORMAL;
};

//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
ConnectData main( VertexIn_PNTTTB IN,
	uniform float4x4 modelview : register(C0),
	uniform float4 viewToObj[3],
        uniform float3 inLightVec : register(VC_LIGHT_DIR1)
)
{
   ConnectData OUT;

   // Normal, BiNormal, and Tangent
   float3x3 objToTangentSpace;
   OUT.WorldTangent = objToTangentSpace[0] = IN.T;
   OUT.WorldBinormal = objToTangentSpace[1] = IN.B;
   OUT.WorldNormal = objToTangentSpace[2] = IN.normal;

   // Calculate the LightVector
   OUT.LightVec.xyz = -inLightVec;
   OUT.LightVec.xyz = mul(objToTangentSpace, OUT.LightVec);
   OUT.LightVec = step( 0.0, dot( -inLightVec, IN.normal ) );

   // Calculate the EyeVector
   OUT.EyeVec = float3(viewToObj[0].x, viewToObj[1].x, viewToObj[2].x);

   // Texture Coordinate
   OUT.TexCoord.xy = IN.uv0;

   // Vertex Position
   OUT.Position = mul(modelview, float4(IN.pos.xyz,1));

   // Deferred RT Lighting  
   OUT.screenspacePos = OUT.Position;

   return OUT;
}

PS:
// Dependencies:  
#include "shadergen:/autogenConditioners.h"
#include "torque.hlsl"

uniform sampler2D DiffSampler: register( S0 );
uniform sampler2D NormalSampler: register( S1 );
uniform sampler2D SpecSampler: register( S2 );
uniform sampler2D MicroSampler: register( S3 );
uniform sampler2D TransSampler: register( S4 );
uniform sampler2D lightInfoBuffer : register(S5); 
              
....

uniform float4 rtParams5 : register(C0); 

struct ConnectData
{
   float4 Position      : POSITION;
   float3 TexCoord      : TEXCOORD0;
   float3 EyeVec        : TEXCOORD1;
   float3 LightVec      : TEXCOORD2;
   float4 screenspacePos: TEXCOORD3;
   float3 WorldNormal   : NORMAL;
   float3 WorldTangent  : TANGENT;
   float3 WorldBinormal : BINORMAL;
};

....

//================================Pixel shader - Complete================================
float4 main( ConnectData IN ) : COLOR
{
   // Deferred RT Lighting -> 
   float2 uvScene = IN.screenspacePos.xy / IN.screenspacePos.w;  
   uvScene = ( uvScene + 1.0 ) / 2.0;  
   uvScene.y = 1.0 - uvScene.y;  
   uvScene = ( uvScene * rtParams5.zw ) + rtParams5.xy;  
   float3 d_lightcolor;  
   float d_NL_Att;  
   float d_specular;  
   lightinfoUncondition(tex2D(lightInfoBuffer, uvScene), d_lightcolor, d_NL_Att, d_specular);
   // <- Deferred RT Lighting

   float2 TexUV = IN.TexCoord.xy;
   float4 a = tex2D(DiffSampler, TexUV);  

   // Cause the loss of the smoothing ->
   a *= (float4(d_lightcolor, 1.0) * 2 - 1) * d_NL_Att;
   // <- Cause the loss of the smoothing

   float3 normal = tex2D(NormalSampler, IN.TexCoord.xy ).xyz * 2.0 - 1.0;
   normal = normalize( normal );
   
   float3 WN = IN.WorldNormal;
   float3 TN = IN.WorldTangent;
   float3 BN = IN.WorldBinormal;

   float3 N = (WN * normal.z * 2) + (normal.x * BN + normal.y * -TN);
   N = normalize(N);
   //float3 N = normalize( normal );

   float3 EV = IN.EyeVec;
   float3 LV = IN.LightVec;
   float4 DotLN = dot(N, LV);
 
   ....

   float4 LightingOutput = BaseLighting + Spec;         

   return LightingOutput;
}
#19
04/23/2011 (5:06 pm)
Here are the latest results. Unfortunately i still can not find the two significant vectors for me.

img852.imageshack.us/img852/6628/immagine002a.th.png img863.imageshack.us/img863/2355/immagine003.th.png

EDIT: I'm not using the #lightinfo in this version of the shader

#define IN_HLSL  
#include "shdrConsts.h"  
#include "hlslStructs.h"   
#include "shadergen:/autogenConditioners.h"  

//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
struct ConnectData
{
   float4 Position      : POSITION;
   float2 TexCoord      : TEXCOORD0;
   float3 EyeVec        : TEXCOORD1;
   float3 LightVec      : TEXCOORD2;
   //float4 screenspacePos: TEXCOORD3;
   float3 WorldNormal   : NORMAL;
   float3 WorldTangent  : TANGENT;
   float3 WorldBinormal : BINORMAL;
};

//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
ConnectData main( VertexIn_PNTTTB IN,
	uniform float4x4 modelview,
	uniform float4x4 objTrans,
	uniform float3   eyePosWorld
)
{
   ConnectData OUT;

   float4x4 a = transpose(objTrans);
   float3 WorldSpacePos = mul(IN.pos, objTrans).xyz;

   // Normal, Tangent, and Binormal
   OUT.WorldNormal = IN.normal; //mul(IN.normal, a);
   OUT.WorldTangent = IN.T; //mul(IN.T, a);
   OUT.WorldBinormal = cross(OUT.WorldTangent, OUT.WorldNormal); //mul(cross(OUT.WorldTangent, OUT.WorldNormal), a).xyz;

   // EyeVec
   OUT.EyeVec = -normalize(IN.pos - eyePos);

   // LightVector
   OUT.LightVec = -normalize(float3(-133.69, 88.4143, -0.878019)); 

   // Texture Coordinate
   OUT.TexCoord.xy = IN.uv0;

   // Vertex Position
   OUT.Position = mul(modelview, float4(IN.pos.xyz, 1));

   // Deferred RT Lighting  
   //OUT.screenspacePos = OUT.Position;

   return OUT;
}
#20
04/23/2011 (9:13 pm)
wow, but it looks way better!
Page «Previous 1 2