The most basic Shader
by Anthony Rosenbaum · in Torque Game Engine Advanced · 06/16/2004 (7:27 am) · 4 replies
I decided to finally test my knowledge of shaders and actually write one, nothing special but something quick and able too see a change, not to mention to get acustomed to the attribute variables used in TSE that can be passed into the Shaders.
Now for anyone who has already read many articles on Shading this will not be so important. However in several months when we have people who don't know the basics of shaders, well they might find this post interesting.
This is a green shader, inspired by The Cg Tutorial first example. BTW I never have read any HLSL books but Cg and HLSL are similar (designed in tandem with each other) so code from that book can be ported easily.
First off there is always 2 shaders Vertex and Pixel ( aka Fragment)
This Vertex shader does nothing but change the vertex coord ( recieved from the game engine, we call these attribute variables) and multiply it my the modelview matrix ( also an attribute variable)
then the Pixel Shader which changes the color to green
open up shader.cs add this at the bottom
then in material add this at the bottom
Then load up the game the orc should look like this
this

Now for anyone who has already read many articles on Shading this will not be so important. However in several months when we have people who don't know the basics of shaders, well they might find this post interesting.
This is a green shader, inspired by The Cg Tutorial first example. BTW I never have read any HLSL books but Cg and HLSL are similar (designed in tandem with each other) so code from that book can be ported easily.
First off there is always 2 shaders Vertex and Pixel ( aka Fragment)
This Vertex shader does nothing but change the vertex coord ( recieved from the game engine, we call these attribute variables) and multiply it my the modelview matrix ( also an attribute variable)
#define IN_HLSL
#include "shdrConsts.h"
//---------------------------------------------------------------
// Constants
//---------------------------------------------------------------
struct Appdata
{
float4 position : POSITION;
};
struct Conn
{
float4 HPOS : POSITION;
};
//---------------------------------------------------------------
// Main
//---------------------------------------------------------------
Conn main( Appdata In, uniform float4x4 modelview : register(VC_WORLD_PROJ) )
{
Conn Out;
//take vert and get coord by transforming by modelviewmatrix
Out.HPOS = mul(modelview, In.position);
return Out;
}save that as greenShaderV.HLSLthen the Pixel Shader which changes the color to green
//---------------------------------------------------------------
//Structures
//---------------------------------------------------------------
struct Fragout
{
float4 col : COLOR0;
};
//---------------------------------------------------------------
//Main
//---------------------------------------------------------------
Fragout main( )
{
Fragout OUT;
OUT.col = float4(0.0, 1.0, 0.0, 1.0);
return OUT;
}save that as greenP.HLSLopen up shader.cs add this at the bottom
datablock ShaderData( greenShader )
{
DXVertexShaderFile = "shaders/greenV.hlsl";
DXPixelShaderFile = "shaders/greenP.hlsl";
pixVersion = 1.1;
};then in material add this at the bottom
datablock CustomMaterial( aGreenShader )
{
shader = greenShader;
version = 1.1;
};Finally add change the binding of the SpaceOrc's skin in materialMap.cs from addMaterialMapping("orc_ID1_skin", "material: OrcSkin");toaddMaterialMapping("orc_ID1_skin", "material: aGreenShader");Then load up the game the orc should look like this
this
About the author
Torque 3D Owner Pat Wilson