fight4dream

Unity Water shader VR

Dec 8th, 2019
700
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2.  
  3. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  4.  
  5. Shader "FX/Water" {
  6. Properties {
  7.     _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063
  8.     _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44
  9.     _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40
  10.     _RefrColor ("Refraction color", COLOR)  = ( .34, .85, .92, 1)
  11.     [NoScaleOffset] _Fresnel ("Fresnel (A) ", 2D) = "gray" {}
  12.     [NoScaleOffset] _BumpMap ("Normalmap ", 2D) = "bump" {}
  13.     WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
  14.     [NoScaleOffset] _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {}
  15.     _HorizonColor ("Simple water horizon color", COLOR)  = ( .172, .463, .435, 1)
  16.     [HideInInspector] _ReflectionTex0 ("Internal Reflection", 2D) = "" {}
  17.     [HideInInspector] _ReflectionTex1 ("Internal Reflection", 2D) = "" {}
  18.     [HideInInspector] _RefractionTex ("Internal Refraction", 2D) = "" {}
  19. }
  20.  
  21.  
  22. // -----------------------------------------------------------
  23. // Fragment program cards
  24.  
  25.  
  26. Subshader {
  27.     Tags { "WaterMode"="Refractive" "RenderType"="Opaque" }
  28.     Pass {
  29. CGPROGRAM
  30. #pragma vertex vert
  31. #pragma fragment frag
  32. #pragma multi_compile_fog
  33. #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE
  34.  
  35. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  36. #define HAS_REFLECTION 1
  37. #endif
  38. #if defined (WATER_REFRACTIVE)
  39. #define HAS_REFRACTION 1
  40. #endif
  41.  
  42.  
  43. #include "UnityCG.cginc"
  44.  
  45. uniform float4 _WaveScale4;
  46. uniform float4 _WaveOffset;
  47.  
  48. #if HAS_REFLECTION
  49. uniform float _ReflDistort;
  50. #endif
  51. #if HAS_REFRACTION
  52. uniform float _RefrDistort;
  53. #endif
  54.  
  55. struct appdata {
  56.     float4 vertex : POSITION;
  57.     float3 normal : NORMAL;
  58. };
  59.  
  60. struct v2f {
  61.     float4 pos : SV_POSITION;
  62.     #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  63.         float4 ref : TEXCOORD0;
  64.         float2 bumpuv0 : TEXCOORD1;
  65.         float2 bumpuv1 : TEXCOORD2;
  66.         float3 viewDir : TEXCOORD3;
  67.         float eyeIndex : TEXCOORD4;
  68.         UNITY_FOG_COORDS(5)
  69.     #else
  70.         float2 bumpuv0 : TEXCOORD0;
  71.         float2 bumpuv1 : TEXCOORD1;
  72.         float3 viewDir : TEXCOORD2;
  73.         UNITY_FOG_COORDS(3)
  74.     #endif
  75. };
  76.  
  77. v2f vert(appdata v)
  78. {
  79.     v2f o;
  80.     o.pos = UnityObjectToClipPos (v.vertex);
  81.    
  82.  
  83.     // scroll bump waves
  84.     float4 temp;
  85.     float4 wpos = mul (unity_ObjectToWorld, v.vertex);
  86.     temp.xyzw = wpos.xzxz * _WaveScale4 + _WaveOffset;
  87.     o.bumpuv0 = temp.xy;
  88.     o.bumpuv1 = temp.wz;
  89.    
  90.     // object space view direction (will normalize per pixel)
  91.     o.viewDir.xzy = WorldSpaceViewDir(v.vertex);
  92.    
  93.     #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
  94.     o.ref = ComputeNonStereoScreenPos(o.pos);
  95.  
  96.     #ifdef UNITY_SINGLE_PASS_STEREO
  97.         o.eyeIndex = unity_StereoEyeIndex;
  98.     #else
  99.     // When not using single pass stereo rendering, eye index must be determined by testing the
  100.     // sign of the horizontal skew of the projection matrix.
  101.     if (unity_CameraProjection[0][2] > 0) {
  102.         o.eyeIndex = 1.0;
  103.     } else {
  104.         o.eyeIndex = 0.0;
  105.     }
  106.     #endif
  107.     #endif
  108.  
  109.     UNITY_TRANSFER_FOG(o,o.pos);
  110.     return o;
  111. }
  112.  
  113. #if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
  114. sampler2D _ReflectionTex0;
  115. sampler2D _ReflectionTex1;
  116. #endif
  117. #if defined (WATER_REFLECTIVE) || defined (WATER_SIMPLE)
  118. sampler2D _ReflectiveColor;
  119. #endif
  120. #if defined (WATER_REFRACTIVE)
  121. sampler2D _Fresnel;
  122. sampler2D _RefractionTex;
  123. uniform float4 _RefrColor;
  124. #endif
  125. #if defined (WATER_SIMPLE)
  126. uniform float4 _HorizonColor;
  127. #endif
  128. sampler2D _BumpMap;
  129.  
  130. half4 frag( v2f i ) : SV_Target
  131. {
  132.     i.viewDir = normalize(i.viewDir);
  133.    
  134.     // combine two scrolling bumpmaps into one
  135.     half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb;
  136.     half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb;
  137.     half3 bump = (bump1 + bump2) * 0.5;
  138.    
  139.     // fresnel factor
  140.     half fresnelFac = dot( i.viewDir, bump );
  141.    
  142.     // perturb reflection/refraction UVs by bumpmap, and lookup colors
  143.    
  144.     #if HAS_REFLECTION
  145.     float4 uv1 = i.ref; uv1.xy += bump * _ReflDistort;
  146.     half4 refl0 = tex2Dproj( _ReflectionTex0, UNITY_PROJ_COORD(uv1) );
  147.     half4 refl1 = tex2Dproj( _ReflectionTex1, UNITY_PROJ_COORD(uv1) );
  148.     half4 refl = lerp(refl0, refl1, i.eyeIndex);
  149.     #endif
  150.     #if HAS_REFRACTION
  151.     float4 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;
  152.     half4 refr = tex2Dproj( _RefractionTex, UNITY_PROJ_COORD(uv2) ) * _RefrColor;
  153.     #endif
  154.    
  155.     // final color is between refracted and reflected based on fresnel
  156.     half4 color;
  157.    
  158.     #if defined(WATER_REFRACTIVE)
  159.     half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel, float2(fresnelFac,fresnelFac) );
  160.     color = lerp( refr, refl, fresnel );
  161.     #endif
  162.    
  163.     #if defined(WATER_REFLECTIVE)
  164.     half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  165.     color.rgb = lerp( water.rgb, refl.rgb, water.a );
  166.     color.a = refl.a * water.a;
  167.     #endif
  168.    
  169.     #if defined(WATER_SIMPLE)
  170.     half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
  171.     color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
  172.     color.a = _HorizonColor.a;
  173.     #endif
  174.  
  175.     UNITY_APPLY_FOG(i.fogCoord, color);
  176.     return color;
  177. }
  178. ENDCG
  179.  
  180.     }
  181. }
  182.  
  183. }
RAW Paste Data