Jump to content
  • Advertisement
Sign in to follow this  
w00

HLSL Cel (Toon) shader in XNA

This topic is 3310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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... :(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!