Textures in custom shader feature
by Brian Burke · in Torque 3D Professional · 11/18/2010 (11:28 pm) · 5 replies
Is there a good guide to allowing texture filenames to be specified in the script description of a new shader feature?
I've added the field in the material definitions (along with the corresponding filename definition.) And I added a line to ShaderConstHandles::init(..) to initialize the handle. ... but so far as I can see my texture never gets loaded.
Any suggestions of where I might look? How do the existing fields like diffuseMap and normalMap get loaded?
Any suggestions would be appreciated. Thanks!
-Andy
I've added the field in the material definitions (along with the corresponding filename definition.) And I added a line to ShaderConstHandles::init(..) to initialize the handle. ... but so far as I can see my texture never gets loaded.
Any suggestions of where I might look? How do the existing fields like diffuseMap and normalMap get loaded?
Any suggestions would be appreciated. Thanks!
-Andy
About the author
#2
If you need to read and sample a texture just see how the other maps (normal,specular,etc...) are loaded.
Here is an example:
You don't need to call the previous call all the time.
Once registered,try to find it this way:
The var "bumpMap" is assigned this way:
In shaderGenVars.cpp add
Features are batched into several groups,I think you should use the MFG_Texture group.
All this stuff is a bit tricky.
11/19/2010 (2:46 pm)
Hey Andy,in the old days I did a resource on how to add a new shader feature.Check my profile.If you need to read and sample a texture just see how the other maps (normal,specular,etc...) are loaded.
Here is an example:
// the map
Var *bumpMap = new Var;
bumpMap->setType( "sampler2D" );
bumpMap->setName( "bumpMap" ); // bumpMap !!!
bumpMap->uniform = true;
bumpMap->sampler = true;
bumpMap->constNum = Var::getTexUnitNum();
//the sampler
bumpSample = new Var;
bumpSample->setType( "vec4" );
bumpSample->setName( "bumpSample" );
LangElement *bumpSampleDecl = new DecOp( bumpSample );
// the injection
output = new GenOp( " @ = texture2D(@, @);rn", bumpSampleDecl, bumpMap, texCoord );You don't need to call the previous call all the time.
Once registered,try to find it this way:
Var *normalMap = (Var*)LangElement::find( "bumpMap" );
The var "bumpMap" is assigned this way:
mBumpMapTexSC = shader->getShaderConstHandle(ShaderGenVars::bumpMap);Where mBumpMapTexSC is a Shader Const Handle.
In shaderGenVars.cpp add
const String ShaderGenVars::bumpMap("$bumpMap");Features are batched into several groups,I think you should use the MFG_Texture group.
All this stuff is a bit tricky.
#3
So, I would like to be able to assign a "DefectMap" to features, but I can't *quite* figure out how to make it load textures it isn't already "expecting". I'm missing a step somewhere.
11/19/2010 (3:15 pm)
Well ultimately I will be combining a couple of normal maps to show "defects" on my object. Ideally there will be another map that controls how the other normals are blended.So, I would like to be able to assign a "DefectMap" to features, but I can't *quite* figure out how to make it load textures it isn't already "expecting". I'm missing a step somewhere.
#4
11/19/2010 (3:18 pm)
Thanks Ivan, I'll take a look at that resource.
#5
When a new texture is used,(I am not really sure if it is necessarily or not),Torque is using getResources() to pull new resources.It is part of the shaderFeature class.
getResources() is connected to MaterialFeatureData,that contains data about the material stage.
When you create your feature,you must overload this method and add an information about the number of textures used in your hlsl injection.
If you are using SM2 or SM3,each texture corresponds to a single sampler register.If you use the sampler several times, numTexReg < numTex,because you sample several times using a single register.
All this stuff depends on your shader feature.The basic case is one register and one texture.Below SM2 sampler registers(S#) are not supported (by the gpu) therefore more registers are used (numTexReg > numTex).
11/19/2010 (3:25 pm)
There are several directives,that declare ,implement and register a new feature. Check this out.When a new texture is used,(I am not really sure if it is necessarily or not),Torque is using getResources() to pull new resources.It is part of the shaderFeature class.
getResources() is connected to MaterialFeatureData,that contains data about the material stage.
When you create your feature,you must overload this method and add an information about the number of textures used in your hlsl injection.
ShaderFeature::Resources YourNewFeatureHLSL::getResources( const MaterialFeatureData &fd )
{
Resources res;
res.numTex = 1; // how many textures are used
res.numTexReg = 1; // how many registers are used to pass data
return res;
}If you are using SM2 or SM3,each texture corresponds to a single sampler register.If you use the sampler several times, numTexReg < numTex,because you sample several times using a single register.
All this stuff depends on your shader feature.The basic case is one register and one texture.Below SM2 sampler registers(S#) are not supported (by the gpu) therefore more registers are used (numTexReg > numTex).
Torque Owner Bobby Leighton
Imagn' Games