Feb
19
2012

SeaShoreShader (test)

Testing something similar to this ( http://unitycoder.com/blog/2012/02/12/fake-water-shader-refraction/ )
but for sea & beach. (its all-in-one-shader & all-in-one-mesh for unity indie..)

Current features v1.0: (main image^)

– Looks horrible
– Beach texture
– Sea surface texture (uv-animated, with some distortions)
– Surface Hue property

Ideas:

– Substances? *edit: well well, http://u3d.as/content/allegorithmic/beach-shore-with-moving-waves/24V
– CameraDepthmap?
– Foam / waves that come to the sand?
– Sand looks tiled.. (maybe try uv mixing, like here: http://unitycoder.com/blog/2012/02/03/multi-uv-mixing-shader/ )

——————

v2

– Starting to get some depth to the distance, colors are bit off.. (using worldpos.. _CameraDepthTexture didnt work)
– Trying to use “linear” workspace, instead of “gamma” (its in the file / build settings / player settings..)
– Next should try fake caustics, cubemap?, some foam map? animated waves? (now it just has “ripples”)
– Distort the bottom also? (similar to surface..hmm)

v3

– Some mesh problems underwater..so cannot use transparent surface..
– This is not really working.. giving up until get some more ideas / time : )

Shader source:


// mSeaShoreShader test v1.0 - http://unitycoder.com/blog


Shader "mShaders/mSeaShore3" {
    Properties {
        _WaveSpeed ("WaterSpeed", Float) = 0.2
        _FakeDepth ("FakeDepth", Float) = 0.05        

      _SurfaceTex ("SeaSurfaceTexture", 2D) = "white" {}
      _BeachTex ("BeachTexture", 2D) = "white" {}
      _BottomTex ("BottomTexture", 2D) = "white" {}
      _Surface_hue ("SurfaceHue", Color) = (1,1,1,1)

//      _Cube ("Cubemap", CUBE) = "" {}

    }
    SubShader {
        Tags { "Queue"="Geometry"  "IgnoreProjector"="True" "RenderType" = "Opaque" }        
        
        CGPROGRAM
        #pragma target 3.0 
        #pragma surface surf Lambert alpha vertex:vert
        #include "UnityCG.cginc"

        struct Input 
        {
        
            float3 depth;
            float4 pos;

        
            float2 uv_SurfaceTex;
            float2 uv_BottomTex;
            float2 uv_BeachTex;
            
            float3 worldPos;
            float3 worldRefl;
            float3 worldNormal;
            float4 screenPos;
            INTERNAL_DATA
        };


void vert (inout appdata_full v, out Input o) {
          //v.vertex.y += v.normal*2;
//            v2f o;
            o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
            //UNITY_TRANSFER_DEPTH(o.depth);
            
//            return o;
}

//        void vert (inout appdata_full v, out Input o) {
//        }
      
        sampler2D _SurfaceTex;
        sampler2D _BeachTex;
        sampler2D _BottomTex;
        sampler2D _CameraDepthTexture;
        //samplerCUBE _Cube;

      uniform float _WaveSpeed;
      uniform float _FakeDepth;
      uniform float4 _Surface_hue;

        void surf (Input IN, inout SurfaceOutput o) 
        {
        
            float waveslide = _WaveSpeed*_Time.x;
            
            //float3 camDepth = tex2D (_CameraDepthTexture, IN.uv_SurfaceTex).rgb;
//            camDepth = Linear01Depth(camDepth);

            half2 uv = IN.screenPos.xy / IN.screenPos.w;
//            float depth = 1-tex2D(_CameraDepthTexture, screenuv).r;
            
//            float depth = UNITY_SAMPLE_DEPTH(tex2D (_CameraDepthTexture, uv));
//            depth = LinearEyeDepth (depth);

//        float z1 = tex2Dproj(_CameraDepthTexture,  IN.screenPos)*1; // in /.w space
//        float z1 = tex2D(_CameraDepthTexture,  IN.screenPos.xy).r; // in /.w space
        //float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.pos))));
        //z1 =  LinearEyeDepth(z1)/2;   
        //float z2 = (IN.screenPos.z);
        //z1 = saturate( 0.125 * (abs(z2-z1)) );
        
//        float testdepth = 1+IN.worldPos.x*0.5;
        float depth_distance_orig = IN.worldPos.z*0.02;
        float depth_distance = -saturate(IN.worldPos.z+30)*0.1;

            
//            half3 surface_orig = tex2D(_SurfaceTex, float2(IN.uv_SurfaceTex.x,IN.uv_SurfaceTex.y+waveslide)).rgb*_FakeDepth;
            half3 surface_orig = tex2D(_SurfaceTex, float2(IN.uv_SurfaceTex.x,IN.uv_SurfaceTex.y+waveslide)).rgb*_FakeDepth;
//            half3 surface_norm = UnpackNormal (tex2D (_SurfaceTex, IN.uv_SurfaceTex));

//            half3 surface_refracted = tex2D(_BottomTex, float2(IN.uv_SurfaceTex.x+surface_orig.b,IN.uv_SurfaceTex.y+surface_orig.g)).rgb;
            half3 surface_refracted = tex2D(_BottomTex, float2(IN.uv_SurfaceTex.x+surface_orig.b,IN.uv_SurfaceTex.y+surface_orig.r)).rgb;
//            half3 surface_refracted = tex2D(_SurfaceTex, float2(IN.uv_BottomTex.x+surface_orig.b,IN.uv_BottomTex.y+surface_orig.g)).rgb;



//            half3 bottom_orig = tex2D(_SurfaceTex, float2(IN.uv_SurfaceTex.x,IN.uv_SurfaceTex.y+waveslide)).rgb*_FakeDepth;
            half3 bottom_norm = UnpackNormal (tex2D (_BottomTex, IN.uv_BottomTex));
            //half3 emis1 = texCUBE (_Cube, WorldReflectionVector (IN, o.Normal)).rgb;


            half3 bottom_col_refract = tex2D (_BottomTex, float2(IN.uv_BottomTex.x+surface_orig.b,IN.uv_BottomTex.y+surface_orig.g)).rgb*1.5;
            half3 bottom_col = tex2D (_BottomTex, IN.uv_BottomTex).rgb*1.5;

            // sea surface
//              if (IN.worldPos.y>-0.0001 && IN.worldPos.y<0.0001)
              if (IN.worldPos.y>-0.0001 && IN.worldPos.y<0.0001)
            
//              if (IN.worldNormal.y==1)
            {
            
                half3 col = surface_refracted+surface_refracted;
//                o.Albedo = col+float3(0,0,depth_distance)*10; //float3(testdepth,testdepth,testdepth);//float3(z1,z1,z1); //col;//+float3(1,0,0);
                o.Albedo = col; 
//                o.Albedo = float3(0,1,0); 
//                o.Alpha = _Surface_hue.a+bottom_norm*0.2+col;//+testdepth;
                o.Alpha = 1; //Surface_hue.a+bottom_norm*0.2*float3(0,0,depth_distance)*10; //_Surface_hue.a+bottom_norm*0.2+col;//+testdepth;
                //o.Alpha =Surface_hue.a;//+bottom_norm*0.2;//*float3(0,0,depth_distance)*10; //_Surface_hue.a+bottom_norm*0.2+col;//+testdepth;

//                o.Alpha = camDepth.r*4;
//                o.Emission = texCUBE (_Cube, IN.worldRefl).rgb*0.3;
                //o.Normal = surface_norm; // sticky pixels, if over 1?
//                o.Emission = emis1*emis1; //+foam1*0.5;
                o.Emission = (_Surface_hue.rgb)*depth_distance_orig;//+_Surface_hue.rgb*float3(0,0,depth_distance_orig)*10;
            
            }else{ // beach
            
                // underwater beach
//                if (IN.worldPos.y<0)// && IN.worldPos.z>222)
//                {
//                    o.Albedo = float3(0,0,1); //bottom_col_refract+bottom_col_refract*2; 
//                    o.Albedo = (bottom_col_refract+bottom_col_refract); 
//                }else{
                    o.Albedo = bottom_col+bottom_col*1.5; 
//                    o.Albedo = float3(1,0,0); //bottom_col_refract+bottom_col_refract*2; 
//                }
                
//                    o.Normal = bottom_norm;
//                o.Albedo = float3(testdepth,testdepth,testdepth); //IN.pos;//float3(z1,z1,z1); 
//                o.Albedo = float3(1,0,0); 
//            o.Normal = bottom_norm;
                o.Alpha = 1;
                
            }
            
            
            
        }
        ENDCG
    } 
    FallBack "Diffuse"
}



 

 


1 Comment + Add Comment

  • ^ shader source added

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.