anatolanatol Posts: 4Member

Hi, this is somewhat related to an older post of mine. I had a bit of a break since then but am trying to get this to work with a shader.

I got a solution that isn't very refined yet, but in principle it does what I'm after.

The effect above is created by the following shader code as a material for a simple sphere primitive. (The scale is animated via an AnimationPlayer for the flickering, and the sphere is attached to a the red orb sprite).

shader_type spatial;
render_mode unshaded, cull_disabled; 

uniform vec4 main_color : hint_color; // transparent colour
uniform vec4 intersection_color : hint_color; // red in this case
uniform float intersection_max_threshold = -1.0;
uniform float near = 1.0;
uniform float far = 100.0;

float linearize(float c_depth) {
    c_depth = 2.0 * c_depth - 1.0;
    return near * far / (far + c_depth * (near - far));

void fragment()
    float zdepth = linearize(texture(DEPTH_TEXTURE, SCREEN_UV).x);
    vec4 col = mix(intersection_color, main_color, intersection_max_threshold);
    ALBEDO = col.rgb * ( zdepth * 0.005 );
    ALPHA = col.a;

Well, I'd be happy with this to further refine it, but it turns out that this is only good for GLES3. GLES2 doesn't do anything, and I suspect the culprit is the DEPTH_TEXTURE.

While GLES2 supports DEPTH_TEXTURE in shaders, it may not work on some old hardware (especially mobile).

It doesn't even work in the Godot preview, so I don't think this only affects older mobile hardware in this case.

Does anyone who's more experienced with shaders have any idea if there's a workaround for this situation? Thank you for any hints.


  • MegalomaniakMegalomaniak Posts: 3,810Admin
    edited February 22

    Well, it says especially mobile not specifically mobile so some desktop GPUs likely apply to that too.

    You could try and do it via (world-space) vertex colors but it wouldn't be pixel perfectly smooth and round, unless everything is sufficiently high-poly to have (near-)sub-pixel geometry in relation to the final render.

    It could be another shader added as a shader pass to all objects, but this would add another draw call and need to be evaluated if it's even worth the performance cost.

  • anatolanatol Posts: 4Member

    Thank you @Megalomaniak . Yes, you're right, it doesn't mean it'll only affect mobiles. I just assumed that on a Macbook it should work, but that may as well be some other issue.

    I'll do some research around your suggestions. I don't have a lot of experience with shaders, so it doesn't tell me much yet. But it's a good hint in a possible direction, and it's always good to discover and learn new things.

    A shader pass to all objects, yes, it may work, I'll see what the performance cost is.

    Basically all objects will be flat 2d shapes. For this test I'm using 3d Sprites with a billboard texture of a viewport and the content is drawn as Polygon2D. I don't even know if that's a good choice performance wise. I think I'll eventually change this to just 2D bitmap sprites or imported flat shapes from Blender or somehow as a Vector, I think it might become a pain trying to write code to draw all game sprites.

    I was also considering to do it all in 2d, and I tried that initially, but I felt with what I have in mind I'd end up having to write some sort of virtual camera module that can imitate 3d, and it felt that I'm reinventing the wheel for something that Godot can already do a lot better than such a module could ever achieve. So I switched to 3d.

    Anyway, I still need to learn a lot around Godot, but it's a fantastic engine.

  • MegalomaniakMegalomaniak Posts: 3,810Admin

    If it is just a 2D game then this might be much easier...

  • anatolanatol Posts: 4Member

    Yes, I'll do some tests. I might go back to 2D where I still need some shader (or maybe some mask setup) for this effect, and items would still need a virtual z value. It's parallaxing and the "light circle" scale changes based on z position of the item that it illuminates. The parallaxing part is easy, I've done that in an early test.

    In 3d it just seemed easier to set up level sprites, rather than working with virtual z values I can just position items freely in the editor.

    On the other hand, I always quite like working within limits, so for me that's another plus for 2d.

    I'll see, it's an interesting problem to solve.

  • SIsilicon28SIsilicon28 Posts: 769Moderator
    edited February 25

    Do you have MSAA enabled? Last time I wanted to use a depth texture in GLES 2, the depth texture had no data with MSAA. It worked when I disabled it.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file