Generating Water Textures with Perlin Noise

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 :slight_smile:

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

[ sample video ]

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

float4 fragment_shader( v2f IN, out float4 finalcolor): COLOR
{	
	float2 ph 	= float2(shift_x, shift_y);	// Phase Shift
	float2 time = float2(_SinTime.w, _CosTime.w);
	
	ph 			+= time /3.0;
	freq_0.xy 	+= time /5.0;
		
	float n0 	= noise(IN.texcoord*freq_0.xy	 )*freq_0.w;
	float n1 	= noise(IN.texcoord*freq_0.xy +ph)*freq_0.w;
	
	float nx = n0-n1;
	float ny = n0-n1;
		  
	float nz = sqrt(max(0, 1-(nx*nx+ny*ny*freq_0.z)));
	float3 normal = normalize(float3(nx, ny, nz ));
	
	float4 c = tex2D(color_map,IN.texcoord*8.0 +ph*0.1)*nz;
	
	float3 L 	= normalize(IN.light);
	float3 V 	= normalize(IN.eye);
	float diff 	= saturate(dot(L,normal));
	float spec 	= saturate(dot(normalize(L-V),normal));
	float att 	= 1.0 - max(0,L.z); 
	att = 1.0 - att*att;
		
	return c*ambient_color + att*(c*diffuse_color*diff+specular_color*pow(spec,shine));
}

I also, put in some code to animate the vertex too :stuck_out_tongue:

	float4 pos = float4(IN.vertex.xyz, 1.0);
	float3 d = 0.25- min(0.25, distance(_ObjectSpaceCameraPos, pos.xyz) *0.025);
	pos.y = cos(pos.x *_SinTime.x + pos.z *_CosTime.x) *d;

Enjoy !

Nice! :slight_smile:

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

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?

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…

Shader "Custom/waterTest" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}