GLSL cel/toon shader with textures

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
gd_anon
Posts: 98
Joined: Sun Nov 30, 2008 11:03 am
Location: Nowhere

GLSL cel/toon shader with textures

Post by gd_anon »

The toon shader from Lighthouse3d doesnt support textures. Anyone has one that does?
Last edited by gd_anon on Sat Oct 03, 2009 6:44 am, edited 2 times in total.
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

i think that the way it should go :
u model your mesh,
you texture it without adding any shaders(because they wont appear in irrlicht or any other 3d engine ...i guess)
then u add the toon shader to the model using irrlicht
gd_anon
Posts: 98
Joined: Sun Nov 30, 2008 11:03 am
Location: Nowhere

Post by gd_anon »

I dont think you get what i mean. When i apply the shader from lighthouse3d the texture isnt rendered.
Btw. I think youre right that you cant use your materials from modeling applications , but Blender is different, in the "Game" menu you can set the materials to GLSL mode.
Then with Python theres a way to get the vertex and fragment shaders generated by Blender as python strings.
Youll use

Code: Select all

getVertexProg()
And

Code: Select all

getFragmentProg()
for that. However i dont know python.
Makes me wonder why nobody has made an export script till now.
Link:
http://download.blender.org/documentati ... r.htm#GLSL
Viz_Fuerte
Posts: 91
Joined: Sun Oct 19, 2008 5:29 pm
Location: Valencia (Spain)
Contact:

Post by Viz_Fuerte »

Toon shader with texture by VIZ.fuerte

toon.vert

Code: Select all

varying vec3 vNormal;
varying vec3 vVertex;

void main(void)
{
	gl_Position = ftransform();
	gl_TexCoord[0] = gl_MultiTexCoord0;
	vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
	vNormal = normalize(gl_NormalMatrix * gl_Normal);
}
toon.frag

Code: Select all

varying vec3 vNormal;
varying vec3 vVertex;

uniform float silhouetteThreshold;

uniform sampler2D tex;

void main (void)
{

vec4 materialColor = gl_FrontMaterial.diffuse;

vec4 silhouetteColor = vec4(0.0, 0.0, 0.0, 1.0);

vec4 specularColor = gl_FrontMaterial.specular;

vec3 eyePos = normalize(-vVertex);
vec3 lightPos = gl_LightSource[0].position.xyz;

vec3 Normal = vNormal; //normalize(vNormal);
vec3 EyeVert = normalize(eyePos - vVertex);
vec3 LightVert = normalize(lightPos - vVertex);
vec3 EyeLight = normalize(LightVert+EyeVert);
vec4 texture = texture2D(tex,gl_TexCoord[0].st);

	float sil = max(dot(Normal,EyeVert), 0.0);
	if( sil < silhouetteThreshold )
		gl_FragColor = silhouetteColor;
	else
	{
		gl_FragColor = materialColor*texture;

		float spec = pow(max(dot(Normal,EyeLight),0.0), 5.0);
		if( spec < 0.05 )
			gl_FragColor *= 0.9;
		else
			gl_FragColor = specularColor*texture;

		float diffuse = max(dot(Normal,LightVert),0.0);
		if( diffuse < 0.3 )
			gl_FragColor *=0.8;

	}
}
I hope you serve. :D
gd_anon
Posts: 98
Joined: Sun Nov 30, 2008 11:03 am
Location: Nowhere

Post by gd_anon »

Cool:
Image
Image
Mind if this will be added to the wiki?
And once again, thanks.
Viz_Fuerte
Posts: 91
Joined: Sun Oct 19, 2008 5:29 pm
Location: Valencia (Spain)
Contact:

Post by Viz_Fuerte »

If you want to draw the silhouette, adds value to:

Code: Select all

uniform float silhouetteThreshold; 
gd_anon
Posts: 98
Joined: Sun Nov 30, 2008 11:03 am
Location: Nowhere

Post by gd_anon »

The silhouette is the outline, right? I tried it but it looks weird.
I think there should be a better way to achive that.
But anyway, dude, do you mind if this will be added to the wiki?
Viz_Fuerte
Posts: 91
Joined: Sun Oct 19, 2008 5:29 pm
Location: Valencia (Spain)
Contact:

