Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vestax_ion · Jul 21, 2011 at 12:11 AM · arraylooptreematrixrecursion

Recursive Tree Loop? How do i make the matrix-array?

I would like to make fractal tree in Unity3d, omitting branch angles and lengths, just to instantiate cubes as a tree, like 2,4,8,16... i look for solution all day and i figure it could take me days yet? Should i use an array to store and recall a list of coordinates that each new branch should be at? would it contain 2 or 16 values?

Here is an interactive JS code for trees: http://www.processing.org/learning/topics/tree.html

It uses the following code, please bypass the canvas, rotations, stalk length, i'd be so happy to just have a tree like in the image.

   branch(120);

 }

 void branch(float h) {
 // Each branch will be 2/3rds the size of the previous one
 h *= 0.66;
  
 // All recursive functions must have an exit condition!!!!
 // Here, ours is when the length of the branch is 2 pixels or less
 if (h > 2) {
 pushMatrix();    // Save the current state of transformation (i.e. where are we now)
 rotate(theta);   // Rotate by theta
 line(0, 0, 0, -h);  // Draw the branch
 translate(0, -h); // Move to the end of the branch
 branch(h);       // Ok, now call myself to draw two new branches!!
 popMatrix();     // Whenever we get back here, we "pop" in order to restore the previous matrix state
 
 // Repeat the same thing, only branch off to the "left" this time!
 pushMatrix();
 rotate(-theta);
 line(0, 0, 0, -h);
 translate(0, -h);
 branch(h);
 popMatrix();

} }

alt text

here is my funny attempt thus far:

 var bullet : Transform;
 private var rot : Quaternion;

 function Start()
 {
 for (var j : float = 0; j<10; j++)
 {
 for (var i : float = 0; i<10; i++)
 {
     rot.eulerAngles = Vector3(0,10,0);//Rotation to use with instatiation 
     var InstantiateObject = Instantiate(bullet, Vector3(Mathf.Sin(i*432)*i*j,j*5,Mathf.Cos(i*4375)*i*j), rot);//call clones
     InstantiateObject.localScale += Vector3(25-j*2,22-j*2,22-j*2);//resize clones
 
 }
 
 }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Herman-Tulleken · Jul 21, 2011 at 07:27 AM

Try this as a staring point:

 public class CubeSpawn : MonoBehaviour 
 {
     public GameObject cubePrefab;
     public GameObject root;
 
     private Vector3[] CORNERS = 
     {
         new Vector3(-1, 1, -1),
         new Vector3(-1, 1, 1),
         new Vector3(1, 1, 1),
         new Vector3(1, 1, -1),
     };
 
     public void Start()
     {
         AddChildren(root, 0.5f, 5);
     }
 
     public void AddChildren(GameObject parent, float childToParentRatio, int depth)
     {
 
 
         foreach (Vector3 corner in CORNERS)
         {
             GameObject cube = (GameObject) Instantiate(cubePrefab);
             Vector3 newPosition = parent.transform.TransformPoint(corner * 0.5f);
             
             //align parent and child
             cube.transform.rotation = parent.transform.rotation;
             
             cube.transform.position = newPosition;
             cube.transform.localScale *= childToParentRatio;
 
             if(depth > 0)
             {
                 AddChildren(cube, childToParentRatio * childToParentRatio, depth - 1);
             }
         }
 
 
 
     }
 
     public Vector3 prod(Vector3 v1, Vector3 v2)
     {
         return new Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
     }
 }

Put the script on an empty game object. Drag in the parent (you can modify it later to instantiate or whatever), and drag in your cube prefab.

The function above is called recursively on the new corners; the depth value makes sure it is not called indefinitely.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vestax_ion · Jul 21, 2011 at 09:39 AM 0
Share

Thankyou so much! That's very interesting. i will take a long while to understand and tailor it for other shapes now. I had to add the line "using UnityEngine;" at the top.

avatar image vestax_ion · Jul 21, 2011 at 10:38 AM 0
Share

the childToParentRatio was scaling exponentially small so i changed line 37 to this: AddChildren(cube, childToParentRatio - (childToParentRatio /2), depth - 1);

because otherwise you could only see the first 3 rows although it would make more sense to write:

             AddChildren(cube, childToParentRatio - (childToParentRatio *.6), depth - 1); 

but it wouldnt let me! CubeSpawn.AddChildren(UnityEngine.GameObject, float, int)' Argument #2' cannot convert double' expression to type float'

so it's saying 0.6 is a double not a float? so i have to declare a var as a float and then say it is 0.6? ok i get it. brb ;)

avatar image Herman-Tulleken · Jul 21, 2011 at 11:26 AM 0
Share

For float literals, just put an f, for example, 0.6f.

avatar image vestax_ion · Jul 21, 2011 at 04:55 PM 0
Share

what is the most computationally efficient way to use such a structure in Unity3d? should "combine mesh" to reduce draw calls, should i do some postprocessing to cull hiddent vertexes and objects? I was only just beginning JS, even instantiation seems very tricky in C#, i haven't yet managed to instantiate the central parent cube.

avatar image Herman-Tulleken · Jul 24, 2011 at 07:02 PM 0
Share

I think you may want to ask a separate question for that; I am not a 3D model optimisation guru :P.

avatar image
2

Answer by vestax_ion · Sep 06, 2011 at 05:19 PM

Here is a JS version of your C# script:

 var cubePrefab: GameObject ;
 var root : GameObject ;

 private var corners = new Array (Vector3(-1, 1, -1), Vector3(-1, 1, 1),Vector3(1, 1, 1),Vector3(1, 1, -1));

 function Start()
 {
     var root = Instantiate(cubePrefab,Vector3.zero,Quaternion.identity);
     AddChildren( root,0.5, 4);
 }

 function AddChildren( ploppy, ratio ,depth)
 {


     for (var x = 0; x < 4; x++) 
     {         
         var cube = Instantiate(cubePrefab,Vector3.zero,Quaternion.identity);
         

         //align parent and child
         //cube.transform.rotation = prt.transform.rotation;

         cube.transform.position = ploppy.transform.TransformPoint(corners[x]*.5);
         cube.transform.localScale *= ratio;

         if(depth > 0)
         {
             AddChildren( cube, ratio - (ratio*.6) , depth - 1);
         }
     }



 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tonydincau · Feb 01, 2013 at 08:15 AM

There is some info and a source project to draw reference from here http://tonydincau.com/fractal-trees/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Large matrix formation and multiplication 0 Answers

How to correctly shorten this script using arrays and iterations 2 Answers

Checking an array variable in C# vs JavaScript 3 Answers

OverlapSphere for parallel arrays 1 Answer

Create a button from an int, remove button when clicked, and never load it again 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges