Improve close-up ship cameras and marine rendering
This commit is contained in:
@@ -7,6 +7,8 @@ Shader "Soundings/Harbor Surface"
|
||||
_Metal ("Metal", Range(0,1)) = 0
|
||||
_Emissive ("Emission", Range(0,1)) = 0
|
||||
_Weather ("Weathering", Range(0,1)) = .1
|
||||
_Finish ("Surface finish", Float) = 0
|
||||
_Waterline ("Opaque hull at the waterline", Float) = 0
|
||||
}
|
||||
SubShader
|
||||
{
|
||||
@@ -14,14 +16,24 @@ Shader "Soundings/Harbor Surface"
|
||||
Pass
|
||||
{
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#include "UnityCG.cginc"
|
||||
#include "OceanWaves.cginc"
|
||||
|
||||
fixed4 _Color;
|
||||
float _Roughness, _Metal, _Emissive, _Weather;
|
||||
float _Roughness, _Metal, _Emissive, _Weather, _Finish, _Waterline;
|
||||
float _SoundingsSeaWeather, _SoundingsSeaSailing, _SoundingsSeaTime;
|
||||
struct Input { float4 vertex : POSITION; float3 normal : NORMAL; };
|
||||
struct Output { float4 clip : SV_POSITION; float3 normal : TEXCOORD0; float3 world : TEXCOORD1; };
|
||||
struct Output
|
||||
{
|
||||
float4 clip : SV_POSITION;
|
||||
float3 normal : TEXCOORD0;
|
||||
float3 world : TEXCOORD1;
|
||||
float3 local : TEXCOORD2;
|
||||
float3 localNormal : TEXCOORD3;
|
||||
};
|
||||
|
||||
Output vert(Input input)
|
||||
{
|
||||
@@ -29,33 +41,122 @@ Shader "Soundings/Harbor Surface"
|
||||
output.clip = UnityObjectToClipPos(input.vertex);
|
||||
output.normal = UnityObjectToWorldNormal(input.normal);
|
||||
output.world = mul(unity_ObjectToWorld, input.vertex).xyz;
|
||||
output.local = input.vertex.xyz;
|
||||
output.localNormal = input.normal;
|
||||
return output;
|
||||
}
|
||||
|
||||
float PanelShade(float2 cell)
|
||||
{
|
||||
float3 p = frac(cell.xyx * .1031);
|
||||
p += dot(p, p.yzx + 33.33);
|
||||
return frac((p.x + p.y) * p.z);
|
||||
}
|
||||
|
||||
// Fade details whose period falls beneath a pixel, especially around the railings.
|
||||
float Detail(float phase)
|
||||
{
|
||||
return 1 - smoothstep(.25, .86, fwidth(phase));
|
||||
}
|
||||
|
||||
float Groove(float phase, float width)
|
||||
{
|
||||
float edge = abs(frac(phase + .5) - .5);
|
||||
float feather = max(.002, fwidth(phase) * .56);
|
||||
return (1 - smoothstep(width, width + feather, edge)) * Detail(phase);
|
||||
}
|
||||
|
||||
float SurfaceTone(float3 p, float3 normal)
|
||||
{
|
||||
float paint = sin(p.x * .82 + p.z * 1.7) * sin(p.y * 3.5 + p.z * 4.8);
|
||||
float tone = 1 + paint * _Weather * .16;
|
||||
if (_Finish == 1)
|
||||
{
|
||||
float horizontal = step(.6, abs(normal.y));
|
||||
float crossGrain = lerp(p.y, p.z, horizontal);
|
||||
float across = crossGrain / .215;
|
||||
float row = floor(across);
|
||||
float along = p.x * .383 + row * .381966;
|
||||
float plank = PanelShade(float2(floor(along), row)) - .5;
|
||||
float fibre = crossGrain * 53 + .73 * sin(p.x * 2.8 + crossGrain * 32);
|
||||
float grain = sin(fibre) * Detail(crossGrain * 8.4);
|
||||
float seams = Groove(across, .014) + Groove(p.x * .383 + frac(row * .381966), .0026);
|
||||
tone = 1 + plank * .16 * Detail(across) + grain * .052 - seams * lerp(.12, .27, horizontal);
|
||||
}
|
||||
else if (_Finish == 3)
|
||||
{
|
||||
float crossFibre = abs(normal.y) > .55 ? p.z : p.y;
|
||||
float weave = cos(p.x * 142) * cos(crossFibre * 153) * Detail(crossFibre * 24.4) * Detail(p.x * 22.6);
|
||||
float mottling = sin(p.x * 11 + crossFibre * 16) * sin(p.x * 23 - crossFibre * 13);
|
||||
tone = 1 + .037 * weave + .028 * mottling * Detail(p.x * 3.7);
|
||||
}
|
||||
else if (_Finish == 4)
|
||||
{
|
||||
float brush = abs(normal.y) > .55 ? p.z : p.y;
|
||||
float scratches = sin(brush * 82 + sin(p.x * 1.25)) * Detail(brush * 13);
|
||||
tone = 1 + scratches * .04 + paint * .025;
|
||||
}
|
||||
else if (_Finish == 5)
|
||||
{
|
||||
float along = p.x + p.z * .42;
|
||||
float runoff = sin(along * 9.1 + sin(along * 2.1)) * sin(along * 2.9 - .2 * p.y);
|
||||
float plates = Groove(p.y * .86, .005) + Groove(along * .23 + floor(p.y * .86) * .5, .0038);
|
||||
float salt = 1 - smoothstep(-.04, .82, p.y);
|
||||
tone = 1 + salt * .11 + (paint * .1 + runoff * .18 * Detail(along * 1.45) - plates * .16) * _Weather;
|
||||
}
|
||||
return tone;
|
||||
}
|
||||
|
||||
fixed4 frag(Output input) : SV_Target
|
||||
{
|
||||
float3 p = input.world;
|
||||
float3 normal = normalize(input.normal);
|
||||
float3 eye = normalize(_WorldSpaceCameraPos.xyz - p);
|
||||
if (_Finish == 6)
|
||||
{
|
||||
float2 p = input.local.xz;
|
||||
float rippleX = p.x * 5.7 + p.y * 2.2 - _Time.y * 2.7;
|
||||
float rippleZ = p.y * 8.3 - p.x * 1.3 + _Time.y * 3.1;
|
||||
float3 ripple = float3(sin(rippleX) * Detail(p.x * .91), 0, sin(rippleZ) * Detail(p.y * 1.32));
|
||||
normal = normalize(normal + mul((float3x3)unity_ObjectToWorld, ripple) * .085);
|
||||
}
|
||||
float3 eye = unity_OrthoParams.w > .5 ? UNITY_MATRIX_V[2].xyz
|
||||
: normalize(_WorldSpaceCameraPos.xyz - input.world);
|
||||
float3 sun = normalize(float3(-.69,.40,-.43));
|
||||
float3 halfVector = normalize(sun + eye);
|
||||
float sunlight = saturate(dot(normal,sun));
|
||||
float sky = saturate(normal.y * .5 + .5);
|
||||
float fresnel = pow(1 - saturate(dot(eye,normal)), 3.5);
|
||||
float roughExponent = lerp(220, 9, _Roughness);
|
||||
float specular = pow(saturate(dot(normal,halfVector)), roughExponent) * (1 - _Roughness * .65);
|
||||
float roughExponent = lerp(280, 6, _Roughness * _Roughness);
|
||||
float specular = pow(saturate(dot(normal,halfVector)), roughExponent) * (1 - _Roughness * .68);
|
||||
float3 reflected = reflect(-eye,normal);
|
||||
float sunsetStripe = smoothstep(-.08,.13,reflected.y) * (1 - smoothstep(.13,.42,reflected.y));
|
||||
float3 skyReflection = lerp(float3(.11,.19,.27), float3(.39,.48,.60), saturate(reflected.y))
|
||||
+ sunsetStripe * float3(.20,.12,.061);
|
||||
float3 light = lerp(float3(.25,.34,.46),float3(.61,.68,.77),sky) + sunlight * float3(.62,.44,.30);
|
||||
float micro = sin(p.x * 13.7 + p.y * 19.3) * sin(p.z * 15.4 - p.x * 8);
|
||||
float abrasion = sin(p.x * .82 + p.z * 1.7) * sin(p.y * 3.5 + p.z * 4.8);
|
||||
float surfaceGrain = 1 + _Weather * (.055 * micro + .10 * abrasion);
|
||||
float3 diffuse = _Color.rgb * light * surfaceGrain;
|
||||
float3 shine = skyReflection * (.07 + fresnel * .24 + _Metal * .21) * (1 - _Roughness * .48)
|
||||
+ specular * float3(.72,.51,.34) * (.14 + .59 * _Metal);
|
||||
return fixed4(saturate(diffuse + shine + _Color.rgb * _Emissive * .60), 1);
|
||||
float overcast = _SoundingsSeaWeather * _SoundingsSeaSailing;
|
||||
float3 direct = lerp(float3(.58,.46,.34), float3(.20,.24,.28), overcast);
|
||||
float3 light = lerp(float3(.26,.34,.43), float3(.62,.69,.75), sky) + sunlight * direct;
|
||||
float3 diffuse = _Color.rgb * light * SurfaceTone(input.local, normalize(input.localNormal));
|
||||
if (_Waterline > .5 && input.world.y < 2.15)
|
||||
{
|
||||
// The ocean is drawn as a screen-wide pass, so mask submerged paint against the same wave field.
|
||||
float level = _SoundingsSeaSailing > .5
|
||||
? OceanWave(input.world.xz, _SoundingsSeaTime, _SoundingsSeaWeather).x : 0;
|
||||
float aboveWater = input.world.y - level;
|
||||
clip(aboveWater);
|
||||
diffuse *= lerp(.73, 1, smoothstep(0, .62, aboveWater));
|
||||
}
|
||||
if (_Finish == 2)
|
||||
{
|
||||
float horizon = 1 - smoothstep(.005, .067 + _Roughness * .12, abs(reflected.y - .025));
|
||||
float3 glazing = skyReflection * (.30 + fresnel * .62) + float3(.21,.32,.36) * horizon * .28;
|
||||
float facing = smoothstep(-.12, .49, reflected.y);
|
||||
float3 glass = diffuse * (.47 + facing * .30) + glazing + specular * float3(.65,.55,.41) * .33;
|
||||
return fixed4(saturate(lerp(glass, diffuse + glazing * .32 + _Color.rgb * .22, _Emissive)), 1);
|
||||
}
|
||||
if (_Finish == 6)
|
||||
return fixed4(saturate(diffuse * .81 + skyReflection * (.24 + fresnel * .61) + specular * .44), 1);
|
||||
float3 shine = skyReflection * (.045 + fresnel * .21 + _Metal * .16) * (1 - _Roughness * .56)
|
||||
+ specular * float3(.73,.60,.43) * (.10 + .47 * _Metal) * saturate(3 * sunlight);
|
||||
return fixed4(saturate(diffuse + shine + _Color.rgb * _Emissive * .53), 1);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user