Take advantage of Stencil buffer in Post Process
As I posted in 山寨SSSSS 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:
W/O Stencil:
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…)
RenderTextureFormat format = RenderTextureFormat.Default; |
Instead of downsample in OnRenderImage
, we have to Blit
in OnPostRender
public void OnPostRender(){ |
The shaderlab side is quite simple, and you can find many resources online. Here are two code snippets:
Ocean Shader
Stencil { |
Post Processing Shader
Stencil{ |
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.