Pages: 1
  Print  
Author Topic: Toon / Cel Shader Example  (Read 5434 times)
Offline (Unknown gender) time-killer-games
Posted on: September 12, 2013, 06:38:46 pm
"Guest"


Email
Toon / Cel Shader Example

Works in ENIGMA and GameMaker:Studio, and probably other
programs that have use the same shaders system. It's GLSL.

I belive this shader looks a little more interesting in use when
compared to the toon shader Robert included in his 1 million
cubes demo. I took me several hours to find this so please
enjoy. I got it from here.


Vertex:
Code: [Select]
varying vec3 normal, lightDir;
varying vec2 texCoord;

void main()
{
vec4 ecPos;
ecPos = vec4(gl_ModelViewMatrix * gl_Vertex);
lightDir = normalize(vec3(gl_LightSource[0].position) - ecPos.xyz);
normal = normalize(gl_NormalMatrix * gl_Normal);

texCoord = vec2(gl_MultiTexCoord0);
gl_Position = ftransform();
}

The Fragment:
Code: [Select]
varying vec3 normal, lightDir;
varying vec2 texCoord;
uniform sampler2D texture;

void main()
{
float intensity;
vec3 n;
vec4 _color;

n = normalize(normal);
intensity = dot(lightDir, n);

if (intensity > 0.98)
_color = vec4(1.0,1.0,1.0,1.0);
else if (intensity > 0.5)
_color = vec4(0.8,0.8,0.8,1.0);
else if (intensity > 0.35)
_color = vec4(0.4,0.4,0.4,1.0);
else
_color = vec4(0.0,0.0,0.0,1.0);
gl_FragColor = _color * texture2D(texture, texCoord);
}

You can use it with rendering your
entire 3D scene like this:

Code: [Select]
//this should be in your camera object
shader_set(shr_toon);
d3d_set_projection_ext(...);
//Notice I didn't use shader_reset();, and in this case, you shouldn't either.
Or you can is it only with specific
models individually like so:

Code: [Select]
//This should only be used in the objects whose models you actually want to apply the effect to.
shader_set(shr_toon);
//Do all your translating, scaling, and rotating here.
d3d_model_draw(...);
shader_reset(); //In this case you should use shader_reset(); because you don't the entire scene to render this way.

I can't post the screens right now but I'll edit this
post when I capture something that actually looks presentable.
Cheers!
« Last Edit: September 12, 2013, 06:44:41 pm by time-killer-games » Logged
Offline (Male) Goombert
Reply #1 Posted on: September 12, 2013, 06:53:38 pm

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2993

View Profile
I had to rip a bunch of stuff out of mine to get it to work a couple of months ago because I didn't know GLSL. Also, I am wanting to get a forum board for shader created, for now I have my topic at the top of this subforum, maybe you could post this there instead? :P
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) TheExDeus
Reply #2 Posted on: September 13, 2013, 02:05:36 am

Developer
Joined: Apr 2008
Posts: 1860

View Profile
When I replaced the toon one in perftester with this one, I didn't get anything. So maybe post an example as well.
Logged
Pages: 1
  Print