As I posted in SSSSS in Unity before, I cannot find a way to take advantage of stencil buffer in
OnRenderImage. This makes the post effect full screen all the time. Other guys have come up with the same question in the formu or AnswerHub. After several days hard work, I finally find a way to use stencil.
Here is an example for fast Bloom:
With Stencil(Ocean Only):
The key idea is keeping the depth buffer (along with stencil) when rendering, with the help of
Graphics.SetRenderTarget. First of all, create and set the camera render target. (I also tried depth bit as 16, which makes
RenderTexture.SupportsStencil return false, and it still works. I don’t know why…)
Instead of downsample in
OnRenderImage, we have to
The shaderlab side is quite simple, and you can find many resources online. Here are two code snippets:
Post Processing Shader
ps. You could try draw to screen directly, without setting an extra
renderTexture. Currently I am combining some Post Process together, along with dynamic resolution, which makes this unavoidable.