Search Unity

Procedural trees

Discussion in 'Made With Unity' started by cblarsen, Mar 14, 2007.

  1. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    Just wanted to show you what I ported to Unity in a day.

    Two "trees"

    I wrote the original code a couple of years ago in C++
    and back then it took me at least a week to write and debug. It is hard for an old assembler-coder to admit this, but this GUI-scripting thingamabob is really cool.

    I can think of lots of things to improve about the trees, but I just wanted to show you anyway.
     

    Attached Files:

  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Funky!
    AC
     
  3. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    So, these trees are all generated from code? Neat!
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Those look like good ol' L-system trees which would be code generated... (am I right? do I win anything? ;) :p) Good stuff, keep us posted on how things progress!
     
  5. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    @Targos: Thank you.

    @Moldorma:
    Yes generated from code. I make them in the Start() function by creating a new mesh. And I call mesh.Optimize() on them, which makes them run quite smoothly on my 1Ghz G4

    @HiggyB:
    I had not heard of L-trees before. But now that you mentioned them I googled them and found the math behind it to be very similar. I basically reinvented the same thing.
    So maybe you win the honor of knowing more about the subject that me :)
     
  6. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I looked at L-system too, and was pretty impressed, and dumbfounded at the same time. It was over my head swiftly.

    Will you be collaborating with the Unitree posse and maybe show us the recipe?
    AC
     
  7. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    L-systems are a great way of doing procedural elements like this and it's awesome that you (Carsten) managed to stumble upon the same recipe on your own. Good stuff! And I definitely recommend that you share your thoughts with the Unitree folks as Targos hinted at, it would seem you have some good insight to offer given that you're going through some similar exercises.
     
  8. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I was thinking that it would be really awesome to get a unity tree making script that let you create trees in the Unity editor and save as .obj for later use.

    This would be a first step in achieving that goal.
     
  9. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    @Targos:If by recipe, you mean the code, you can have what I have done so far. There are installation instructions in the header

    It still needs lots of improvements before it is useful in practice, but you can play around with the controls in the inspector, to get a feel for how it works. Be careful about setting recursion level too high.

    @HiggyB
    Joachim Ante knows that I am doing this. It was his suggestion, that I ported my this to Unity. I believe he is one of the main persons behind UniTree?

    Anyway I am not going to join the UniTree project right now, I may still improve the tree generator, but I also have to get back to my main project, which doesn't really need large landscapes or trees. At least I haven't thought of where they would fit in yet :)

    Edit: forgot the actual code the first time :oops:
     

    Attached Files:

  10. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    There are a few folks working on the UniTree project, here's the site URL in case you don't have it handy:

    http://unify.bluegillweb.com/unitree/

    Naturally we all understand that you have other commitments to handle first (your main project), we were just making the suggestion to join the UniTree project in case it struck your fancy. Either way, thanks for sharing your work!
     
  11. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Nice one! this should go in the Wiki!
     
  12. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    Hi again.

    I made an updated version with support for texturing. You can specify (to some degree) how the the texture in the first material should be mapped onto the tree. The second material is assumed to be something that looks like a leaf, and is simply mapped onto a little rectangle. As you can see from the example, I have room for improvement in the texture drawing department.

    New version of script included.

    About the wiki - well when I get less lazy, i might figure out how to do that. (No, don't tell me, I just need to get less lazy)
     

    Attached Files:

  13. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    That looks really cool! I had in my head a long time ago an idea that one could create a system that lets you create a structure for the tree eg. I first make a procedural trunk and specify parameters for it, then I add on small alpha channel branches on points specified before (the ends of solid geometry branches) then I add leaf polygons onto those alpha mapped branches (probably the alpha mapped branches are prefabs with transform children for positions and rotations at which the leaves should be instantiated) then the whole thing is cleaned up and combined into one mesh.

    Not asking you to make such a system but just putting forth the idea I had. This looks like a great start as far as procedural tree generation !
     
  14. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    Have you looked at the tree gen script for maya?
    It's a free download that some Russian dude just wrote.

    http://www.highend3d.com/maya/downloads/mel_scripts/modeling/misc/Tree-Generator-1729.html

    The idea is basically, generate trunks, generate branches, then it places leaves based on a prefab you provide.

    It isn't perfect, it takes about 5-10 trees before you get one you like, but it allows you to redo any phase, or start over, and its pretty fast.

    Here is an oak tree I made in about 30 minutes (most of that time was remaking iterations of trees)

    I'm not sure if any of this code would be useful, but maybe it is worth looking at. Sadly I can't take credit for this amazing script, but maybe you can improve on it :D
     

    Attached Files:

  15. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Sounds a lot like the blender LSystem script I Have been using. All of my trees on the Unitrees page of the wiki are generated with it.
     
  16. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    Yeah, i just substitute branches or whatever for leaves, and it does a great job of creating game ready trees.

    Beats the heck out of hand placing leaves....

    I just hate making branch textures that much that I find myself more willing to place leaves then make textures.
     
  17. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
  18. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    Thanks for the tip Pete. Yes cool little forest he made.
     
  19. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    Here is the venerable L-System script (in a Blend file) to which Higgy and Yoggy and others have referred: http://greenpriest.com/lsystem.blend Brian, you might find this useful, if you ever find yourself w/out Maya at hand :) But hopefully Carsten will adapt some of the techniques expand his awesome script: stem and branch curvature and spin, multipoint leaves, overall LOD, etc... It's python, I think, but still.

    I can't wait to see how this thing evolves. The Unitree goals LOD and animation might be easier to accomplish using this script as a basis; i.e. gen multiple versions w/ different recursion values, and use culling or something to show the right one, depending on distance. I'm also wonder if there's a way to use the techniques in the grass script w/ these procedural prefabs, to "paint" a distribution of them.
     
  20. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I think what this script needs most is a way to prevent me generating too many branches. A default override of some sort.

    This is really good work btw.
    AC
     
  21. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    @polytropoi: good deal, thanks for sharing! I'm gonna give that a look as soon as I can... :)
     
  22. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
  23. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    @Polytropoi: Thanks for the link, but I am not even going to look until I run out of ideas myself. Currently the limiting factor is how much effort I am spending on the trees.

    Whenever I get going again, my next step would be to improve the way polygons are generated around the branching points. Before that is fixed, there is not much sense in adding smoothing, curvature, etc.
    And then I could continue with:
    - randomness (needs a deterministic pseudo-random generator, because I haven't found a way of setting the seed of the Random class)
    - polygon subdivision (for smoothing things out)
    - more curvature, twisting, etc.
    - a new parameter setup, that allows parameters to be set freely for each recursion level
    - automatic generation of prefabs of subtrees
    - LOD animated, growing trees
    - automatic generation of textures for leaves (I would need to upgrade to Pro for that, because of RenderToTexture)
    - all the stuff I think of while implementing the above points

    I have thought about the grass too, but can't examine it, until I upgrade my machine (I am currently waiting to see if new mac models are around the corner)

    @Targos: I thought about a failsafe, but I am not quite sure how it should work. The script currently runs in the "Start" function. Should it reset the parameters to something smaller at that point? Should it generate half a tree, and stop when it runs out of vertices?
    The hard limit is 65000 vertices per mesh. Currently this number can be calculated in advance, but once randomness enters into it, you can only make a ballpark estimate. But ok, I guess I'll cross that bridge when I get to it.

    @bigkahuna: very pretty ivy. You could do an ivy-project completely separately, because the ivy just needs to crawl on whatever polygon mesh is there. Which could be a tree generated by my script, or a wall as demonstrated on his website.
     
  24. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    wow the Ivy thing is cool! Man there are so many cool things that could be created for UniTree! Now if we can just find programers interested in implementing them. Jeff