Search Unity

Per-MeshRenderer bounds?

Discussion in 'General Graphics' started by ZimM, Nov 28, 2014.

  1. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    I am writing a shader that distorts the positions of mesh vertices heavily, so in many cases they can extend 10x beyond the original mesh bounds. Obvisouly, the mesh would get frustum culled because the culling is still done based on the original bounds. The vertex shader takes light position into account, so obviously, bounds have to be regenerated per-object. After finishing the shader, I've wrote some code that generates bounds for the distorted model based on the original bounds. The problem is... there is no way to update the bounds per-MeshRenderer.
    Why in the world Renderer.bounds is read-only?.. I am seriously stumped. This pretty much cancels any possibility for shader vertex modifications without getting culling artifacts or making culling ineffective. Even the manual has a vertex modication example, but it never tells you that you'll get objects popping into existence.
    Basically, it looks like vertex modification from inside vertex shader without getting culling artifacts is practically impossible in Unity now. Even SkinnedMeshRenderer has a writable localBounds property, but MeshRenderer has nothing. Am I missing something?
     
    Last edited: Nov 28, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can add 8 vertices at the corners of the desired bounding box, and not reference them with triangles, so they are invisible, but they still count toward the bounds.

    --Eric
     
  3. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    I can't do that for two reasons:
    1) I don't know the bounds beforehand. They can be pretty much zero size compared to the original bounds, and they can be 100x depending on the light position, which is changed dynamically. Setting bounds to be "Big enough, one size fits all" would effectively mean disabling frustum culling for that mesh, which is hugely ineffective as objects will be rendered even when they are outside the frustum.
    2) That'd mean allocating and copying a whole Mesh for every object. And there can be hundreds of objects, so this would consume memory like crazy.