// OceanWaveReal.hlsl // // Realistic rolling-ocean vertex displacement using summed Gerstner waves // (sharper crests, flatter troughs than plain sine waves -- the standard // technique behind most real-time ocean shaders). Separate from and // unrelated to WaterRipple.hlsl, for side-by-side comparison. #ifndef OCEAN_WAVES_REAL_INCLUDED #define OCEAN_WAVES_REAL_INCLUDED // Single Gerstner wave contribution, accumulated into `offset` by the caller. void GerstnerWave( float2 posXZ, float time, float2 dir, float wavelength, float amplitude, float speed, float steepness, inout float3 offset) { float2 d = normalize(dir); float w = 6.28318530718 / max(wavelength, 0.0001); // 2*PI / wavelength float phase = speed * w; float theta = dot(d, posXZ) * w + time * phase; // Q is a direct multiplier here (NOT divided by w*amplitude). The // previous version divided by amplitude, which cancelled amplitude out // of the result entirely -- meaning horizontal displacement ignored // WaveHeightScale and scaled directly with wavelength instead, causing // the extreme stretching at larger wavelengths. This version keeps // horizontal and vertical displacement properly tied together. float Q = steepness; offset.x += Q * amplitude * d.x * cos(theta); offset.z += Q * amplitude * d.y * cos(theta); offset.y += amplitude * sin(theta); } // Four waves of varying direction/wavelength/amplitude/speed, summed for a // natural, non-repeating swell. HeightScale/SpeedScale tune the overall // intensity without needing to touch the individual wave numbers below. float3 OceanWaveOffset(float2 posXZ, float time, float heightScale, float speedScale) { float3 offset = float3(0, 0, 0); // Two broad wave layers sized so each wavelength spans several mesh // segments on a sparsely-subdivided plane (roughly 79 units/segment on // a default Unity Plane stretched across a 788-unit object). Real open- // ocean swells are long and broad anyway, so this isn't just a // compromise -- it reads as more realistic too. If you later swap in a // finely subdivided mesh (e.g. via ProBuilder), a third/fourth shorter // wave can be added back in for extra chop/detail. GerstnerWave(posXZ, time, float2(1.0, 0.3), 300.0, 0.5 * heightScale, 0.8 * speedScale, 0.7, offset); GerstnerWave(posXZ, time, float2(-0.5, 0.85), 210.0, 0.3 * heightScale, 1.1 * speedScale, 0.6, offset); return offset; } // Entry point for the Shader Graph Custom Function Node (Vertex stage). // Inputs: WorldPos, WorldNormal, Time, HeightScale, SpeedScale, SampleOffset // Outputs: OutWorldPos, OutWorldNormal, WaveHeight01 (0..1, crest to trough -- feed into color) void OceanWaveVertex_float( float3 WorldPos, float3 WorldNormal, float Time, float HeightScale, float SpeedScale, float SampleOffset, out float3 OutWorldPos, out float3 OutWorldNormal, out float WaveHeight01) { float2 posXZ = WorldPos.xz; float3 offset = OceanWaveOffset(posXZ, Time, HeightScale, SpeedScale); OutWorldPos = WorldPos + offset; float maxAmplitude = (0.5 + 0.3) * max(HeightScale, 0.0001); WaveHeight01 = saturate(offset.y / max(maxAmplitude, 0.0001) * 0.5 + 0.5); // Recompute the normal from two nearby samples so lighting follows the // actual wave shape instead of staying flat. float3 offX = OceanWaveOffset(posXZ + float2(SampleOffset, 0), Time, HeightScale, SpeedScale); float3 offZ = OceanWaveOffset(posXZ + float2(0, SampleOffset), Time, HeightScale, SpeedScale); float3 posX = WorldPos + float3(SampleOffset, 0, 0) + offX; float3 posZ = WorldPos + float3(0, 0, SampleOffset) + offZ; OutWorldNormal = normalize(cross(posZ - OutWorldPos, posX - OutWorldPos)); // If lighting looks inside-out on your mesh, flip to cross(posX - OutWorldPos, posZ - OutWorldPos). } #endif