当前位置:网站首页>简单的折射效果
简单的折射效果
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” } 即可达到一样的效果
边栏推荐
- Reconfiguration of nebula integration testing framework based on BDD theory (Part 2)
- 现货黄金有哪些眩人的小技术?
- Camera calibration (calibration purpose and principle)
- bjdctf_2020_babystack
- [frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog
- Tidb operator source code reading (IV) control cycle of components
- JVM debugging tool -jmap
- Tutorial on simple use of Modbus to BACnet gateway
- Event related | reveal how Ti-One's support ability for large-scale events is developed
- [tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design
猜你喜欢

Win10 build webservice

Tutorial on simple use of Modbus to BACnet gateway

Maxcompute remote connection, uploading and downloading data files
![[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design](/img/74/f615191715a9ac58a8546f8d1e8f8d.png)
[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design

只显示两行,超出部分省略号显示

How to connect the Bluetooth headset to the computer and how to connect the win10 computer to the Bluetooth headset

软件性能测试分析与调优实践之路-JMeter对RPC服务的性能压测分析与调优-手稿节选

How to open the soft keyboard in the computer, and how to open the soft keyboard in win10

伦敦金的资金管理比其他都重要
╯︵ ┻━┻](/img/26/6986a8ae6c00eb2431a082dc0ff978.png)
[DDCTF2018](╯°□°)╯︵ ┻━┻
随机推荐
jarvisoj_level2
[机缘参悟-29]:鬼谷子-内揵篇-与上司交往的五种层次
简单使用Modbus转BACnet网关教程
Tidb operator source code reading (IV) control cycle of components
[image fusion] image fusion based on pseudo Wigner distribution (PWD) with matlab code
(cve-2020-11978) command injection vulnerability recurrence in airflow DAG [vulhub range]
[image fusion] image fusion based on NSST and PCNN with matlab code
[mrctf2020] thousand layer routine
相機標定(標定目的、原理)
[frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog
Huawei experimental topology set, learning methods are attached at the end of the article!
Global and Chinese market of water massage column 2022-2028: Research Report on technology, participants, trends, market size and share
[wustctf2020] climb
Learning to use BACnet gateway of building control system is not so difficult
[Proteus] Arduino uno + ds1307+lcd1602 time display
MaxCompute远程连接,上传、下载数据文件操作
[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design
[signal recognition] signal modulation classification based on deep learning CNN with matlab code
How to turn on win11 notebook power saving mode? How to open win11 computer power saving mode
现货黄金有哪些值得借鉴的心态