Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Mine
Monday, May 21st, 2012 at 7:41:34am MDT 

  1. Shader "FX/SimpleWater4" {
  2. Properties {
  3.         _ReflectionTex ("Internal reflection", 2D) = "white" {}
  4.        
  5.         _MainTex ("Fallback texture", 2D) = "black" {} 
  6.         _BumpMap ("Normals ", 2D) = "bump" {}
  7.                                
  8.         _DistortParams ("Distortions (Bump waves, Reflection, Fresnel power, Fresnel bias)", Vector) = (1.0 ,1.0, 2.0, 1.15)
  9.         _InvFadeParemeter ("Auto blend parameter (Edge, Shore, Distance scale)", Vector) = (0.15 ,0.15, 0.5, 1.0)
  10.        
  11.         _AnimationTiling ("Animation Tiling (Displacement)", Vector) = (2.2 ,2.2, -1.1, -1.1)
  12.         _AnimationDirection ("Animation Direction (displacement)", Vector) = (1.0 ,1.0, 1.0, 1.0)
  13.  
  14.         _BumpTiling ("Bump Tiling", Vector) = (1.0 ,1.0, -2.0, 3.0)
  15.         _BumpDirection ("Bump Direction & Speed", Vector) = (1.0 ,1.0, -1.0, 1.0)
  16.        
  17.         _FresnelScale ("FresnelScale", Range (0.15, 4.0)) = 0.75       
  18.  
  19.         _BaseColor ("Base color", COLOR)  = ( .54, .95, .99, 0.5)       
  20.         _ReflectionColor ("Reflection color", COLOR)  = ( .54, .95, .99, 0.5)   
  21.         _SpecularColor ("Specular color", COLOR)  = ( .72, .72, .72, 1)
  22.        
  23.         _WorldLightDir ("Specular light direction", Vector) = (0.0, 0.1, -0.5, 0.0)
  24.         _Shininess ("Shininess", Range (2.0, 500.0)) = 200.0   
  25.                
  26.         _GerstnerIntensity("Per vertex displacement", Float) = 1.0
  27.         _GAmplitude ("Wave Amplitude", Vector) = (0.3 ,0.35, 0.25, 0.25)
  28.         _GFrequency ("Wave Frequency", Vector) = (1.3, 1.35, 1.25, 1.25)
  29.         _GSteepness ("Wave Steepness", Vector) = (1.0, 1.0, 1.0, 1.0)
  30.         _GSpeed ("Wave Speed", Vector) = (1.2, 1.375, 1.1, 1.5)
  31.         _GDirectionAB ("Wave Direction", Vector) = (0.3 ,0.85, 0.85, 0.25)
  32.         _GDirectionCD ("Wave Direction", Vector) = (0.1 ,0.9, 0.5, 0.5) 
  33. }
  34.  
  35.  
  36. CGINCLUDE
  37.        
  38.         #include "UnityCG.cginc"
  39.         #include "WaterInclude.cginc"
  40.  
  41.         struct appdata
  42.         {
  43.                 float4 vertex : POSITION;
  44.                 float3 normal : NORMAL;
  45.         };
  46.  
  47.         // interpolator structs
  48.        
  49.         struct v2f
  50.         {
  51.                 float4 pos : SV_POSITION;
  52.                 float4 normalInterpolator : TEXCOORD0;
  53.                 float3 viewInterpolator : TEXCOORD1;    
  54.                 float4 bumpCoords : TEXCOORD2;
  55.                 float4 screenPos : TEXCOORD3;   
  56.                 float4 grabPassPos : TEXCOORD4;
  57.         };
  58.  
  59.         struct v2f_noGrab
  60.         {
  61.                 float4 pos : SV_POSITION;
  62.                 float4 normalInterpolator : TEXCOORD0;
  63.                 float3 viewInterpolator : TEXCOORD1;    
  64.                 float4 bumpCoords : TEXCOORD2;
  65.                 float4 screenPos : TEXCOORD3;   
  66.         };
  67.                
  68.         struct v2f_simple
  69.         {
  70.                 float4 pos : SV_POSITION;
  71.                 float3 viewInterpolator : TEXCOORD0;    
  72.                 float4 bumpCoords : TEXCOORD1;
  73.         };     
  74.  
  75.         // textures
  76.         sampler2D _BumpMap;
  77.         sampler2D _ReflectionTex;
  78.         sampler2D _RefractionTex;
  79.         sampler2D _ShoreTex;
  80.         sampler2D _CameraDepthTexture;
  81.  
  82.         // colors in use
  83.         uniform float4 _RefrColorDepth;
  84.         uniform float4 _SpecularColor;
  85.         uniform float4 _BaseColor;
  86.         uniform float4 _ReflectionColor;
  87.        
  88.         // edge & shore fading
  89.         uniform float4 _InvFadeParemeter;
  90.  
  91.         // specularity
  92.         uniform float _Shininess;
  93.         uniform float4 _WorldLightDir;
  94.  
  95.         // fresnel, vertex & bump displacements & strength
  96.         uniform float4 _DistortParams;
  97.         uniform float _FresnelScale;   
  98.         uniform float4 _BumpTiling;
  99.         uniform float4 _BumpDirection;
  100.  
  101.         uniform float4 _GAmplitude;
  102.         uniform float4 _GFrequency;
  103.         uniform float4 _GSteepness;                                    
  104.         uniform float4 _GSpeed;     
  105.         uniform float4 _GDirectionAB;      
  106.         uniform float4 _GDirectionCD;
  107.        
  108.         // shortcuts
  109.         #define PER_PIXEL_DISPLACE _DistortParams.x
  110.         #define REALTIME_DISTORTION _DistortParams.y
  111.         #define FRESNEL_POWER _DistortParams.z
  112.         #define VERTEX_WORLD_NORMAL i.normalInterpolator.xyz
  113.         #define DISTANCE_SCALE _InvFadeParemeter.z
  114.         #define FRESNEL_BIAS _DistortParams.w
  115.        
  116.         //
  117.         // HQ VERSION
  118.         //
  119.                
  120.         v2f vert(appdata_full v)
  121.         {
  122.                 v2f o;
  123.                
  124.                 half3 worldSpaceVertex = mul(_Object2World,(v.vertex)).xyz;
  125.                 half3 vtxForAni = (worldSpaceVertex).xzz * unity_Scale.w;                  
  126.  
  127.                 half3 nrml;
  128.                 half3 offsets;
  129.                
  130.                 Gerstner (
  131.                         offsets, nrml, v.vertex.xyz, vtxForAni,                                         // offsets, nrml will be written
  132.                         _GAmplitude,                                          // amplitude
  133.                         _GFrequency,                                         // frequency
  134.                         _GSteepness,                                     // steepness
  135.                         _GSpeed,                                                                                                        // speed
  136.                         _GDirectionAB,                        // direction # 1, 2
  137.                         _GDirectionCD                                    // direction # 3, 4
  138.                 );
  139.                                
  140.                 v.vertex.xyz += offsets;               
  141.                                                                
  142.                 half2 tileableUv = worldSpaceVertex.xz;  
  143.                                
  144.                 o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;   
  145.  
  146.                 o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
  147.  
  148.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  149.  
  150.                 ComputeScreenAndGrabPassPos(o.pos, o.screenPos, o.grabPassPos);
  151.                
  152.                 o.normalInterpolator.xyz = nrml;
  153.                
  154.                 o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
  155.                
  156.                 return o;
  157.         }
  158.  
  159.         half4 frag( v2f i ) : COLOR
  160.         {                            
  161.                 half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, VERTEX_WORLD_NORMAL, PER_PIXEL_DISPLACE);
  162.                 half3 viewVector = normalize(i.viewInterpolator.xyz);
  163.  
  164.                 half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
  165.                 half4 screenWithOffset = i.screenPos + distortOffset;
  166.                 half4 grabWithOffset = i.grabPassPos + distortOffset;
  167.                
  168.                 half4 rtRefractionsNoDistort = tex2Dproj(_RefractionTex, UNITY_PROJ_COORD(i.grabPassPos));
  169.                 half refrFix = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(grabWithOffset)));
  170.                 half4 rtRefractions = tex2Dproj(_RefractionTex, UNITY_PROJ_COORD(grabWithOffset));
  171.                
  172.                 #ifdef WATER_REFLECTIVE
  173.                         half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));   
  174.                 #endif
  175.  
  176.                 #ifdef WATER_EDGEBLEND_ON
  177.                 if (LinearEyeDepth(refrFix) < i.screenPos.z)
  178.                         rtRefractions = rtRefractionsNoDistort; 
  179.                 #endif
  180.                
  181.                 half3 reflectVector = normalize(reflect(viewVector, worldNormal));         
  182.                 half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
  183.                 float nh = max (0, dot (worldNormal, -h));
  184.                 float spec = max(0.0,pow (nh, _Shininess));          
  185.                
  186.                 half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
  187.                
  188.                 #ifdef WATER_EDGEBLEND_ON
  189.                         half depth = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos)));
  190.                         depth = LinearEyeDepth(depth);
  191.                         edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.w));  
  192.                 #endif 
  193.                
  194.                 // shading for fresnel term
  195.                 worldNormal.xz *= _FresnelScale;
  196.                 half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
  197.                                
  198.                 // base, depth & reflection colors
  199.                 half4 baseColor = _BaseColor;
  200.                 #ifdef WATER_REFLECTIVE
  201.                         half4 reflectionColor = lerp (rtReflections,_ReflectionColor,_ReflectionColor.a);
  202.                 #else
  203.                         half4 reflectionColor = _ReflectionColor;
  204.                 #endif
  205.                
  206.                 baseColor = lerp (lerp (rtRefractions, baseColor, baseColor.a), reflectionColor, refl2Refr);
  207.                 baseColor = baseColor + spec * _SpecularColor;
  208.                
  209.                 baseColor.a = edgeBlendFactors.x;
  210.                 return baseColor;
  211.         }
  212.        
  213.         //
  214.         // MQ VERSION
  215.         //
  216.        
  217.         v2f_noGrab vert300(appdata_full v)
  218.         {
  219.                 v2f_noGrab o;
  220.                
  221.                 half3 worldSpaceVertex = mul(_Object2World,(v.vertex)).xyz;
  222.                 half3 vtxForAni = (worldSpaceVertex).xzz * unity_Scale.w;                  
  223.  
  224.                 half3 nrml;
  225.                 half3 offsets;
  226.                 Gerstner (
  227.                         offsets, nrml, v.vertex.xyz, vtxForAni,                                         // offsets, nrml will be written
  228.                         _GAmplitude,                                          // amplitude
  229.                         _GFrequency,                                         // frequency
  230.                         _GSteepness,                                     // steepness
  231.                         _GSpeed,                                                                                                        // speed
  232.                         _GDirectionAB,                        // direction # 1, 2
  233.                         _GDirectionCD                                    // direction # 3, 4
  234.                 );
  235.                                
  236.                 v.vertex.xyz += offsets;               
  237.                                                                
  238.                 half2 tileableUv = worldSpaceVertex.xz;  
  239.                                
  240.                 o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;   
  241.  
  242.                 o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
  243.  
  244.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  245.  
  246.                 o.screenPos = ComputeScreenPos(o.pos);
  247.                
  248.                 o.normalInterpolator.xyz = nrml;
  249.                
  250.                 o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
  251.                
  252.                 return o;
  253.         }
  254.  
  255.         half4 frag300( v2f_noGrab i ) : COLOR
  256.         {              
  257.                 half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, VERTEX_WORLD_NORMAL, PER_PIXEL_DISPLACE);
  258.                 half3 viewVector = normalize(i.viewInterpolator.xyz);
  259.  
  260.                 half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
  261.                 half4 screenWithOffset = i.screenPos + distortOffset;
  262.                
  263.                 #ifdef WATER_REFLECTIVE  
  264.                         half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));   
  265.                 #endif
  266.                
  267.                 half3 reflectVector = normalize(reflect(viewVector, worldNormal));         
  268.                 half3 h = normalize (_WorldLightDir.xyz + viewVector.xyz);
  269.                 float nh = max (0, dot (worldNormal, -h));
  270.                 float spec = max(0.0,pow (nh, _Shininess));     
  271.                
  272.                 half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
  273.                
  274.                 #ifdef WATER_EDGEBLEND_ON
  275.                         half depth = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos)));
  276.                         depth = LinearEyeDepth(depth);
  277.                         edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.z));  
  278.                 #endif   
  279.                
  280.                 worldNormal.xz *= _FresnelScale;               
  281.                 half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
  282.                
  283.                 half4 baseColor = _BaseColor;
  284.                 #ifdef WATER_REFLECTIVE 
  285.                         baseColor = lerp (baseColor, lerp (rtReflections,_ReflectionColor,_ReflectionColor.a), saturate(refl2Refr * 1.0));
  286.                 #else
  287.                         baseColor = _ReflectionColor;//lerp (baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));      
  288.                 #endif
  289.                
  290.                 baseColor = baseColor + spec * _SpecularColor;
  291.                
  292.                 baseColor.a = edgeBlendFactors.x * saturate(0.5 + refl2Refr * 1.0);
  293.                 return baseColor;
  294.         }       
  295.        
  296.         //
  297.         // LQ VERSION
  298.         //
  299.        
  300.         v2f_simple vert200(appdata_full v)
  301.         {
  302.                 v2f_simple o;
  303.                
  304.                 half3 worldSpaceVertex = mul(_Object2World, v.vertex).xyz;
  305.                 half2 tileableUv = worldSpaceVertex.xz;
  306.  
  307.                 o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;   
  308.  
  309.                 o.viewInterpolator.xyz = worldSpaceVertex-_WorldSpaceCameraPos;
  310.                
  311.                 o.pos = mul(UNITY_MATRIX_MVP,  v.vertex);
  312.                                
  313.                 return o;
  314.  
  315.         }
  316.  
  317.         half4 frag200( v2f_simple i ) : COLOR
  318.         {              
  319.                 half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, half3(0,1,0), PER_PIXEL_DISPLACE);
  320.                 half3 viewVector = normalize(i.viewInterpolator.xyz);
  321.  
  322.                 half3 reflectVector = normalize(reflect(viewVector, worldNormal));         
  323.                 half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
  324.                 float nh = max (0, dot (worldNormal, -h));
  325.                 float spec = max(0.0,pow (nh, _Shininess));     
  326.  
  327.                 worldNormal.xz *= _FresnelScale;               
  328.                 half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER)
  329.  
  330.                 half4 baseColor = _BaseColor;
  331.                 baseColor = lerp(baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
  332.                 baseColor.a = saturate(2.0 * refl2Refr + 0.5);
  333.  
  334.                 baseColor.rgb += spec * _SpecularColor.rgb;
  335.                 return baseColor;       
  336.         }
  337.                        
  338. ENDCG
  339.  
  340. Subshader
  341. {
  342.         Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  343.        
  344.         Lod 500
  345.         ColorMask RGB
  346.        
  347.         GrabPass { "_RefractionTex" }
  348.        
  349.         Pass {
  350.                         Blend SrcAlpha OneMinusSrcAlpha
  351.                         ZTest LEqual
  352.                         ZWrite Off
  353.                         Cull Off
  354.                        
  355.                         CGPROGRAM
  356.                        
  357.                         #pragma target 3.0
  358.                        
  359.                         #pragma vertex vert
  360.                         #pragma fragment frag
  361.                        
  362.                         #pragma glsl
  363.                        
  364.                         #pragma fragmentoption ARB_precision_hint_fastest
  365.                                                
  366.                         #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
  367.                         #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF       
  368.                         #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE                    
  369.                                                                    
  370.                         ENDCG
  371.         }
  372. }
  373.  
  374. Subshader
  375. {      
  376.         Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  377.        
  378.         Lod 300
  379.         ColorMask RGB
  380.        
  381.         Pass {
  382.                         Blend SrcAlpha OneMinusSrcAlpha
  383.                         ZTest LEqual
  384.                         ZWrite Off
  385.                         Cull Off
  386.                        
  387.                         CGPROGRAM
  388.                        
  389.                         #pragma target 3.0
  390.                        
  391.                         #pragma vertex vert300
  392.                         #pragma fragment frag300
  393.                        
  394.                         #pragma glsl
  395.                        
  396.                         #pragma fragmentoption ARB_precision_hint_fastest
  397.                         #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
  398.                         #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF                       
  399.                         #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE                    
  400.                                                                    
  401.                         ENDCG
  402.         }       
  403. }
  404.  
  405. Subshader
  406. {      
  407.         Tags {"RenderType"="Transparent" "Queue"="Transparent"}
  408.        
  409.         Lod 200
  410.         ColorMask RGB
  411.        
  412.         Pass {
  413.                         Blend SrcAlpha OneMinusSrcAlpha
  414.                         ZTest LEqual
  415.                         ZWrite Off
  416.                         Cull Off
  417.                        
  418.                         CGPROGRAM
  419.                        
  420.                         #pragma vertex vert200
  421.                         #pragma fragment frag200
  422.                         #pragma fragmentoption ARB_precision_hint_fastest                                          
  423.                                                                    
  424.                         ENDCG
  425.         }       
  426. }
  427.  
  428. Fallback "Transparent/Diffuse"
  429. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
worth-right