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

发布时间:2024年01月20日

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
		}
    }
}

文章来源:https://blog.csdn.net/wnagleiming/article/details/109841679
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。