Procedural Technology for Stylized and Optimized 2D Foliage

0
149
Procedural Technology for Stylized and Optimized 2D Foliage

[ad_1]


Observe: This text was initially revealed on LinkedIn. In case you get pleasure from my article, please click on by way of and think about sharing on social media and connecting with me!
Video games want detailed background artwork. Whereas usually not central to the gameplay, high quality and different background artwork is required for storytelling, immersion, visible attraction, and basic polish. An absence of selection in background bushes is noticeable in a sport. Creating a wide range of bushes by hand takes lots of effort, and every variation wants bespoke work from an Artist.
Procedural Technology can be utilized for this downside by decreasing the necessity for bespoke content material creation. As a developer, you usually gained’t have full management over your Procedural era work. This makes it significantly effectively suited to such background content material – content material that’s necessary however gained’t break the sport if it isn’t fully excellent.
A programmatic strategy to foliage additionally permits for a particular look and magnificence to be created by way of parameters, after which additional different barely utilizing these parameters.LSystems For Foliage Technology
LSystems have been used extensively for foliage era. The algorithm is of course suited to producing branching bushes and vegetation. It permits for a level of randomization and allows you to management the aesthetics of what you generate by way of using parameters and guidelines.
Fundamental LSystems are straightforward. Branching, size, angle, and iteration depend covers this.However What Can You Do To Generate Extra Aesthetic Paintings?
I liked the aesthetic of Kate Compton’s work and modeled my very own after it, with the added standards that no matter I create have to be appropriate for real-time simulation in Unity, so it may very well be used as a instrument for artwork era for 2D Unity video games. Kate has shared her foliage PCG work in addition to guides for these beginning out with PCG – http://www.galaxykate.com/weblog/generator.html
I added the next parameters:
Iterations
Department angles
Department width
Max department size
Max leaf size
Base color
Color variation
Saturation
Leaf shapes
Leaf facet ratio
Petal depend
Petal measurement
Petal form
Petal base color
Petal saturation
Department spikiness
Department taper
Including Visible Variance
What if you need extra selection for every generated tree, so there aren’t simply duplicates throughout?
Generate a forest by “altering it slightly”. Take the bottom mannequin, saved in a construction (DNA), after which change related parameters by just a bit to generated an identical tree of the identical species. Repeat x10 or x100 and also you’ve received a forest that’s uniform however not filled with the identical cloned tree.
Past this, Kate additionally shares some basic ideas for extra visible selection whenever you’re utilizing PCG methods.
Listed below are some concepts she suggests for enhancing distribution, so every little thing you generate appears pure and has some variance.
1. Barnacling – Encompass massive objects with a couple of smaller or possibly even lots of tiny ones to offer it a extra pure look.
2. Footing – Ensure that every little thing you generate appears prefer it suits in with the world, mainly by dealing with the place objects would work together (On this case the tree department and the bottom would overlap within the type of roots)
3. Greebling – Initially utilized by these working at Industrial Gentle & Magic for the particular results of Star Wars. Add small particulars to massive surfaces to make them seem extra advanced and attention-grabbing.

