I'll be going back to the drawing board if this isn't possible but I was pleasantly surprised to find out how well transparent materials worked in Godot and it got me thinking back to a topic I made awhile back covering influence border effects and the like because people were telling me there were better solutions to the problem of how you create border effects like this one.

If I could get this all working I could probably whip up way more efficient border code, does anyone know if it's possible to stop the overlap with the rings from happening when nodes come together? I managed to sort of get it working messing about with the material blend modes but as you can see mix seems to mess up the colour big time. The outline in the second screenshot is exactly the effect that I want though.


What are the rings? A plane with a texture, a 3D object? In general, what you are asking for is complex, but I can try to find a solution.

    cybereality It's a cylinder with a material on it that has an outline and a transparent outline, I'm just experimenting right now so if you know of better methods to do the sort of thing I'm looking at feel free to tell me. I made a thread about this topic awhile back and unfortunately the method that gave the best results for a nice influence ring that went across terrain was a generated mesh.

    I guess before I start delving into the problem again and having to re-write mesh generation code from Unity to Godot I'm wondering if there's another solution to this.

    cybereality Correct me if I'm wrong but I took a proper look at this and it seems to be about creating outlines only? It doesn't seem to address the issue of how to prevent overlap. My goal when looking at this is to have two different circles representing a radius and then as the radius expands the circles expand with it merging together if they overlap.

    Now I think about it and this kind of surprised me but the most modern example I can find of this technique is Civilization VI, in this screenshot I searched up you can see though that the border even follows the terrain. See the border on the bottom right of the screenshot going over the hill.

    I admit I have something of an obsession with this sort of effect, me and two other programmers managed to get it just about working in Unity and it even took some ridiculous shader trickery by making the engine think one type of maths was another but it is possible to do. I'm actually kind of hoping because of Godot's more open source nature there's a better way to implement it, although maybe that's wishful thinking.

    Learning about merging shaders together this way is something I'd find particularly useful for the kind of effects I'm looking at because when you nail this effect for influence borders and the like it looks fantastic and clean. Even better if you could get a more 3D look from it.

    I think that being able to grab vertices within a radius could be the main key to getting the sorts of influence border effects that I'm after. If I could get the vertices added to an array then I could potentially use a shader to hide those by simply lowering the alpha to 0 that are within the radius then preventing the overlap. That would be really nice to be able to do compared to the absolute craziness we were messing with in Unity although then again, I wonder if that would just be a more efficient solution overall since it's purely for a shader.

    I don't know, it depends on what people here would suggest I guess?

    Yes, what you want is essentially an outline, which is why I linked that article. The way I would do it is render all the shapes, the silhouette of the objects with the outline to a viewport. These would be the volume of the shape, for example, circles with a solid white color and no outline. Then you could render two, or more, of these to a render target texture (the viewport texture). Then use a shader to draw an outline in any color you want. This will give you the outline color and the shape, everything else will be white. Then you project this texture onto another surface with a multiply blend mode. The white will become invisible, and then the other color will mix with what is there. From the screenshot of Civilization, it appears they did something similar to this, but there are other ways.

      cybereality Interesting, I really want to learn in detail about these sorts of methods, I guess the outline shader isn't actually going to be that big of a deal as it turns out so thank your for those resources. What's confusing me now is going to be the multiply blend mode because that's what is making the magic happen with stopping the overlap. I'll poke at the documentation some more, one thing I've also been doing is taking a look at how other pieces of software are handling these types of modes as there seems to be a lot of overlap ( Aha ) where you've got very similar modes but different ideas on how to approach them and what they're used for.

      Wait, what? LOL okay, it looks like the thing I need to learn about most is what the blend modes actually do especially multiply, confirmed what you're writing about checking out this video, I didn't know about any of this, that's fascinating thanks.

      I'll have an experiment with the colours and see what I can come up with, the reason it would be better if it was 3D is I could have the vertices drop down to the terrain individually without any of the crazy mesh generation nonsense. Plus I could also potentially fiddle around with some shader trickery and make some fancy animation effect for the outline.

      Maybe it will make more sense with an image.

      It's pretty simple actually. Multiply just uses multiplication on the components of a color (additive uses addition, etc.). So let use just take one color floating point number (such as a grayscale image). If the original color value of the red component is 0.67 and you multiply by 1.0 you get the original value of 0.67 (0.67 * 1.0 = 0.67). This is why white is invisible, because the color white is (1.0, 1.0, 1.0) so any color you multiply will be the original color. Black is (0.0, 0.0, 0.0) so if you multiply by black, you get black (0.67 * 0.0 = 0.0). Additive works the same way, but you add the two colors rather than multiply.

      Okay, that's definitely making way more sense now thanks, I guess my main question is now why is it that you can't use a standard material and have to draw the outline yourself? Is it just down to the way the materials work that it can't happen? I think this is what has been throwing me off all this time with trying to reproduce these kinds of influence border effects.

      Well a lot of stuff in game development you have to do yourself. The engine gives you the tools, just like an artist may get a paint brush and a blank canvas, it's up to you to draw something.

        cybereality Sorry, I should be clear, I guess I meant to ask what is is about how materials work in the backend that simply putting in a transparent material with an outline doesn't work? It looks like the same sort of method to me on the front end, but clearly it's not.

        In a 3D engine, each object is drawn one by one. If you have transparent objects, they are also drawn one by one (after the opaque objects in a particular order, such as back-to-front). Nothing is really ever combined (though by use of the z-buffer and other tricks, it appears to be one scene). Transparency is very difficult to do properly and is basically a hack and causes all sorts of weird issues if you do it wrong. So there is not a good way to handle this generically. But the concept is complex, and there are a few ways around it, like rendering to a separate buffer like I am suggesting.

          cybereality So I cheated a little just to do some testing and grabbed an outline shader from the asset library, what seems to be happening is the outline is automatically registering the existence of the two top and bottom faces and rendering them. This obviously isn't the effect that I want any ideas why this might be? I suppose the good thing is that the two cylinders are blending correctly.


          8 days later

          Okay, now I think I have a clearer understanding of what's been confusing me. I did some blender trickery to make my mesh sort of work and I made two different materials for the top and bottom faces and sides of the cylinder. The top and bottom faces are white and set to multiply so they're all transparent this prevents the outline shader from just blindly creating an outline around the whole mesh.

          However where I'm struggling is the last bit where you get a quad or decal? What does that involve? I think I might need a step by step on how to do that. I can't really wrap my head around the concept terribly well even though it makes total sense in theory.

          Should I be declaring the blend mode in the shader itself in this instance? Or is this something I'm badly misunderstanding in how the method is supposed to work?

            Lethn I took the 3D pixel perfect outline shader from the asset library and did some editing to the shader but no dice on the blend mode? I feel like I'm badly misunderstanding the process. On the bright side I do seem to be able to edit and tweak shaders a bit at least, got rid of the culling so I could see everything.

            shader_type spatial;
            render_mode cull_disabled, unshaded, skip_vertex_transform;
            render_mode blend_mul;
            uniform vec4 albedo : hint_color;
            uniform float outline_width = 1.0;
            
            void vertex() {
            	mat4 matrix_m = WORLD_MATRIX;
            	mat4 matrix_vp = PROJECTION_MATRIX * INV_CAMERA_MATRIX;
            	
            	vec4 clip_position = matrix_vp * (matrix_m * vec4(VERTEX, 1.0));
            	vec3 clip_normal = mat3(matrix_vp) * (mat3(matrix_m) * NORMAL);
            
            	vec2 screen_size = VIEWPORT_SIZE;
            	vec2 offset = normalize(clip_normal.xy) / screen_size.xy * outline_width * clip_position.w * 2.0;
            	clip_position.xy += offset;
            	
            	VERTEX = (INV_PROJECTION_MATRIX * clip_position).xyz;
            }
            
            void fragment() {
            	ALBEDO = albedo.rgb;
            }
            6 days later

            Bumping because it would be nice to get this problem out of the way.

            cybereality Thanks, I've got more to search for now that I've got new keywords, I just came across this tutorial which is exactly what I want but it's for 2D.

            Almost there though, trying to keep digging, I would be very surprised if somebody hasn't done this before me, yes it's a complicated shader technique, but also it's a very specific one I've noticed. I think that's mainly the problem I have with searching up information and tutorials.

            Okay, I think after looking at this tutorial on viewports I've realised I barely know how to use viewports properly and that's why I'm struggling, this basic video helped.

            Believe it or not I think the issue is less to do with the outline shader itself, I believe that's setup correctly unless I'm blatantly missing something and the pixel checks are what's needed to turn the overlapping transparent most of all. It's the viewport trickery I'm struggling with. Amazingly with this sort of code messing with the vertices positioning is actually the easy part, I already know how I'm going to deal with that now.

            5 days later

            I have so many questions about this method that I'm actually going to have to do a write up but I think I'm getting something of a breakthrough with it thanks to the video I found on the 2D version of it. At the moment though, I've got some errors on it and I don't know why. I also have no idea where I'm supposed to be placing the quad mesh with the viewport texture on it in order for the multiply blend to work on the shader.

            I'll be coming back to this thread when I've had more time to think on the specifics.

            E 0:00:01.245 get_path: Cannot get path of node as it is not in a scene tree.
            <C++ Error> Condition "!is_inside_tree()" is true. Returned: NodePath()
            <C++ Source> scene/main/node.cpp:1587 @ get_path()

            E 0:00:01.247 get_node: (Node not found: "ViewportContainer/Viewport" (relative to "").)
            <C++ Error> Condition "!node" is true. Returned: nullptr
            <C++ Source> scene/main/node.cpp:1325 @ get_node()

            E 0:00:01.248 setup_local_to_scene: ViewportTexture: Path to node is invalid.
            <C++ Error> Condition "!vpn" is true.
            <C++ Source> scene/main/viewport.cpp:69 @ setup_local_to_scene()

            Right, I've at least made something happen now with this confusing viewport texture method, however because I'm still learning how it all works I'm struggling to have the rings even do anything with this effect. I'm 99% sure it's probably not the shader because even in the 2D example I managed to find it was all viewport black magic trickery.

            I tried to keep things as simple as possible for now with both cameras pointed down on top of each other and in the exact same position. I've managed to get a viewport texture onto the quad mesh and it's set to multiply. I do understand how these viewports seem to work, that concept I'm fine with it's just the way the multiply blend mode is working in this case with preventing the overlap that I'm totally struggling with. I don't even really know as you can probably tell where the quad is supposed to be and what it's supposed to be doing to deal with the circle overlap problem.

            You can see in the screenshot that there's nothing really happening with the circles on the quad mesh, I've shown how I've got my hierarchy setup as well a where the viewport texture actually is because it's a really awesome effect when you can pull it off and can be used for all kind of pretty borders among other things. I need to do something about these duplicates as well that seem to keep popping up when it should be purely happening on the quad?

            Holy crap I really am having something of a breakthrough with this technique, I'm going to do a full write up tutorial on this thread just to detail the steps for anyone trying to look at this effect because it's ridiculous now even on how older engines people don't really seem to have looked at the topic so much. Probably because it's a pain in the arse to implement if you don't know anything about viewports, blend modes and how they work.

            There are problems I'm running into with this effect though at the moment, there is currently some z fighting I think happening with the outlines. I think maybe Z Write off will fix that, I will test it, the resolution for the quad is too small so I'm going to have to do some more fiddling. I hadn't realised when you posted in your example @cybereality that when you meant make the cylinders white as in a solid white, I thought I needed to keep them transparent and use multiply mode on those as well. Is this the effect that's supposed to happen when you blend the two solid shapes in on the quad?

            I remember people on the Unity forums describing the same sort of method and back then I had no idea how to really use it effectively and this time it was for a fog of war. I had no idea this was what they were on about but I did keep a note of. This will take some tweaking but I'm definitely going to do a proper written tutorial on this.

            This is what I've managed so far with some resolution tweaking, but I need to get rid of the black and the quad is making the borders look fairly 2D and flat, I don't know if that's maybe the camera setup though.

            The clear color of the viewport also needs to be white. But you are pretty close.

            Got it! So I couldn't seem to set the clear color in the UI for the viewport but I changed the default colour to white through the code, I re-enabled the background and now the quad is completely transparent thanks to the multiply mode. Definitely doing a step by step write up on this when I'm in the mood to help anyone who likes this sort of effect.

            My main complaint with the overlapping and I'm convinced that this is z-fighting is there are some tiny bits of pixels still left reading the overlap of the outline. I'll also do a write up with how to do the vertex positioning which should be more straightforward ironically because it's just grabbing the right ones and raycasting them to the floor each time there's a change in scale.

            I've noticed though there are some input problems, I think the quad might be interfering with the raycasting and camera movement somehow despite collision being off but I'll double check that. It may be I just need to take another look at the code and check my second viewport camera is working correctly.

            So hilariously this works great in the viewport but I need to know how to set the viewport's blend mode to multiply, there must be a blend mode option tucked away somewhere? Because of the way multiply works if I set the background to transparent that's why it showed up black initially. I found out that in the GUI if you change the custom colour rather than the clear colour you don't need code for that bit.

            Any ideas? I did have a glance through the documentation but this is very unfamiliar to me so I'm experimenting now.

            You don't need to use transparent. You want it to be opaque with a white background. The viewport is just basically a render texture. If you want a specific clear color, this would be set on the camera that is tied to that viewport.

              cybereality Yeah that's what I hadn't realised about how multiply mode works, my problem now is though when I start the project my camera is solid white so that's the next problem to deal with.

              Yep, stuck for now, not sure why it's doing things correctly in the viewport but not the build.

              Weird, okay, now I know the steps better I did the classic thing of wiping and starting over so I must have clicked something somewhere that screwed up but I now don't have the white background anymore and it's all showing correctly.

              Now there's something that's bothering me quite a bit and I just think it might be the nature of the technique being used because it's projecting the circles on a flat surface. You can see there's a line still visible very thinly even though the intersection is now mostly gone, there's also a bit of an issue I'd like to tweak with the circles now looking quite flat since bearing in mind this is a fully 3D cylinder I have going. Does anyone have any ideas how I could fix this and made it work across the terrain I have? I know how I'm going to do the positioning itself of the vertices but the outline is doing this billboard effect which I don't really want.

              Oh for crying out loud I fixed the main angle and positioning problems right after I made that post, I put the quad over my main player camera and then the quad a child of the camera so it would follow no matter where the camera went and the circles are looking a lot better and almost in the position they should be within the game world and are also looking more 3D.

              The thin lines are still an issue, not sure how to deal with that and the quad behaviour still needs more tweaking, there are an awful lot of steps to getting this right but I'm almost at the end now. It's going to be really interesting for me seeing how this looks with the vertices placed properly compared to the other mesh generated version I had before.

              Very close now that I've adjusted the viewport resolution to match with the build resolution but I'll leave it here for now until I've had more of a think on how to fix things. The quad isn't exactly following the movement as it should, I think I need to make it a child of my pivot like I have done with the camera and yes it did work. Just a quick write up on that and there was a bit of colour in the quad that was still there and I had forgotten I needed to flag the quad as unshaded, now it's all looking like it should in the main view.

              Still not satisfied yet though, there are bits I need to tweak like the Z-Fighting.

              You probably want to use a torus, not a cylinder. Or find a way to give the cylinder a border thickness. Tori are round, but perhaps there is a way to model it with a flat top.

                cybereality I could experiment with some blender trickery again to see what I can do now I know more about how the effect works. One of the reason I used Blender with making this current cylinder is because it enabled me to place the outline on just the sites which is how I got the effect on the outline in the first place. Alternatively, instead of a torus, I could bevel the cylinder to give it more rounded edges.

                Any ideas on how to deal with the zfighting in the overlapping parts of the circle?

                16 days later

                I thought I'd post a video now to show where exactly I'm at with the results as a video in a lot of cases can be better than a bunch of screenshots in showing some behaviour that I'm not all that happy with. The intersection is working, I think in order to fix the pixelation problem in the centre which I swear looks like z-fighting to me I need to turn z-write off for the shader itself. I just don't know what the syntax is for that.

                The biggest problem at the moment though that I have with the viewport's behaviour is the rings are not staying put. Now, I think I know why this is and it's because they're a child of the viewport which is following the camera. As a result this is causing the influence rings to move around in an almost billboard like effect.

                I suppose the question I have is how do I keep the rings static? Once I do that I'll have them working exactly as I want and can then work on dealing with the vertices. I suppose I could override the child behaviour somehow? That might work, should I set as top level in the ready function? Or would that be too much of a hacky work around? In other games like Stellaris and Civilization VI they can keep the borders completely still without any movement so it must be possible.

                Oh for crying out loud, I think setting as top level partly did the job, however because of the way the viewport is seeing the influence rings it's still not quite how I want it yet and the viewport is rotating with the camera in a bit of an odd way. I think the main culprit of this behaviour is definitely the quad I'm using. Depending on where the camera is rotating I don't think the quad is quite covering the camera as it should as well and moving it closer screws up the positioning of the rings.