Game Development Community

CustomMaterial/Shaders I dont know what I am doing. Help please.

by CdnGater · in Torque 3D Beginner · 03/07/2010 (7:32 pm) · 2 replies

I am trying to get a Custom Material to Shader working and I am running into issues (Like me not understanding what I am doing properly)

I create a CustomMaterial like so:
singleton CustomMaterial(cm_test_1)
{   
   texture[0] = "texture0";   

   shader = test_shader_1;
   version = 2.0;
};


A shader definition like so:
new ShaderData( test_shader_1 )
{
   DXVertexShaderFile   = "shaders/test_v.hlsl";
   DXPixelShaderFile    = "shaders/test_P.hlsl";
    
   pixVersion = 2.0;
};

And the pixel shader like so:
#include "shaders/common/lighting.hlsl"
#include "shaders/common/torque.hlsl"

struct ConnectData
{
   float2 texCoord        : TEXCOORD0;
};

struct Fragout
{
   float4 col : COLOR0;
};

uniform sampler2D texture0 : register(S0);

Fragout main( ConnectData IN )
{
   Fragout OUT;

   // Base Texture
   OUT.col = tex2D(texture0, IN.texCoord);
   
   return OUT;
}

I have not included the vertex shader as surprisingly it works.


With in code, I do basicaly the following.
CustomMaterial *templateMat = dynamic_cast<CustomMaterial*>( Sim::findObject( "cm_test_1") );
CustomMaterial* newMat = new CustomMaterial(emplateMat);  // Make a copy

newMat->mTexFilename[0] = "art/test_1.jpg";

newMat->registerObject();
Sim::getRootGroup()->addObject( newMat );

BaseMatInstance * matInst = newMat->createMatInstance();
const GFXVertexFormat *flags = getGFXVertexFormat<GFXVertexPNTTB>();
FeatureSet features = MATMGR->getDefaultFeatures();

matInst->init( features, flags );


The key element above is the newMat->mTexFilename[0] = "art/test_1.jpg";. I do not see my test_1 texture mapped on the object, I see some other texture. I think it's to do with the mapping between the CustomMateral and the actual Pixel Shader.

I should state, that stepping though the code, I see the Texture assigned to the CustomMaterial, and the material assigned to the object fine. It's just not getting to the Shader from what I see.

If I change texture[0] = "texture0"; to something like sampler2D["texture0"] = "texture0"; then I get the warning material. So, I don't realy know what I am doing. All I want, is to have a texture on a object, via a shader that is controled by a CustomMaterial, and yes it has to be done this way.

Can anyone help please?

Thanks





#1
03/07/2010 (8:05 pm)
Found something interesting, so I must be getting closer.

if I change the following

texture[0] = "texture0"; 
[/code[

to

[code]
sampler["texture0"] = "art/test_1.jpg";

and comment out the assignment in code, it worked!


Humm interesting, if I do the material like above, and assing a different texture instead, i get the texture thats defined with the material and not the one I manualy set. Wonder if I messed something up in the code and am using the orginal CustomMaterial and not the copy? Code Diving I go.

#2
03/07/2010 (8:21 pm)
OK, Now I feel absolutly stupid. I figured out what my issue is.

when I register the new CustomMaterial I was resetting the overridden texture.

What was
newMat->mTexFilename[0] = "art/test_1.jpg";   
  
newMat->registerObject();

should have been
newMat->registerObject(); 

newMat->mTexFilename[0] = "art/test_1.jpg";

<SLAP SELF>

Later all.