1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| Shader "Custom/28" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _WaterShallowColor("WaterShallowColor",Color) = (1,1,1,1) _WaterDeepColor("WaterDeepColor",Color) = (1,1,1,1) _DeepPower("Deep",Range(0,100)) = 2 _TranAmount("TranAmount",Range(0,100)) = 0.5 _Normal("Normal",2D) = "bump" {} _WaterSpeed("WaterSpeed",float) = 2 _Refract("Refract",Float) = 0.2 _Metallic ("Specular", Float) = 0.0 _Glossiness ("Gloss", Float) = 0.5 _SpecularColor("高光颜色",Color) = (1,1,1,1) _WaveTex("WaveTex",2D) = "white"{} _NoiseTex("NoiseTex",2D) = "white"{} _WaveSpeed("WaveSpeed",float) = 1 _WaveRange("WaveRange",float) = 0.5 _WaveRangeA("WaveRangeA",float) = 1 _WaveDelta("WaveDelta",float) = 0.5 _Distortion("Distortion",float) = 0.5 _Cubemap("Cubemap",Cube) = "_Skybox"{} _FresnelScale("Fresnel",Range(0,1)) = 0.5
} SubShader { Tags { "RenderType"="Transparent" "Queue" = "Transparent"} LOD 200 GrabPass{"GrabPass"} ZWrite Off CGPROGRAM #pragma surface surf WaterLight vertex:vert alpha noshadow
#pragma target 3.0
sampler2D _MainTex; sampler2D _CameraDepthTexture; sampler2D _Normal; sampler2D _WaveTex; sampler2D _NoiseTex; sampler2D GrabPass; float4 GrabPass_TexelSize; samplerCUBE _Cubemap;
struct Input { float2 uv_MainTex; float4 proj; float2 uv_Normal; float2 uv_WaveTex; float2 uv_NoiseTex; float3 worldRefl; float3 viewDir; float3 worldNormal; INTERNAL_DATA };
half _Glossiness; half _Metallic; fixed4 _Color; fixed4 _WaterShallowColor; fixed4 _WaterDeepColor; half _DeepPower; half _TranAmount; float _WaterSpeed; float _Refract; fixed4 _SpecularColor;
float _WaveSpeed; float _WaveRange; float _WaveRangeA; float _WaveDelta; float _Distortion; float _FresnelScale;
fixed4 LightingWaterLight(SurfaceOutput s,fixed3 lightDir,half3 viewDir,fixed atten) { float diffuse = dot(normalize(lightDir),s.Normal) * 0.5 + 0.5; half3 halfView = normalize(lightDir + viewDir); float nh = max(0,dot(halfView,s.Normal)); float spec = pow(nh,s.Specular * 128) * s.Gloss; fixed4 color; color.rgb = (s.Albedo * _LightColor0.rgb * diffuse + _SpecularColor.rgb * spec * _LightColor0.rgb) * atten; color.a = s.Alpha + spec * _SpecularColor.a; return color; }
UNITY_INSTANCING_BUFFER_START(Props) UNITY_INSTANCING_BUFFER_END(Props)
void vert(inout appdata_full v,out Input i) { UNITY_INITIALIZE_OUTPUT(Input,i); i.proj = ComputeScreenPos(UnityObjectToClipPos(v.vertex)); COMPUTE_EYEDEPTH(i.proj.z); }
void surf (Input IN, inout SurfaceOutput o) { half depth = LinearEyeDepth(tex2Dproj(_CameraDepthTexture,UNITY_PROJ_COORD(IN.proj)).r); half deltaDepth = depth - IN.proj.z; fixed4 c = lerp(_WaterShallowColor,_WaterDeepColor,min(_DeepPower,deltaDepth)/_DeepPower);
float4 bumpOffset1 = tex2D(_Normal,IN.uv_Normal + float2(_WaterSpeed * _Time.x,0)); float4 bumpOffset2 = tex2D(_Normal,IN.uv_Normal + float2(1-IN.uv_Normal.y,IN.uv_Normal.x)+float2(_Time.x * _WaterSpeed,0)); float4 offsetColor = (bumpOffset1+ bumpOffset2)/2; float2 offset = UnpackNormal(offsetColor).xy * _Refract; float4 bumpColor1 = tex2D(_Normal,IN.uv_Normal +offset+ float2(_WaterSpeed * _Time.x,0)); float4 bumpColor2 = tex2D(_Normal,offset+float2(1-IN.uv_Normal.y,IN.uv_Normal.x)+float2(_Time.x * _WaterSpeed,0)); float3 normal = UnpackNormal((bumpColor1 + bumpColor2)/2);
half waveB = 1 - min(_WaveRangeA, deltaDepth) / _WaveRangeA; fixed4 noiserColor = tex2D(_NoiseTex, IN.uv_NoiseTex); fixed4 waveColor = tex2D(_WaveTex, float2(waveB + _WaveRange * sin(_Time.x * _WaveSpeed + noiserColor.r), 1) + offset); fixed4 waveColor2 = tex2D(_WaveTex, float2(waveB + _WaveRange * sin(_Time.x * _WaveSpeed + _WaveDelta + noiserColor.r), 1) + offset);
offset = normal.xy * _Distortion * GrabPass_TexelSize.xy; IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
fixed3 refrCol = tex2D(GrabPass, IN.proj.xy / IN.proj.w).rgb;
fixed3 reflaction = texCUBE(_Cubemap, WorldReflectionVector(IN, normal)).rgb;
fixed fresnel = _FresnelScale + (1 - _FresnelScale) * pow(1 - dot(IN.viewDir, WorldNormalVector(IN, normal) ), 5);
fixed3 refrAndRefl = lerp(reflaction, refrCol, saturate(fresnel)); o.Albedo = (c + (waveColor.rgb + waveColor2.rgb) * waveB) * refrAndRefl;; o.Normal = normal; o.Specular = _Metallic; o.Gloss = _Glossiness; o.Alpha = min(_TranAmount,deltaDepth)/_TranAmount; } ENDCG } }
|