效果图:
shader 代码
Shader "My/WaveDiffusion"
{
Properties
{
//shader放一张图片,图片需要与要显示模型上材质球上图片一致,不放没有效果,
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
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 = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
float4x4 _VPMatrix4x4_inverse;
float4 _WaveColor;
float4 _WaveCenter;
float _WaveSpeed;
float _WaveInterval;
float _WavePower;
float _WaveColorPower;
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed depth = tex2D(_CameraDepthTexture, i.uv).r;
fixed4 ndc = fixed4(i.uv.x * 2 - 1, i.uv.y * 2 - 1, -depth * 2 + 1, 1);
fixed4 worldPos = mul(_VPMatrix4x4_inverse, ndc);
worldPos /= worldPos.w;
float mask = 1 - Linear01Depth(depth);
float dis = length(worldPos.xyz - _WaveCenter.xyz);
dis -= _Time.y * _WaveSpeed;
dis /= _WaveInterval;
dis = dis - floor(dis);
dis = (pow(dis, _WavePower) + pow(1 - dis, _WavePower * 4)) * 0.5;
dis *= _WaveColorPower;
return dis * mask * _WaveColor + col;
}
ENDCG
}
}
}
c#代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 本脚本挂到摄像机上
/// </summary>
public class WaveDiffusion_Postprocessing : MonoBehaviour
{
public Shader waveDiffusionShader;
public Vector4 waveCenter = Vector4.zero; //扩散中心
public Color waveColor = Color.red; //波纹颜色
public float waveSpeed = 1; //扩散速度
public float waveInterval = 5; //扩散间隔
public float wavePower = 10; //波纹强度
public float waveColorPower = 10; //波纹颜色强度
private Material waveDiffusionMat; //渲染材质
private Matrix4x4 vpMatrix4x4_inverse; //VP逆矩阵
private void Start()
{
//设置相机获取深度
Camera.main.depthTextureMode = DepthTextureMode.Depth;
waveDiffusionMat = new Material(waveDiffusionShader);
//计算VP逆矩阵
Matrix4x4 vpMatrix4x4_inverse = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix;
vpMatrix4x4_inverse = vpMatrix4x4_inverse.inverse;
//设置矩阵
waveDiffusionMat.SetMatrix("_VPMatrix4x4_inverse", vpMatrix4x4_inverse);
}
private void OnRenderImage(RenderTexture src, RenderTexture dst)
{
//设置渲染材质参数
waveDiffusionMat.SetVector("_WaveCenter", waveCenter);
waveDiffusionMat.SetFloat("_WaveSpeed", waveSpeed);
waveDiffusionMat.SetFloat("_WaveInterval", waveInterval);
waveDiffusionMat.SetFloat("_WavePower", wavePower);
waveDiffusionMat.SetFloat("_WaveColorPower", waveColorPower);
waveDiffusionMat.SetColor("_WaveColor", waveColor);
//渲染
Graphics.Blit(src, dst, waveDiffusionMat);
}
}
shader放一张图片,图片需要与要显示模型上材质球上图片一致,不放没有效果
我用的是plane 脚本设置参数如下
根据自己需要调整