HLSL: Converting a Toon Shader
by Glen "ThaBonadingus" Reece · in Torque 3D Beginner · 11/15/2011 (5:25 pm) · 3 replies
Ok, some things to know first:
1: It's a Post Processing Effect
2: The original effect is here
3: It's my first shader conversion
4: I'm new to Post-Processing effects in Torque3D
As I've made known it's my first shader conversion and I'm new to adding PPEs in Torque 3D. What I'm trying to do is convert the original shader (link above) and change it a bit. I've modified the shader slightly to make it where there are two levels of shadowing one being a slight shadow and the other being a darker shadow.
I'm going to need some help actually running the shaders in Torque, if anyone has a template or something I can use.
I've split the shaders up, please read post #2
1: It's a Post Processing Effect
2: The original effect is here
3: It's my first shader conversion
4: I'm new to Post-Processing effects in Torque3D
As I've made known it's my first shader conversion and I'm new to adding PPEs in Torque 3D. What I'm trying to do is convert the original shader (link above) and change it a bit. I've modified the shader slightly to make it where there are two levels of shadowing one being a slight shadow and the other being a darker shadow.
I'm going to need some help actually running the shaders in Torque, if anyone has a template or something I can use.
I've split the shaders up, please read post #2
SimpleVertOutput MakeNormalMapVS(AppData IN) {
SimpleVertOutput OUT = (SimpleVertOutput)0;
float4 Po = float4(IN.Position.xyz,1.0);
float3 Nn = mul(IN.Normal, WorldIT).xyz;
Nn = normalize(Nn);
OUT.HPosition = mul(Po, WvpXf);
OUT.diffCol.xyz = float3(0.5,0.5,0.5) + (0.5 * Nn);
OUT.diffCol.w = 1.0;
return OUT;
}VS_OUTPUT_BLUR VS_Quad_Horizontal_9tap(float3 Position : POSITION,
float3 TexCoord : TEXCOORD0)
{
VS_OUTPUT_BLUR OUT = (VS_OUTPUT_BLUR)0;
OUT.Position = float4(Position, 1);
float TexelIncrement = BlurWid/QuadScreenSize.x;
#ifdef NO_TEXEL_OFFSET
float2 nuv = TexCoord;
#else /* NO_TEXEL_OFFSET */
float2 nuv = float2(TexCoord.xy+QuadTexelOffsets.xy);
#endif /* NO_TEXEL_OFFSET */
float2 Coord = nuv;
OUT.TexCoord0 = float2(Coord.x + TexelIncrement, Coord.y);
OUT.TexCoord1 = float2(Coord.x + TexelIncrement * 2, Coord.y);
OUT.TexCoord2 = float2(Coord.x + TexelIncrement * 3, Coord.y);
OUT.TexCoord3 = float2(Coord.x + TexelIncrement * 4, Coord.y);
OUT.TexCoord4 = Coord;
OUT.TexCoord5 = float2(Coord.x - TexelIncrement, Coord.y);
OUT.TexCoord6 = float2(Coord.x - TexelIncrement * 2, Coord.y);
OUT.TexCoord7 = float2(Coord.x - TexelIncrement * 3, Coord.y);
OUT.TexCoord8 = float2(Coord.x - TexelIncrement * 4, Coord.y);
return OUT;
}VS_OUTPUT_BLUR VS_Quad_Vertical_9tap(float3 Position : POSITION,
float3 TexCoord : TEXCOORD0)
{
VS_OUTPUT_BLUR OUT = (VS_OUTPUT_BLUR)0;
OUT.Position = float4(Position, 1);
float TexelIncrement = BlurWid/QuadScreenSize.y;
#ifdef NO_TEXEL_OFFSET
float2 nuv = TexCoord;
#else /* NO_TEXEL_OFFSET */
float2 nuv = float2(TexCoord.xy+QuadTexelOffsets.xy);
#endif /* NO_TEXEL_OFFSET */
float2 Coord = nuv;
OUT.TexCoord0 = float2(Coord.x, Coord.y + TexelIncrement);
OUT.TexCoord1 = float2(Coord.x, Coord.y + TexelIncrement * 2);
OUT.TexCoord2 = float2(Coord.x, Coord.y + TexelIncrement * 3);
OUT.TexCoord3 = float2(Coord.x, Coord.y + TexelIncrement * 4);
OUT.TexCoord4 = Coord;
OUT.TexCoord5 = float2(Coord.x, Coord.y - TexelIncrement);
OUT.TexCoord6 = float2(Coord.x, Coord.y - TexelIncrement * 2);
OUT.TexCoord7 = float2(Coord.x, Coord.y - TexelIncrement * 3);
OUT.TexCoord8 = float2(Coord.x, Coord.y - TexelIncrement * 4);
return OUT;
}float4 PS_Blur_Horizontal_9tap(VS_OUTPUT_BLUR IN) : COLOR
{
float4 OutCol = tex2D(NormalSamp, IN.TexCoord0) * (WT9_1/WT9_NORMALIZE);
OutCol += tex2D(NormalSamp, IN.TexCoord1) * (WT9_2/WT9_NORMALIZE);
OutCol += tex2D(NormalSamp, IN.TexCoord2) * (WT9_3/WT9_NORMALIZE);
OutCol += tex2D(NormalSamp, IN.TexCoord3) * (WT9_4/WT9_NORMALIZE);
OutCol += tex2D(NormalSamp, IN.TexCoord4) * (WT9_0/WT9_NORMALIZE);
OutCol += tex2D(NormalSamp, IN.TexCoord5) * (WT9_1/WT9_NORMALIZE);
OutCol += tex2D(NormalSamp, IN.TexCoord6) * (WT9_2/WT9_NORMALIZE);
OutCol += tex2D(NormalSamp, IN.TexCoord7) * (WT9_3/WT9_NORMALIZE);
OutCol += tex2D(NormalSamp, IN.TexCoord8) * (WT9_3/WT9_NORMALIZE);
return OutCol;
}// now that first blur is done, we can use x instead of w
float4 PS_Blur_Vertical_9tap(VS_OUTPUT_BLUR IN) : COLOR
{
float4 OutCol = tex2D(BlurSampler1, IN.TexCoord0) * (WT9_1/WT9_NORMALIZE);
OutCol += tex2D(BlurSampler1, IN.TexCoord1) * (WT9_2/WT9_NORMALIZE);
OutCol += tex2D(BlurSampler1, IN.TexCoord2) * (WT9_3/WT9_NORMALIZE);
OutCol += tex2D(BlurSampler1, IN.TexCoord3) * (WT9_4/WT9_NORMALIZE);
OutCol += tex2D(BlurSampler1, IN.TexCoord4) * (WT9_0/WT9_NORMALIZE);
OutCol += tex2D(BlurSampler1, IN.TexCoord5) * (WT9_1/WT9_NORMALIZE);
OutCol += tex2D(BlurSampler1, IN.TexCoord6) * (WT9_2/WT9_NORMALIZE);
OutCol += tex2D(BlurSampler1, IN.TexCoord7) * (WT9_3/WT9_NORMALIZE);
OutCol += tex2D(BlurSampler1, IN.TexCoord8) * (WT9_3/WT9_NORMALIZE);
//float4 orig = tex2D(NormalSamp,IN.TexCoord4);
//return orig.w * OutCol;
return OutCol;
}float BlurWid = 1;
float ShadowCutoff1 = 0.75;
float ShadowCutoff2 = 0.25;
float3 ShadowColor = (0.5,0.5,0.5);
////////////////////////// read-in overlay /////////
float4 drawFakePS(QuadVertexOutput IN) : COLOR
{
float4 t = tex2D(BlurSampler2, IN.UV);
float4 c = tex2D(ColorSamp, IN.UV);
float3 Nn = 2.0 * (t.xyz - float3(0.5,0.5,0.5));
float ldn = dot(-LightDir,Nn);
float ldn2 = dot(-LightDir,Nn);
float cut = 1;
if (ldn < ShadowCutoff1) {
cut = 0;
}
float cut2 = 1;
if (ldn2 < ShadowCutoff2) {
cut2 = 0;
}
float3 s = c.xyz * lerp(ShadowColor,float3(1,1,1),cut) * lerp(ShadowColor,float3(1,1,1),cut2);
return c.w*float4(s,1);
}About the author
Christian, Star Wars fan, Expert Americanese Speaker, The Legendary Bonadingus boss monster, Frighteningly Sexy, Lover of Anime (especially Gundam), MangaPage and ItBGames Partner, and IndieOtaku Blogger.
#2
See the first post for the shaders!
can anyone who has experience with shaders help out a bit? All I really need is advice and some pointers because it's almost (not quite) like staring into the sun at the moment.
11/30/2011 (5:04 pm)
Ok, I've split up the individual Vertex and Pixel shaders...(I kept the Vertex and Pixel Shader definitions temporarily until I figure out the arguments I need to modify/use from Torque, and it helps show what each shader is.)See the first post for the shaders!
can anyone who has experience with shaders help out a bit? All I really need is advice and some pointers because it's almost (not quite) like staring into the sun at the moment.
#3
12/06/2011 (3:02 pm)
Bumping again and hoping for some help....
Torque 3D Owner Glen "ThaBonadingus" Reece