Post by Viz_Fuerte »

The silhouette is the outline, right?
Yes, but only obscures the edges of the mesh,
not create a line.
...added to the wiki?
:D Of course :D


But, I would prefer to edit this code in ARB(GPU assembly language), for more compatibility.

ARB (only for OpenGL)
ToonShader.vp

Code: Select all

!!ARBvp1.0
PARAM c[12] = { program.local[0],
		state.matrix.modelview.transpose,
		state.matrix.modelview.inverse.row[0..2],
		state.matrix.mvp };
TEMP R0;
TEMP R1;
MUL R0.xyz, vertex.normal.y, c[6];
MAD R0.xyz, vertex.normal.x, c[5], R0;
MAD R1.xyz, vertex.normal.z, c[7], R0;
MUL R0.xyz, vertex.position.y, c[2];
DP3 R0.w, R1, R1;
MAD R0.xyz, vertex.position.x, c[1], R0;
RSQ R0.w, R0.w;
MAD R0.xyz, vertex.position.z, c[3], R0;
MOV result.texcoord[0], vertex.texcoord[0];
MUL result.texcoord[1].xyz, R0.w, R1;
MAD result.texcoord[2].xyz, vertex.position.w, c[4], R0;
DP4 result.position.w, vertex.position, c[11];
DP4 result.position.z, vertex.position, c[10];
DP4 result.position.y, vertex.position, c[9];
DP4 result.position.x, vertex.position, c[8];
END
ToonShader.fp

Code: Select all

!!ARBfp1.0
OPTION ARB_precision_hint_nicest;

#var float silhouetteThreshold :  : c[3] : -1 : 1

PARAM c[6] = { state.material.front.diffuse,
		state.material.front.specular,
		state.light[0].position,
		program.local[3],
		{ 0, 1, 0.30000001, 5 },
		{ 0.050000001, 0.89999998, 0.80000001 } };
TEMP R0;
TEMP R1;
TEMP R2;
TEMP R3;
TEMP R4;
ADD R1.xyz, -fragment.texcoord[2], c[2];
DP3 R1.w, R1, R1;
RSQ R1.w, R1.w;
DP3 R0.x, -fragment.texcoord[2], -fragment.texcoord[2];
RSQ R0.x, R0.x;
MAD R0.xyz, R0.x, -fragment.texcoord[2], -fragment.texcoord[2];
DP3 R0.w, R0, R0;
RSQ R0.w, R0.w;
MUL R0.xyz, R0.w, R0;
MUL R3.xyz, R1.w, R1;
ADD R1.xyz, R0, R3;
DP3 R0.x, fragment.texcoord[1], R0;
DP3 R0.w, R1, R1;
RSQ R0.w, R0.w;
MUL R1.xyz, R0.w, R1;
DP3 R0.w, fragment.texcoord[1], R1;
MAX R0.w, R0, c[4].x;
POW R0.y, R0.w, c[4].w;
SLT R4.x, R0.y, c[5];
MAX R0.x, R0, c[4];
SLT R1.x, R0, c[3];
ABS R2.x, R1;
TEX R0, fragment.texcoord[0], texture[0], 2D;
MUL R1, R0, c[0];
CMP R3.w, -R2.x, c[4].x, c[4].y;
CMP R1, -R3.w, R1, c[4].xxxy;
ABS R4.y, R4.x;
MUL R2, R1, c[5].y;
MUL R4.x, R3.w, R4;
CMP R1, -R4.x, R2, R1;
CMP R2.y, -R4, c[4].x, c[4];
DP3 R2.x, fragment.texcoord[1], R3;
MAX R2.x, R2, c[4];
SLT R2.x, R2, c[4].z;
MUL R2.y, R3.w, R2;
MUL R0, R0, c[1];
CMP R0, -R2.y, R0, R1;
MUL R1, R0, c[5].z;
MUL R2.x, R3.w, R2;
CMP result.color, -R2.x, R1, R0;
END
gd_anon
Posts: 98
Joined: Sun Nov 30, 2008 11:03 am
Location: Nowhere

Post by gd_anon »

Cool.
Someone please add this to the wiki.
I cant, im on a handheld.
Edit. added to the wiki
Post Reply