[Unity 2020.2.1f1 | Entities Version 0.17.0-preview.42]
Problem: My entities instantiate as expected only when I enter play mode for the first time after opening my project, or after I make and save changes to the code. Each subsequent time I enter play mode without doing one of those two things, some entities generate in unexpected ways, but a majority are correct.
For reference, the scene operates like this:
Start → generate entities and store in native array for later cloning & positioning → (on ui button press) generate terrain data using mathf.perlinnoise for entity positions (done on main thread) → c# job clones entities and sets positions using terrain data → wait a frame (updates broadphase) → c# job ray casts against entities and stores results in native array → c# job (dependent on prev job) then runs comparisons on ray casts and spawns additional entities. The comparisons sometimes trigger false positives which creates the out of place entities on subsequent play mode sessions (but never the first as mentioned above).
My initial thoughts were that it was a scheduling issue (false positives/weird behavior & jobs is a red flag) or floating point imprecision in the comparisons, but it always works right once then never again after.
The last job (the one with the actual issue) also runs mathf.perlinnoise called from within the job for some comparisons between terrain chunk borders (think minecraft style chunks). I’ve done a little research and using mathf in a job doesn’t seem like its bad (just slower maybe) but i noticed that unity.mathematics also has a bunch of different noise implementations as well which may be worth switching to down the line. I dont think mathf.perlinnoise is the actual issue tho because it is marked as static and thread safe, but then again this issue only cropped up when i started using it so…
Possible Solution: IF calling mathf.perlinnoise from the job is in fact the issue i could cache the borders of neighboring terrain chunks in each terrain chunk so i could eliminate the call to perlin noise in the job at the cost of some memory. (i would like to avoid this since it will take a teeny bit of refactoring but am more than willing if its necessary)
Questions:
-
Does the unity editor have a known issue caching information between play sessions in some weird way that would cause an issue with c#jobs/native collections like this? (i feel like i remember reading a forum post about something like this a few years ago but i couldnt come up with anything)
-
Is using mathf.perlinnoise in a c# job is actually bad practice? If yes, would it be safe to use the unity.mathematics implementation or should i try something like my proposed solution instead?
I might try and implement my caching solution when i have time, and will share the results if i do. Regardless if y’all need more info/have any input it would be super appreciated ^-^