c# 的代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GraphicsBlitTest : MonoBehaviour
{
public Texture2D source;//原紋理
public Material material;//效果材質(zhì)
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就是經(jīng)過材質(zhì)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,第三個參數(shù)是使用哪個pass 0表示是使用第一個文章來源:http://www.zghlxwxcb.cn/news/detail-810914.html
下面是例子對應(yīng)的shader,是一個模糊效果文章來源地址http://www.zghlxwxcb.cn/news/detail-810914.html
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)
{
//原理是取相鄰節(jié)點,最后取平均顏色
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
}
}
}
到了這里,關(guān)于unity 利用Graphics.Blit來制作圖片效果的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!