Files
cruiseSimulator/Assets/Soundings/Resources/HarborWater.shader

183 lines
9.7 KiB
GLSL

Shader "Soundings/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)
_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;
float4 _BackdropCrop, _HullSpan, _HullCourse, _ViewSize;
float3 _ViewOrigin, _ViewForward, _ViewRight, _ViewUp;
float _Underway, _WeatherSeverity, _SeaTime;
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(.50,.63,.68), float3(.91,.66,.46), warmth * .88);
float elevation = pow(saturate(ray.y), .52);
float3 sky = lerp(horizon, float3(.084,.218,.347), elevation);
float3 squall = lerp(float3(.39,.48,.53), float3(.102,.156,.202), elevation);
return lerp(sky, squall, _WeatherSeverity);
}
float3 Sky(float3 ray, float time)
{
float2 wind = float2(.0014,-.0010) * time;
float2 layers = ray.xz * (.76 / (max(0, ray.y) + .21)) + wind;
float cloud = tex2Dlod(_OceanDetail, float4(layers * .19, 0, 2)).b * .68
+ tex2Dlod(_OceanDetail, float4(layers * .40 + .38, 0, 2)).b * .32;
float coverage = smoothstep(.57 - .15 * _WeatherSeverity, .82, cloud);
coverage *= smoothstep(-.015,.16,ray.y) * (1 - smoothstep(.66, 1, ray.y));
float facing = pow(saturate(dot(ray, Sun)), 5);
float3 color = SkyGradient(ray);
float3 vapor = lerp(color * 1.14 + float3(.124,.106,.087) * facing,
color * .65 + float3(.038,.048,.056), _WeatherSeverity);
return lerp(color, vapor, coverage * (.65 + .3 * _WeatherSeverity));
}
float2 HullPoint(float2 world)
{
float2 relative = world - _HullSpan.zw;
return float2(dot(relative, float2(_HullCourse.x, -_HullCourse.y)), dot(relative, _HullCourse.yx));
}
float HullShade(float2 hullPoint)
{
float halfLength = _HullSpan.x * .5;
float along = abs(hullPoint.x / max(1, halfLength));
float beam = _HullSpan.y * lerp(.24, 1, saturate(1 - pow(along, 3.2)));
return (1 - smoothstep(.1, 5.8, abs(hullPoint.y) - beam)) * (1 - smoothstep(.91, 1.105, along));
}
float BowWhitewater(float2 hull, float breakup)
{
float beam = _HullSpan.y;
float fromStem = _HullSpan.x * .49 - hull.x;
float steer = sqrt(_HullCourse.z);
float bandWidth = 1.55 + max(0, fromStem) * .022;
float glass = abs(hull.y) - (1 + max(0, fromStem) * .37);
float wedge = 1 - smoothstep(.2, bandWidth, abs(glass));
float stem = 1 - smoothstep(.4, 2.9 + steer * 2.8, length(float2(fromStem + .9, hull.y * .68)));
float flare = smoothstep(-1.9, 7, fromStem) * (1 - smoothstep(_HullSpan.x * .56, _HullSpan.x * 1.16, fromStem));
float rip = .75 + .25 * sin(fromStem * 1.45 + hull.y + _SeaTime * 7);
float nearBeam = beam * lerp(.22, 1, smoothstep(0, _HullSpan.x * .33, fromStem));
float hullWash = 1 - smoothstep(.18, 2.05, abs(abs(hull.y) - nearBeam));
hullWash *= smoothstep(0, 8, fromStem) * (1 - smoothstep(_HullSpan.x * .88, _HullSpan.x, fromStem));
return saturate((wedge * flare * .81 + stem * .89 + hullWash * .57) * rip * breakup) * (steer * .93);
}
float3 OpenSea(float3 origin, float3 ray, float2 screen)
{
float skyLine = smoothstep(-.0033, .0010, ray.y);
float down = max(.00042, -ray.y);
float distance = min(5800, origin.y / down);
float2 position = origin.xz + ray.xz * distance;
float3 waves = OceanWave(position, _SeaTime, _WeatherSeverity);
float intersectionBlend = 1 - smoothstep(180, 600, distance);
position -= ray.xz * clamp(waves.x / down, -9, 9) * intersectionBlend;
waves = OceanWave(position, _SeaTime, _WeatherSeverity);
float2 rotated = float2(position.x * .82 - position.y * .57, position.x * .57 + position.y * .82);
float mip = clamp(log2(max(1, distance) / 76), 0, 7);
float3 chop = tex2Dlod(_OceanDetail, float4(position * .041 + float2(-.022,.013) * _SeaTime, 0, mip)).rgb;
float3 ripples = tex2Dlod(_OceanDetail, float4(rotated * .107 + float2(.032,.020) * _SeaTime, 0, mip + .65)).rgb;
float detail = 1 - smoothstep(85, 670, distance);
float swellDetail = 1 - smoothstep(180, 690, distance);
float roughness = lerp(.43, .96, _WeatherSeverity);
float2 slope = waves.yz * swellDetail
+ ((chop.rg - .5) * .47 + (ripples.rg - .5) * .25) * detail * roughness;
float3 normal = normalize(float3(-slope.x, 1, -slope.y));
float rough = ((chop.b - .5) * .045 + (ripples.b - .5) * .015) * detail;
float light = saturate(dot(normal, Sun));
float3 fromBelow = lerp(float3(.031,.159,.220), float3(.064,.205,.274), .23 + .53 * light + rough * 7);
fromBelow = lerp(fromBelow, fromBelow * .60 + float3(.019,.028,.030), _WeatherSeverity * .82);
float reflection = .19 + .64 * pow(1 - saturate(dot(-ray, normal)), 4);
float3 reflected = reflect(ray, normal);
float3 color = lerp(fromBelow, SkyGradient(reflected) * .75, reflection);
float glint = pow(saturate(dot(reflected, Sun)), lerp(78, 29, _WeatherSeverity));
color += glint * float3(.48,.38,.24) * (1 - .86 * _WeatherSeverity);
float nightFoam = smoothstep(.74,.96, chop.b * .67 + ripples.b * .33 + slope.x * .39);
color = lerp(color, float3(.53,.67,.69), nightFoam * detail * (.22 + _WeatherSeverity * .57));
if (_HullSpan.x > 1)
{
float2 hull = HullPoint(position);
color *= 1 - HullShade(hull) * .35;
float breakup = .63 + smoothstep(.28,.73, chop.b + ripples.b * .33) * .55;
float foam = BowWhitewater(hull, breakup);
color = lerp(color, float3(.79,.89,.89), foam);
}
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;
}
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 (_Underway > .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 textureWave = sin(world.y * 1.94 + world.x * .30 + sin(world.x * .44 - _Time.y * .30)) * .0015;
uv += waterMask * float2(textureWave, textureWave * .14);
float3 color = tex2D(_Backdrop, uv).rgb;
if (_HullSpan.x > 1)
color *= 1 - HullShade(HullPoint(world)) * .33 * waterMask;
return fixed4(color, 1);
}
ENDCG
}
}
}