HLSL Cel (Toon) shader in XNA

Started by
6 comments, last by user88 14 years ago
Hey, i've found a toon shader:

float4x4 mWorldViewProj;
float3 mLightPos;
float3 mCamPos;

struct VS_OUTPUT
{
   float4 Position: POSITION0;
   float2 TexCoord: TEXCOORD0;
   float3 Normal: 	TEXCOORD1;
   float3 LightDir: TEXCOORD2;
   float3 EyeDir: TEXCOORD3;
};


VS_OUTPUT vertexMain(
   float4 Position: POSITION0,
   float2 TexCoord: TEXCOORD0,
   float2 diffuse: TEXCOORD1,
   float3 Normal:   NORMAL0)
{
   VS_OUTPUT Output;

   Output.Position = mul(Position, mWorldViewProj);
   Output.Normal = normalize(Normal);
   Output.LightDir = normalize(mLightPos - Position.xyz);
   Output.EyeDir = normalize(mCamPos - Position.xyz);
   Output.TexCoord = TexCoord;

   return(Output);
}
   
sampler2D tex0 : register(s0);
sampler1D diffuseRamp : register(s1);

float4 pixelMain
(
   float2 TexCoord:     TEXCOORD0,
   float3 Normal:       TEXCOORD1,
   float3 LightDir:     TEXCOORD2,
   float3 EyeDir:     	TEXCOORD3
) : COLOR0
{
	float4 texCol = tex2D(tex0, TexCoord);

	float diffuse = clamp(dot(Normal, LightDir), 0.0, 1.0);
	
	float4 lightColor = tex1D(diffuseRamp, diffuse);

	float fresnal = dot(Normal, EyeDir);
	fresnal = clamp((fresnal - 0.2) * 1000.0, 0.0, 1.0);
	lightColor *= fresnal;
	
	float3 reflect = (2.0 * diffuse * Normal) - LightDir;
	float specular = pow(clamp(dot(reflect, EyeDir), 0.0, 1.0), 8.0);
	specular = clamp((specular - 0.5) * 1000.0, 0.0, 1.0);
	float4 specular4 = specular * float4(1.0, 1.0, 1.0, 1.0);
	
	return(saturate(lightColor * texCol + specular4));
} 


source: http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=18281&sid=784b3ff1f5fd3b9c03729737589d32cf I was wondering how i can use this in XNA... Can someone please help me out with that?? Thanks for any help.
Advertisement
Head over to http://creators.xna.com/en-US/education/. There is a ton of stuff there about using custom effects, and writing your own HLSL.
Yes, i know. I am using that site occasionally for information. Learning HLSL is on my task list, but just now right now...

All i want right now is to implement a basic toon shader. And it seems to be a good basic one.

I think it shouldn't be to difficult to use this in a XNA game right?? I'm just not sure how to implement it. Would be great if anyone could get me started with this first.
Did you try to google a little? Here is an example on XNA.

[Edited by - user88 on March 26, 2010 2:22:19 AM]
Ofcourse i googled... That code is also availible on the XNA site in a complete solution. The problem with that code however, is that its made for the GS 2.0 framework. This code doesn't work anymore in the GS3.0 framework.

Well, it still works, but it only draws the last mesh, so if you have more meshes then you'll only see the last one you try to draw.

But i'm still wondering what the problem is with the code that i found...
I just want try to implement that...
Quote:Original post by w00
Well, it still works, but it only draws the last mesh, so if you have more meshes then you'll only see the last one you try to draw.


Strange, can be that Device.Clear() calls between each mesh drawing.
Yeh, i don't get it either. I removed all the Clear() but i still get the same result.

I also tried the shader fromt his site. It only uses one different RenderTarget instead of two like in the other example:

http://digierr.spaces.live.com/blog/cns!2B7007E9EC2AE37B!420.entry

But i still get the same problem. Only one mesh is drawn. I really don't get what the problem is and why it's doing that. It's probably got something to do with switching between rendertargets. Just don't know how to fix it... :(
i think PIX For Windows will be helpful for you.

Set up PIX to "Set Call Capture/Single frame capture Direct3D" and see whats happens in your rendering loop.

This topic is closed to new replies.

Advertisement