unity Shader屏幕后处理(1)简单应用

一、简介:

1、OnRenderImage函数:

Ⅰ、MonoBehaviour.OnRenderImage(RenderTexture src,RenderTexture dest)

Unity会把当前渲染得到的图像存储在第一个参数对应的源渲染纹理中,通过函数中的一系列操作后,再把目标渲染纹理,即第二个参数对应的渲染纹理显示在屏幕上。通常用Graphics.Blit函数来完成对渲染纹理的处理。

Ⅱ、public static void Blit(Texture src,RenderTexture dest,Material mat,int pass=-1);

其中src对应了源纹理,dest是目标渲染纹理,mat是我们使用的材质,材质使用的unity shader将会进行各种屏幕后处理操作,而src纹理将会被传递给Shader中名为_MainTex的纹理属性。pass的默认值为-1,表示将会依次调用Shader内的所有Pass,否则只会调用给定索引的Pass。

Ⅲ、默认情况下,OnRenderImage函数会在所有的不透明和透明的pass执行完毕后被调用,但有时,我们希望在不透明的pass执行完毕后立即调用OnRenderImage函数,此时可以在OnRenderImage函数前添加ImageEffectOpaque属性来实现这个目的。

2、unity中实现屏幕后处理过程通常如下:

首先需要在摄像机中添加一个用于屏幕后处理的脚本,脚本中会实现OnRenderImage函数,然后再调用Graphics.Bit函数使用特定的unity shader来对当前图像进行处理,再把返回的渲染纹理显示到屏幕上。对于一些复杂的屏幕特效,我们可能需要多次调用Graphics.Bit函数,不断对图像处理。但是在进行屏幕后处理之前,我们需要检查一系列条件是否满足,因此可以下面代码片段作为检查。

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class PostEffectsBase : MonoBehaviour {

	// Called when start
	protected void CheckResources() {
		bool isSupported = CheckSupport();
		
		if (isSupported == false) {
			NotSupported();
		}
	}

	// Called in CheckResources to check support on this platform
	protected bool CheckSupport() {
		if (SystemInfo.supportsImageEffects == false || SystemInfo.supportsRenderTextures == false) {
			Debug.LogWarning("This platform does not support image effects or render textures.");
			return false;
		}
		
		return true;
	}

	// Called when the platform doesn't support this effect
	protected void NotSupported() {
		enabled = false;
	}
	
	protected void Start() {
		CheckResources();
	}

	// Called when need to create the material used by this effect
	protected Material CheckShaderAndCreateMaterial(Shader shader, Material material) {
		if (shader == null) {
			return null;
		}
		
		if (shader.isSupported && material && material.shader == shader)
			return material;
		
		if (!shader.isSupported) {
			return null;
		}
		else {
			material = new Material(shader);
			material.hideFlags = HideFlags.DontSave;
			if (material)
				return material;
			else 
				return null;
		}
	}
}

二、具体实现(这里可以使用unity自带的屏幕后期处理插件,更容易用,但是效率可能会慢)

1、调整亮度、饱和度、对比度

C#控制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BrightnessSaturationAndContrast : PostEffectsBase
{
    public Shader briSatConShader;
    private Material briSatConMaterial;
    public Material material
    {
        get
        {
            briSatConMaterial = CheckShaderAndCreateMaterial(briSatConShader, briSatConMaterial);
            return briSatConMaterial;
        }
    }
    [Range(0.0f,3.0f)]
    public float brightness = 1.0f;
    [Range(0.0f,3.0f)]
    public float saturation = 1.0f;
    [Range(0.0f,3.0f)]
    public float contrast = 1.0f;

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if(material!=null)
        {
            material.SetFloat("_Brightness", brightness);
            material.SetFloat("_Saturation", saturation);
            material.SetFloat("_Contrast", contrast);

            Graphics.Blit(source, destination, material);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }
}

shader控制

Shader "Unlit/BrightnessSaturationAndContrastShader"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
		_Brightness("Brightness",Float)=1
		_Saturation("Saturation",Float)=1
		_Contrast("Contrast",Float)=1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        Pass
        {
			ZTest Always Cull Off ZwRITE Off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
			half _Brightness;
			half _Saturation;
			half _Contrast;

            v2f vert (appdata_img v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				//亮度
                fixed4 renderTex = tex2D(_MainTex, i.uv);
				fixed3 finalColor = renderTex.rgb*_Brightness;
				//饱和度
				fixed luminance = 0.2125*renderTex.r + 0.7154*renderTex.g + 0.0721*renderTex.b;
				fixed3 luminanceColor = fixed3(luminance, luminance, luminance);
				finalColor = lerp(luminanceColor, finalColor, _Saturation);
				//对比度
				fixed3 avgColor = fixed3(0.5, 0.5, 0.5);
				finalColor = lerp(avgColor, finalColor, _Contrast);
                return fixed4(finalColor,renderTex.a);
            }
            ENDCG
        }
    }
	Fallback Off
}

 

 

 

 

你可能感兴趣的:(图像处理,unity)