ReRendering specific GameObjects for PostProcessing

Hi.

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

Seems that method (1) isn’t as easy as it looked like.
I need to re-render objects with different material parameters per object (change color). Using method (2) it works perfectly, since i control all the rendering process and can change parameters as i need, but it has issue with static objects. When using method (1) it requires even more hacks than it has already.

So the question is stripped to one main issue - what should i do to render static objects correctly with DrawMeshNow() ?
Perhaps i’m missing something obvious?

Aaaand bumping up!

Anything that changes materials should not be statically batched, as that breaks it.
You also have the option to render with replacement shaders.

Thanks, but i guess there is a small missunderstanding.
I already have statically batched meshes. I just need to render them again with DrawMeshNow() and my shader. It’s just that when a gameobject is marked as statically batched, it’s mesh doesn’t render with DrawMeshNow(). And when i clear this flag it works.
That is the problem right now.

That’s what I was saying, you can’t do that because that’s what statically batching implies, it is static and the drawcall can’t change, because then it wouldn’t be batched anymore. Unity may even throw away the individual meshes used to create the batch, because they are not needed anymore, this may be why DrawMeshNow renders nothing for you.
Somone else feel free to correct me if I’m wrong.

1 Like

Oh… i get you now. So seems that a mesh of static object might not be suitable for separate rendering.
I was thinking something like that, but there was no such info in the docs, like “note, that this won’t work with static meshes”.
Thanks! I guess i’ll kep with camera method then.

1 Like

DeadMeat, how many cameras you use in your method 2? only one main camera and some “no-main” cameras or only one camera which is “main-camera”.