• 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
Question by StuwuStudio · Aug 11, 2017 at 07:47 PM · gameobjectmeshtree creatorprototypetree-creator

Generating a Mesh instead of a bunch of gameobject

I created a tree generator for my game. It uses unity cubes and pre-made leaves and assembles them to create a mesh. The problem? The tree is only 4.7k tris but it lowers the fps (from 113 fps to 78 fps). If was wondering if I could create the exact same tree but all in one mesh. The problem: I cannot scale/rotation a bunch of triangles to create this exact same tree: alt text

The code:

         case TreeCreatorTools.TreeShape.Round:
             TreeHeight = Random.Range(LogMinHeight,LogMaxHeight);
             TreeSize = Random.Range(LogMinSize,LogMaxSize);
             DistanceBettwenBranch = Random.Range(MinDistanceBettwenBranch,MaxDistanceBettwenBranch);
             CurrentHeight = 0;
             CurrentScale = 1f;
             beforeRadius = 0;
 
             float BranchDistance = DistanceBettwenBranch;
             float BranchDirection = Random.Range(0f,360f);
 
             //GameObject Obj;
 
             for(int tH = 0; tH < TreeHeight; tH++) { //Tree Height
                 Obj = (GameObject)Instantiate(ObjectTemplate,transform);
                 Obj.transform.localPosition = new Vector3(0f,CurrentHeight,0f);
                 CurrentScale = TreeSize * (logCurve.Evaluate((float)tH/TreeHeight)*logCurveInfluence);
                 Obj.transform.localScale = new Vector3(CurrentScale,CurrentScale,CurrentScale);
                 Obj.transform.GetChild(0).GetComponent<MeshRenderer>().material = LogMaterial;
 
                 if((float)tH/TreeHeight > HeightBeforeBranch) {
                     if(BranchDistance >= DistanceBettwenBranch) {
                         //Setup a branch
                         Obj = (GameObject)Instantiate(ObjectTemplate,transform);
                         Obj.transform.localPosition = new Vector3(0f,CurrentHeight,0f);
                         float THICCness = Random.Range(0.14987f,0.3756f);
                         Obj.transform.localScale = new Vector3(THICCness,RoundFoliage.Evaluate(((float)tH-beforeRadius)*(1f/(TreeHeight-beforeRadius)))*7.8f+Random.Range(MinBranchLenght*0.075f,MaxBranchLenght*0.075f),THICCness);
                         Obj.transform.eulerAngles = new Vector3(0f,BranchDirection,BranchRotation-100);
                         Obj.transform.GetChild(0).GetComponent<MeshRenderer>().material = LogMaterial;
 
                         Mesh UVEditor = Obj.GetComponentInChildren<MeshFilter>().mesh;
                         List<Vector2> UVs = new List<Vector2>();
 
                         for(int i = 0; i < UVEditor.uv.Length; i++) {
                             if(i < 4) {
                                 UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
                             } else if(i < 6) { //8 -> 12 
                                 UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.x,UVEditor.uv[i].y*Obj.transform.localScale.z));
                             } else if(i < 8) {
                                 UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
                             } else if(i < 10) {
                                 UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.x,UVEditor.uv[i].y*Obj.transform.localScale.z));
                             } else if(i < 12) {
                                 UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
                             } else if(i < 16) {
                                 UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.x,UVEditor.uv[i].y*Obj.transform.localScale.z));
                             } else if(i < 20) {
                                 UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
                             } else if(i < 24) {
                                 UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
                             }
                         }
                         Obj.GetComponentInChildren<MeshFilter>().mesh.uv = UVs.ToArray();
 
                         //Add the FLUFF at the end of the branch! (Also don't forget to do the prepared branch mode too)
 
                         if(Obj.transform.localScale.y < 1.2f) {
                             continue;
                         }
 
                         int FluffQ = Mathf.RoundToInt(Random.Range(RoundFluffMinSize*0.6f,RoundFluffMaxSize*0.6f));
                         GameObject Fluff;
 
                         for(int i = 0; i < FluffQ; i++) {
                             Fluff = (GameObject)Instantiate(FluffyTemplate,Obj.transform.GetChild(0));
                             Fluff.transform.localEulerAngles = Vector3.zero;
                             Fluff.transform.localPosition = new Vector3(0f,Random.Range(0.1f,0.5f),0f);
                             Fluff.transform.position += new Vector3(Random.Range(-RoundFluffMinSize,RoundFluffMinSize)*1f, 0f, Random.Range(RoundFluffMinSize,-RoundFluffMinSize)*1f);
                             float Size = Random.Range(8f,11f);
                             TreeCreatorTools.SetGlobalScale(Fluff.transform,new Vector3(Size,Size,Size));
                             Fluff.transform.GetChild(0).eulerAngles += new Vector3(Random.Range(-7f,7f),Random.Range(-7f,7f),Random.Range(-7f,7f));
                             Fluff.transform.GetChild(1).eulerAngles += new Vector3(Random.Range(-7f,7f),Random.Range(-7f,7f),Random.Range(-7f,7f));
                             Fluff.transform.GetChild(2).eulerAngles += new Vector3(Random.Range(-7f,7f),Random.Range(-7f,7f),Random.Range(-7f,7f));
 
                             Fluff.transform.GetChild(0).GetChild(0).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
                             Fluff.transform.GetChild(0).GetChild(1).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
                             Fluff.transform.GetChild(1).GetChild(0).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
                             Fluff.transform.GetChild(1).GetChild(1).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
                             Fluff.transform.GetChild(2).GetChild(0).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
                             Fluff.transform.GetChild(2).GetChild(1).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
                         }
 
                         BranchDistance = 0f;
                         BranchDirection += 150+Random.Range(-16.4f,16.4f);
                     } else {
                         BranchDistance += CurrentScale;
                     }
                 } else {
                     beforeRadius++;
                 }
 
                 CurrentHeight += CurrentScale;
             }
             break;


round-tree.png (83.3 kB)
Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make the all the leaves face one way ? 0 Answers

How to rotate child 3D object to match bend of mesh 1 Answer

C# Preserving GameObjects' Previous Meshes 1 Answer

Is it possible to set boundaries for a 3D object in Unity that can show the cross section plane? 0 Answers

Turning off renderer in other game object (C#) 2 Answers


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