• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

creating a mesh procedurally 4 Answers

weird shadows in the procedurally created mesh 1 Answer

Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer

minimize vertex artifacting for procedural mesh manipulations - vertex collision same object? 0 Answers

Procedurally generated mesh JS - explain the error please 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges