Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Adding Trees in to the Terrain?

Discussion in 'Scripting' started by lil_billy, Oct 28, 2012.

  1. lil_billy

    lil_billy

    Joined:
    Oct 26, 2012
    Posts:
    6
    So this seems to be a tough question

    How can i take a tree model and procedurally place it INTO the terrain

    by this my question is
    how do i take a tree GO and add it to the terrain data
    so that its rendered as part of the terrain rendering system

    I cant figure out how to use addTreeInstance()

    I posted this question in the ask area and so far no one knows

    http://answers.unity3d.com/questions/338831/forest-generator-help-optimization.html

    id really would not to want to have to reverse engineer the terrain optimization algorithms if possible
    I'd love to just hack in my own trees procedurally and just use the terrains rendering algorithms
     
  2. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    That other thread you linked has the answer.. It is indeed Terrain.AddTreeInstance that will get your trees into the terrain system.

    Setup your tree in the terrain system the same way you would were to to paint trees on the terrain. (Basically drag drop tree model to Terrain Tool tree section).

    Once your tree model works OK with the terrain you can then fill out a TreeInstance structure and call Terrain.AddTreeInstance to create new trees (copies of one of the base terrain prototype trees that you setup earlier)
     
  3. Gabo_campitelli

    Gabo_campitelli

    Joined:
    Oct 17, 2012
    Posts:
    426
    i found this:

    Terrain terrain = (Terrain)GameObject.FindObjectOfType(typeof(Terrain));

    tempInstance = new TreeInstance ();

    tempInstance.prototypeIndex = 0;

    tempInstance.color = Color.white;

    tempInstance.heightScale = 1;

    tempInstance.widthScale = 1;

    tempInstance.position = new Vector3 (Random.value, 0, Random.value);

    terrain.AddTreeInstance (tempInstance);

    terrain.Flush ();
     
  4. lil_billy

    lil_billy

    Joined:
    Oct 26, 2012
    Posts:
    6
    @BPP no it flipping doesnt, AddTree instance does not work as easily as you seem to think

    @Gabo yeah I found that too after a lot more searching
    I did get it to place a black version of the tree
    however for some reason it only places trees along an edge

    I tried (.5,0,.5)
    which i would have thought would have placed it in the center of the map
    but it just placed it center of an edge '

    thanks for the effort, I appreciate it
     
  5. Gabo_campitelli

    Gabo_campitelli

    Joined:
    Oct 17, 2012
    Posts:
    426
    the tree instance position is a vector3 position in world data (from what i interpret) so if your terrain base is at 0, 0. Seting the position to .5, .5 would be pretty much at the center of the scene. i haven't done it yet. But im working in terrain generation from script so if you found something out about it i'd be glad if you can share.
     
  6. lil_billy

    lil_billy

    Joined:
    Oct 26, 2012
    Posts:
    6
    my theory is that it isnt world position because if it were it wouldnt use decimals
    no I suspect its done this way in case terrain is scaled
    so .5 would always be 1/2
    0 would always be bottom and 1 would be always top

    yet still I have no idea why it will only spawn along an edge
    the x value works
    but it seems whatever i put in for z doesnt do anything

    I suspect that the y value is more of a vertical offset from the terrain normal, and so should always be 0


    making a system to find where to put the tree is the easy part
    even converting the world vector to this decimal ratio seems easy in comparison
    getting the damned thing there is a pain in the ass
    because whatever i try to do this silly ass thing doesnt follow logic
     
  7. Gabo_campitelli

    Gabo_campitelli

    Joined:
    Oct 17, 2012
    Posts:
    426
    You are right. position goes from 0 to 1. Also aparently you shouldn't left any atribute of the tree instance unasigned. like color, lightmap, etc. At least that's all i could google. When i get home i'll try placing some trees and see what happen since i need this too.
     
  8. lil_billy

    lil_billy

    Joined:
    Oct 26, 2012
    Posts:
    6
    oh yeah thats how i got it to place the black trees, you have to declare all the values

    oh i should warn you this gets a little crash prone so save

    and tree additions made to the data during runtime remained saved in the terraindata after runtime
    so you have to manually delete the trees with the brush every now and then.
     
  9. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    You wouldn't have to manually delete trees if you'd backup the tree instances array and restore it on application exit when you're testing in the editor.
     
  10. lil_billy

    lil_billy

    Joined:
    Oct 26, 2012
    Posts:
    6
    alright it seems ive fixed the edge bug
    it seems it was just a bug with the project I had open at the time

    next bug
    the position vector only supports one decimal point
    so if I try .75 it rounds it to .8

    so i put 4 trees into the scene with the brush and read in their positions

    their vectors were the same but their visible positions were separated
    I wonder how i get more defined positions.
    edit:well that was solved
     
    Last edited: Oct 30, 2012
  11. Gabo_campitelli

    Gabo_campitelli

    Joined:
    Oct 17, 2012
    Posts:
    426
    Sorry to bump this, I'm having the same issue as you with the trees. They are placed along an edge. how did you fix this?