Topic: Outline via glsl?

Hi - Just to get it right (inside my brain..)

If i want normal rendering and also a black outline (like in toon shading but without the toon shading)...
Then i need to write a glsl shader and add them in the blender export panel under materials/maritas/customshader in the vertex/fragment shader slots?

Found goggling the following code, haven't been able to test it yet, but could it work?

for Vertex:
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();
}

for Fragment:
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);   
}

Last edited by Kralle (2011-03-03 10:42:51)

Re: Outline via glsl?

Hi,
for cell shading, you can do it using a glsl shader that you link using blender and custom material,
like example named "cartoon t-pot". Look in "Example/Demos/shaders/" for glsl simple cartoon example.

For outlines, it depend on the technique you want to use,
but it will generally be a post process effect comparing pixels depth and/or normals.

Post process is not implemented as a one clic feature yet,
it can only be done manually using c++ and game plugin,
I'm working on a example for this, I hope to finish it soon.

If you are not programming, there is a "cheap" technique to do outlines,
it's not very evolved but it can give good results and doesn't need shaders.
Using blender, you duplicate your mesh, scale it a little bit and reverse the normals,
you then apply to this copy a black material. The copy will create an outilne thanks to z-buffer,
it is not a pretty technique, but it has been used in lots of commercial games.
(in blender, make sure to disable "double sided" for both meshs)

Re: Outline via glsl?

Thanx for the fast answer.

Yeah - i know the "cheap" trick.
It works very well on playermodels and static props (boxes, barrels, crates, furniture, etc.)
but it's insane if you want outline in the enviroment, you can't duplicate and scale the whole level itself.

Re: Outline via glsl?

Yes, i agree for the environment, it's globally not a pretty technique. Stay in touch with the website news, I will publish more example and post process will be one.

Re: Outline via glsl?

It's not exactly what you need but I made a simple ink shader example there :
http://www.maratis3d.org/?p=548

Re: Outline via glsl?

Nice - i will take a look at it.
---
mmmh - the ink shader looks strange.
it seems to be 45 degree rotated (on my system).
I can see half white and half black on the teacup.
Will post a screen later (after work).

Last edited by Kralle (2011-03-15 13:22:09)

Re: Outline via glsl?

As it looks for me...
http://img855.imageshack.us/i/inkshade.jpg/

Last edited by Kralle (2011-03-16 10:58:56)

Re: Outline via glsl?

how, strange... thank you for the reply.
There is maybe a clamp computation or a negative value working differently on your 3d card,
it's probably a small thing to check. I will have a look, if you can also try to modify the shader
on your machine tell us.

Re: Outline via glsl?

I will watch to correct the full example, but for now you can try this :

vec3 N = normalize(normal);

float ink = dot(normalize(-position), N);
ink = clamp(pow(ink*3.0, 20.0), 0.0, 1.0);

gl_FragColor = vec4(ink, ink, ink, 1.0);

Re: Outline via glsl?

Yep - that made it.

Nice. Thumb up.
Thanx for all your help and work!