#2D
Shader "Unlit/Water"
{
Properties
{
_ColorOpacity("Water Tint (RGB) & Opacity (A)", 2D) = "white" {}
_DistortionNormalMap("Normal Map", 2D) = "bump" {}
_DistortionStrength("Distortion strength", Float) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Transparent" }
LOD 100
// Grab the screen behind the object into _BackgroundTexture
GrabPass
{
"_BackgroundTexture"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
};
struct v2f
{
float2 uv : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float4 grabPos : TEXCOORD2;
float4 vertex : SV_POSITION;
};
sampler2D _BackgroundTexture;
sampler2D _ColorOpacity;
sampler2D _DistortionNormalMap;
float _DistortionStrength;
float4 _DistortionNormalMap_ST;
float4 _BackgroundTexture_TexelSize;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.grabPos = ComputeGrabScreenPos(o.vertex);//获取截取屏幕的采样坐标
#if !UNITY_UV_STARTS_AT_TOP
if(_BackgroundTexture_TexelSize.y < 0)
o.grabPos.y = 1.0 - o.grabPos.y;
#endif
o.uv = v.uv;
o.uv2 = v.uv2;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float2 properuv = _DistortionNormalMap_ST.zw + (i.uv2.xy * _DistortionNormalMap_ST.xy);
float3 normal1 = UnpackNormal(tex2D(_DistortionNormalMap, properuv + _Time.y));
float3 normal2 = UnpackNormal(tex2D(_DistortionNormalMap, (1.0f - properuv) + _Time.y));
float3 norm = (normal1 + normal2) * 0.5f * _DistortionStrength;
half4 coloropa = tex2D(_ColorOpacity, i.uv + norm.xy * 0.1f);
half4 bgcolor = tex2Dproj(_BackgroundTexture, i.grabPos + float4(norm.x*0.5f, norm.y*0.5f, 0, 0));
//col = lerp(col, float4(1, 1, 1, 1), step(0.98f, i.uv.y));
bgcolor.rgb = lerp(bgcolor.rgb, coloropa.rgb, coloropa.a);
bgcolor.rgb *= coloropa.rgb;
return bgcolor;
//return float4(i.uv2.x, i.uv2.y, 0, 1);
}
ENDCG
}
}
}
#3D
Shader "Unlit/Wave"
{
Properties
{
_Color("MainColor", Color) = (1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_WaveMap("WaveMap",2D) = "while"{}
_CubMap("CubMap",Cube) = "_Skybox"{}
_WaveSpeedX("WaveSpeedX",Range(-0.1,0.1)) = 0.1
_WaveSpeedY("WaveSpeedY",Range(-0.1,0.1)) = 0.1
_Distortion("Distortion",Range(0,100)) = 10
}
SubShader
{
Tags {"Queue" = "Transparent" "RenderType"="Opaque" }
//GrabPass{"_RefractionTex"}
Pass
{
CGPROGRAM
float4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _WaveMap;
float4 _WaveMap_ST;
samplerCUBE _CubMap;
fixed _WaveSpeedX;
fixed _WaveSpeedY;
float _Distortion;
//sampler2D _RefractionTex;
//float4 _RefractionTex_ST;
//float4 _RefractionTex_TexeSize;
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct a2v
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float4 normal :NORMAL;
float4 tangent :TANGENT;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 scrPos : TEXCOORD0;
float4 uv : TEXCOORD1;
float4 TtoW0 :TEXCOORD2;
float4 TtoW1 :TEXCOORD3;
float4 TtoW2 : TEXCOORD4;
SHADOW_COORDS(5)
};
v2f vert (a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.scrPos = ComputeGrabScreenPos(o.pos);
o.uv.xy = TRANSFORM_TEX(v.texcoord,_MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord,_WaveMap);
float3 worldPos = mul(unity_ObjectToWorld,v.vertex);
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
float3 worldBinormal = cross(worldNormal,worldTangent)*v.tangent.w;
o.TtoW0 = float4 (worldTangent.x,worldBinormal.x,worldNormal.x,worldPos.x);
o.TtoW1 = float4 (worldTangent.y,worldBinormal.y,worldNormal.y,worldPos.y);
o.TtoW2 = float4 (worldTangent.z,worldBinormal.z,worldNormal.z,worldPos.z);
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3 worldPos = float3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w);
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
float2 speed = _Time.y*float2(_WaveSpeedX,_WaveSpeedY);
fixed3 bump1 = UnpackNormal(tex2D(_WaveMap,i.uv.zw+speed)).rgb;
fixed3 bump2 = UnpackNormal(tex2D(_WaveMap,i.uv.zw-speed)).rgb;
fixed3 bump = normalize(bump1+bump2);
float2 offset = bump.xy*_Distortion;//*_RefractionTex_TexeSize.xy;
i.scrPos.xy = offset*i.scrPos.z + i.scrPos.xy;
//fixed3 refrCol = tex2D(_RefractionTex,i.scrPos.xy/i.scrPos.w).rgb;
bump = normalize(half3(dot(i.TtoW0.xyz,bump),dot(i.TtoW1.xyz,bump),dot(i.TtoW2.xyz,bump)));
fixed4 texColor = tex2D(_MainTex,i.uv.xy+speed);
fixed3 refDir = reflect(-viewDir,bump);
fixed3 refCol = texCUBE(_CubMap,refDir).rgb * texColor.rgb * _Color.rgb;
fixed fresnel = pow(1-saturate(dot(viewDir,bump)),4);
fixed shadow = SHADOW_ATTENUATION(i);
fixed3 finalColor = (fresnel+refCol*(1-fresnel))*shadow;
return fixed4(finalColor,1);
}
ENDCG
}
}
FallBack "Diffuse"
}