Toon shader, improvements?

Post your questions, suggestions and experiences regarding to Image manipulation, 3d modeling and level editing for the Irrlicht engine here.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Toon shader, improvements?

Post by serengeor »

Ok so I found a shader that got integrated without too much of an effort.
And I would like to know what could be improved

Heres .frag:

Code: Select all

varying vec3 normal;
varying vec3 ec_pos;

void main()
{
	vec4 color = vec4(0.3, 0.7, 1.0, 1.0);
	
	vec3 light = vec3( gl_LightSource[0].position );
	vec3 lightdir = light - ec_pos;
	vec3 reflectVec = normalize(reflect( -lightdir, normal ));
	vec3 viewVec = normalize( -ec_pos );
	//Diffuse intensity
	float diff = max( dot(normalize(lightdir), normalize(normal)), 0.0);
	//Specular intensity
	float spec = 0.0;
	
	//Specular intensity
	if (diff > 0.0)
	{
		spec = max(dot(reflectVec, viewVec), 0.0);
		//The specular highlight only gets smaller when
		//raised to some power.
		//Leaving it out gives a nice big highlight.
		spec = pow(spec,16);
	}
	
	diff =  diff * 0.8 + spec * 0.4;
	
	//Diffuse intensity
	if (diff > 0.90)
		diff = 1.5;
	else if (diff > 0.7)
		diff = 1.2;
	else if (diff > 0.5)
		diff = 0.9;
	else 
		diff = 0.5;
	
	gl_FragColor = color * diff;
}
Heres .vert:

Code: Select all

//Interpolates the vertex normal across the texture fragment
varying vec3 normal;
//Interpolate the texel position in eye space.
varying vec3 ec_pos;

void main()
{
	normal = gl_NormalMatrix * gl_Normal;
	ec_pos = gl_ModelViewMatrix * gl_Vertex;
	
	//Perform fixed transformation for the vertex
	gl_Position = ftransform();		
}
Heres how it looks when integrated into shaders sample:
Image

PS. I'm currently just toying around with shaders.
Working on game: Marrbles (Currently stopped).
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

Do you maybe have some more complex models to apply the shader to?
A sphere doesn't give a good impression of what the shader can do, nor what can be optimized/changed about it
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Radikalizm wrote:Do you maybe have some more complex models to apply the shader to?
A sphere doesn't give a good impression of what the shader can do, nor what can be optimized/changed about it
Okay, will post it in a minute or two.
EDIT: Heres a teapot using the shader:
Image

Image

Do you need more detailed mesh, or is this enough?
Working on game: Marrbles (Currently stopped).
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Post by Mel »

You can do it light dependant, and you can add also code to create the borders, though that can be very tricky.

To add borders you can duplicate the mesh, shift the vertices a bit in the normal's direction and then flip the triangle's normals in order to have a shell over the original mesh that will draw the outlines. This approach, for instance, is used here:

Image
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Wouldn't be edge-detection shader better? Sure, it would require another render pass, but your option basically does too.
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

I'd go for an edge-detection solution too instead of redrawing the geometry, although I don't know how much of a performance gain this would give over redrawing in a forward renderer (would be perfect for a deferred renderer though)
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Post by Mel »

There would be very little impact on the performance, compared to an edge detection shader.

Duplicating the amount of polygons represents very little overhead with that kind of rendering (i am not speaking of skinned meshes, though, only static). Besides, drawing the outlines after the first render would cull almost completely the second mesh. The gain is little, though, but the borders are perfect, can't be easily matched by a edge detection shader. And would enjoy MSAA, for instance.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

Mel wrote:There would be very little impact on the performance, compared to an edge detection shader.

Duplicating the amount of polygons represents very little overhead with that kind of rendering (i am not speaking of skinned meshes, though, only static). Besides, drawing the outlines after the first render would cull almost completely the second mesh. The gain is little, though, but the borders are perfect, can't be easily matched by a edge detection shader. And would enjoy MSAA, for instance.
You're probably right, I'm always thinking in a deferred context these days, and deferred it's just a matter of a rather simple post-processing effect to achieve perfect edge detection
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

you could anti-alias your boundaries

instead of

Code: Select all

if (col>0.6)
   col = 1.0;
else if (col>0.3)
   col = 0.6;
else
   col = 0.0;
use

min((x+0.7)^128,0.6)+min((x+0.4)^128,0.4)

which should be waaaay faster because of branching (ewww control statements in shaders)
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

devsh wrote:(ewww control statements in shaders)
I can agree with you that these weren't needed in this case, but no need to generalize, most of the time you can't really do without them ;)
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Thank you all.
I'd love to have borders but, wouldn't it cost too much if I'd make my all level with borders?(tough it wouldn't be too much of poly's as my levels would be made from simple geometries).
And I'm not really good at shaders, only started yesterday, so you know :roll:

How would I actually use that:

Code: Select all

min((x+0.7)^128,0.6)+min((x+0.4)^128,0.4) 
I'll look up glsl cheat sheet, and look if I can figure it out.
Last edited by serengeor on Thu Mar 17, 2011 8:58 pm, edited 1 time in total.
Working on game: Marrbles (Currently stopped).
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Post by Mel »

The levels can be designed with textures with borders, or you could always draw the level textures using toon style, but no toon shader at all. The good thing of the levels in toon style is that they don't need accurate lighting. That is done in many of the Dragon Ball fighting games for PS2, perhaps.

(images > 1000 words :lol: )

Image
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

New pic with mel's suggestion:
Image
Working on game: Marrbles (Currently stopped).
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Post by Mel »

Looks quite nice. although the model should have a uniform outline though it is a really interesting toon shader.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

I actually like this more than the uniform outline style
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Post Reply