Ecosystem Simulator in Unity DOTS:Basic plants.

posted in AntHillPlan
Published February 06, 2020
Advertisement

In this post, I will cover my last few weeks work on an Ecosystem Simulator in Unity DOTS. I have started with a minimal plant model. It gains energy and uses it to grow leaves stem height and seedPods. It does not have to use all its energy each tick. Excess energy is stored until the next tick. Maintenance is paid each tick based on the current age, stem height, leaf area and a base cost set as an environment variable. If the energy store goes negative the plant dies. if the energy put into a seed pod surpasses a set amount a seed moves a random distance based on the height and size of the seed. Then a sprout grows in the place. Each plant has a genome with genes that determines the proportion of energy that goes to each leaf, stem, seed pod, and energy storage each tick. Also maximum leaf and stem sizes. Once the maximum size is reached no more energy will be added to a plant part. Seedpods do not have a maximum but instead, have a seed size. Each tick a seed pod will release as many seeds as it has the energy to create. There is also a gene for the aging rate. Plants pay maintenance based on this each tick. They also pay a maintenance cost based on age/(age gene value). The plant's liv in a square field where the sunlight ( energy source) varies in different locations. Plants also create shade around depending on their leaf value. Plant offspring genome varies by random mutation of +/- some % at randomly at some genes.

So far the code has focused on getting this to run fairly efficiently on DOTS and cleaning the code up to do things properly on DOTS. Future work will be on adding sex, genome crossover, speciation, more optimizations, and nice output. Also, a way to explore environment parameters is needed. Currently, I have just been changing these by hand to try and get plants that live in different amounts of sunlight and do not interact too much. Interaction comes from plant shading each other and is the main computation expense. Interaction is what ecology is about but computing resources are limited. Since once plants reach max-height & max-leaf their colliders will not change again, it may be possible to track this and save shading effect between mature plants so it does not have to be recalculated, but it may be that mature plants do not shade each other very often.

I will probably use a genetic algorithm to search the space environment parameters optimizing for high frames per second, a large number of species, and a large number of individuals. This will mean finding a quantifiable definition of species for my simulation.

This 58-second clip shows a run starting from one plant. The height of the terrain matches the amount of sunlight. Only stems and leaves are rendered. The red pole marks the center of the field and is the place with the least sunlight. The clip is made with Unity Recorder so the frame rate of the video is constant 30 fps. in the Editor the actual fps varied from a few hundred to start down 10 fps at the end.

Some of the things I learned were that the ECS hybrid system for instantiating and destroy hierarchical prefabs does not work when I have 600+ prefabs instantiate and am destroying and creating 150+ prefabs each frame. My prefab had a root with one child(stem) which in turn had two children (leaf & seedpod). So I moved the stem on to the root since root did not have a mesh, and wrote code to move the leaf mesh entity up and down based the height of the stem. So in Unity, my stem and leaf are not parent-child objects. I manually handle instantiating, destroying and resizing them together. I think the Unity system is already stable for projects that only instantiate and destroy 10s of multigeneration prefabs per frame. I also found out that although you can pass parameters in class static fields to jobs it will be controlled later on because the compiler will not be able to guaranty this will work scheduling on multithreaded jobs. So I put my environmental variables in NativeArrays often single item arrays. NativeArrays are designed to support ReadWrite and ReadOnly control with the job system.

The version of the code used to make the video clip is here. The repository is here https://github.com/ryuuguu/ECS-Ecosystem and is MIT licensed. Between blog posts, the repository will often not run correctly and sometimes not compile. This a one-man exploration, not a production project. I am using GIT mainly as an offsite backup and for the occasional rollback when I have done something really silly, not for team collaboration or public releases.

Notes: Things I am considering for next features

  • Sex: w/crossover between two parent plants
  • Species: a set of genes that limit interbreeding between plants with different values
  • Flower: color possibly from Species genes.
  • Stats: Displaying and storing some stats on population
  • Species definition: Some quantifiable definition of species in the simulation
  • Parameter tuning: Using a genetic algorithm to tune environment parameters
  • Smaller steps each tick: divide all maintenance by N. Animal simulations may work better at a smaller step. Shade would only be updated each full-plant step to keep things running fast.
  • Get light from Terrain: instead of SunLight being used to make the height of the terrain. Make terrain and Calculate lighting at each location from shadows on the terrain.
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement