Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MakakWasTaken · Mar 30, 2015 at 11:26 AM · colliderterraintreebillboard

Billboard system

I have create a billboard system, and I would like to get any improvements, because when I instantiate it it takes 5000-10000ms for the UpdateBillboard function, this script is attached to every gameobject, is there a way to do this in a shader instead, which should give better performance right? NOTE: You manually assign quad and tree through the inspector and the billboard is generated by another script at runtime (Once for each tree type, so only 1-10 times or so) Code:

     public BillboardClass tree;
     public float BillboardDistance = 250.0f;
     private Renderer render;
     private Renderer QuadRenderer;
     private Collider col;
 
     [Serializable]
     public class BillboardClass
     {
         public GameObject tree;
         public Texture2D billboard;
         public GameObject Quad;
         [HideInInspector]
         public Mesh mesh; 
         [HideInInspector]
         public Vector3 Scale;

         public void UpdateSize()
         {
             mesh = new Mesh();
             mesh = Quad.GetComponent<MeshFilter>().sharedMesh;
             Bounds bb = tree.GetComponent<Renderer>().bounds; 
             Scale = new Vector3(bb.max.x - bb.min.x, bb.max.y - bb.min.y, 1);
         }
     }
 
     void CreateQuad()
     {
         GameObject Quad = GameObject.Instantiate(tree.Quad); //Create quad (Used to display billboard)
         Quad.GetComponent<MeshFilter>().mesh = tree.mesh; //Assign the custom sized mesh to it.
         Quad.transform.parent = transform; //Make parent of the tree
         Quad.transform.localPosition = Vector3.zero; //Reset position
         Quad.transform.localScale = tree.Scale;
 
         QuadRenderer = Quad.GetComponent<Renderer>(); //Cache the renderer for the update function
         QuadRenderer.material.mainTexture = tree.billboard;
         QuadRenderer.enabled = false;
     }
 
     void Start()
     {
         render = GetComponent<Renderer>();
         col = GetComponent<Collider>();

         CreateQuad();
 
         InvokeRepeating("UpdateBillboard", UnityEngine.Random.Range(0.0f,1.0f), 1.0f);
     }
 
     Transform target;
     bool ShowTree;
 
     void UpdateBillboard()
     {
         if (target == null)
         {
             if (Camera.main != null)
             {
                 target = Camera.main.transform;
             }
             return;
         }
         ShowTree = false;
         if (Vector3.Distance(transform.position, target.position) < BillboardDistance)
         {
             ShowTree = true;
         }
         QuadRenderer.enabled = !ShowTree;
         render.enabled = ShowTree;
         col.enabled = ShowTree; //Should I disable the collider or not
     }
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

1 Reply

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

Answer by _met44 · Mar 30, 2015 at 11:49 AM

Hi, you're instantiating a gameObject just to get its bounds, seems like an overkill to me.

Try to break down your code with Profiler.BeginSample()/Profiler.EndSample() to find what parts are costing you so much and figure a way to get rid of or go arround them !

Good luck :)

Comment
Add comment · Show 4 · 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 MakakWasTaken · Mar 30, 2015 at 11:51 AM 0
Share

Here I changed the quad resize to be this:

         public void UpdateSize()
         {
             Bounds bb = tree.GetComponent<Renderer>().bounds;
             Quad.transform.localScale = new Vector3(bb.max.x - bb.$$anonymous$$.x, bb.max.y - bb.$$anonymous$$.y, 1);
         }

The scale is shown correctly in the inspector, but the quad isn't affected at all

avatar image MakakWasTaken · Mar 30, 2015 at 12:09 PM 0
Share

4/5 of the ms is from running the UpdateBillboard void itself.

avatar image _met44 · Mar 30, 2015 at 01:18 PM 0
Share

If I'm right the Quad field of BillboardClass contains your prefab rather than the instantiated version no?

And you don't seem to keep a reference to the gameObject you instantiate at all. So unless the object handles is own lifetime and has a autonomous behavior it's going to stay there forever without ever being used !

avatar image MakakWasTaken · Mar 30, 2015 at 10:40 PM 0
Share

@_met44 The the localScale is correct in the inspector but nothing is happening with the quad, even if I manually try to rescale it it doesn't change size Code:

     private void CreateQuad()
     {
         GameObject Quad = GameObject.Instantiate(quad); //Create quad (Used to display billboard)
 
         Quad.transform.parent = transform; //$$anonymous$$ake parent of the tree
         Quad.transform.localPosition = Vector3.zero; //Reset position
         Quad.transform.localScale = tree.Scale; //Set scale correctly
 
         QuadRenderer = Quad.GetComponent<Renderer>();
         QuadRenderer.material.mainTexture = tree.billboard;
         QuadRenderer.enabled = false;
     }

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

21 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

Related Questions

Disabling colliders in Terrain Trees? 1 Answer

Is it possible to edit the tree billboards generated by a terrain? 0 Answers

Colliding with trees using mesh colliders 1 Answer

Tree Colliders not working 1 Answer

CreateTerrainGameObject not creating tree colliders? 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