, 9 tweets, 4 min read Read on Twitter
A little thread on generating and rendering a procedural trajectory mesh! #madewithunity #unity3d
Trajectories under constant linear acceleration, gravity in our case, has a simple equation

p = p₀ + v₀t + ½gt²

p = Position
p₀ = Initial position
v₀ = Initial velocity
t = Time
g = Gravity
To get the velocity v at any given point, we derive the formula to get

v = v₀ + gt

Quite usefully, this vector is tangent to the curve! When normalized, it represents the direction of motion at any given time. In our 2D case, we can also get the normal by rotating it 90°
So here's my "concept art" for what I wanted the trajectory mesh structure to look like! One long quadstrip through the center, with perpendicular quads along the whole thing at equal time intervals. Both the tangent and the normal are used in positioning each of these vertices!
Now, procedural meshes are often defined on the CPU, but calculating all of this on the CPU is a bit of waste. After my initial test, it hit me that all of this math, could be done in the vertex shader instead, that way we don't have to update the mesh data every frame anymore 🎉
So instead of calculating each vert on the CPU and writing that to the mesh data every frame, we generate the mesh *once*! Part of this involves making sure the GPU has all data on what vertex is where in its local quad. For example, here's the code for the perpendicular quads
This all means we can now provide the constants (p₀, v₀ and g) through a material property block, while the time value and vertex local positioning info is in the vertex UV data, so the GPU/shader can do all the positioning work ✨
A quirk with GPU vertex positioning is that frustum culling will use the bounds of the mesh data rather than that of the rendered positions of each vertex. You *could* do the CPU math for its real bounds, or just, you know

make it cover the observable universe and call it a day
This was created earlier on stream - I'm also going live right now, come hang out! I always try to keep things educational, so feel free to ask anything at any time 💖

twitch.tv/acegikmo
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Freya Holmér
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!