Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

treeInstances, manipulating the array

Discussion in 'Scripting' started by jomsa, Dec 11, 2017.

  1. jomsa

    jomsa

    Joined:
    Nov 10, 2013
    Posts:
    23
    So.. here is whats going on.

    I am generating static class containing world data (basicly heightmap and index for what splatmap index to use at any given x,y).. procedurally generated one ofcourse.

    This same class also has 2d array where a tree should exist or not (and what type, size etc)..

    So i can get these trees from this class to the treeinstances no prob. But how the heck i am supposed to remove a tree from the instances when i remove it from the class? Oh and this should be done pretty much at runtime.

    My first attempt was to assing instance number of the generated tree to the 2d array, but.. then i realized if i would to erase tree number 5467 out of 10035 .. the rest would collapse, and the instance numbering would be e'ffed up.

    Another attempt was to iterate thru the instances list and do x,y coord comparison, but this turned out to be a majorly stupid idea (or maybe i did it wrong). I grabbed a list of the instances, found the right tree, yanked it out, and returned the list. But this was slow as heck when dealing +10000 trees.

    I read about couple other ideas, one suggested instansing a collider with the tree with attached instance ID. But this just made me wonder, would unity suffer through the +10000 colliders in single scene?

    As far as i can tell, i found no way to get the instance id of a particular tree and yank it out directly...

    So i am at a loss with this. Any suggestions, ideas?
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    I think you have to overwrite the entire array.
    Fetch the instances into a list. Remove a tree from the list. Overwrite the tree instances array with your modified list.
    https://forum.unity.com/threads/finally-removing-trees-and-the-colliders.110354/

    10000? Maybe ok, not sure. 60 000? Enable and disable them based on range.

    If you don't want to measure the trees individually at a radius, you can simply check the terrain tile distance, if in range, then fire up a coroutine that loops all its trees and enables x of its tree colliders every frame. If the tile is not in range, then disable them in the same way. Don't enable too many colliders in a single frame, else they will freeze your game for a millisecond (last I checked which was years ago). If you want to enable tree colliders at a radius, then you're checking tree collider distance of the terrain tiles within range.

    In any of these scenarios, you won't want to work with 10k trees at a time. You'll want to split your terrain tiles so that you are working with a lot less Trees at a time.
     
    Last edited: Dec 11, 2017
  3. jomsa

    jomsa

    Joined:
    Nov 10, 2013
    Posts:
    23
    Tried that..
    But turns out the solution was very close to this.. I just realized that why bother keeping the old instances.. why not just remake the whole list. Since i know where the trees are supposed to be by the "world data", i just made new instances array, populated it with the trees, excluding the removed one(s).. and tossed it back to terrain data. Lighning fast. Only snag was that i had to do the same to heights[,]... But it seems very fast either way.