Unity3D Shader 空气扭动效果
//预览图

//原理
一个摄像机CullingMask设置只可见"Distortion"的Layer(需要自己手动加),输入到一张RenderTexture,其实就是用于确定哪里要扭曲。
另外一个摄像机CullingMask设置成对除了"Distortion"的Layer可见,并挂上后期效果脚本。
//Shader代码
Shader "Hidden/Distortion"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Noise ("Noise", 2D) = "black" {}
_DistortionMask ("Distortion Mask", 2D) = "black" {}
_DistortionStrength ("Distortion Strength", Float) = 0.1
_DistortTimeFactor ("_Distort Time Factor", Float) = 0.1
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always 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 = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
} sampler2D _MainTex; sampler2D _DistortionMask;
sampler2D _Noise;
float _DistortionStrength;
float _DistortTimeFactor; fixed4 frag (v2f i) : SV_Target
{
//wave strength
fixed strength = tex2D(_DistortionMask, i.uv).r; //noise
fixed2 noi = tex2D(_Noise, i.uv-fixed2(,_Time.y * _DistortTimeFactor)); //uv offset
fixed2 uvOffset = strength*noi.xy*_DistortionStrength; fixed4 col = tex2D(_MainTex, i.uv.xy+uvOffset.xy); return col;
}
ENDCG
}
}
}
//C#代码
using UnityEngine;
using System.Collections; [ExecuteInEditMode]
public class DistortionPostEffect : MonoBehaviour
{
private Camera distortionCam;
public Material DistortionMat;
private RenderTexture rt;
void Awake()
{
Transform go = transform.Find("Distortion");
if (null == go) {
go = (new GameObject ("Distortion")).transform;
}
go.transform.parent = transform;
go.transform.localPosition = Vector3.zero;
go.transform.rotation = Quaternion.identity; distortionCam = go.GetComponent<Camera> ();
if (null == distortionCam) {
distortionCam = go.gameObject.AddComponent<Camera> ();
}
distortionCam.clearFlags = CameraClearFlags.Color;
distortionCam.backgroundColor = Color.black;
//rt = RenderTexture.GetTemporary (Screen.width / 2, Screen.height / 2);
rt = RenderTexture.GetTemporary ( , );
rt.wrapMode = TextureWrapMode.Repeat;
distortionCam.targetTexture = rt;
distortionCam.cullingMask = LayerMask.GetMask ("Distortion"); gameObject.GetComponent<Camera> ().cullingMask &= (~distortionCam.cullingMask);
}
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } void OnRenderImage(RenderTexture src,RenderTexture dest)
{
DistortionMat.SetTexture ("_DistortionMask", rt);
Graphics.Blit (src, dest, DistortionMat);
} }
Unity3D Shader 空气扭动效果的更多相关文章
- Unity3D Shader 马赛克后期效果
//效果图 //Shader代码 Shader "Hidden/Mosaic" { Properties { _MainTex ("Texture", 2D) ...
- Unity3D Shader 模型流光效果
Shader "Custom/FlowColor" { Properties { _MainTex ("Base (RGB)", 2D) = "whi ...
- unity3D 涂涂乐使用shader实现上色效果
unity3D 涂涂乐使用shader实现上色效果 之前我博文里面发过一个简单的通过截图方式来实现的模型上色方法,但是那个方法不合适商用,因为你需要对的很准确才可以把贴图完美截取下来,只要你手抖了一下 ...
- 【译】Unity3D Shader 新手教程(1/6)
本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 刚开始接触Unity3D Shader编程时,你会发现有关shader的文档相当散,这也造成初学者对Unity3D ...
- Unity3D shader简介
Unity3D shader简介 可以肯定的说Unity3D使得很多开发者开发游戏更容易.毫无疑问,shader(着色器)编码,仍有很长的路要走.shader是一个专门运行在GPU的程序,经常被神秘包 ...
- 【浅墨Unity3D Shader编程】之一 夏威夷篇:游戏场景的创建 & 第一个Shader的书写
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨) ...
- 转 猫都能学会的Unity3D Shader入门指南(二)
猫都能学会的Unity3D Shader入门指南(二) 关于本系列 这是Unity3D Shader入门指南系列的第二篇,本系列面向的对象是新接触Shader开发的Unity3D使用者,因为我本身自己 ...
- Unity3D Shader入门指南(二)
关于本系列 这是Unity3D Shader入门指南系列的第二篇,本系列面向的对象是新接触Shader开发的Unity3D使用者,因为我本身自己也是Shader初学者,因此可能会存在错误或者疏漏,如果 ...
- 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&颜色、光照与材质
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40955607 作者:毛星云(浅墨) ...
随机推荐
- C++中extern “C”含义及extern、static关键字浅析
https://blog.csdn.net/bzhxuexi/article/details/31782445 1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C ...
- Java -Dfile.encoding=UTF-8 干掉乱码
遭遇乱码问题的来龙去脉 这两天写了一个 Java 程序来玩,结果又遭遇了以前遇到过很多次的乱码问题,具体描述一下:在 Mac 系统里面,常用的 Java 程序启动方式有如下几种:1.通过 eclips ...
- 系统学习NLP(二十一)--SWEM
https://blog.csdn.net/App_12062011/article/details/88655589 这篇发表在 ACL 2018 上的论文来自于杜克大学 Lawrence Cari ...
- Hadoop3集群搭建之——hbase安装及简单操作
折腾了这么久,hbase终于装好了 ------------------------- 上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hado ...
- hive在命令行消除进度等错误信息
大家在使用shell脚本调用hive命令的时候,发现hive的中间过程竟然打印到错误输出流里面,这样在查看错误日志的时候,需要过滤这些没用的信息,那么可以使用如下的配置参数. set hive.ses ...
- python 网络工具 scapy 介绍
作者介绍,这是个万能的网络工具,除了可以查看 TCP/IP 各层的报文,还可以发送报文.可以说是一个万能工具,作者嚣张的说, “it can replace hping, 85% of nmap, a ...
- Memcache及telnent命令具体解释
1.启动Memcache 经常使用參数 memcached 1.4.3 -p <num> 设置port号(默认不设置为: 11211) -U <num> U ...
- PL/SQL学习笔记之函数
一:函数 函数与过程的最大不同就是,函数有返回值.适用于需要返回结果的场景. 二:创建函数 CREATE [OR REPLACE] FUNCTION function_name [(parameter ...
- PL/SQL学习笔记之循环语句
一:基本循环 LOOP 循环体: 退出循环: )IF condition THEN exit; END IF; ) exit WHEN condition; END LOOP; 二:WHILE循环 W ...
- 微信公众号基础02_获取accessToken和用户信息
上一篇分享了搭建微信公众号server,本文分享一下假设获取access_Token和用户信息.工具还是新浪云SAE 1.获取access_Token 相见开发文档:https://mp.weixin ...