UnityShader学习笔记:Caustic水纹焦散
本文教程原地址:https://www.alanzucconi.com/2019/09/13/believable-caustics-reflections/
作者:Alan Zucconi
文章为自己学习记录使用,不含任何商业目的
贴图
开放版权的纹理贴图取自OpenGameArt
代码部分
Shader "Custom/Cautics"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
[Header(Caustics)]
_CausticsTex("Caustics(RGB)1",2D) = "white"{}
// Tiling X, Tiling Y, Offset X, Offset Y
_Caustics1_ST("Caustics 1 ST", Vector) = (1,1,0,0)
_Caustics2_ST("Caustics 2 ST", Vector) = (1,1,0,0)
// Speed X, Speed Y
_Caustics1_Speed("Caustics 1 Speed", Vector) = (1,1,0,0)
_Caustics2_Speed("Caustics 2 Speed", Vector) = (1,1,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque"}
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
// sampler2D _CausticsTex;
float4 _Caustics1_Speed;
float4 _Caustics2_Speed;
sampler2D _CausticsTex;
float4 _Caustics2_ST;
float4 _Caustics1_ST;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
// Caustics Sampling
fixed2 uv1 = IN.uv_MainTex * _Caustics1_ST.xy + _Caustics1_ST.zw;
fixed2 uv2 = IN.uv_MainTex * _Caustics2_ST.xy + _Caustics2_ST.zw;
//move
uv1 += _Caustics1_Speed * _Time.y;
fixed3 caustics1 = tex2D(_CausticsTex, uv1).rgb;
uv2 += _Caustics2_Speed * _Time.y;
fixed3 caustics2 = tex2D(_CausticsTex, uv2).rgb;
//Blend
o.Albedo.rgb += min(caustics2, caustics1);
//Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
关于聚散色彩分离的疑惑
RGB Split
There is one final trick that we can use to make these caustics reflections look good. Different wavelengths of light diffract differently when passing through a medium. This means that light can “split” into different colours, when it moves through water.
To simulate this effect, we can split each caustics sample into three. One for each colour channel. By sampling the red, green and blue channels with a slight offset, we can cause the colours to misalign.
Let’s start by adding a _SplitRGB property, which indicates how strong the split effect will be:
// Caustics UV
fixed2 uv = IN.uv_MainTex * _Caustics_ST.xy + _Caustics_ST.zw;
uv += _CausticsSpeed * _Time.y;
// RGB split
fixed s = _SplitRGB;
fixed r = tex2D(tex, uv + fixed2(+s, +s)).r;
fixed g = tex2D(tex, uv + fixed2(+s, -s)).g;
fixed b = tex2D(tex, uv + fixed2(-s, -s)).b;
fixed3 caustics = fixed3(r, g, b);
这段代码不知道如何实现,诚心求教!
鱼群动画
代码来自:https://blog.csdn.net/jennyhigh/article/details/83027382
视频教程:(需翻墙)
https://www.youtube.com/watch?v=eMpI1eCsIyM&t=765s