How to make mesh follow Path3D? (without using CSG)

Godot Version

Godot Engine 4.6.1

Question

I am trying to make a 3D lighting bolt effect. Basically, I have a 2D black and white texture of a lighting bolt for VFX render. I am gonna add ShaderMaterial to it that’s going create the effect that lighting bolt travels from up to bottom.

For my game I would want my lighting bolt to be any size. Not to be restricted by the mesh’s length the shader is applied to which.

Lighting bolt would be curvy and straight. Straight - from Point A to Point B and curvy - it’s just not straight, it’s like around cylinder or something like that.

The best thing that can used for that would be Path3D. Yet, I don’t understand how to apply a mesh to it so it would be formed along the curve from Path3D. I’ve tried used CSGBox and it kind of worked but I am afraid that it will gradually lower the performance of my game because the lighting bolts are supposed to appear spontanously as a visual effect.

I also need not just a “Plane” mesh. But kind of the mesh that has two planes crossed as 90 degree. Like + sign for rendering of the lighting bolt.

(I am not planning on using GPUParticle3D except if there is a way to make it like stretch from Point A to Point B).

Make a vertex shader that creates a billboarded strip from curve samples:

  • Sample the curve in regular steps. You’ll need position, tangent. Send those samples to the vertex shader via an uniform array or a small texture.

  • For drawing, send any kind of triangle strip topology to the shader. You’ll need UV distributed as if it’s a plane, so Godot’s plane mesh with adequately set subdivisions should suffice.

  • Depending on vertex V, sample/interpolate position and tangent. Project the tangent onto screen plane, rotate it 90 degrees and displace the vertex along its positive or negative direction, depending if vertex U is 0 or 1.

  • Animate the bolt texture along the strip.

1 Like

Thank you so much for helping! It worked perfectly.

EDIT: To be honest, I’ve used chatbot to implement it. I have never had any experience in vertex shaders and I don’t have that much math knowledge to be able to describe at which position specific vertex would be regarding specific point on a curve dynamically. I literally can’t come up with that.

Yet, I believe I should not have used AI. And you reader should NOT use AI.

How does one truly learn if someone just gives straight solution to him? In order to learn you must practice, experience it and find your way to the solution.

Don’t use AI guys.

In any way there is a bit of explanation below on how I implemented it if you’re total newbie like me. It’s also an old version of this post.


Yet… I am not sure on how to make the lighting bolt go spiral along the cylinder.

I’ve only made it go straight between two points. When it comes to the third point it goes after the previous point to go straight to that third point. Which is good but now I geniunely don’t know how to make it spiral.

To be honest, I have never worked with vertex shaders before. But anyways.

I’ve made @tool script connected to Path3D with variable “activate” that when I enable gets set to false. When “activate” true specific function is called that processes and passes “tangent” and “points” data to the ShaderMaterial as Vector4 arrays.
Tangent data is just difference between two points from everyone point in points data. The points data is just get_baked_points() from Curve3D.

(EDIT: I guess above it was just basic pre-calculus math. Nothing more. There literally was no purpose in use of AI).

ShaderMaterial is applied to a PlaneMesh in a MeshInstance3D which has subdivide_width and subidivde_depth set. Subdivide width is 1 and subidivde depth is the amount of points on a curve minus one.

(EDIT: The subdivisions are the only things that allow to “curve” the plane mesh in any way you want. We then use vertex shader on them to make it curve in a specific way along the Path3D.

[shader code deleted]

(EDIT: If you’re really want to learn something just try to make it by yourself. I just realised it’s not that hard. Just google specific things like how to make billboard vertex shader, how to displace a vertex, rotation it and etc.)

Did you just prompt some bot with my description and it blurted that shader out?

To be really honest… yes. But I kind of tried to understand it. Of course it was a bad idea.

I guess, I will try to write it by myself then. Idk.

Yeah it got that really wrong. I will try to come up with something on my own.

No it didn’t. It got it pretty much right. Now you just need to ask it how to sample a curve and feed the samples to the shader. Should I write the prompt for that as well? :smiley:

Alright. Alright. I see what you mean here.

I never had experience with vertex shaders and I am not good as much in math to make it dynamic so it would use which vertex represents what position on a subdivided mesh from an array of some points that’s just a curve. I am literally not good at that…

Before this bots answer I geniunely didn’t even know about existence of subdivide_depth on a meshplane. I’ve never used it.

To be honest, I have edited my post after your first answer.

You were right. You also were right in not sending straight answer to me. I really appreciate your help and explaining a way in how one would make this.

I am really bad at vertex shaders. Maybe later I will be good at them… but perhaps not now (probably because I needed it just for one thing in my game. Perhaps, when I would realy on vertex shaders too much I will actually learn the way vertex shaders works but idk; I am only good at fragment shaders though).

Thank you.

Have a good day.