屏幕扭曲效果

項目中我使用的方法,自己記錄下。

1》扭曲效果本身實現

方向使用渲染物體的法線來標記,渲染完其他物體後,最後渲染扭曲,將一張渲染完成後的rt傳入shader中,根據渲染物體的屏幕座標渲染根據法線偏移過後的像素,達到扭曲效果。

當然這個方法可以使用兩個相機來實現,考慮到項目中經常要對相機做複製,雙層相機複製的步驟繁瑣且不好管理。而且雙相機的性能也有一定的損耗。

最後採用CommandBuffer來實現。我們手動渲染扭曲物體。

 

2》實現流程

先管理一個list用來記錄扭曲物體,同時這些扭曲物體不會被任何相機渲染,主要用來提供renderer和material。供給CommandBuffer.DrawRenderer使用。

項目使用前向渲染,我們需要在透明物體渲染結束之前渲染扭曲物體即可。

Camera.AddCommandBuffer(CameraEvent.AfterForwardAlpha, _distortionCommand);

在渲染物體之前,需要把當前繪製的rt拷貝出來,以供扭曲物體使用。

_distortionCommand.Blit(BuiltinRenderTextureType.CameraTarget, distortionRT);

接着傳入Shader

_distortionCommand.SetGlobalTexture(PARAM_SCENE_COPY, distortionRT);

設置回來

_distortionCommand.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);

接下來手動渲染之前就收集好的扭曲物體

_distortionCommand.DrawRenderer(GameGraphics.Instance.distortionObjList[i].rendererDis, GameGraphics.Instance.distortionObjList[i].materialDis);

搞定,記得rt複用減少gc節省內存。

 

發佈了25 篇原創文章 · 獲贊 9 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章