Unity Shaders——屏幕灰度效果(Screen Effect)

Posted 御雪妃舞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity Shaders——屏幕灰度效果(Screen Effect)相关的知识,希望对你有一定的参考价值。

        以前项目中在后期处理中经常用到屏幕特效,为了让画面更加梦幻或者卡通的效果,就跟添加滤镜一样,那时候只是直接从网上找的或者别人的拿过来用了,并没有深入的理解过,今天看《Unity Shaders and Effects CookBook》刚好涉及到了,就记录下来,供以后学习参考。


首先说下原理,屏幕特效的脚本必须挂在MainCamera上,渲染相机的效果,实际上你在scene中看到的东西没有变化。

如图所示:


然后就是屏幕特效需要shader和脚本共同作用,首先我们看下这个灰度屏幕特效所需要的Shader脚本:

Shader "MyShaders/ZTestImageEffect" 

	Properties
	
		_MainTex("Base (RGB)", 2D) = "white" 
		_LuminosityAmount("Brightness Amount", Range(0.0, 1)) = 1.0
	

	SubShader
	
		Pass
		
			CGPROGRAM
			#pragma vertex vert_img
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest
			#include "UnityCG.cginc"

			uniform sampler2D _MainTex;
			fixed _LuminosityAmount;

			fixed4 frag(v2f_img i) : COLOR
			
				//Get the colors from the RenderTexture and the uv's
				//from the v2f_img struct
				fixed4 renderTex = tex2D(_MainTex, i.uv);

				//Apply the Luminosity values to our render texture
				float luminosity = 0.299 * renderTex.r + 0.587 * renderTex.g + 0.114 * renderTex.b;
				fixed4 finalColor = lerp(renderTex, luminosity, _LuminosityAmount);


				return finalColor;
			

		ENDCG
		
	
	FallBack off



然后C#脚本:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class TestImageEffectGrey : MonoBehaviour

    #region Variables
    public Shader curShader;
    public float grayScaleAmount = 1.0f;
    private Material curMaterial;
    #endregion

    #region Properties
    Material material
    
        get
        
            if (curMaterial == null)
            
                curMaterial = new Material(curShader);
                curMaterial.hideFlags = HideFlags.HideAndDontSave;
            
            return curMaterial;
        
    
    #endregion

    void Start()
    
        if (!SystemInfo.supportsImageEffects)
        
            enabled = false;
            return;
        

        if (!curShader && !curShader.isSupported)
        
            enabled = false;
        
    

    void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
    
        if (curShader != null)
        
            material.SetFloat("_LuminosityAmount", grayScaleAmount);

            Graphics.Blit(sourceTexture, destTexture, material);
        
        else
        
            Graphics.Blit(sourceTexture, destTexture);
        
    

    void Update()
    
        grayScaleAmount = Mathf.Clamp(grayScaleAmount, 0.0f, 1.0f);
    

    void OnDisable()
    
        if (curMaterial)
        
            DestroyImmediate(curMaterial);
        
    

[ExecuteInEditMode]表示在编辑器模式下运行,实时变化灰度参数。这个就是脚本里设置Shader中的参数值,Shader中根据这个参数值把灰度效果渲染到RenderTexture上。




以上是关于Unity Shaders——屏幕灰度效果(Screen Effect)的主要内容,如果未能解决你的问题,请参考以下文章

Unity Shaders——屏幕特效混合模式(Blend mode with screen effects)

Unity Shaders and Effects Cookbook (2-4) 压缩和混合纹理贴图:使用灰度图存储插值信息

Unity Shaders and Effects Cookbook (6-2) 透明裁剪着色器

Unity Shaders and Effects Cookbook (D-1) 设置 ZTest 来实现遮挡半透效果

Unity Shaders and Effects Cookbook (3-6) 创建各向异性高光类型(Anisotropic) 模拟金属拉丝效果

Unity Shaders学习笔记之表面着色器