//预览图

//原理

一个摄像机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 空气扭动效果的更多相关文章

  1. Unity3D Shader 马赛克后期效果

    //效果图 //Shader代码 Shader "Hidden/Mosaic" { Properties { _MainTex ("Texture", 2D) ...

  2. Unity3D Shader 模型流光效果

    Shader "Custom/FlowColor" { Properties { _MainTex ("Base (RGB)", 2D) = "whi ...

  3. unity3D 涂涂乐使用shader实现上色效果

    unity3D 涂涂乐使用shader实现上色效果 之前我博文里面发过一个简单的通过截图方式来实现的模型上色方法,但是那个方法不合适商用,因为你需要对的很准确才可以把贴图完美截取下来,只要你手抖了一下 ...

  4. 【译】Unity3D Shader 新手教程(1/6)

    本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 刚开始接触Unity3D Shader编程时,你会发现有关shader的文档相当散,这也造成初学者对Unity3D ...

  5. Unity3D shader简介

    Unity3D shader简介 可以肯定的说Unity3D使得很多开发者开发游戏更容易.毫无疑问,shader(着色器)编码,仍有很长的路要走.shader是一个专门运行在GPU的程序,经常被神秘包 ...

  6. 【浅墨Unity3D Shader编程】之一 夏威夷篇:游戏场景的创建 & 第一个Shader的书写

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨)  ...

  7. 转 猫都能学会的Unity3D Shader入门指南(二)

    猫都能学会的Unity3D Shader入门指南(二) 关于本系列 这是Unity3D Shader入门指南系列的第二篇,本系列面向的对象是新接触Shader开发的Unity3D使用者,因为我本身自己 ...

  8. Unity3D Shader入门指南(二)

    关于本系列 这是Unity3D Shader入门指南系列的第二篇,本系列面向的对象是新接触Shader开发的Unity3D使用者,因为我本身自己也是Shader初学者,因此可能会存在错误或者疏漏,如果 ...

  9. 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&amp;颜色、光照与材质

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40955607 作者:毛星云(浅墨)  ...

随机推荐

  1. [Sqoop]将Hive数据表导出到Mysql

    业务背景 mysql表YHD_CATEG_PRIOR的结构例如以下: -- Table "YHD_CATEG_PRIOR" DDL CREATE TABLE `YHD_CATEG_ ...

  2. Android 创建单独的服务运行在后台(无界面)

    转自:https://blog.csdn.net/a704225995/article/details/56481934 今天项目有个需求是,开启一个服务单独运行在后台,而且还不能有界面,在度娘搜索了 ...

  3. 理解TCP之Keepalive

    理解HTTP之keep-alive 在前面一篇文章中讲了TCP的keepalive,这篇文章再讲讲HTTP层面keep-alive.两种keepalive在拼写上面就是不一样的,只是发音一样,于是乎大 ...

  4. Android APK 打包过程 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. RPC框架-hessian学习

    先说说hessian有什么优点和缺点 一.优点: 比 Java 原生的对象序列化/反序列化速度更快, 序列化出来以后的数据更小.序列化协议跟应用层协议无关, 可以将 Hessian 序列化以后的数据放 ...

  6. 【SqlServer】SqlServer中的更新锁(UPDLOCK)

    UPDLOCK.UPDLOCK 的优点是允许您读取数据(不阻塞其它事务)并在以后更新数据,同时确保自从上次读取数据后数据没有被更改.当我们用UPDLOCK来读取记录时可以对取到的记录加上更新锁,从而加 ...

  7. 编程调节Win7/Win8系统音量的一种方法

    不得不说, 自Win7(好像是吧), Windows的音量调节功能比以前更人性化了....      但编程接口却变得更加复杂了............. 还要用到IAudioEndpointVolu ...

  8. jQuery的deferred对象详解(转)

    jQuery的开发速度很快,几乎每半年一个大版本,每两个月一个小版本. 每个版本都会引入一些新功能.今天我想介绍的,就是从jQuery 1.5.0版本开始引入的一个新功能----deferred对象. ...

  9. struts2:struts.properties配置文件介绍及常量加载顺序

    1. 背景 struts2框架中有两个核心配置文件,其中struts.xml文件主要负责管理应用中的action映射,以及该action包含的result定义等.除此之外,struts2框架还包括一个 ...

  10. [k8s]jenkins部署在k8s集群

    $ cat jenkins-pvc.yaml kind: PersistentVolumeClaim apiVersion: v1 metadata: name: jenkins-pvc spec: ...