Unity Shader 基礎(2) Image Effect

Unity中 Image Effect 是Post Processing的一種方,Unity自身也提供不少Effect效果供使用。Image Effect的使用官方文檔作了不少介紹,這裏重點Post Processing 作一些介紹。html

1. Post Processing workflow

  • Render the scene to a render target / texture (screen-size, usually same format)
  • Reset render target - either to another render target / texture or the actual backbuffer
  • Set the pixel shader for post-processing, bind the scene texture to a sampler
  • Draw a full-screen quad over the scene using a dummy vertex shader

mark
具體能夠參考文章:https://research.ncl.ac.uk/game/mastersdegree/graphicsforgames/postprocessing/Tutorial%2010%20-%20Post%20Processing.pdf,裏面也說起到多個Image Effect串聯在一塊兒是如何實現的。git

在使用中,Effect Shader的Shader中實際上是處理和近剪裁面大小的四邊形面板,渲染這個四邊形的貼圖是當前Color Buffer中的數據。 實際Pixel Shader中主要是每一個像素或者一個區域進行各類處理。github

2. Image Effect

涉及的方法:app

  • OnRenderImage
  • Graphics.Blit
  • Graphics.SetRenderTarget

OnRenderImage
‘OnRenderImage’ function receives two arguments: source image as a RenderTexture and destination it should render into, as a render texture as well. Typically a postprocessing effect uses Shaders that read the source image, do some calculations on it, and render the result into the provided destination (e.g using Graphics.Blit). It is expected that the image effect will fully replace all pixels of the destination texture.ide

多個效果
When multiple postprocessing effects are added on the camera, they are executed in the order they are shown in the inspector, topmost effect being rendered first. Result of one effect is passed as 「source image」 to the next one; and internally Unity creates one or more temporary render textures to keep these intermediate results in.post

在Unity5.5以後加入了新的管理方式:https://github.com/Unity-Technologies/PostProcessingthis

須要注意:
Destination render texture can be null, which means 「render to screen」 (i.e. the backbuffer). This typically happens on the last image postprocessing effect on a camera.
When OnRenderImage finishes, it is expected that the destination render texture is the active render target. That is, generally a Graphics.Blit or manual rendering into destination texture should be the last rendering operation.
You generally want to turn off depth buffer writes and tests in your image effect shaders – otherwise can end up writing unintended values into destination Z buffer when doing Graphics.Blit. Almost all image effect shader passes should contain Cull Off ZWrite Off ZTest Always states.
If you wish to use stencil or depth buffer values from the original scene render, you should explicitly bind the depth buffer from the original scene render as your depth target. This can be done using Graphics.SetRenderTarget. You should pass the very first source image effects depth buffer as the depth buffer to bind.spa

渲染順序
By default, an image effect is executed after whole scene is rendered. In some cases however, it is desirable to render an effect after all opaque objects are done (but before skybox or transparencies are rendered). Often depth-based effects like Depth of Field use this.3d

Adding an ImageEffectOpaque attribute on the OnRenderImage function allows to achieve that.orm

平臺差別
主要是DirextX和OpenGl座標方向問題個
https://docs.unity3d.com/550/Documentation/Manual/SL-PlatformDifferences.html

參考:
官方文檔: https://docs.unity3d.com/550/Documentation/Manual/WritingImageEffects.html

相關文章
相關標籤/搜索