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
2
Question by femi · Jan 09, 2011 at 12:16 AM · proceduralfrustrumculling

Mesh generated at run-time gets frustum-culled too early

I have a mesh generation function that gets called at runtime OR in the editor:

void GenerateAtRunTime() { if (!generated && someCriterion) { GetComponent<MeshFilter>().sharedMesh = Generator.Generate(/* arguments */); generated = true; } }

[ContextMenu("Pre-generate")] void PreGenerate() { if (!generated) { GetComponent<MeshFilter>().sharedMesh = Generator.Generate(/ arguments /); generated = true; } }

if I call it from the editor, all is fine. If I don't the mesh is culled too early: as soon as the center of bounds is out of view, as if it's bounds had size of 0.

The Generator.Generate is like:

Mesh mesh = new Mesh(); Vector3[] v = new Vector3[vtxCount]; Vector2[] uv = new Vector2[vtxCount]; Color[] c = new Color[vtxCount]; int[] tri = new int[triCount];

 // .... actually generate here .....

 mesh.vertices = v;
 mesh.colors = c;
 mesh.uv = uv;
 mesh.triangles = tri;
 mesh.RecalculateBounds();

 return mesh;

(I know that assigning triangles recalculates bounds, I'm just that desperate)

as a debugging help I set up the following:

[ContextMenu("Print me")] void PrintMe() { Debug.Log(string.Format("{0} renderer bounds: {1}", gameObject.name, GetComponent<MeshRenderer>().bounds)); Debug.Log(string.Format("{0} mesh bounds: {1}", gameObject.name, GetComponent<MeshFilter>().sharedMesh.bounds)); }

void OnDrawGizmos() { Gizmos.DrawWireCube(renderer.bounds.center, renderer.bounds.size); }

...and bounds seem fine and identical in either case. What am I missing when generating meshes on the fly?

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

4 Replies

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

Answer by femi · Jan 09, 2011 at 12:43 AM

I'm not sure why (and I would like to hear the explanation) but the following has solved the problem: the generator now doesn't create a new mesh but modifies a mesh passed as an argument. Script that triggers the generation has become:

void GenerateAtRunTime() {
    if (!generated && someCriterion) {
        Generator.Generate(GetComponent<MeshFilter>().mesh, /* other arguments */);
        generated = true;
    }
}

(if I understand correctly, accessing mesh property creates a mesh when there wasn't one, but apparently it actually does something else to it behind the scene).

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
2

Answer by UnLogick · Jul 05, 2011 at 02:38 PM

I had the exact same problem. And my setup didn't allow me to use your solution.

I knew you were on the right track with the mesh bounds not being handled properly. Since collision with the same mesh worked fine, I reasoned that the MeshRenderer or MeshFilter was the one who needed to be poked.

So I tried playing around with the notifications and it turned out that, this failed:

 renderInfo.mesh.RecalculateBounds();
 renderInfo.gameObject.GetComponent<MeshFilter>().mesh = renderInfo.mesh;

And this succeeded:

 renderInfo.gameObject.GetComponent<MeshFilter>().mesh = renderInfo.mesh;
 renderInfo.mesh.RecalculateBounds();

So this is clearly a bug, either RecalculateBounds doesn't work when the mesh isn't attached to a transform, or setting the mesh property of the MeshFilter doesn't update/notify properly.

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
1

Answer by KingSharaman · Mar 10, 2012 at 12:53 AM

Use RecalculateBounds() after you assign the mesh to a MeshFilter component. So for example in C#:

GetComponent().mesh = aMeshObject; aMeshObject.RecalculateBounds();

This way it will recalculate renderer bounds.

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 Lemon · Apr 14, 2011 at 04:33 AM

Had a similar issue, posted an answer on this thread:

See http://answers.unity3d.com/questions/29948/disable-frustum-culling/52479#52479

Comment
Add comment · Show 1 · 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 UnLogick · Jul 05, 2011 at 02:39 PM 0
Share

link broken

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Procedural Voxel Collision, how? 2 Answers

Strange shading on procedural mesh 0 Answers

Why is my mesh getting messed up 1 Answer

Procedural Skyboxes Based on Scene Data 1 Answer

Creating caves in voxel procedural mesh 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