After including smaller, barely modified bushes subsequent to every one:
So as to add to this, you may as well simulate progress – Improve department size, leaf depend, flower depend, and eventually iteration depend over time.Animation
I went with the plain strategy first – simply manipulating mesh place and rotation utilizing a sine wave. I apply this with a wind energy variable relative to the space from the bottom of the tree – so mainly, the foundation doesn’t transfer in any respect, and the treetops will transfer essentially the most. It appears adequate for a stylized 2D sport.
I later used Shader Graph to maneuver all of the animation to the GPU – manipulating vertex place utilizing shader code works simply as effectively and leads to significantly better efficiency.Knowledge Constructions – Plant DNA
Technology is randomized however deterministic. In case you use the identical parameters, you’ll get the identical tree once more. In a approach, the construction of parameters is a tree’s DNA.
Generate a tree you want? Reserve it’s DNA. Load it later to re-use in your sport.
Taking this additional, you may cross-breed and evolve species utilizing Genetic Algorithms. The tree DNA is already outlined as a struct of floats.
Merely put, use a genetic algorithm to mix two halves of DNA collectively after which mutate them to create a brand new species. Then set it up so a designer can choose two bushes they like and mix them collectively.
You’ll be able to lengthen this idea into gameplay – add some participant generated content material by permitting gamers to generate their very own vegetation, save them, evolve them, re-plant them, and so forth.
How Do You Optimize Mesh Technology For Producing And Simulating Foliage In Actual-Time In Unity
Technology is gradual and does take a while. So long as the code isn’t unnecessarily advanced, you may get 300 bushes in a few minute and possibly add a loading display if it is advisable do that throughout gameplay.
However the generated bushes need to be optimized for efficiency as effectively. Background artwork is strictly that- background. You’ll be able to’t have an excessive amount of CPU/GPU work and draw calls being taken up by background artwork – it’s wanted for foreground artwork, animations, shaders, lighting, AI, gameplay, and so forth.
The branches are particular person sprites and there aren’t that a lot of them.
The leaves are generated meshes, which have 4 vertices every. That is needed for the stylized look and selection. That is the place many of the rendering time is spent, in order that’s the place optimization must be targeted.
Right here’s how I dealt with optimization:Shared supplies
Every leaf has the identical set of parameters and hues are decided inside the shader primarily based on the pixel’s distance to the bottom. Which means that a single occasion of a cloth will be shared by every leaf in a tree.Optimized shaders
For starters, use Unity’s URP and related unlit URP shaders.Shader-based wind animation
The GPU tends to be underused in comparison with the CPU, so it’s virtually all the time helpful to maneuver advanced computations to the GPU. On this case, it’s higher to simulate wind by way of making use of a sine perform to the mesh vertices (I really like Shader Graph for this). The choice could be altering object positions and rotations utilizing C# code, which can virtually all the time carry out worse.Mix meshes to cut back draw calls additional
I found a pair years in the past throughout a distinct mission (VR Instructional Video games for the oil and gasoline trade) that Unity’s Static and Dynamic batching are inefficient. I assumed that the variety of meshes in a scene wouldn’t matter, simply the triangle and vertex depend. It seems, you may often enhance efficiency by fairly a bit by combining meshes wherever attainable, relatively than solely counting on batching.
I really like the Easy Mesh Mix asset from the Unity Asset Retailer for this.
Earlier than combining meshes:
After combining meshes:
Draw Calls decreased to round 33%, Batches decreased to 0.5% of the preliminary depend.
CPU and GPU time virtually halved, which may loosely be translated to imply that efficiency and body charge shall be 2x higher.
Mixed, unanimated
This nonetheless takes up an honest quantity of rendering time due to the variety of meshes used. Framerates are nonetheless okay, however may very well be an issue in case you’re focusing on low-end {hardware} or want different dearer computation for graphics, AI, or gameplay.Billboards For Efficiency – Possibly Not In This Case?
If you may get away with out animations, you don’t want particular person meshes anymore. I wished to check out utilizing Render Texture to supply billboards relatively than rendering bushes with tons of of leaf meshes.
Right here’s what my script does:
Isolate bushes to their very own layer and ensure the render to texture digital camera solely sees that layer (Culling masks).Middle the digital camera on the tree.Render what the digital camera sees to a texture.Apply this texture to a sprite.Disable the tree and change it with a single sprite.
That is gradual although. Every tree must be rendered individually, one a body. Takes about 10 minutes for a 100 bushes. The consequence reduces draw calls drastically and brings down body time.
This didn’t work as anticipated for me – draw calls, triangles, and vertices are decreased, however the precise body rendering time didn’t go down. So in the long run, combining meshes appeared to offer me higher efficiency.Good Instrument Design
For this mission, my focus was on having the ability to iterate and prototype shortly, and never a lot on the standard or usability of the top product. Nonetheless, I gave slightly little bit of thought to instrument design:
A single struct for plant DNA – I am reiterating, however this makes for good instrument design in addition to all parameters that have an effect on era are saved and accessible collectively.Every parameter has fastened constraints each within the code and within the editor UI – Due to the distinctive traits every parameter is chargeable for, setting these constraints makes it simpler to iterate additional in addition to use the instrument. This ensures every little thing you’re producing nonetheless resembles the shapes you’re going for.Unity UI sliders for every parameter, constrained in the identical approach.UI for different features – I can choose vegetation by clicking on them, after which both change them, evolve them with the genetic algorithm button, or save and cargo them.

A really apparent subsequent step for good instrument design could be to make use of Unity Editor Scripting, maybe with the UI Toolkit, to make this UI accessible by way of the Unity editor relatively than by way of in-game UI.Ultimate Ideas
For me, this mission was an honest and quick start line for entering into extra PCG content material. If you wish to discover out extra about PCG, I’d positively suggest trying out Kate’s weblog right here – http://www.galaxykate.com/weblog/generator.html
Efficiency optimization was key for ensuring what I’m producing can truly be utilized in an actual sport. I is perhaps trying into 3D foliage era subsequent, so optimization and simply studying about essentially the most environment friendly methods to render 3D artwork in Unity shall be important for that.
After this, I labored on a instrument for procedural narrative era. Maintain a watch out for an article about that one quickly!

[ad_2]