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. Dismiss Notice

[RELEASED] Runtime Tree Colliders

Discussion in 'Assets and Asset Store' started by robin-theilade, Sep 13, 2014.

  1. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    Note, for this asset to work you also need RobinTheilade.UnityFramework

    Runtime Tree Colliders enables you to have colliders on trees in scenes where there are too many trees for PhysX to handle.

    Simply add the main component to your terrain and make sure you have capsule colliders on your terrain trees and you are up and running.

    Features:
    • Low number of colliders for trees
    • Quadtree for great performance
    • Documented C# source code (available on github)
    • Free
    • Only changes colliders when player have moved
    • Easy to set up
    • Works with Unity and Unity Pro versions 4.5 and 4.6
    • Tested with 70.000 trees in a 2000x2000 area
    • Example scene included
    • Precompiled code with namespaces
    • Tree collision information

    Limitations
    • The colliders does not move with the wind



    Available on the Unity Asset Store
    Sourcecode available on GitHub
     
    Last edited: Dec 13, 2014
  2. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Odd. I just see a black video.
     
  3. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @jc_lvngstn

    Don't worry, there is nothing wrong with your eyes. Joke aside, thanks for telling me.

    I accepted a suggestion from YouTube to let it change the lighting in the video and it didn't go so well. YouTube is currently changing it back. It should be available in a few minutes.

    Best regards,
    Robin
     
  4. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    I reuploaded the video and now it works.
     
  5. Wavinator

    Wavinator

    Joined:
    Dec 24, 2013
    Posts:
    79
    This is awesome. I don't have the beta but I look forward to checking this out. Thanks for sharing it!

    Any idea if something like this could be used dynamically, say with multiple trees assigned to multiple terrains that are generated dynamically just before a player moves into a terrain? I'm working with a grid of terrains that are created procedurally as the player moves through the game world, but I'm not sure if recreating the quad tree or repopulating it with new data would be prohibitively expensive.
     
  6. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @Wavinator
    That should be possible. Just add the component to each of the terrains, it will automatically index all the trees on the terrain. If it is the trees that you are adding dynamically then you can use the AddTree/RemoveTree methods on the component, bear in mind that dynamically adding and removing trees on the terrain itself has a performance hit you might want to investigate. I found that on my machine each terrain should have no more than 1000 trees if the lag should be tolerable.

    The asset will most likely work on earlier versions of Unity but I have only tested it on 4.6B17.
     
  7. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    I'm not sure how your code works, but something I was wondering: Adding and removing trees IS Very slow. I think the main issue it is has to recreate the collision mesh for the entire terrain.
    If this is the case, maybe you could create a clone of the original tree prefabs, removing the colliders but storing the collider details.
    That might make adding/removing trees not a problem at all, but I don't know this for a fact and there are pretty much no details in the documentation...for like...ever.
     
  8. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @jc_lvngstn
    It was a good idea and I just tested it. Sadly it did not improve the performance.

    Just for the fun of it I also tested calling Terrain.Flush and TerrainData.RecalculateTreePositions() (through reflection) instead of reassigning the tree instance array, but still with no improvement.
     
  9. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    So adding and removing trees, with no colliders used at all has the same performance? Darn. It's been so long since I did much with Unity's built in trees, I've forgotten some details. Sorry!
     
  10. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @jc_lvngstn
    Nothing notable at least. I should probably have meantioned that in order to use this component you must uncheck "Create Tree Colliders" on the "Terrain Collider" component so it actually makes sense that it didn't matter. But chances were that it could have played a role so it was a good suggestion you had.
     
  11. Lupin500

    Lupin500

    Joined:
    Mar 6, 2012
    Posts:
    25
    Very interesting! Definitely something that could solve my current issues. Any idea on when it will be available?
     
  12. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @Lupin500
    I'm guessing it will be available very soon. I committed it to Unity about a week ago.
     
  13. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
  14. Lupin500

    Lupin500

    Joined:
    Mar 6, 2012
    Posts:
    25
    Hi Robin, thanks for letting me know. I just added it to my terrain but as soon I enable it the performance goes to 2 FPS making it very difficult to test. I even tried to lower the settings for max colliders and diagonal length but it didn't make any difference.

    My terrain has a lot of trees, could it be that there is a limit on the amount of these before your script can't handle them anymore?
     
  15. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    There is an upper limit but I would guess it would be based on memory only. You can check if you're out of memory.

    You have remembered to disable the terrain's own tree collider system by unchecking the "Create Tree Colliders" checkbox right?

    I'm on my way to work now but if you are able to make me an example scene of your problem I can debug when I get back home.

    You can also try disabling the tree rendering and visualize how the colliders are moved around from a top-down view and see if there are too many colliders being moved around.

    /Robin
     
  16. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @Lupin500 alternatively you can just give me the specs of your scene and I will see if I can reproduce your issue from that.

    How many terrain tiles are there?
    How many tree instances does each tile have?
    How many tree prototypes are you using?
    Approx how many other colliders are there (buildings, living things etc)?
    What are the size of the terrain tiles?

    /Robin
     
  17. Lupin500

    Lupin500

    Joined:
    Mar 6, 2012
    Posts:
    25
    Hi Robin, yes the Create Tree Colliders checkbox is unchecked. My scene contains only one massive terrain with 3 different tree prototypes and no other colliders. But for this specific project I am using a ridiculous amount of trees instances (600K+) which required a lot of optimizations in order to be able to render at 60FPS.

    So I'm not surprised that things are slow because this is definitely a special case (I suspect the foreach loop in your script is the main problem). I ended up solving my tree collisions problem in a very different way but I look forward to test your system when I work on a more "conventional" project.
     
  18. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @Lupin500 that's a lot of trees :)

    I modified the example scene to have 1.000.000 tree instances with only one tree prototype and changed the size to 4000x4000. This did not produce "bad" frame rate. It still rendered at about 60FPS on my machine.

    Is it only when the game starts that you experience the lag? I'm asking because at start I index all the trees and that might happen to be slow.

    I'm happy to hear that you found another solution. What did you end up doing?

    /Robin
     
  19. evilmaster

    evilmaster

    Joined:
    May 10, 2014
    Posts:
    4
    I had to modify the script a little bit, but I'm not sure if I did it correctly.. I have bushes in my "mass place trees" window and they don't have colliders. So it ended up with an error trying to access a collider that doesn't exist.

    Code (CSharp):
    1.  
    2. if (prototypeCollider != null) {
    3.     currentCollider.transform.position = new Vector3(
    4.         instance.position.x * this.data.size.x + this.terrain.transform.position.x,
    5.         instance.position.y * this.data.size.y + this.terrain.transform.position.y,
    6.         instance.position.z * this.data.size.z + this.terrain.transform.position.z
    7.         );
    8.     currentCollider.transform.localScale = Vector3.one * instance.widthScale;
    9.  
    10.     currentCollider.radius = prototypeCollider.radius;
    11.     currentCollider.height = prototypeCollider.height;
    12.     currentCollider.center = prototypeCollider.center;
    13.     currentCollider.direction = prototypeCollider.direction;
    14.     currentCollider.gameObject.SetActive(true);
    15. }
    16.  
    Line 175 in Quadtree.cs (if (!this.boundaries.Overlaps(range, allowInverse: false))) throws me an error when I hit F8 in MonoDevelop (Build All), it's expecting a closing parenthesis after allowInverse. Don't know why this is and how to fix that.

    Script seems to work though! Thanks :) Haven't gotten to the part where I actually control my character yet, but I need lots of trees and the colliders show up around my camera, so that's good.
     
  20. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @evilmaster you can let the script ignore the bushes they way you modified the script.

    Regarding the compiler error, you must remove the "allowInverse: " before the false. MonoDevelop does not approve of this notation, yet. I should remove it as well in the next build.
     
    evilmaster likes this.
  21. evilmaster

    evilmaster

    Joined:
    May 10, 2014
    Posts:
    4
    Excellent! Thanks for the quick reply :)
     
  22. treshold

    treshold

    Joined:
    Nov 9, 2013
    Posts:
    225
    Is there some specific reason why it needs Unity 4.6 beta?
     
  23. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    I've already answered treshold in a private conversation, but for anyone else who are looking to use the package or my Mass Tree Placement asset in earlier versions than 4.6, you are welcome to download them and try them out for yourself.

    I have not tested them on anything else than 4.6 but I don't see why they wouldn't work on 4.5.

    Runtime Tree Colliders (3.2MB)
    https://dl.dropboxusercontent.com/u/9933904/Runtime Tree Colliders.unitypackage

    Mass Tree Placement (29.2MB)
    https://dl.dropboxusercontent.com/u/9933904/Mass Tree Placement.unitypackage

    Enjoy!

    Best regards,
    Robin
     
  24. treshold

    treshold

    Joined:
    Nov 9, 2013
    Posts:
    225
    Yes, It works as expected on Unity 4.5.2f1 :) cheers for great script!
     
  25. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @treshold, thank you for testing and reporting back. Happy it worked for you.
     
  26. broesby

    broesby

    Joined:
    Oct 14, 2012
    Posts:
    116
    Hi Robin,..

    I didn't try your script yet but hope to find the time soon... ;-)

    I was wondering... I know the colliders are just a single capsule representing each trunk...

    But would it be possible in some way to add just 2-4 extra capsule colliders for the main branches in case I thought I needed that??... I know it's listed in the limitations list... but do you would it be possible somehow??
     
  27. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    It might not be impossible but I expect that you will run into a few difficulties like how do you rotate the collider. I did try to use the mesh collider at first to get perfect collision but it didn't work. Maybe you would be able to get the source of the pixel from the depth buffer and get pixel perfect mouse collision at a very cheap price. I don't know if branch collision is for chopping or climbing on so it might not be what you want. I'm busy this week but maybe next week I can look into support for multiple colliders.

    /Robin
     
  28. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @broesby sorry to keep you waiting. I've modified the code so it is now possible to have multiple capsule colliders per tree. I'm thinking about submitting the change to the asset store tomorrow, but if you need it more urgently it is available on github (https://github.com/robintheilade/UnityRuntimeTreeCollision) now.

    /Robin
     
    hopeful likes this.
  29. broesby

    broesby

    Joined:
    Oct 14, 2012
    Posts:
    116
    Hi Robin,.. Absolutely fantastic. Now I really feel bad as I didnt even have time to try the original script yet... My "real" job has kept me from Unity last weeks. I look forward to using it.

    I suppose I have to make a tree prefab with a trunk capsule collider as usual and then just add a few perpedicular short capsule colliders for main branches within the same prefab... Correct?

    By the way. Did you test it with multiple terrains. Should I add the main component to each terrain...?

    Thx a lot. Jesper

    PS: For pokker da... Du er jo fra Danmark :) Dit navn ser ikke synderlig dansk ud?
     
    Last edited: Dec 3, 2014
  30. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @broesby, don't worry about it. A capsule collider for the trunk is what I image you would need at the very least. In the end it is really up to you where and how you use the colliders. As long as there is a collider on the tree game object itself and all the other colliders are on direct child game objects to the tree. Then you can move, rotate and scale the child game objects as needed.

    Each terrain will need the component.

    PS. Jeps, men alligevel er jeg dansk. Meget underlig verden vi lever i ;)
     
  31. Creationbarrel

    Creationbarrel

    Joined:
    Mar 3, 2015
    Posts:
    6
    why is it free? it seems to good to be true. and I've been backstab before by stuff like this
     
  32. Creationbarrel

    Creationbarrel

    Joined:
    Mar 3, 2015
    Posts:
    6
    just curious please answer at your convenience
     
  33. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    It is not really a short answer. But by making it open source I can hopefully limit the amount of time doing support and since the asset itself will never make me rich I cannot hire someone to do the support.
    An obvious question to ask now would be: then why add it to the asset store? I wanted to have my toolbox centralized somewhere and the asset store felt like the right place.

    I can assure you that backstabbing is not on my agenda. If you're still in doubt, go copy the source code from github, look it through to confirm that there is no malicious code and use it.

    Enjoy and feel free to share a few screenshots where you use the asset.
     
    NeatWolf and evilmaster like this.
  34. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
    Is there any way to make the colliders a trigger based on the original collider?
     
  35. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
    Oh figured it out just needed to add a line here.

    Code (csharp):
    1.  
    2. private void CopyCapsuleColliderProperties(CapsuleCollider from, CapsuleCollider to)
    3. {
    4. to.radius = from.radius;
    5. to.height = from.height;
    6. to.center = from.center;
    7. to.direction = from.direction;
    8. to.material = from.material;
    9. to.isTrigger = from.isTrigger;
    10. }
    11.  
     
  36. wightwhale

    wightwhale

    Joined:
    Jul 28, 2011
    Posts:
    397
    Also it would be cool if this worked with not just capsule colliders but with any kind.
     
  37. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @wightwhale, you could try search/replace CapsuleCollider with the collider you want to use. MeshCollider won't work though.

    Just make sure to update CopyCapsuleColliderProperties method so it copies the properties of the collider type you're using.

    Having a mix of collider types should also be possible but requires more code and will be somewhat slower because of the check of collider type.

    /Robin
     
  38. SSL7

    SSL7

    Joined:
    Mar 23, 2016
    Posts:
    336
    It doesn't seem to work on unity 5.6, any updated version would be very welcomed.
     
  39. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @dlevel, I'll have a look at it. Could you supply me with details of the issue you're experiencing?
     
  40. SSL7

    SSL7

    Joined:
    Mar 23, 2016
    Posts:
    336
    @robin-theilade Wow! didnt thought I ll get a response :))) I m trying with the demo at the moment, and I have this:

    https://postimg.org/image/c2m9021s5/

    and no colliders spawning, and these warnings:

    OnLevelWasLoaded was found on MoBlur
    This message has been deprecated and will be removed in a later version of Unity.
    Add a delegate to SceneManager.sceneLoaded instead to get notifications after scene loading has completed

    Error reading types from assembly RobinTheilade.RuntimeTreeCollision, Version=2015.3.18.34900, Culture=neutral, PublicKeyToken=null
    UnityEngine.Debug:LogWarning(Object)
    ScriptInspector.FGParser:.cctor() (at Assets/Plugins/Editor/ScriptInspector3/Scripts/FGParser.cs:404)
    UnityEditor.EditorAssemblies:processInitializeOnLoadAttributes()

    GameObject (named 'Terrain') references runtime script in scene file. Fixing!

    The script behaviour 'RobinTheilade.RuntimeTreeCollision.RuntimeTreeColliders' could not be instantiated!
     
  41. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @dlevel, thank you. I'll get back to you.
     
    SSL7 likes this.
  42. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
  43. SSL7

    SSL7

    Joined:
    Mar 23, 2016
    Posts:
    336
    wow! didnt see that requirement, seems to work, thank you!!
     
  44. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
  45. SSL7

    SSL7

    Joined:
    Mar 23, 2016
    Posts:
    336
    I know I m asking a lot but, I m using also a prefab with a trigger collider for each tree, that it spawns where any tree is spawned, that it has a harvesting script in it. Is there any way to use your script to also enables/disables this collider?
     
  46. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @dlevel, the answer is most likely yes, however I don't understand your setup. Can you provide a few screenshots with notes?
     
  47. SSL7

    SSL7

    Joined:
    Mar 23, 2016
    Posts:
    336
    Sure, I use MapMagic, and the way I use my Harvest Prefabs is that I spawn a prefab which is mostly a trigger collider with the harvesting event, wherever there is a tree. This goes as well with the rocks etc, I spawn a prefab with the trigger collider and the event I need wherever a tree, rock or plant is spawned. So I would like to have these colliders work like your script works with the tree colliders.

    here is the prefab:

    https://postimg.org/image/p5a41i4jp/

    and the same goes for HarvestTree, HarvestMetal etc.
     
  48. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    @dlevel, so if I understand correct, for each tree you have you have a separate GameObject containing a harvest script representing the same tree. The same GameObject also has a Capsule Collider. It is this Capsule Collider you want "Runtime Tree Colliders" to enable/disable depending on tree's collider situation?

    If so, the first solutions that comes to mind is simply to add the harvest script to the tree prefab? But then again, I don't know how MapMagic works. If it requires a separate object then this solution wont work.

    The solution you are probably looking for is modifying the "Runtime Tree Colliders" to work with your objects. That is out of scope for my asset but you can modify the code to make it work to your requirements. The source code is available on Github.
     
  49. SSL7

    SSL7

    Joined:
    Mar 23, 2016
    Posts:
    336