How to set shader constant
by Paul Gaze · in Technical Issues · 08/15/2012 (6:10 pm) · 4 replies
I have written a custom shader that needs a float as input. How can I do this? Below is an example of the sort of thing I want to do. I want to be able to change the value of 'fraction' from torque script. I saw a thread on assigning custom vectors to shaders, but unfortunately it was for an earlier version of torque. My version is Tourque3d 1.2.
#include "torque.hlsl"
uniform sampler2D DiffSampler: register( S0 );
uniform float fraction=0.5f;
struct ConnectData
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
};
float4 main( ConnectData IN ) : COLOR
{
float2 TexUV = IN.TexCoord.xy;
float4 Diff = tex2D(DiffSampler, TexUV);
if( TexUV.y<fraction ){
Diff.w = 0;
}else{
Diff.w = 1.0f;
}
return hdrEncode(Diff);
}
#include "torque.hlsl"
uniform sampler2D DiffSampler: register( S0 );
uniform float fraction=0.5f;
struct ConnectData
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
};
float4 main( ConnectData IN ) : COLOR
{
float2 TexUV = IN.TexCoord.xy;
float4 Diff = tex2D(DiffSampler, TexUV);
if( TexUV.y<fraction ){
Diff.w = 0;
}else{
Diff.w = 1.0f;
}
return hdrEncode(Diff);
}
About the author
#2
08/15/2012 (11:29 pm)
Thanks for reply. This sort of works but isn't exactly what I want. I want to be able to set the value of fraction on the fly. Also this material will be used by several different objects at the same time with different values of fraction.
#3
Then the constants are bound to the globals.
Edit: Btw this function is run automatically you don't have to run it yourself.
08/16/2012 (4:07 am)
function TheFX::setShaderConsts( %this )
{
%this.setShaderConst( "$samples", $TheFX::samples ); //here we set the constants for the shaders
%this.setShaderConst( "$amount", $TheFX::amount );
}Then the constants are bound to the globals.
Edit: Btw this function is run automatically you don't have to run it yourself.
#4
08/17/2012 (1:52 pm)
If i remember correctly, this feature works only with shaders loaded from the PostFXManager. But maybe i'm wrong :)
Torque Owner Alfio Saitta
Collateral Studios