当前位置:网站首页>简单的折射效果
简单的折射效果
2022-06-24 06:43:00 【Kenight_】
通过 GrabPass 实现
Shader "Unlit/Glass"
{
Properties
{
_BumpAmt ("Distortion", Range (0,100)) = 10
_TintAmt ("Tint Amount", Range(0,1)) = 0.1
_BlurAmt ("Blur Amount", Range (0,1)) = 0.1
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
}
SubShader
{
Tags { "Queue" = "Transparent" } // 重要:使用透明队列保障其他物体已绘制在屏幕上
GrabPass { "_GrabBlurTexture" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 grabPos : TEXCOORD1;
float4 vertex : SV_POSITION;
};
float _BumpAmt;
float _TintAmt;
float _BlurAmt;
sampler2D _MainTex, _BumpMap;
float4 _MainTex_ST, _BumpMap_ST;
sampler2D _GrabBlurTexture;
float4 _GrabBlurTexture_TexelSize; // texture size, x:1.0/width y:1.0/height z:width w:height
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.uv, _BumpMap);
o.grabPos = ComputeGrabScreenPos(o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// 从 normalmap 中采样出凹凸值
float2 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw)).xy;
// 偏移量(*_TexelSize.xy 的意义是偏移值按抓取的纹理的单位像素进行缩放)
float2 offset = bump * _BumpAmt * _GrabBlurTexture_TexelSize.xy;
i.grabPos.xy = offset + i.grabPos.xy;
fixed4 col = tex2D(_GrabBlurTexture, i.grabPos.xy / i.grabPos.w); // 除以 w 后得到屏幕空间采样坐标
// blur (这是一个比较垃圾的模糊效果)
float blur = _BlurAmt / 100;
col += tex2D(_GrabBlurTexture, grabuv + float2(-blur, 0));
col += tex2D(_GrabBlurTexture, grabuv + float2(blur, 0));
col += tex2D(_GrabBlurTexture, grabuv + float2(0, blur));
col += tex2D(_GrabBlurTexture, grabuv + float2(0, -blur));
col = col / 5;
fixed4 tint = tex2D(_MainTex, i.uv.xy);
col = lerp (col, tint, _TintAmt);
return col;
}
ENDCG
}
}
}通过 Command Buffers 实现
// 使用 command buffer 来生成 RT
public class ComdBuf : MonoBehaviour
{
// 在摄像机渲染之前执行
void OnWillRenderObject()
{
var cam = Camera.current;
CommandBuffer buff = new CommandBuffer();
// 申请 RT
int rtID = Shader.PropertyToID("_GrabBlurTexture");
buff.GetTemporaryRT(rtID, -1, -1, 0, FilterMode.Bilinear); // -1 for camera pixel width or height
// blit 方法接受 RT 的标识符 RenderTargetIdentifier (即 rtID)
buff.Blit(BuiltinRenderTextureType.CurrentActive, rtID); // 将当前渲染 RT 复制给 rtID
// 通过该命令使在 shader 中可通过 sampler2D _GrabBlurTexture 取得该 RT
buff.SetGlobalTexture(rtID, rtID); // 第一个参数是 shader 中的变量名,第二个是名称所标识的 RT
// 执行时机
cam.AddCommandBuffer(CameraEvent.AfterSkybox, buff);
}
}接着可使用上面的 Shader 并去掉 GrabPass { “_GrabBlurTexture” } 即可达到一样的效果
边栏推荐
- 2.1.1 QML grammar foundation I
- Global and Chinese market of digital fryer 2022-2028: Research Report on technology, participants, trends, market size and share
- How to delete / select an input method on your computer
- MFC multithreaded semaphore csemaphore critical area and mutually exclusive events
- 使用SystemParametersInfo访问用户界面设置
- [WordPress website] 6 Article content copy prevention
- [WordPress website] 5 Set code highlight
- Accessing user interface settings using systemparametersinfo
- jarvisoj_level2
- Face pincher: a hot meta universe stylist
猜你喜欢

PIP install XXX on the terminal but no module named XXX on pycharm

两个链表的第一个公共节点_链表中环的入口(剑指offer)

Description of module data serial number positioning area code positioning refers to GBK code

Tutorial on simple use of Modbus to BACnet gateway
![[GUET-CTF2019]zips](/img/79/22ff5d4a3cdc3fa9e0957ccc9bad4b.png)
[GUET-CTF2019]zips

The fund management of London gold is more important than others

利用微搭低代码实现级联选择
![buuctf misc [UTCTF2020]docx](/img/e4/e160f704d6aa754e85056840e14bd2.png)
buuctf misc [UTCTF2020]docx

基因检测,如何帮助患者对抗疾病?

How to delete / select an input method on your computer
随机推荐
[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design
Event related | reveal how Ti-One's support ability for large-scale events is developed
The latest crawler tutorial in 2021: video demonstration of web crawling
[OGeek2019]babyrop
Étalonnage de la caméra (objectif et principe d'étalonnage)
PCL 计算多边形的面积
Muxvlan principle, Huawei MUX VLAN experimental configuration
L2TP connection failure guide in VPN
Common coding and encryption in penetration testing
Buuctf misc grab from the doll
【Vulhub靶场】】zabbix-SQL注入(CVE-2016-10134)漏洞复现
前缀和专题训练
When MFC uses the console, the project path cannot have spaces or Chinese, otherwise an error will be reported. Lnk1342 fails to save the backup copy of the binary file to be edited, etc
Virtual machine security disaster recovery construction
[WordPress website] 5 Set code highlight
MFC multithreaded semaphore csemaphore critical area and mutually exclusive events
What should I pay attention to after the live broadcast system source code is set up?
Session & cookie details
Global and Chinese market of basketball uniforms 2022-2028: Research Report on technology, participants, trends, market size and share
get_ started_ 3dsctf_ two thousand and sixteen