当前位置: 首页 > news >正文

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

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 updatevoid Start(){}// Update is called once per framevoid 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 depthCull Off ZWrite Off ZTest Always//pass 1Pass {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 1Pass { CGPROGRAM#pragma vertex vert#pragma fragment fragstruct 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}}
}

http://www.lryc.cn/news/285184.html

相关文章:

  • Linux ---- 小玩具
  • 练习题 有奖问答
  • php 文件操作
  • Next-GPT: Any-to-Any Multimodal LLM
  • Angular系列教程之MVC模式和MVVM模式
  • windows虚拟主机和linux虚拟主机的区别有哪些?
  • 微信小程序(七)navigator点击效果
  • 腾讯云服务器价格查询,2024更新
  • 更适合3D项目的UI、事件交互!纯国产数字孪生引擎持续升级中!!!
  • OpenCV-Python(47):支持向量机
  • Centos 8 安装 Elasticsearch
  • Qt5.15.2中加入图片资源
  • 大数据导论(3)---大数据技术
  • Vue-Clipboard3:轻松实现复制到粘贴板功能
  • 【Linux系统编程】进程优先级
  • 华为HCIE课堂笔记第十六章 Qos基本原理
  • 79、avx2 向量指令集优化卷积运算
  • 【AI】人工智能和图像编码(2)
  • 2023 巅峰之作 | AIGC、AGI、GhatGPT、人工智能大语言模型的崛起与挑战
  • com域名注册腾讯云价格
  • mysql从库重新搭建的流程
  • 用户ssh正确密码登陆树莓派镜像均报错Permission denied, please try again.处理方法
  • SpringBoot 统计API接口用时该使用过滤器还是拦截器?
  • Python sleep函数用法:线程睡眠
  • 50-Js控制元素显示隐藏
  • LC213. 打家劫舍 II
  • Django REST Framework入门之序列化器
  • AI对比:ChatGPT与文心一言的异同与未来
  • elasticsearch备份恢复,elasticdump使用
  • 【C++干货铺】C++11新特性——右值引用、移动构造、完美转发