My friend needs some help with this glsl code

Hello, my friend iyad needs some helo with his glsl code, here is what he asked me to ask the issue here:

"I have this shader which is just a prewitt operator, I feed it the depth texture, the goal was to outline objects, and it works great when the camera is close to surface, but breakes otherwise

is there a way to create true stencil outliunes in upbge, and access them in shader?
if not how can this one be fixed, to get something similar to Unity’s “Edge Detect Effect Normals” post processing effect

Link to the current broken code:

Link to unitys edge detection

This is what iyad said, he needs your help, pls help him, he doesnt use blender artist so he asked me to post the issue here, pls help him, thanks!

i think it cannot be done at the filter level.
but someone comments in the video below:
To get an outline around an object in front of another object, couldn’t you just take the difference between the depth values of the pixels and see if it’s above a certain threshold? All you would have to do is change your second if statement to something like:

if (abs(depth.r - new_depth.r) >= .01)

This would make some objects have outlines around parts in front of it (a character’s arm would have an outline if it were in front of its body), but I don’t think this behavior would be a problem.

uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_DepthTexture;

uniform float offset_x = 0.003;
uniform vec4 outline_color = vec4(0.0, 0.0, 0.0, 1);
uniform mat2 rot = mat2(vec2(0, -1), vec2(1, 0));

void main(void){
    vec2 coord = gl_TexCoord[0].xy;
	vec4 color = texture2D(bgl_RenderedTexture,coord);
    vec4 depth = texture2D(bgl_DepthTexture,coord);
    vec2 offset = vec2(offset_x, 0);
    vec4 new_depth;
	if (depth.r == 1.0){

        for (int i = 0; i < 4; ++i ){
            new_depth = texture2D(bgl_DepthTexture,coord + offset);
            if (new_depth.r < 1.0){
            //if (abs(depth.r - new_depth.r) >= .01)
                color = outline_color;
            offset = rot * offset;

	gl_FragColor = color;

tell iyad to come to BA.

edgeDetectAO.blend (522.4 KB)

  • this works better even when the camera is far from the surface
  • has alpha blur implemented
  • looks horrible
  • can be improved
1 Like

Oh, thanks, I will inform him about this msg, thanks!

Hey Murilo, the blend file u sent doesnt work on my pc…
pls help me
heres the error which the console gives me when I run the blend file

take out ‘const’ from the variables declarations
use the mipmap only algorithm

i learn a lot by solving console erros
i think you are using old pc or old olpenGL version
i’ll see it later

u mean the const word in the user variable in alpha blur script? If yes, I will try it but I didn’t understood “mishap only algorithm”

yes u can try that, the const word in all variables not only the user variable in alpha blur script.

see the comments in the line 8, mipmap only algorithm is 3 and linear blur is 1

and uncomment the last line in the edgeDetect filter to disable blur and combine the outline with image

test this:
edgeDetectAOv2.blend (517.7 KB)

1 Like

ok, so this script does something, but something extremely weird
see this pic

And heres the new error

V1 indeed got errors, V2 does not have errors

Also if i put yellow plane below monkey, monkey outline will be for 50% so it’s not 100% working but nice results, only had a cellshader before and that shaded every cell, ill keep an eye on this because this outline thingy is very usefull.


I dont know what that last error message means but its not a shader error so it works.
the filter do what it should do: blur the outline in a cheap way. It was your idea :wink:

play with the LOD_BIAS and BLUR_SIZE to control the amount of blur.

I think it’s not working 100% because of itsome current blur algorithm, which is currently set to mipmap, you can change it though

Hey dude, my friend in iyad said that it looks of low quality because of the mipmap blur algorithm, is it true?

yes and no.
kernel blur is reavy
if you want it high quality and fast you have to do multisampling and downscaling
that is harder.

Oh, but what’s the heavy one?

i removed the heavy ones
use ‘linear blur’, it looks better than ‘mipmap only’

1 Like

Can they be implemented again? If yes, tell me the steps and I will do them

i have already…

1 Like

in V4 i added user variables in the edgeDetect script to more easilly control the effect.

edgeDetectAOv4.blend (523.9 KB)