Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Generating Water Textures with Perlin Noise

Discussion in 'Shaders' started by apple_motion, Nov 9, 2009.

  1. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    just did one more test on the perlin noise - a poor man version of ocean shader. well, not realities at all but it is cheap only using 1 drawing call and everything running inside the shader :)



    [ webplayer ]
    http://homepage.mac.com/antonioh/unity3d/build/Perlin_Ocean.html

    [ sample video ]
    http://www.youtube.com/watch?v=RYClXUSh-S4

    Basically, it is a very simple shader. Its use 2 channel of 2D Perlin noise to generate the normal.

    Code (csharp):
    1. float4 fragment_shader( v2f IN, out float4 finalcolor): COLOR
    2. {  
    3.     float2 ph   = float2(shift_x, shift_y); // Phase Shift
    4.     float2 time = float2(_SinTime.w, _CosTime.w);
    5.    
    6.     ph          += time /3.0;
    7.     freq_0.xy   += time /5.0;
    8.        
    9.     float n0    = noise(IN.texcoord*freq_0.xy    )*freq_0.w;
    10.     float n1    = noise(IN.texcoord*freq_0.xy +ph)*freq_0.w;
    11.    
    12.     float nx = n0-n1;
    13.     float ny = n0-n1;
    14.          
    15.     float nz = sqrt(max(0, 1-(nx*nx+ny*ny*freq_0.z)));
    16.     float3 normal = normalize(float3(nx, ny, nz ));
    17.    
    18.     float4 c = tex2D(color_map,IN.texcoord*8.0 +ph*0.1)*nz;
    19.    
    20.     float3 L    = normalize(IN.light);
    21.     float3 V    = normalize(IN.eye);
    22.     float diff  = saturate(dot(L,normal));
    23.     float spec  = saturate(dot(normalize(L-V),normal));
    24.     float att   = 1.0 - max(0,L.z);
    25.     att = 1.0 - att*att;
    26.        
    27.     return c*ambient_color + att*(c*diffuse_color*diff+specular_color*pow(spec,shine));
    28. }
    29.  
    I also, put in some code to animate the vertex too :p
    Code (csharp):
    1.     float4 pos = float4(IN.vertex.xyz, 1.0);
    2.     float3 d = 0.25- min(0.25, distance(_ObjectSpaceCameraPos, pos.xyz) *0.025);
    3.     pos.y = cos(pos.x *_SinTime.x + pos.z *_CosTime.x) *d;
    Enjoy !
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
  3. MythicalCity

    MythicalCity

    Joined:
    Feb 10, 2011
    Posts:
    418
    Hi there, this shader actually looks pretty good for me game. But I'm new to shaders and I'm not sure how to create it. Do I create a new Shader in unity and then paste in the code into the shader? if so, do I replace the original code in there?

    -JJ
     
  4. snaker

    snaker

    Joined:
    Feb 25, 2012
    Posts:
    11
    Hi,friend,I'm a new to shader, I just new a shader and use your code to replace the CGPROGRAM part,but it said " Shader error in 'Custom/wate': Syntax error at line 12 ",I don't know how to use your code to make a complete shader,could you tell the whole shader code or give us some advice about how to make a shader with HLSL?
     
  5. Junkie_XL

    Junkie_XL

    Joined:
    Jan 27, 2011
    Posts:
    12
    I second this. Can someone please explain further on where this fits in? This is all I see right now when creating a new shader...no idea where this goes. trial and error aint working...

    Code (csharp):
    1. Shader "Custom/waterTest" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags { "RenderType"="Opaque" }
    7.         LOD 200
    8.        
    9.         CGPROGRAM
    10.         #pragma surface surf Lambert
    11.  
    12.         sampler2D _MainTex;
    13.  
    14.         struct Input {
    15.             float2 uv_MainTex;
    16.         };
    17.  
    18.         void surf (Input IN, inout SurfaceOutput o) {
    19.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    20.             o.Albedo = c.rgb;
    21.             o.Alpha = c.a;
    22.         }
    23.         ENDCG
    24.     }
    25.     FallBack "Diffuse"
    26. }
     
    Last edited: Jul 5, 2012