Optimizing perlin wind for custom segmented grass

Intro

Perlin is slow and laggy and bad for performance, especially when you call it 311,040 times per second. Those are oh nose numbers.

Obviously this (18,662,400 calls per minute) is going to nuke any computer that tries to run my grass (coming soon). We can fix this using a few methods which I will go over in this post.

For context, the scene has 5476 objects that are updated to visualize the wind, The grass system should have even less objects in more normal use cases. The pinkish red ball represents the camera

baseline with no optimizations:

20FPS highs are actually horrible and will ruin the game for anyone brave enough to play.

Dropping the refresh rate

Since the grass animation intensity will be interpolated from the noise but not directly tied to it, we can simulate having the grass get noise less often.

the system fetches the current noise value via a module, the updates are being done through a seperate script. this means behavior isnt 1:1 but should be close enough

From 60hz to 16hz (ingame would probably be closer to 4hz or even 2hz)

This is nearly a 1.5x framerate improvement however 35ish fps is still not good enough for my computers specs.

Data Caching

We can cache data and reuse it which should dramatically improve our performance.
This will pair up nicely with our next feature.

Level of detail + Noise Quality

Up until now the noise has been running with 16 layers/octaves, This is pretty bad for performance because we are generating tons of noise which for almost every usecase you cant even see/notice.

Alongside dropping the amount of octaves we can also add LOD which essentially filters the cache based on how similar and recent the cached data is.

We hit a pretty consistent 60FPS with more objects than this system will probably ever see.

A takeaway/summary

We can do a ton of things to optimize our games, Take the time to optimize something before discarding it, Being clever can mean the difference between a scrapped laggy system and something to marvel at.

Quick note : Im not sure if I made this clear enough, brightness represents the winds intensity.

2 Likes

Nice work! How largely can this be expanded?

You can expand it virtually to the floating point because of the lod efficiency and how the noise works, however it depends moreso on the amount of objects. The ideal usecase is for things like clumps grass and cloth effects scattered throughout the map.

1 Like