Files
cruiseSimulator/Assets/BigShipTycoon/Resources/HarborWater.shader
josh 290e4cb45d Add riverboat captain career and distinct ship classes
Add class-specific vessel geometry and sea motion, a guided helm qualification path, dockside sailing orders, and local excursions at all 24 ports.
2026-11-29 20:41:00 -05:00

267 lines
15 KiB
GLSL

Shader "BigShipTycoon/Harbor Water"
{
Properties
{
_Backdrop ("Harbor photograph", 2D) = "black" {}
_OceanDetail ("Ocean surface detail", 2D) = "gray" {}
_BackdropCrop ("Harbor crop", Vector) = (1,1,0,0)
_HullSpan ("Hull length and beam", Vector) = (71,7,0,0)
_HullCourse ("Hull heading and wake strength", Vector) = (1,0,0,0)
_PortBerth ("Cinematic berth position and dimensions", Vector) = (0,0,0,0)
_PortMotion ("Port maneuver", Float) = 0
_PortSky ("Port sunset color", Color) = (.86,.62,.43,1)
_PortShallows ("Shallow harbor water", Range(0,1)) = .25
_PortMatte ("Port waterfront matte", 2D) = "black" {}
_PortMatteCrop ("Port waterfront crop", Vector) = (1,1,0,0)
_PortMatteActive ("Port waterfront matte is available", Float) = 0
_Underway ("Open water", Float) = 0
_WeatherSeverity ("Sea conditions", Range(0,1)) = 0
}
SubShader
{
Tags { "Queue"="Background" "RenderType"="Opaque" }
Pass
{
Cull Off
ZWrite Off
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "OceanWaves.cginc"
sampler2D _Backdrop, _OceanDetail, _PortMatte;
float4 _BackdropCrop, _HullSpan, _HullCourse, _PortBerth, _ViewSize;
float4 _PortSky, _PortMatteCrop;
float3 _ViewOrigin, _ViewForward, _ViewRight, _ViewUp;
float _Underway, _WeatherSeverity, _SeaTime, _PortMotion, _PortShallows, _PortMatteActive;
static const float3 Sun = float3(-.761,.441,-.474);
struct Output { float4 clip : SV_POSITION; float2 screen : TEXCOORD0; };
Output vert(appdata_base input)
{
Output output;
float2 corner = input.vertex.xz / 2000;
output.clip = float4(corner.x, corner.y * _ProjectionParams.x, .99, 1);
output.screen = corner * .5 + .5;
return output;
}
float3 SkyGradient(float3 ray)
{
float warmth = pow(saturate(.55 + dot(ray.xz, Sun.xz) * .55), 2.3);
float3 horizon = lerp(float3(.55,.69,.75), float3(.92,.71,.51), warmth * .84);
float elevation = sqrt(saturate(ray.y));
float3 sky = lerp(horizon, float3(.081,.242,.408), elevation);
float3 squall = lerp(float3(.41,.50,.56), float3(.112,.174,.230), elevation);
if (_PortMotion > .5)
{
float3 shoreGlow = lerp(float3(.62,.70,.76), _PortSky.rgb, .28 + warmth * .64);
sky = lerp(shoreGlow, float3(.16,.30,.48), elevation);
}
return lerp(sky, squall, _WeatherSeverity);
}
float3 Sky(float3 ray, float time)
{
float2 wind = float2(.00095,-.00068) * time;
float2 layers = ray.xz * (.73 / (max(0, ray.y) + .18));
float cloud = tex2Dlod(_OceanDetail, float4(layers * float2(.14,.22) + wind, 0, 2)).b * .74
+ tex2Dlod(_OceanDetail, float4(layers * .31 + wind * 1.41 + .38, 0, 2)).b * .26;
float coverage = smoothstep(.52 - .13 * _WeatherSeverity, .76, cloud);
coverage *= smoothstep(-.010,.097,ray.y) * (1 - smoothstep(.61, 1, ray.y));
float sunlight = saturate(dot(ray, Sun));
float3 color = SkyGradient(ray);
float3 vapor = lerp(color * 1.08 + float3(.134,.115,.095) * (.44 + .56 * pow(sunlight, 5)),
color * .63 + float3(.044,.056,.069), _WeatherSeverity);
float glow = .12 * pow(sunlight, 23) + .37 * smoothstep(.9989, .9997, sunlight);
return lerp(color, vapor, coverage) + float3(1,.85,.69) * glow * (1 - _WeatherSeverity) * (1 - coverage);
}
float2 HullPoint(float2 world)
{
float2 relative = world - _HullSpan.zw;
return float2(dot(relative, float2(_HullCourse.x, -_HullCourse.y)), dot(relative, _HullCourse.yx));
}
float HullEdge(float2 hull)
{
// Match the outline near the waterline, where the stern stays wider than the bow.
float station = saturate(hull.x / _HullSpan.x + .5);
float stern = lerp(.52, 1, sin(saturate(station / .16) * 1.570796));
float bow = cos(saturate((station - .69) / .31) * 1.570796);
float side = abs(hull.y) - .95 * _HullSpan.y * stern * bow;
float cap = abs(hull.x) - .495 * _HullSpan.x;
return length(max(float2(side, cap), 0));
}
float2 BowWhitewater(float2 hull, float gap, float eddies, float grain)
{
float length = _HullSpan.x;
float speed = sqrt(_HullCourse.z);
float behind = length * .494 - hull.x;
float distance = max(0, behind);
float current = eddies - .5;
float wedge = abs(hull.y) - (1.1 + distance * .35 + current * (1 + distance * .04));
float width = .85 + distance * .032;
float bowWave = 1 - smoothstep(.05, width, abs(wedge));
bowWave *= smoothstep(-2.3, 5.2, behind) * (1 - smoothstep(length * .48, length * .93, behind));
float stem = 1 - smoothstep(.1, 1.9 + speed * 2.1, abs(behind + .6) + abs(hull.y) * .80);
float wash = (1 - smoothstep(.16, 1.05 + speed + .75 * eddies, gap)) *
smoothstep(-1.0, 7, behind) * (1 - smoothstep(length * .78, length * .99, behind));
float froth = saturate(.75 * bowWave + .98 * stem + .55 * wash);
float foamCells = smoothstep(.31, .75, eddies * .66 + grain * .34 + froth * .17);
return speed * float2(saturate(.64 * froth + .37 * wash), froth * (.15 + .78 * foamCells));
}
float2 WindSlope(float2 position, float eddies, float time)
{
// Short cross-seas add structure between the long swells and the texture ripples.
float2 wind = float2(.45, .89);
float2 swell = float2(-1.08, .67);
float drift = (eddies - .5) * 4.6;
float a = cos(dot(position, wind) - time * 2.15 + drift);
float b = cos(dot(position, swell) - time * 2.76 - drift * .9);
return wind * (.069 * a) + swell * (.043 * b);
}
float3 OpenSea(float3 origin, float3 ray, float2 screen)
{
float skyLine = smoothstep(-.0033, .0010, ray.y);
float down = max(.00042, -ray.y);
float distance = clamp(origin.y / down, 0, 5800);
float2 position = origin.xz + ray.xz * distance;
float3 waves = OceanWave(position, _SeaTime, _WeatherSeverity);
float intersectionBlend = 1 - smoothstep(180, 600, distance);
float approach = max(down * .5, down + dot(waves.yz, ray.xz));
distance = max(0, distance - clamp(waves.x / approach, -9, 9) * intersectionBlend);
position = origin.xz + ray.xz * distance;
waves = OceanWave(position, _SeaTime, _WeatherSeverity);
float2 rotated = float2(dot(position, float2(.835, -.550)), dot(position, float2(.550, .835)));
float2 crossed = float2(dot(position, float2(.939, .344)), dot(position, float2(-.344, .939)));
float3 chop = tex2D(_OceanDetail, position * .028 + float2(-.018,.012) * _SeaTime).rgb;
float3 ripples = tex2D(_OceanDetail, rotated * .076 + float2(.026,.019) * _SeaTime).rgb;
float3 surface = tex2D(_OceanDetail, crossed * .179 + float2(-.042,.036) * _SeaTime).rgb;
float2 rippleSlope = ripples.rg - .5;
rippleSlope = float2(dot(rippleSlope, float2(.835, .550)), dot(rippleSlope, float2(-.550, .835)));
float2 surfaceSlope = surface.rg - .5;
surfaceSlope = float2(dot(surfaceSlope, float2(.939, -.344)), dot(surfaceSlope, float2(.344, .939)));
float swellDetail = 1 - smoothstep(175, 700, distance);
float textureDetail = 1 - smoothstep(68, 525, distance);
float surfaceDetail = 1 - smoothstep(23, 175, distance);
float roughness = lerp(.60, 1.18, _WeatherSeverity) * lerp(1, .66, _TycoonRiverWater);
float2 slope = waves.yz * swellDetail + WindSlope(position, chop.b, _SeaTime) * textureDetail * roughness
+ ((chop.rg - .5) * .34 + rippleSlope * .29) * textureDetail * roughness
+ surfaceSlope * (.115 * surfaceDetail * roughness);
float3 normal = normalize(float3(-slope.x, 1, -slope.y));
float3 reflected = reflect(ray, normal);
float facing = 1 - saturate(dot(-ray, normal));
float reflection = .095 + .81 * facing * facing * facing * facing * facing;
float cap = saturate(dot(normal, Sun) * .84 + (chop.b - .5) * textureDetail * .63 + .35);
float3 fromBelow = lerp(float3(.028,.125,.192), float3(.078,.250,.294), cap);
fromBelow = lerp(fromBelow, fromBelow * .57 + float3(.022,.032,.036), _WeatherSeverity);
float3 riverWater = lerp(float3(.061,.130,.113), float3(.139,.249,.215), cap);
fromBelow = lerp(fromBelow, riverWater, _TycoonRiverWater);
if (_PortMotion > .5)
{
float3 shallowWater = lerp(float3(.03,.31,.32), float3(.15,.52,.48), cap);
fromBelow = lerp(fromBelow, shallowWater, _PortShallows);
}
float3 color = lerp(fromBelow, SkyGradient(reflected) * .83, reflection);
float sunHit = saturate(dot(reflected, Sun));
float nearSun = pow(sunHit, lerp(162, 60, _WeatherSeverity));
float reflectedLight = pow(sunHit, lerp(28, 15, _WeatherSeverity));
color += (nearSun * .43 + reflectedLight * .13) * float3(1,.86,.65) * (1 - _WeatherSeverity * .88);
float crest = waves.x / WaveAmplitude(_WeatherSeverity);
float churn = .55 * chop.b + .28 * ripples.b + .17 * surface.b;
float capFoam = smoothstep(.65, .85, churn + crest * .19 + length(slope) * .17);
color = lerp(color, float3(.64,.74,.76), capFoam * textureDetail * (.035 + _WeatherSeverity * .58)
* lerp(1, .26, _TycoonRiverWater));
if (_HullSpan.x > 1)
{
float2 hull = HullPoint(position);
if (abs(hull.x) < _HullSpan.x * .5 + 6 && abs(hull.y) < _HullSpan.x * .34 + 6)
{
float gap = HullEdge(hull);
color *= 1 - (1 - smoothstep(.18, 5.3, gap)) * .31;
float2 foam = BowWhitewater(hull, gap, .67 * ripples.b + .33 * chop.b, surface.b);
color = lerp(color, float3(.32,.57,.61), foam.x * .52);
color = lerp(color, float3(.88,.94,.94), foam.y);
}
}
if (_PortMotion > .5)
{
float2 quayEdge = max(0, abs(position - _PortBerth.xy) - _PortBerth.zw);
color *= 1 - (1 - smoothstep(.14, 3.4, length(quayEdge))) * .29;
}
float mist = 1 - exp(-distance * (.00040 + .00077 * _WeatherSeverity));
color = lerp(color, SkyGradient(float3(ray.x, 0, ray.z)) * .90, mist * .80);
if (skyLine > 0)
color = lerp(color, Sky(ray, _SeaTime), skyLine);
if (_WeatherSeverity > .75)
{
float2 grid = float2(screen.x * 165 + screen.y * 42, screen.y * 23 + _SeaTime * 17);
float rain = step(.978, frac(grid.x + floor(grid.y) * .381)) * smoothstep(.56, .68, frac(grid.y));
color += rain * float3(.095,.133,.15);
}
return color;
}
float3 PortMatteWater(float3 origin, float3 ray, float2 screen)
{
float2 uv = (screen - .5) * _PortMatteCrop.xy + .5 + _PortMatteCrop.zw;
float water = 1 - smoothstep(.28,.55,uv.y);
float viewDistance = clamp(origin.y / max(.00042, -ray.y), 0, 1200);
float2 world = origin.xz + ray.xz * viewDistance;
float ripple = sin(dot(world, float2(.54,1.16)) - _SeaTime * 1.72);
uv.x += ripple * (.00058 * water);
float3 matte = tex2D(_PortMatte, uv).rgb;
if (water < .009 || ray.y > 0)
return matte;
float3 sea = OpenSea(origin, ray, screen);
// Preserve the distant quay's photographic reflection while the ship and its wake move over live water.
return lerp(matte, sea, water * .74);
}
fixed4 frag(Output input) : SV_Target
{
float2 screen = input.screen;
float2 offsetOnFilm = (screen * 2 - 1) * _ViewSize.xy;
float3 viewOffset = offsetOnFilm.x * _ViewRight + offsetOnFilm.y * _ViewUp;
float3 origin = _ViewOrigin + viewOffset * _ViewSize.z;
float3 ray = normalize(_ViewForward + viewOffset * (1 - _ViewSize.z));
if (_PortMotion > .5 && _PortMatteActive > .5)
return fixed4(PortMatteWater(origin, ray, screen), 1);
if (_Underway > .5 || _PortMotion > .5)
{
if (ray.y >= .0010)
return fixed4(Sky(ray, _SeaTime), 1);
return fixed4(OpenSea(origin, ray, screen), 1);
}
float2 uv = (screen - .5) * _BackdropCrop.xy + .5 + _BackdropCrop.zw;
float waterMask = 1 - smoothstep(.55, .69, uv.y);
float distance = min(2600, max(0, -origin.y / min(ray.y, -.0001)));
float2 world = origin.xz + ray.xz * distance;
float wave = sin(dot(world, float2(.52,1.27)) - _Time.y * 1.3);
float crossWave = sin(dot(world, float2(-.85,.41)) - _Time.y * 1.6);
uv += waterMask * float2(.00089 * wave + .00045 * crossWave, .00028 * crossWave);
float3 color = tex2D(_Backdrop, uv).rgb;
float3 harborRipples = tex2D(_OceanDetail, world * .072 + float2(.009,-.014) * _Time.y).rgb;
float glimmer = (harborRipples.r - .5) * (.63 + .37 * wave) + (harborRipples.b - .5) * .22;
color += waterMask * (1 - smoothstep(28, 200, distance)) * glimmer * float3(.019,.028,.035);
if (_HullSpan.x > 1)
color *= 1 - (1 - smoothstep(.18, 5.3, HullEdge(HullPoint(world)))) * .29 * waterMask;
return fixed4(color, 1);
}
ENDCG
}
}
}