Performance boost
hi, another update this time it's an performance boost for the species mechanics as they are so laggy.
here i will explain the process that i went through to create Species.
First Algorithm:
The first algorithm was simply create a 3D texture were every slice (layer in z axis) represent an specie. This 3D texture stores the B values for every specie. What about A? a different 2D texture stores it. So it will be a local B slices in the 3D texture that every specie use and a global A that all the species use. The problem was how about the the B values that A need to use so it can update its value.
What B values it will use from all the slices? It will basically use the highest one in value. Now what?
Each frame and for every pixel it will first loop through all the slices in the 3D texture and update their values, then when it finish update the A values from the 2D texture, but now a problem arise before i even test it. I want the species to not connect to each other nor overlap but how. An idea was when updating every slice and taking the diffusion value by taking the average B cells around it in the same slice. it will check if there are cells values (in the same position but different in the slice) that is higher than its current value. Without thinking a realized how bad this idea. So for calculating diffusion values for a cell by this method it will need to loop through all the slices for every slice, or simply i will have a big o notation of O(n^2) where without this method it will be only O(n) so if there was a 1 million pixel the old method will only take 1 million of time for every pixel where the new method will 1,000,000 ^ 2 or 1 million squared which is a trillion of time!! this is really bad. In fact this is 1 million times worse!! but luckily when i tested the simulation without it to see how the species overlap i saw something weird. They are not touching each other already without the method!! how? When i though about it for a while i realized how good is the idea of global A and local B. It was even better than i though. What i thinks the reason was 2 things. First the problem earlier "how the A would choose B" that means that the other species will get ignored. The second thing is that when more than one specie exist in the same place all of them will take from A by the AB^2 (part in the equation) so fast that the A will diminish and all the B slices will too as they wont find enough A for every one of them to survive so the highest slice in value will only survive as it will stand longer. good but even without that method the whole process of looping through all the pixel in a 3D texture is still laggy.
Second Algorithm:
after another period of thinking I a came up with another method that is really efficient compared to the first algorithm. Create a 2D texture that stores 3 values in 3 channels (RGB). These 3 values are the A value for this pixel, the B value for this pixel and the specie ID for this pixel. The specie ID is an id that indicates which specie this pixel relates to. When calculating diffusion value it will check first if the pixel is from the same specie, if so it will return its value, otherwise it will return the negative of the same value. This way we wont have slices to loop through. The problem is that in unity the default texture format is "8bit unorm" that means that every color channel only stores values between 0 to 1. With these the idea of specie ID cant be done, but luckily unity provides a way to change the format of a texture so i changed it to "ARGBFloat" which extend the positive range to 2^31 different values, .which is way more than enough. after this fix there are some small problem. First because of this process the default specie ID would be 0 so we need to change it when the value change from 0 to other value so can other species than the first one can spread, and the opposite so when the B value of a pixel reach 0 the specie ID reset.
That's it. I tried to shorten the topic as much as possible. Thanks for reading :)
Get Reaction Diffusion Simulator
Reaction Diffusion Simulator
A simulation of two virtual chemicals reacting and diffusing on a 2D grid using the Gray-Scott model
Status | Released |
Author | Unknown3301 |
Genre | Simulation |
Tags | 2D, artgame, Singleplayer, Unity |
More posts
- Fullscreen Control PanelDec 17, 2021
- I ForgotSep 02, 2021
- Import ImageAug 25, 2021
- Colorful SpeciesAug 21, 2021
- Dark Theme & BloomAug 01, 2021
Leave a comment
Log in with itch.io to leave a comment.