如图所示,可以做出类似音浪滤镜的效果,原理比较简单,是rbg颜色通道的偏移
Shader "Custom/MusicWave" {
Properties{
_BOfY("BofY", Float) = 1.0
_BOfX("BofX", Float) = 1.0
_GOfY("GofY", Float) = 1.0
_GOfX("GofX", Float) = 1.0
_ROfY("RofY", Float) = 1.0
_ROfX("RofX", Float) = 1.0
_se_of("_se_of", Float) = 1.0
}
SubShader{
// Need to disable batching because of the vertex animation
// Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "DisableBatching" = "True" }
Pass{
//Tags{ "LightMode" = "ForwardBase" }
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _BOfY;
fixed _BOfX;
fixed _GOfY;
fixed _GOfX;
fixed _ROfY;
fixed _ROfX;
fixed _se_of;//倍率
struct a2v {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
half2 r_uv:TEXCOORD1;
half2 g_uv:TEXCOORD2;
half2 b_uv:TEXCOORD3;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex );
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.r_uv = v.texcoord - float2(_ROfX, _ROfY)*_se_of;
o.g_uv = v.texcoord - float2(_GOfX, _GOfY)*_se_of;
o.b_uv = v.texcoord - float2(_BOfX, _BOfY)*_se_of;
return o;
}
fixed4 frag(v2f i) : SV_Target{
fixed R = tex2D(_MainTex, i.r_uv).r;
fixed G = tex2D(_MainTex, i.g_uv).g;
fixed B = tex2D(_MainTex, i.b_uv).b;
return fixed4(R, G, B, 1);
}
ENDCG
}
}
FallBack Off
}
后处理脚本
using UnityEngine;
using System.Collections;
public class Music_Wave: PostEffectsBase {
public Shader briSatConShader;
private Material briSatConMaterial;
public Material material {
get {
briSatConMaterial = CheckShaderAndCreateMaterial(briSatConShader, briSatConMaterial);
return briSatConMaterial;
}
}
public Vector2 rOf;
public Vector2 gOf;
public Vector2 bOf;
public float _se_of;
void OnRenderImage(RenderTexture src, RenderTexture dest) {
if (material != null) {
material.SetColor("_Color", color1);
material.SetFloat("_BOfY", bOf.y);
material.SetFloat("_BOfX", bOf.x);
material.SetFloat("_GOfY", gOf.y);
material.SetFloat("_GOfX", gOf.x);
material.SetFloat("_ROfY", rOf.y);
material.SetFloat("_ROfX", rOf.x);
material.SetFloat("_se_of", _se_of);
Graphics.Blit(src, dest, material);
} else {
Graphics.Blit(src, dest);
}
}
}
PostEffectsBase
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class PostEffectsBase : MonoBehaviour {
// Called when start
protected void CheckResources() {
bool isSupported = CheckSupport();
if (isSupported == false) {
NotSupported();
}
}
// Called in CheckResources to check support on this platform
protected bool CheckSupport() {
if (SystemInfo.supportsImageEffects == false || SystemInfo.supportsRenderTextures == false) {
Debug.LogWarning("This platform does not support image effects or render textures.");
return false;
}
return true;
}
// Called when the platform doesn't support this effect
protected void NotSupported() {
enabled = false;
}
protected void Start() {
CheckResources();
}
// Called when need to create the material used by this effect
protected Material CheckShaderAndCreateMaterial(Shader shader, Material material) {
if (shader == null) {
return null;
}
if (shader.isSupported && material && material.shader == shader)
return material;
if (!shader.isSupported) {
return null;
}
else {
material = new Material(shader);
material.hideFlags = HideFlags.DontSave;
if (material)
return material;
else
return null;
}
}
}
调整rbg偏移参数就可以实现改效果,不同参数就是不同颜色