In my project i want to make some custom postprocessing for specific game objects.
For example, i have 10 cubes in the scene, but need to re-render (for a postprocess step) only specific 5 of them. Currently, the postprocess consists of rendering to a texture with a simple shader, which outputs white color.
While searching for ways to do this in Unity (i’m new to Unity) i found 2 methods:
- add a second camera, set it up same as main camera, set it’s render target and set specific layers to render. At the same time set these layers for objects, which i need to render.
- draw objects with Graphics.DrawMeshNow.
Method (1) looks a kind of hacky to me. The “problem” is that objects that i need to re-render are changing constantly. It’s just something like a selected list of objects in RTS. A user is able to pick some of them and not the others. And i need to re-render those, which are picked and not the others. For this i will need to:
- before redering, save previous object’s layer (used for physics raycastings and stuff)
- set the one which is needed for second camera rendering
- render objects
- set the layer back
Looks a bit messy.
So i took method (2). It needs only one main camera and all the steps are kind of straightforward (during OnRenderImage()):
- set my shader pass as current
- set render target
- iterate through needed objects and through their renderers
- draw each of them with Graphics.DrawMeshNow(mesh, renderer.transform.localToWorldMatrix)
- put everything back
This looks much better and cleaner, but has one little-bit problem.
DrawMeshNow() doesn’t render statically batched meshes. Don’t know why, but seems that when i simply remove the appropriate flag, they begin to render as they should. But when flag is set, all i get is a blank screen. No matter what has been drawn previously.
So my question is, what should i do with this? Take the “hacky” method (1) with the camera, or there is something that could be done for method (2) to work with statically batched meshes?
Btw, this was tested in Unity 5.0.1f1 Personal.