Adding Dynamic Variable to customMaterials
by robo · in Torque 3D Professional · 07/08/2010 (8:44 am) · 0 replies
HI
I make this resource(www.torquepowered.com/community/resources/view/15211) modification to t3d.
This is what I did.
script
Shader
engine change
materialDefinition.h
materialDefinition.cpp
shaderGenVars.h
shaderGenVars.cpp
processedShaderMaterial.h
processedShaderMaterial.cpp
I make this resource(www.torquepowered.com/community/resources/view/15211) modification to t3d.
This is what I did.
script
singleton ShaderData( ambientShader )
{
DXVertexShaderFile = "shaders/common/ambientV.hlsl";
DXPixelShaderFile = "shaders/common/ambientP.hlsl";
pixVersion = 2.0;
};
singleton CustomMaterial(testMaterial) // v1.1
{
shader = OutLineShader;
stateBlock = ambientShader;
testColor = 0.5f; //This is Dynamic Variable
version = 2.0;
};Shader
//Vertex Shader
#define IN_HLSL
#include "shdrConsts.h"
uniform float4x4 modelview : register(VC_WORLD_PROJ);
uniform float testColor ; //this variable is problem.
//Script Controlled Variables is not working...
struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Color : COLOR0;
};
VS_OUTPUT main( float4 Pos : POSITION ,float3 Normal : NORMAL )
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul(modelview,Pos);
Out.Color = float4(testColor,testColor,0.0f,1.0f); //Script Controlled Variables is not working...
return Out;
}
//Pixel Shader
#define IN_HLSL
#include "shdrConsts.h"
struct PS_INPUT
{
float4 Color : COLOR0;
};
float4 main(PS_INPUT Input) : COLOR
{
float4 color;
color = Input.Color;
return color ;
}engine change
materialDefinition.h
class Material : public BaseMaterialDefinition
{
...
F32 mtestColor; //add
...
}materialDefinition.cpp
void Material::initPersistFields()
{
// ~ ommition
addField("testColor", TypeF32, Offset(mtestColor, Material)); //add
}
Material::Material()
{
// ~ ommition
public:
mtestColor = 0.0085f; //add
}shaderGenVars.h
struct ShaderGenVars
{
...
const static String mtestColorString; //add
..
}shaderGenVars.cpp
...
const String ShaderGenVars::mtestColorString("$testColor"); //add
...processedShaderMaterial.h
class ShaderConstHandles
{
...
public:
GFXShaderConstHandle* mtestColorConstHandle; //add
...
}processedShaderMaterial.cpp
void ShaderConstHandles::init( GFXShader *shader, ShaderData* sd /*=NULL*/ )
{
....
mtestColorConstHandle = shader->getShaderConstHandle(ShaderGenVars::mtestColorString); //add
}
void ProcessedShaderMaterial::_setShaderConstants(SceneState * state, const SceneGraphData &sgData, U32 pass)
{
.....
if ( handles->mtestColorConstHandle->isValid() ) //add
shaderConsts->set( handles->mtestColorConstHandle, mMaterial->mtestColor); //add
...
}About the author