5/28/2023 0 Comments Stencyl shaders![]() finds location on ramp texture that we should sample _WorldSpaceLightPos0 provided by Unityįloat3 lightDir = normalize(_WorldSpaceLightPos0.xyz) convert light direction to world space & normalize Here’s what the fragment shader looks like: Since lighting is one of two colors (dark blue or white), you get a cel-shaded effect with sharp edges on the shadows and no blending. This color is multiplied with the input texture’s albedo. Areas on the model which are less exposed to light (in shadow) thus get the dark blue color, and areas on the model which are more exposed to the light get the white color. Because our light value returns 0-1, it will sample an area on the texture between 0 (the far right x-pos) to 1 (the far left x-pos). The position on the x-axis of the ramp shader then corresponds to either of those colors. The important thing to achieve the cel look is that the ramp texture is blocky, not smooth. You could also use your own texture with multiple colors. The ramp shader is a 2d texture that only has two colors on it: dark blue on the left, and white on the right. To calculate lighting, it takes the dot product of the surface normal and light direction to result in a normalized (0-1) scaler, which it then uses to sample a position on the ramp texture on the x-axis. The texture/lighting pass is nothing advanced. The shader’s input, which are configured in the Unity editor, are the main texture, the ramp texture (used for lighting), and the outline size and color. The shader has two passes: one that applies the texture and lighting, and one that applies the outline. The cel shader was written in Unity’s shader language, which is CG, but with some preprocesser tags and formatting that let you access Unity features. –> Link to final code for the cel and outline shader ![]() This is the end result, applied to an adorable Boston Terrier model made by amazing artist Kytana Le:Īnd here’s the full code for you to reference while following the tutorial: If you do write shader code in Unity, you’ll find the rest of the techniques extra helpful □ If you’ve never written shader code before, or used Unity, you still may get plenty out of this post, because the algorithms used to write the cel shader and the outline are applicable to any shader. If you like this post, be sure to follow me on Twitter □ In addition to this code, you may want to check out the Unity graphics settings for this tutorial, which are important for lighting a cel-shaded game well. Įssentially Ref is the value to test against, Comp is what kind of comparison to make, and Pass is what the resulting action should be.Today, I want to cover two super popular topics for shaders in Unity: ![]() In actual shader code, here is what the hole shader does, in the SubShader section: Tags. Essentially it would look like this, if it was written in normal code: // Wall shader psuedocode bool drawWallPixel = stencilValue != 1 ![]() If there’s a zero, we draw the wall as usual. If there’s a 1, we do not draw that pixel. Once those 1s are there, we will do a test in the wall shader to check if there is a 1 at that wall pixel. In this case, I am filling it with 1s where the hole is, if you were looking directly forward at the hole: Stencil Buffer 000000000 000010000 000111000 000010000 000000000 This works by filling in the stencil buffer with a value for those pixels where the hole is. ![]()
0 Comments
Leave a Reply. |