UnityShader 学习笔记 21 屏幕后期之亮度、对比度、饱和度

  • Shader:


Shader "_MyShader/9_PostScreenEffect/0_BriSatCon"

{

    Properties

    {

        _MainTex ("MainTex", 2D) = "white" {}

            //_Brightness ("Brightness", float) = 1

            //_Saturation ("Saturation", float) = 1

            //_Contrast ("Contrast", float) = 1

            //_RadiusSquared ("RadiusSquared", Range(0,1)) = 1

    }

    SubShader

    {

        Pass

        {

            ZTest Always

            ZWrite off

            Cull off



            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag



            #include "UnityCG.cginc"



            struct v2f

            {

                float2 uv : TEXCOORD0;

                float4 vertex : SV_POSITION;

                float y : TEXCOORD1;

            };



            sampler2D _MainTex;

            float4 _MainTex_ST;

            float _Brightness;

            float _Saturation;

            float _Contrast;

            float _RadiusSquared;



            v2f vert (appdata_img v)

            {

                v2f o;

                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                o.y = v.vertex.y;

                return o;

            }



            fixed4 frag (v2f i) : SV_Target

            {

                fixed4 col = tex2D(_MainTex, i.uv);

                fixed4 col2 = col;



                //float leng = (i.uv.x - 0.5) * (i.uv.x - 0.5) + (i.uv.y - 0.5) * (i.uv.y - 0.5);

                float leng = (i.uv.x - 0.5) * (i.uv.x - 0.5) + (i.uv.y / 2 - 0.5) * (i.uv.y / 2 - 0.5) + i.uv.y / 4;

                clip(_RadiusSquared - leng);



                col.rgb *= _Brightness;



                fixed luminance = 0.2125 * col.r + 0.7154 * col.g + 0.0721 * col.b;

                fixed3 luminanceColor = fixed3(luminance,luminance,luminance);

                col.rgb = lerp(luminanceColor,col,_Saturation);



                fixed3 avgColor = fixed3(0.5,0.5,0.5);

                col.rgb = lerp(avgColor,col,_Contrast);



                return col;

            }

            ENDCG

        }

    }

    FallBack off

}




  • C#

基类:



using UnityEngine;

using System.Collections;



[ExecuteInEditMode]

[RequireComponent(typeof(Camera))]

public class ScreenEffectBase : MonoBehaviour {

    protected void Start(){

        CheckResources ();

    }





    protected void CheckResources(){

        bool isSupport = CheckSupport ();

        if (!isSupport)

            this.enabled = false;

    }





    protected bool CheckSupport(){

        if (!SystemInfo.supportsImageEffects || !SystemInfo.supportsRenderTextures)

            return false;

        return true;

    }





    protected Material CheckShaderAndCreateMaterial(Shader shader, Material mat){

        if (shader == null || !shader.isSupported)

            return null;



        if (mat && mat.shader == shader)

            return mat;



        mat = new Material (shader);

        mat.hideFlags = HideFlags.DontSave;

        if (mat)

            return mat;

        else

            return null;

    }





}


BrightnessSaturationContrast:



using UnityEngine;

using System.Collections;



public class BrightnessSaturationContrast : ScreenEffectBase {

    [Range(0,3.0f)]

    public float brightness = 1;



    [Range(0,3.0f)]

    public float saturation = 1;



    [Range(0,3.0f)]

    public float contrast = 1;



    [Range(0,1.0f)]

    public float radiusSquared = 1;



    public Shader briSatConShader;

    private Material briSatConMat;

    public Material material {

        get { 

            briSatConMat = CheckShaderAndCreateMaterial (briSatConShader, briSatConMat);

            return briSatConMat;

        }

    }





    void OnRenderImage(RenderTexture src, RenderTexture dest){

        if (material != null) {

            material.SetFloat ("_Brightness", brightness);

            material.SetFloat ("_Saturation", saturation);

            material.SetFloat ("_Contrast", contrast);

            material.SetFloat ("_RadiusSquared", radiusSquared);



            Graphics.Blit (src, dest, material);

        } else {

            Graphics.Blit (src, dest);

        }

    }







}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值