Edit in GitHubLog an issue

Basic toon shader


Import from libraries.

Copied to your clipboard
import lib-sampler.glsl

We define the global light position

Copied to your clipboard
const vec3 light_pos = vec3(10.0, 10.0, 10.0);

We bind the auto param world eye position to our uniform camera_pos.

Copied to your clipboard
//: param auto world_eye_position
uniform vec3 camera_pos;

We bind the document's channel base color to our uniform basecolor_tex.

Copied to your clipboard
//: param auto channel_basecolor
uniform SamplerSparse basecolor_tex;

We bind the mesh curvature to our uniform curvature_tex. If no curvature is available, transparent texture is provided.

Copied to your clipboard
//: param auto texture_curvature
uniform SamplerSparse curvature_tex;

We define a new custom tweak for this shader, along with its default value. This one is used to tweak the thickness of outline, when shadowed.

Copied to your clipboard
//: param custom {
//: "default": 0.4,
//: "min": 0.0,
//: "max": 1.0,
//: "label": "Unlit outline thickness"
//: }
uniform float unlit_outline_thickness;

We define a new custom tweak for this shader, along with its default value. This one is used to tweak the thickness of outline, when lit.

Copied to your clipboard
//: param custom {
//: "default": 0.1,
//: "min": 0.0,
//: "max": 1.0,
//: "label": "Lit outline thickness"
//: }
uniform float lit_outline_thickness;

Whether we prefer using the curvature or not.

Copied to your clipboard
//: param custom {
//: "default": false,
//: "label": "Use curvature"
//: }
uniform bool use_curvature;

Entry point of the shader.

Copied to your clipboard
void shade(V2F inputs)
{

We compute a few useful values.

Copied to your clipboard
vec3 V = normalize(camera_pos - inputs.position);
vec3 N = normalize(inputs.normal);
vec3 L = normalize(light_pos - inputs.position);
float NdV = dot(N, V);
float NdL = max(0.0, dot(N, L));

Priority is to performs the outline detection. Allow the user to choose whether he prefers using the curvature map for outline detection or not.

Copied to your clipboard
if (use_curvature) {
float curv = textureSparse(curvature_tex, inputs.sparse_coord).r;
NdV = 1.0 - curv;
}

If outline condition is reach, exit with black color.

Copied to your clipboard
if (NdV < mix(unlit_outline_thickness, lit_outline_thickness, NdL)) {
return;
}

Here, we perform a 4 steps discretization of color.

Copied to your clipboard
vec3 color = getBaseColor(basecolor_tex, inputs.sparse_coord);
if (NdL > 0.75) {
color = color;
} else if (NdL > 0.5) {
color = color * 0.5;
} else if (NdL > 0.1) {
color = color * 0.1;
}
else

Fallback is black.

Copied to your clipboard
color = vec3(0.0);
diffuseShadingOutput(color);
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.