Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with the tutorial on creating a toon shader. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |


Hi, first of all I would like to thank you for making these tutorials. They are really nice for a beginner like me.
I would like to use your toon shader to render a test model of mine. But I can't figure out how to use it in my XNA code… In my draw-method I've set the effect parameters "World", "View" and "Projection" as shown in an earlier tutorial. When it comes to mesh.Draw() I get an InvalidOperationException saying that "The current vertext declaration does not include alle elements required b the current vertext shader. TextureCoordinate0 is missing".
Is it because I did not set the effect parameter "Texture"? Please help me out, I've no clue what to do. :-/
(I'm using XNA 4.0)
Thanks!
Did you ever solve this? I'm having the same problem :(
Is your model using a texture, or just colors? If your 3D model does not have any textures associated with it, then when XNA loads the model, it will use colors instead of texture coordinates. (It is probably using something like VertexPositionColor, instead of VertexPositionTexture, if you know what I mean by that. If not, I discuss it a bit in the Primitives tutorials.)
Since our shader uses texture coordinates, I can easily imagine getting an error when you try to supply geometry to draw that does not have texture coordinates (but rather, vertex colors instead).
If this is the problem, there are a couple of ways that you might be able to go about fixing it. For one, just use a texture instead. The texture would probably be rediculously simple—something along the line of several colored regions, and you just map the right parts to the right colors in your 3D modeling/texturing tool. That's what I've done with my Helicopter model, and the frog model in the picture for the tutorial, and I'm about to upload a beach ball model for people to use as well.
If that's not an option, then you'd need to modify the shader to work with vertex colors instead. My version of that is below, though I'll admit I haven't tested it. (If you get it to work, please post that on here. It compiles, but I ran it with my original helicopter model, and it crashes in a very similar way to what you described, because now it is looking for colors, while my model supplies texture coordinates—so the opposite of the problem you're having, meaning that this one will probably work for you.)
Hi,
could you share the model as well as the texture used in this tutorial please?
Thanks
I'd love to, but I'm not sure I can. I don't know where I stuck it. I just looked around for a while on my computer and I can't seem to find it. The Helicopter model, which is already in the model library should have a very similar effect, as well as the beach ball model that I'm about to put in there too.
If I find the frog model, I'll upload that as well.
After playing around with this shader a bit, I realized something looked wrong.
I increased the LineThickness and set the render to wireframe and the vertices were transformed in odd directions. Shouldn't a transformation along the normal result in a scaling?
I have a picture of it here: i<dot>imgur<dot>com/Yrj6V<dot>png (in wouldn't let me post a link, so you'll have to copy it)
To clarify, I have FillMode set to WireFrame, and CullMode set to None in order to better show what I'm talking about.
Hmm… after playing around with this for a little bit, I think you're right. I'm going to see if I can fix this sometime over the next little bit. I've put it on my To Do list. If you haven't heard anything back from me by, let's say, a week from now (Monday, October 3rd 2011) then send me and email to remind me I said I'd look into this.
Ok. Thanks. Now that I know that it's not just me I can rule out a ton of possibilities. That should make finding the solution a lot easier.
I just solved it. You need to change LineThickness to a float instead of a float4. I'm double checking all my code to make sure that's the only change I made, but that seems to be the issue.
You're right. It's that simple. I don't know why I was putting float4 in there instead. I guess just out of habit. Too many vectors to pass over.
At any rate, EmpiresBane is correct, it should be float, not float4. I've updated the tutorial to reflect this.
Hi;
I've tried to mix this tutorial with Skybox. However after first draw, graphics.GraphicsDevice.RasterizerState becomes null hence I get an error. (First draw gives an expected render with model in toon shader and skybox in background)
I expect the reason is CullMode is set in Draw method code for skybox, then is set in HLSL technique for Toon Shader. Which nulls graphics.GraphicsDevice.RasterizerState.
So I took originalRasterizerState as a property, not a local instance in Draw method, and then added a bool, firstRun to check if it's the first Draw or not.
if (firstRun == true)
{
originalRasterizerState = graphics.GraphicsDevice.RasterizerState;
firstRun = false;
}
else
It works.
However I'd like to know if there would be another way to interoperate CullMode in HLSL and .NET code without doing this trick?
Thanks
Yeah, there should be a way of doing this without the special code for the first time. Would you mind posting the relevant code here, or perhaps emailing it to me or something? I'd be happy to take a look and see.
After all, it sounds like what you're describing is that I'm setting something one way in one of my tutorials and not setting it back correctly, so it doesn't behave well when you combine the ideas in the various tutorials. I'm just having a hard time pinpointing the actual problem without seeing how you're putting it all together.
Thanks for doing these tutorials!
I need some help implementing this shader in my game. Got it all up and running and everything works great.
I would however like to make a change where there is a toon Outline but with Diffuse surface‎ so no celpixelshader? i guess?
How do i go around this and edit the shader to just outline my object and just show diffuse texture? Heres what it looks like atm.
Regards
This should be do-able by turning the pixel shader code into something more like what you find back in the diffuse lighting shader tutorial. The shader I presented in this tutorial is actually doing a lookup of the model's texture and then just "discretizing" the intensity. That's what gives it the cartoon-like feel.
If would seem like if you just remove the code below from the pixel shader, you'd get what you're looking for.