//预览图

//原理

一个摄像机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. No module named 'pandas._libs.tslib'

    用pip命令安装: pip install pandas python3的: pip3 install pandas

  2. SpringBoot中配置起动时的数据库初始化角本

    一.简介 我们使用SpringBoot + JPA时,需要程序在启动时执行数据表的初始化或者数据库记录的初始化.一般数据表的初始化可以通过在Spring Boot的application.proper ...

  3. [Python]编码声明:是coding:utf-8还是coding=urf-8呢

    推荐: #!/usr/bin/env python3 # -*- coding: utf-8 -*- 我们知道在Python源码的头文件中要声明编码方式,如果你不只是会用到ascii码,很多人都写得都 ...

  4. WEB前端工程师的职业发展路线图、怎样做WEB前端职业规划

    20151028整理 —————————— 知乎-Web前端的路该怎么走?(2015年发表) 在规模越大的团队,工作划分得越细腻,专注的点就越深,但同时就可能会被限制在某个狭窄点上,成为某个角落的技术 ...

  5. Monodraw for Mac(基于 ASCII 码设计编辑工具)破解版安装

    1.软件简介    Monodrawp 是 macOS 系统上一款专为 Mac 设计的强大的 ASCII 码设计编辑器,纯文本历经几十年而不衰.Monodraw for mac 可以创建基于文本的艺术 ...

  6. 使用EF操作不同数据库(以SQLite为例)

    最近一直在和数据库作对. 从安卓平台上给了我个SQLite数据库,要求程序能够读取不同的文件.由于字段实在太多,不愿意直接使用原来直接读取datatable的方式来做,手动写映射太痛苦...于是想起来 ...

  7. imp-oracle10g数据库dmp导入到11g数据库提示IMP-00058,表或试图不存在

    拿了一份从10g数据库导出的dmp,本机安装的是11.2g的oracle数据库,执行imp导入后提示: IMP-00058: 遇到 ORACLE 错误 942ORA-00942: 表或视图不存在 开始 ...

  8. String的split方法支持正则表达式

    String的split方法支持正则表达式: 1. 正则表达式\s表示匹配任何空白字符 2. +表示匹配一次或多次

  9. React Native库版本升级与降级

    迄今为止React Native获得了超过48K的star,最新版本0.44,已经趋于稳定.(官网地址:https://github.com/facebook/react-native).随着Reac ...

  10. vue项目eslint环境配置与vscode配置eslint

    eslint基础环境搭建 全局安装eslint:npm install eslint -g 项目eslint初始化:eslint --init,按团队或自己的编程风格回答三道题. ? How woul ...