- Home /
How to prevent frustum culling when using a vertex shader?
Here's the setup:
Unity 2019.3.6f1
I'm using GPU instancing to draw many things.
Each thing has it's own size (not scale), but uses the same base mesh
Vertex shader slices the mesh to correct size (similar to a 2d 9-slice sprite but in 3d, so 27-slice)
This all works great - very few draw calls, very low memory usage (no per-instance mesh), etc.
Except for two issues:
Frustum culling seems to go off the base mesh or maybe mesh bounds.
Ditto for editor hit testing.
Not unexpected since I didn't think it would run my vertex shader, but how do I work around it?
My first though was to set bounds on the Renderer - nope, it's read only.
Setting it on the mesh doesn't help because the size is per-instance and many instances use the same mesh.
Thoughts?
Answer by Bunny83 · Mar 24, 2020 at 03:08 AM
Well in your specific case your only solution would be to simply prevent frustum culling by setting a very large Bounds for your mesh. Keep in mind that the Mesh.bounds is defined in local space. Since you do some sort of 26 slice you probably have one corner fixed?! and you are moving the opposite corner depending on your size. If that's the case you could could just set the mesh bounds to a similar size but just increase your variable ends up to your expected max size. Of course this would almost in all cases disable frustum culling. However if one corner is fixed (in local space) you could still benefit from frustum culling in some cases. If your mesh can grow in any direction you would need to set the bounds to a large value in all directions. I'm not sure if you can use infinity for the bounds size / extent in order to get an infinitely large AABB so it will always overlap is the view frustum.
Answer by $$anonymous$$ · Mar 25, 2020 at 07:20 PM
Thanks for the answer @Bunny83. That's what I suspected, but was hoping there was something I missed.
It's unfortunate that furstum culling is so inflexable...if anyone from unity is listening, please support per-instance / renderer culling bounds.
SkinnedMeshRenderers support this... I am now thinking about some custom "Renderer" Script that would use a MeshRenderer with an empty per-instance mesh that has some bounds, then using OnWillRenderObject to call Graphics.DrawMesh, or something like that
Your answer
Follow this Question
Related Questions
How to compute shadows in custom Surface Shaders? 0 Answers
Weapon system 0 Answers
UNITY4 all i see is the triangle/verts of objects 1 Answer
Animation not rendered when object is close to one another 1 Answer
Culling Masks with Deferred Lighting 0 Answers