• 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 tall-josh · Mar 21, 2017 at 03:21 AM · meshfilterlodgroupboundingbox

Draw 2d bounding box around prefab with multiple LOD.

Hi Community,

I'm attempting to draw a 2d bounding box around a 3d SpeedTree. I've been trying to follow this post.

https://forum.unity3d.com/threads/finding-a-3d-objects-2d-bounds.231304/

I have made a few modifications to make the bounding box script accessible from another script I am running, however, I get a property access error saying the gameobject I have passed into my function does not have a Mesh Filter associated with it. My guess is, its because a SpeedTree has several LODs and I need to access the Mesh Filter of the relevant LOD being rendered.

Is there a way to access the relevant mesh, if that's the right way to go about it?

Cheers, Josh

Comment
Add comment · Show 1
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 tall-josh · Mar 21, 2017 at 02:59 AM 1
Share

Found it!!!

I was using GetComponent to try and access the $$anonymous$$eshFilter. At a guess (and this is a total guess), the prefab I'm interested is the abstract base class of the various LOD Children. So using GetComponentInChildren to find the $$anonymous$$eshFilter is the way to go.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BoundingBox : $$anonymous$$onoBehaviour {
 
     public float margin = 10.0f;
     private GameObject go;
     private Vector3[] v;
     private Rect rect;
 
     private Camera cam;
 
     // Use this for initialization
     void Start ()
     {
         cam = Camera.main;
         //v = go.GetComponent<$$anonymous$$eshFilter>().mesh.vertices;
     }
     
     public void FindBoundingBox(GameObject objIn)
     {
         cam = Camera.main;
         go = objIn;
 
         //The object is behind us
         if (cam.WorldToScreenPoint(go.GetComponentInChildren<Renderer>().bounds.center).z < 0) return;
 
         v = go.GetComponentInChildren<$$anonymous$$eshFilter>().mesh.vertices;
 
         for (int i = 0; i < v.Length; i++)
         {
             //World space
             v[i] = go.transform.TransformPoint(v[i]);
             //GUI space
             v[i] = cam.WorldToScreenPoint(v[i]);
             v[i].y = Screen.height - v[i].y;
         }
 
         Vector3 $$anonymous$$ = v[0];
         Vector3 max = v[0];
 
         for (int i = 1; i < v.Length; i++)
         {
 
             $$anonymous$$ = Vector3.$$anonymous$$in($$anonymous$$, v[i]);
             max = Vector3.$$anonymous$$ax(max, v[i]);
         }
 
         //Construct a rect of the $$anonymous$$ and max positions and apply some margin
         rect = Rect.$$anonymous$$in$$anonymous$$axRect($$anonymous$$.x, $$anonymous$$.y, max.x, max.y);
 
         rect.x$$anonymous$$in -= margin;
         rect.x$$anonymous$$ax += margin;
         rect.y$$anonymous$$in -= margin;
         rect.y$$anonymous$$ax += margin;
 
         Debug.Log("x$$anonymous$$in: " + rect.x$$anonymous$$in + ", x$$anonymous$$ax: " + rect.x$$anonymous$$ax + ", y$$anonymous$$in: " + rect.y$$anonymous$$in + ", y$$anonymous$$ax: " + rect.y$$anonymous$$ax);
 
     }
 
     private void OnGUI()
     {
         //Render the box
         GUI.Box(rect, "THIS IS A BOUNDING BOX");
     }
 
 }
 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Bounding box not moving with the gameobject during the animation 0 Answers

LodGroup manually set Bounds 0 Answers

How to prevent editor to set the scene dirty if a script changes a mesh (@ExecuteInEditMode) 1 Answer

Getting Bounding box from a collider with children 1 Answer

change front color or back color programmatically 0 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