Simple toon shader
by Kevin Johnson · in Torque Game Engine Advanced · 01/14/2005 (3:31 pm) · 24 replies
New shaders posted 11/17/08

Here is a simple toon shader that actually works.. 1.7.x , 1.8b1
toonShaderV.hlsl
toonShaderP.hlsl
Change the intensity test to widen/narrow/add/remove bands
shaders.cs
materials.cs
Using a solid color diffuse texture should yield something like this

Anyway, thats about what the previous version(lol) attemped to do..
Enjoy
k

Here is a simple toon shader that actually works.. 1.7.x , 1.8b1
toonShaderV.hlsl
#define IN_HLSL
#include "hlslStructs.h"
#include "shdrConsts.h"
//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------
struct ConnectData
{
float4 hpos : POSITION;
float2 outTexCoord : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 outLightVec : TEXCOORD2;
};
//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
ConnectData main( VertexIn_PNT IN,
uniform float4x4 modelview : register(VC_WORLD_PROJ),
uniform float4x4 texMat : register(VC_TEX_TRANS1),
uniform float4x4 objTrans : register(VC_OBJ_TRANS),
uniform float4 lightPos : register(VC_LIGHT_POS1)
)
{
ConnectData OUT;
OUT.hpos = mul(modelview, IN.pos);
float4 texCoordExtend = float4( IN.uv0, 0.0, 1.0 );
OUT.outTexCoord = mul(texMat, texCoordExtend);
float3 N = normalize(IN.normal);
OUT.normal = N;
OUT.outLightVec = normalize(lightPos.xyz - IN.pos.xyz);
return OUT;
}toonShaderP.hlsl
//-----------------------------------------------------------------------------
// Structures
//-----------------------------------------------------------------------------
struct ConnectData
{
float2 texCoord : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 lightVec : TEXCOORD2;
};
struct Fragout
{
float4 col : COLOR0;
};
//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
uniform sampler2D diffuseMap : register(S0)
)
{
Fragout OUT;
float4 diffuseColor = tex2D(diffuseMap, IN.texCoord);
float intensity;
float4 toonMask;
intensity = dot(normalize(IN.lightVec.xyz) ,IN.normal.xyz);
if (intensity > 0.95)
toonMask = float4(1.0,1.0,1.0,1.0);
else if (intensity > 0.75)
toonMask = float4(0.75,0.75,0.75,1.0);
else if (intensity > 0.50)
toonMask = float4(0.5,0.5,0.5,1.0);
else if (intensity > 0.25)
toonMask = float4(0.25,0.25,0.25,1.0);
else if (intensity > 0.1)
toonMask = float4(0,0,0,1.0);
OUT.col = diffuseColor * toonMask;
return OUT;
}Change the intensity test to widen/narrow/add/remove bands
shaders.cs
new ShaderData( ToonShader )
{
DXVertexShaderFile = "shaders/toonshaderV.hlsl";
DXPixelShaderFile = "shaders/toonshaderP.hlsl";
pixVersion = 2.0;
};materials.cs
new CustomMaterial(toon_orc_dynamic)
{
mapTo = "player";
texture[0] = "orc_toon";
shader = ToonShader;
version = 2.0;
};
new CustomMaterial(toon_orc)
{
mapTo = "player";
texture[0] = "orc_toon";
shader = ToonShader;
dynamicLightingMaterial = toon_orc_dynamic;
version = 2.0;
};Using a solid color diffuse texture should yield something like this

Anyway, thats about what the previous version(lol) attemped to do..
Enjoy
k
#22
-Dante
11/18/2008 (7:40 am)
Wow Kevin! Awesome! I keep learning something new every day about this engine. The dynamic lighting thing should definitely be public knowledge. Do you mind posting a resource on this, or if I post one?-Dante
#23
Like I said, I only came here out of curriosity to check out the new beta, but from what I'm seeing TGEA has really matured now!
Maybe I'll resurrect some of my old ideas and get back into it..
k
11/18/2008 (9:47 am)
Go for it..Like I said, I only came here out of curriosity to check out the new beta, but from what I'm seeing TGEA has really matured now!
Maybe I'll resurrect some of my old ideas and get back into it..
k
#24
I got all the code in the game as stated above, but when I run the game the Orc appears completely black. I think my problem might be in materials.cs. Do I replace the code in this file or add to it? Also is it the one located in scriptsAndAssets\data\shapes\players\TorqueOrc? What else might be the cause of this behavior? Sorry if my questions seem completely clueless, but I'm a fresh noob. Any advice would be greatly appreciated.
Thanks =)
02/23/2009 (10:17 am)
Hey,I got all the code in the game as stated above, but when I run the game the Orc appears completely black. I think my problem might be in materials.cs. Do I replace the code in this file or add to it? Also is it the one located in scriptsAndAssets\data\shapes\players\TorqueOrc? What else might be the cause of this behavior? Sorry if my questions seem completely clueless, but I'm a fresh noob. Any advice would be greatly appreciated.
Thanks =)
Torque Owner Kevin Johnson
I updated the shader to get the light vector from dynamic lighting as well.
(just basically adding a dynamicLightingMaterial). Looks like it adds another pass
when an object is affected by dynamic lighting..
After adding that parameter, the registers have the needed values.
I found this thread
and it discusses the params you mentioned.. I'd investigate more but i keep running out of registers....
k