FAE_Water.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "FAE/Water" {
  3. Properties {
  4. _WaterColor ("Water Color", Color) = (0.1467344,0.4798458,0.8676471,1)
  5. _RimColor ("Rim Color", Color) = (1,1,1,1)
  6. _Transparency ("Transparency", Range(0, 1)) = 0
  7. _Glossiness ("Glossiness", Range(0, 1)) = 0
  8. _Depth ("Depth", Range(0, 30)) = 0.5
  9. _Depthdarkness ("Depth darkness", Range(0, 1)) = 1
  10. _RimSize ("Rim Size", Range(0, 4)) = 1.5
  11. _Rimfalloff ("Rim falloff", Float ) = 1.5
  12. _RefractionAmount ("Refraction Amount", Range(0, 0.2)) = 0
  13. [NoScaleOffset][Normal]_Normals ("Normals", 2D) = "bump" {}
  14. [NoScaleOffset]_Shadermap ("Shadermap", 2D) = "bump" {}
  15. _Tiling ("Tiling", Float ) = 0.05
  16. _FlowSpeed ("FlowSpeed", Float ) = 1
  17. [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
  18. }
  19. SubShader {
  20. Tags {
  21. "IgnoreProjector"="True"
  22. "Queue"="Transparent-1"
  23. "RenderType"="Transparent"
  24. }
  25. GrabPass{ }
  26. Pass {
  27. Name "FORWARD"
  28. Tags {
  29. "LightMode"="ForwardBase"
  30. }
  31. Blend SrcAlpha OneMinusSrcAlpha
  32. Cull Off
  33. ZWrite Off
  34. CGPROGRAM
  35. #pragma vertex vert
  36. #pragma fragment frag
  37. #define UNITY_PASS_FORWARDBASE
  38. #define _GLOSSYENV 1
  39. #include "UnityCG.cginc"
  40. #include "UnityPBSLighting.cginc"
  41. #include "UnityStandardBRDF.cginc"
  42. #pragma multi_compile_fwdbase
  43. #pragma multi_compile_fog
  44. #pragma exclude_renderers xbox360 ps3 psp2
  45. #pragma target 3.0
  46. uniform sampler2D _GrabTexture;
  47. uniform sampler2D _CameraDepthTexture;
  48. uniform float4 _TimeEditor;
  49. uniform float _RimSize;
  50. uniform float4 _WaterColor;
  51. uniform float4 _RimColor;
  52. uniform float _Rimfalloff;
  53. uniform sampler2D _Shadermap;
  54. uniform float _RefractionAmount;
  55. uniform float _Transparency;
  56. uniform sampler2D _Normals;
  57. uniform float _Glossiness;
  58. uniform float _Depth;
  59. uniform float _Depthdarkness;
  60. uniform float _Tiling;
  61. uniform float _FlowSpeed;
  62. struct VertexInput {
  63. float4 vertex : POSITION;
  64. float3 normal : NORMAL;
  65. float4 tangent : TANGENT;
  66. };
  67. struct VertexOutput {
  68. float4 pos : SV_POSITION;
  69. float4 posWorld : TEXCOORD0;
  70. float3 normalDir : TEXCOORD1;
  71. float3 tangentDir : TEXCOORD2;
  72. float3 bitangentDir : TEXCOORD3;
  73. float4 screenPos : TEXCOORD4;
  74. float4 projPos : TEXCOORD5;
  75. UNITY_FOG_COORDS(6)
  76. };
  77. VertexOutput vert (VertexInput v) {
  78. VertexOutput o = (VertexOutput)0;
  79. o.normalDir = UnityObjectToWorldNormal(v.normal);
  80. o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );
  81. o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  82. o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  83. float3 lightColor = _LightColor0.rgb;
  84. o.pos = UnityObjectToClipPos(v.vertex );
  85. UNITY_TRANSFER_FOG(o,o.pos);
  86. o.projPos = ComputeScreenPos (o.pos);
  87. COMPUTE_EYEDEPTH(o.projPos.z);
  88. o.screenPos = o.pos;
  89. return o;
  90. }
  91. float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
  92. float isFrontFace = ( facing >= 0 ? 1 : 0 );
  93. float faceSign = ( facing >= 0 ? 1 : -1 );
  94. #if UNITY_UV_STARTS_AT_TOP
  95. float grabSign = -_ProjectionParams.x;
  96. #else
  97. float grabSign = _ProjectionParams.x;
  98. #endif
  99. i.normalDir = normalize(i.normalDir);
  100. i.normalDir *= faceSign;
  101. i.screenPos = float4( i.screenPos.xy / i.screenPos.w, 0, 0 );
  102. i.screenPos.y *= _ProjectionParams.x;
  103. float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
  104. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  105. float4 node_8305 = _Time + _TimeEditor;
  106. float node_3981 = (node_8305.r*0.8);
  107. float2 node_4686 = i.posWorld.rgb.rb;
  108. float2 node_5510 = (node_4686*_Tiling);
  109. float2 node_9360 = (node_5510+node_3981*float2(0,1.1));
  110. float3 node_4911 = UnpackNormal(tex2D(_Normals,node_9360));
  111. float2 node_1488 = (node_5510+node_3981*float2(0.9,0));
  112. float3 node_49111 = UnpackNormal(tex2D(_Normals,node_1488));
  113. float3 node_1309_nrm_base = node_4911.rgb + float3(0,0,1);
  114. float3 node_1309_nrm_detail = node_49111.rgb * float3(-1,-1,1);
  115. float3 node_1309_nrm_combined = node_1309_nrm_base*dot(node_1309_nrm_base, node_1309_nrm_detail)/node_1309_nrm_base.z - node_1309_nrm_detail;
  116. float3 node_1309 = node_1309_nrm_combined;
  117. float3 Normals = node_1309;
  118. float3 normalLocal = Normals;
  119. float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
  120. float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
  121. float sceneZ = max(0,LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)))) - _ProjectionParams.g);
  122. float partZ = max(0,i.projPos.z - _ProjectionParams.g);
  123. float2 Refraction = (float2(node_4911.r,node_49111.g)*_RefractionAmount);
  124. float2 sceneUVs = float2(1,grabSign)*i.screenPos.xy*0.5+0.5 + Refraction;
  125. float4 sceneColor = tex2D(_GrabTexture, sceneUVs);
  126. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  127. float3 lightColor = _LightColor0.rgb;
  128. float3 halfDirection = normalize(viewDirection+lightDirection);
  129. ////// Lighting:
  130. float attenuation = 1;
  131. float3 attenColor = attenuation * _LightColor0.xyz;
  132. float Pi = 3.141592654;
  133. float InvPi = 0.31830988618;
  134. ///////// Gloss:
  135. float gloss = 1.0 - (saturate(( node_49111.b > 0.5 ? (1.0-(1.0-2.0*(node_49111.b-0.5))*(1.0-node_4911.b)) : (2.0*node_49111.b*node_4911.b) ))*_Glossiness); // Convert roughness to gloss
  136. float specPow = exp2( gloss * 10.0+1.0);
  137. /////// GI Data:
  138. UnityLight light;
  139. #ifdef LIGHTMAP_OFF
  140. light.color = lightColor;
  141. light.dir = lightDirection;
  142. light.ndotl = LambertTerm (normalDirection, light.dir);
  143. #else
  144. light.color = half3(0.f, 0.f, 0.f);
  145. light.ndotl = 0.0f;
  146. light.dir = half3(0.f, 0.f, 0.f);
  147. #endif
  148. UnityGIInput d;
  149. d.light = light;
  150. d.worldPos = i.posWorld.xyz;
  151. d.worldViewDir = viewDirection;
  152. d.atten = attenuation;
  153. #if defined(UNITY_SPECCUBE_BLENDING) || defined(UNITY_SPECCUBE_BOX_PROJECTION) || defined(UNITY_ENABLE_REFLECTION_BUFFERS)
  154. d.boxMin[0] = unity_SpecCube0_BoxMin;
  155. d.boxMin[1] = unity_SpecCube1_BoxMin;
  156. #endif
  157. #ifdef UNITY_SPECCUBE_BOX_PROJECTION
  158. d.boxMax[0] = unity_SpecCube0_BoxMax;
  159. d.boxMax[1] = unity_SpecCube1_BoxMax;
  160. d.probePosition[0] = unity_SpecCube0_ProbePosition;
  161. d.probePosition[1] = unity_SpecCube1_ProbePosition;
  162. #endif
  163. d.probeHDR[0] = unity_SpecCube0_HDR;
  164. d.probeHDR[1] = unity_SpecCube1_HDR;
  165. Unity_GlossyEnvironmentData ugls_en_data;
  166. ugls_en_data.roughness = 1.0 - gloss;
  167. ugls_en_data.reflUVW = viewReflectDirection;
  168. UnityGI gi = UnityGlobalIllumination(d, 1, normalDirection, ugls_en_data );
  169. lightDirection = gi.light.dir;
  170. lightColor = gi.light.color;
  171. ////// Specular:
  172. float NdotL = max(0, dot( normalDirection, lightDirection ));
  173. float LdotH = max(0.0,dot(lightDirection, halfDirection));
  174. float3 specularColor = 0.0;
  175. float specularMonochrome;
  176. float2 node_1318 = ((node_4686*0.5)+node_3981*float2(0.9,0));
  177. float4 node_1028 = tex2D(_Shadermap,node_1318);
  178. float2 node_6391 = ((0.2*node_4686)+node_3981*float2(0,1.1));
  179. float4 node_6468 = tex2D(_Shadermap,node_6391);
  180. float node_8987 = saturate((pow(saturate((sceneZ-partZ)/_RimSize),_Rimfalloff) > 0.5 ? (1.0-(1.0-2.0*(pow(saturate((sceneZ-partZ)/_RimSize),_Rimfalloff)-0.5))*(1.0-(node_1028.b*node_6468.b))) : (2.0*pow(saturate((sceneZ-partZ)/_RimSize),_Rimfalloff)*(node_1028.b*node_6468.b))) );
  181. float3 node_5570 = lerp(_RimColor.rgb,_WaterColor.rgb,node_8987);
  182. float node_4175 = saturate((sceneZ-partZ)/_Depth);
  183. float2 node_2371 = ((node_5510*0.25)+(node_3981*_FlowSpeed)*float2(0,1));
  184. float4 _node_1611 = tex2D(_Shadermap,node_2371);
  185. float3 diffuseColor = (lerp(node_5570,(node_5570*(1.0 - _Depthdarkness)),node_4175)+((1.0 - _node_1611.b)*0.1)); // Need this for specular when using metallic
  186. diffuseColor = DiffuseAndSpecularFromMetallic( diffuseColor, specularColor, specularColor, specularMonochrome );
  187. specularMonochrome = 1.0-specularMonochrome;
  188. float NdotV = max(0.0,dot( normalDirection, viewDirection ));
  189. float NdotH = max(0.0,dot( normalDirection, halfDirection ));
  190. float VdotH = max(0.0,dot( viewDirection, halfDirection ));
  191. float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, 1.0-gloss );
  192. float normTerm = max(0.0, GGXTerm(NdotH, 1.0-gloss));
  193. float specularPBL = (NdotL*visTerm*normTerm) * (UNITY_PI / 4);
  194. if (IsGammaSpace())
  195. specularPBL = sqrt(max(1e-4h, specularPBL));
  196. specularPBL = max(0, specularPBL * NdotL);
  197. float3 directSpecular = (floor(attenuation) * _LightColor0.xyz)*specularPBL*FresnelTerm(specularColor, LdotH);
  198. half grazingTerm = saturate( gloss + specularMonochrome );
  199. float3 indirectSpecular = (gi.indirect.specular);
  200. indirectSpecular *= FresnelLerp (specularColor, grazingTerm, NdotV);
  201. float3 specular = (directSpecular + indirectSpecular);
  202. /////// Diffuse:
  203. NdotL = dot( normalDirection, lightDirection );
  204. float3 NdotLWrap = NdotL * ( 1.0 );
  205. float3 forwardLight = max(float3(0.0,0.0,0.0), NdotLWrap);
  206. NdotL = max(0.0,dot( normalDirection, lightDirection ));
  207. half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
  208. float nlPow5 = Pow5(1-NdotLWrap);
  209. float nvPow5 = Pow5(1-NdotV);
  210. float3 directDiffuse = (forwardLight + ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL)) * attenColor;
  211. float3 indirectDiffuse = float3(0,0,0);
  212. indirectDiffuse += UNITY_LIGHTMODEL_AMBIENT.rgb; // Ambient Light
  213. //indirectDiffuse += UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, viewReflectDirection).rgb * 0.0; // Diffuse Ambient Light
  214. float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
  215. /// Final Color:
  216. float3 finalColor = diffuse + specular;
  217. fixed4 finalRGBA = fixed4(lerp(sceneColor.rgb, finalColor,saturate(( lerp(1.0,_Transparency,node_8987) > 0.5 ? (1.0-(1.0-2.0*(lerp(1.0,_Transparency,node_8987)-0.5))*(1.0-node_4175)) : (2.0*lerp(1.0,_Transparency,node_8987)*node_4175) ))),1);
  218. UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
  219. return finalRGBA;
  220. }
  221. ENDCG
  222. }
  223. }
  224. //FallBack "Diffuse"
  225. }