Ocean Update #2

Not done a huge amount of work, but a few things:

  • I wasn’t happy with not being able to control wave speed. I get this is not physically accurate, but I want “looks good”, not “100% accurate”. I needed more control, so I’ve added in a Vector4 to help me control wave speed – the xyz properties refer to waves A, B, C speed, and the w speed property is a global modifier.
  • I’ll be needing a specular workflow for water, so I’ve just changed the surface type to be this for now. May end up with a custom frag shader yet, who knows – for now this is more in-line with the final intended result than metallic (obviously).
  • I’ve just created a quick test scene for now. Some free Asset Store rocks, and a subdivided plane for water.

So nothing major, hoping to do some substantial updates later this week.

Shader "Custom/Waves" {
	Properties {
		[Header(Standard Properties)]
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Glossiness("Smoothness", Range(0,1)) = 0.5
		_SpecColor("Specular Color", Color) = (1,1,1,1)

		[Header(Water Surface Properties)]
		_FoamColor("Foam Color", Color) = (1,1,1,1)
		_FoamTex("Foam Texture (RGB)", 2D) = "white" {}
		
		[Header(Wave Properties)]
		[Tooltip(dir xy, steepness, wavelength)]
		_WaveA ("Wave A", Vector) = (1,0,0.5,10)
		_WaveB ("Wave B", Vector) = (0,1,0.25,20)
		_WaveC ("Wave C", Vector) = (1,1,0.15,10)
		[Tooltip(wave A speed, wave B speed, wave C speed, final multiplier)]
		_WaveSpeed("Wave Speed", Vector) = (1,1,1,1)

		[Header(Tessellation Properties)]
		_Tess("Tessellation", Range(1,32)) = 1
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200

		CGPROGRAM
		#pragma surface surf StandardSpecular fullforwardshadows vertex:vert tessellate:tess addshadow
		#pragma target 4.6
		#include "Tessellation.cginc"

		sampler2D _MainTex, _FoamTex;
		half _Glossiness, _Tess;
		fixed4 _Color, _FoamColor, _WaveA, _WaveB, _WaveC, _WaveSpeed;

		struct Input {
			float2 uv_MainTex;
			float2 uv2_FoamTex;
			float4 color : COLOR;
		};

		float4 tess() {
			return _Tess;
		}

		float3 GerstnerWave (float4 wave, float3 p, inout float3 tangent, inout float3 binormal, float speedModifier) {
		    float k = 2 * UNITY_PI / wave.w;
			float c = sqrt(9.81 / k);
			float2 d = normalize(wave.xy);
			float f = k * (dot(d, p.xz) - c * _Time.y * speedModifier *_WaveSpeed.w);
			float a = wave.z / k;

			/*
			> sin(f), cos(f) are used a few times in the final calulations, so pre-clculate them once
			> We also multiply these a few times by steepness, so pre-calculate these as well
			> Store it as a float4 to keep memory footprint small
			> It's a bit tricky to read, but the gains are worthwhile
			*/
			float4 sinFCosF = float4( sin(f), cos(f), 0, 0);
			sinFCosF.z = sinFCosF.x * wave.z;
			sinFCosF.w = sinFCosF.y * wave.z;

			tangent += float3(
				-d.x * d.x * sinFCosF.z,
				d.x * sinFCosF.w,
				-d.x * d.y * sinFCosF.z
			);
			binormal += float3(
				-d.x * d.y * sinFCosF.z,
				d.y * sinFCosF.w,
				-d.y * d.y * sinFCosF.z
			);
			return float3(
				d.x * (a * sinFCosF.y),
				a * sinFCosF.x,
				d.y * (a * sinFCosF.y)
			);
		}


		void vert(inout appdata_full vertexData) {

			float3 gridPoint = vertexData.vertex.xyz;
			float3 tangent = float3(1, 0, 0);
			float3 binormal = float3(0, 0, 1);
			float3 p = gridPoint;
			float3 a, b, c;
			a = GerstnerWave(_WaveA, gridPoint, tangent, binormal, _WaveSpeed.x);
			b = GerstnerWave(_WaveB, gridPoint, tangent, binormal, _WaveSpeed.y);
			c = GerstnerWave(_WaveC, gridPoint, tangent, binormal, _WaveSpeed.z);
			p += a + b + c;
			float3 normal = normalize(cross(binormal, tangent));
			vertexData.vertex.xyz = p;
			vertexData.normal = normal;

			// For later: Get current wave height offset and assign per vertex color RGB
			// vertexData.color.rgb = saturate(float3(a.y, b.y, c.y));
		}

		void surf(Input IN, inout SurfaceOutputStandardSpecular o) {
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
			// For later: Foam map
			// fixed4 f = tex2D(_FoamTex, IN.uv2_FoamTex) * _FoamColor;
			o.Albedo = c.rgb;
			o.Specular = _SpecColor;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Unlit"
}

Leave a Reply

Your email address will not be published. Required fields are marked *