void OnRenderImage(RenderTexture source, RenderTexture destination) { if (CMainMaterial) { // 申请两块降低了分辨率的RT RenderTexture rt1 = RenderTexture.GetTemporary(source.width >> down_sample_factor, source.height >> down_sample_factor, 0, source.format); RenderTexture rt2 = RenderTexture.GetTemporary(source.width >> down_sample_factor, source.height >> down_sample_factor, 0, source.format); Graphics.Blit(source, rt1); // 使用降低分辨率的rt进行模糊:pass0 CMainMaterial.SetFloat("_BlurFactor", blur_factor); CMainMaterial.SetVector("_BlurCenter", blur_center); Graphics.Blit(rt1, rt2, CMainMaterial, 0); // 使用rt2和原始图像lerp:pass1 CMainMaterial.SetTexture("_BlurTex", rt2); CMainMaterial.SetFloat("_LerpFactor", lerp_factor); Graphics.Blit(source, destination, CMainMaterial, 1); // 释放RT RenderTexture.ReleaseTemporary(rt1); RenderTexture.ReleaseTemporary(rt2); } else { Graphics.Blit(source, destination); } }
void OnRenderImage(RenderTexture source, RenderTexture destination) { if (CMainMaterial) { //申请RenderTexture,RT的分辨率按照downSample降低 RenderTexture rt1 = RenderTexture.GetTemporary(source.width >> downSample, source.height >> downSample, 0, source.format); RenderTexture rt2 = RenderTexture.GetTemporary(source.width >> downSample, source.height >> downSample, 0, source.format); //直接将原图拷贝到降分辨率的RT上 Graphics.Blit(source, rt1); //进行迭代高斯模糊 for (int i = 0; i < iteration; i++) { //第一次高斯模糊,设置offsets,竖向模糊 CMainMaterial.SetVector("_offsets", new Vector4(0, BlurRadius, 0, 0)); Graphics.Blit(rt1, rt2, CMainMaterial); //第二次高斯模糊,设置offsets,横向模糊 CMainMaterial.SetVector("_offsets", new Vector4(BlurRadius, 0, 0, 0)); Graphics.Blit(rt2, rt1, CMainMaterial); } //将结果输出 Graphics.Blit(rt1, destination); //释放申请的两块RenderBuffer内容 RenderTexture.ReleaseTemporary(rt1); RenderTexture.ReleaseTemporary(rt2); } }
void OnRenderImage(RenderTexture source, RenderTexture destination) { if (CMainMaterial) { CMainMaterial.SetFloat("_BlurFactor", blur_factor); CMainMaterial.SetVector("_BlurCenter", blur_center); Graphics.Blit(source, destination, CMainMaterial); } else { Graphics.Blit(source, destination); } }
public float saturation = 1.0f; //饱和度 //覆写OnRenderImage函数 void OnRenderImage(RenderTexture src, RenderTexture dest) { //仅仅当有材质的时候才进行后处理,如果_Material为空,不进行后处理 if (CMainMaterial) { //通过Material.SetXXX("name",value)可以设置shader中的参数值 CMainMaterial.SetFloat("_Brightness", brightness); CMainMaterial.SetFloat("_Saturation", saturation); CMainMaterial.SetFloat("_Contrast", contrast); //使用Material处理Texture,dest不一定是屏幕,后处理效果可以叠加的! Graphics.Blit(src, dest, CMainMaterial); } else { //直接绘制 Graphics.Blit(src, dest); } }