unity 利用Graphics.Blit来制作图片效果

文章介绍了如何在C#的Unity中使用Graphics.Blit方法配合自定义Shader实现图像模糊效果,包括对Texture2D和RenderTexture的处理,以及DualFilterBlurshader的使用方法。
摘要由CSDN通过智能技术生成

c# 的代码

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

public class GraphicsBlitTest : MonoBehaviour
{
    public Texture2D source;//原纹理
    public Material material;//效果材质

    public RawImage rawImage;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            UseBlit();
        }
    }

    void UseBlit(){
        material.SetFloat("_HalfPixelX", 0.005f);
		material.SetFloat("_HalfPixelY", 0.008f);
        RenderTexture renderTexture = RenderTexture.GetTemporary(source.width,source.height,0);
        RenderTexture renderTexture1 = RenderTexture.GetTemporary(source.width,source.height,0);
        Graphics.Blit(source,renderTexture,material,0);//renderTexture就是经过材质shader之后的效果
        // Graphics.Blit(renderTexture,renderTexture1,material,1);
        // RenderTexture.active = renderTexture;
        // Texture2D texture2D = new Texture2D(renderTexture.width,renderTexture.height,TextureFormat.ARGB32,false);
        // texture2D.ReadPixels(new Rect(0,0,renderTexture.width,renderTexture.height),0,0);
        // texture2D.Apply(false);
        rawImage.texture = renderTexture;
    }
}

source可以是当前相机的RenderTexture也可以是准备好的一张图,然后利用material提供的效果将效果输出到renderTexture,第三个参数是使用哪个pass 0表示是使用第一个

下面是例子对应的shader,是一个模糊效果

Shader "JJ/ImageEffect/DualFilterBlur"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

		//pass 1
		Pass 
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
			v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

			sampler2D _MainTex;
			float _HalfPixelX;
			float _HalfPixelY;

			fixed4 Downsample(float2 uv)
			{
				//原理是取相邻节点,最后取平均颜色
				float2 UV;
				fixed4 sum;
				sum = tex2D(_MainTex, uv) * 4.0;
				UV = float2(uv.x - _HalfPixelX, uv.y - _HalfPixelY);
				sum += tex2D(_MainTex, UV);
				UV = float2(uv.x + _HalfPixelX, uv.y + _HalfPixelY);
				sum += tex2D(_MainTex, UV);
				UV = float2(uv.x + _HalfPixelX, uv.y - _HalfPixelY);
				sum += tex2D(_MainTex, UV);
				UV = float2(uv.x - _HalfPixelX, uv.y + _HalfPixelY);
				sum += tex2D(_MainTex, UV);
				return sum * 0.125;
			}

			fixed4 frag (v2f i) : SV_Target
			{
				return Downsample(i.uv);
			}
			ENDCG
		}
		
		//pass 1
		Pass 
		{ 
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
			v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

			sampler2D _MainTex;
			float _HalfPixelX;
			float _HalfPixelY;
			float _MaskPower;
			fixed4 Upsample(float2 uv) 
			{
				float2 UV;
				fixed4 sum;
				UV = float2(uv.x - _HalfPixelX * 2.0, uv.y);
				sum = tex2D(_MainTex, UV);
				UV = float2(uv.x - _HalfPixelX, uv.y + _HalfPixelY);
				sum += tex2D(_MainTex, UV) * 2.0;
				UV = float2(uv.x, uv.y + _HalfPixelY * 2.0);
				sum += tex2D(_MainTex, UV);
				UV = float2(uv.x + _HalfPixelX, uv.y + _HalfPixelY);
				sum += tex2D(_MainTex, UV) * 2.0;
				UV = float2(uv.x + _HalfPixelX * 2.0, uv.y);
				sum += tex2D(_MainTex, UV);
				UV = float2(uv.x + _HalfPixelX, uv.y - _HalfPixelY);
				sum += tex2D(_MainTex, UV) * 2.0;
				UV = float2(uv.x, uv.y - _HalfPixelY * 2.0);
				sum += tex2D(_MainTex, UV);
				UV = float2(uv.x - _HalfPixelX, uv.y - _HalfPixelY);
				sum += tex2D(_MainTex, UV) * 2.0;
				sum /= 12.0;
				sum.rgb *= (1-_MaskPower);
				sum.a = 1;
				return sum;
			}
			fixed4 frag(v2f i) : SV_Target 
			{
				return Upsample(i.uv);
			}
			ENDCG
		}
    }
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值