Repository navigation
2D Dynamic Grass
This is a 2D shader for Unity to create dynamic grass that's affected by a game's Wind system.
It uses the SpriteRenderer to render the material into a Square sprite
The material's shader functions by modifying the given sprite Texture 2D UV, to rotate it and distort it, based on the values provided by 2 generated Gradient noises, that emulate the ghusty blows of wind, going at the given Wind direction. at the given wind speed in a WorldSpace Uv relation. (It means that the position of theese noises affects each blades UV differently dependng on where are they placed in the world)
It includes a PlayerPos variable, inside the grass material, where you can assign the transform information of the Player Position so it pushes blades of grass surrounding it.
Implementation
This shader although designed for a 2D environment can be perfectly used in 3D environments although finding the best implementation for them may vary between use cases
Performance and Testing
In terms of performance you need to consider the next, It is theoretically fast, Nothing is happening at the GameObject level else than a change in scale to make each blade of grass look slightly different, in terms of height
Testing I instantiated 20 blades of grass per block, with different heights from 1.5u to 2u in a world of 5000 blocks of distance which turns out to be 100.000 blades of grass.
It got me around 450 fps using chunk culling with chunks of 16 blocks of distance and a chunk render distance of 32 blocks per side or 2 chunks on each side, wich are 1280 blades of grass rendered from the 100.000 we have in the world
If I didn't use chunk culling I would get around 120 or 160 frames depending on the case with 100.000 blades of grass rendered even if you could not see them.
(By my understanding)
The SRP batcher handles the draw calls by object texture assigned to material, and by material. If they both share the same texture and material properties, they should theoretically be rendered in one single Batch, although when testing it, I saw that it drew them in 2 Separate batches, in one batch the left ones were rendered and later the ones in the right,
This doesen't concern me in performance at all for my games, and if I later use a single texture atlas for all my textures it may resolve that batch thingy. But maybe there is something that can be done with GPU instancing. If you're interested in it., you're not gonna be able to use sprites, and will need to create mesh Quads to apply the material., and handle the GPU instances accordingly.
As a general rule it's recommended that to run a big-sized world you create it in chunks so you can control way better what's visible to the player. When I instantiate the blades of grass, they're automatically assigned to the closest chunk.
Do not attempt to use Sprite Masking use frustum culling instead if you want to extract even more performance or add more things to it
_Using sprite masking increases the number of draw calls by 1 every frame (Ruining the performance improvement of the batches)
The given sprite can be changed by any sprite design of your liking,
You can iterate over it and use it Anywhere you want.

