Improve close-up ship cameras and marine rendering
This commit is contained in:
@@ -21,10 +21,11 @@ namespace Soundings
|
||||
private string currentBackplate;
|
||||
private ulong lastLayout;
|
||||
private const float DefaultYaw = 37f, DefaultPitch = 17.5f;
|
||||
private const float OrbitNearClip = .25f, OrbitClearance = .90f;
|
||||
private float yaw = DefaultYaw, pitch = DefaultPitch, zoom = 1f;
|
||||
private float targetYaw = DefaultYaw, targetPitch = DefaultPitch, targetZoom = 1f;
|
||||
private Vector3 orbitOffset, targetOrbitOffset;
|
||||
private float seaTime, seaSeverity, passageDistance;
|
||||
private Vector2 seaCenter;
|
||||
private Vector3 hullMotion, hullMotionVelocity;
|
||||
private bool sailing;
|
||||
private Bounds hullBounds;
|
||||
@@ -40,12 +41,18 @@ namespace Soundings
|
||||
private ShipWalkSpace walkSpace;
|
||||
|
||||
private enum CameraMode { Orbit, DeckWalk, Bridge, Service }
|
||||
internal enum SurfaceFinish { Paint, Timber, Glass, Fabric, Alloy, Hull, Pool }
|
||||
|
||||
public RenderTexture Frame { get; private set; }
|
||||
internal Vector3 PassageWorldPosition => vessel != null ? vessel.position : Vector3.zero;
|
||||
internal bool PassageUsesPerspective => sailing && cameraMode == CameraMode.Orbit && !viewCamera.orthographic;
|
||||
internal Vector3 FirstPersonPosition => freePosition;
|
||||
internal bool FirstPersonPositionIsClear => walkSpace != null && walkSpace.CanStand(freePosition);
|
||||
internal Vector3 OrbitTarget => OrbitFocus(orbitOffset);
|
||||
internal float OrbitVerticalSpan => viewCamera.orthographic ? viewCamera.orthographicSize * 2f
|
||||
: 2f * Vector3.Distance(OrbitTarget, viewCamera.transform.position) * HalfFieldOfView(viewCamera.fieldOfView);
|
||||
internal bool OrbitCameraIsClear => vessel == null ||
|
||||
!OrbitVolume().Contains(vessel.InverseTransformPoint(viewCamera.transform.position));
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
@@ -106,14 +113,37 @@ namespace Soundings
|
||||
freeYaw = Mathf.Repeat(freeYaw + degrees, 360);
|
||||
}
|
||||
|
||||
public void Zoom(float delta)
|
||||
public void Zoom(float delta, Vector2? viewportPoint = null)
|
||||
{
|
||||
if (cameraMode != CameraMode.Orbit)
|
||||
{
|
||||
freeFov = Mathf.Clamp(freeFov * Mathf.Exp(delta * .035f), 25f, 78f);
|
||||
return;
|
||||
}
|
||||
targetZoom = Mathf.Clamp(targetZoom * Mathf.Exp(delta * .050f), sailing ? .62f : .64f, sailing ? 1.82f : 1.51f);
|
||||
float aspect = ViewAspect;
|
||||
float overview = OverviewZoom;
|
||||
float next = Mathf.Clamp(targetZoom * Mathf.Exp(delta * .082f), MinimumZoom(aspect), sailing ? 1.82f : 1.51f);
|
||||
if (next < targetZoom && next < overview && viewportPoint is { } point && IsShipAt(point))
|
||||
{
|
||||
// Keep the point under the pointer on the orbit's focal plane while it grows on screen.
|
||||
float distanceOnFilm = OverviewHalfHeight(aspect) * (Mathf.Min(overview, targetZoom) - next);
|
||||
targetOrbitOffset = BoundOrbitOffset(targetOrbitOffset + FilmToShip((point - Vector2.one * .5f) * 2f,
|
||||
aspect, distanceOnFilm));
|
||||
}
|
||||
else if (next > targetZoom && targetZoom < overview)
|
||||
{
|
||||
targetOrbitOffset *= Mathf.Clamp01((overview - next) / (overview - targetZoom));
|
||||
}
|
||||
targetZoom = next;
|
||||
}
|
||||
|
||||
public void Pan(Vector2 viewportDrag)
|
||||
{
|
||||
if (cameraMode != CameraMode.Orbit || vessel == null || targetZoom >= OverviewZoom)
|
||||
return;
|
||||
float aspect = ViewAspect;
|
||||
targetOrbitOffset = BoundOrbitOffset(targetOrbitOffset - FilmToShip(viewportDrag * 2f,
|
||||
aspect, OverviewHalfHeight(aspect) * targetZoom));
|
||||
}
|
||||
|
||||
public void ResetView(bool immediate = false)
|
||||
@@ -142,12 +172,14 @@ namespace Soundings
|
||||
walkSpace = null;
|
||||
targetYaw = deckIndex >= 0 ? 17f : sailing ? 24f : DefaultYaw;
|
||||
targetPitch = deckIndex >= 0 ? 57f : sailing ? 13f : DefaultPitch;
|
||||
targetZoom = deckIndex >= 0 ? .92f : 1f;
|
||||
targetZoom = OverviewZoom;
|
||||
targetOrbitOffset = Vector3.zero;
|
||||
if (!immediate)
|
||||
return;
|
||||
yaw = targetYaw;
|
||||
pitch = targetPitch;
|
||||
zoom = targetZoom;
|
||||
orbitOffset = Vector3.zero;
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
@@ -245,7 +277,10 @@ namespace Soundings
|
||||
|
||||
public bool IsShipAt(Vector2 viewportPoint)
|
||||
{
|
||||
return vessel != null && hullBounds.IntersectRay(viewCamera.ViewportPointToRay(viewportPoint));
|
||||
if (vessel == null)
|
||||
return false;
|
||||
Ray ray = viewCamera.ViewportPointToRay(viewportPoint);
|
||||
return OrbitVolume().IntersectRay(new Ray(vessel.InverseTransformPoint(ray.origin), vessel.InverseTransformDirection(ray.direction)));
|
||||
}
|
||||
|
||||
public void BeginPassage(WeatherCondition weather = WeatherCondition.Calm, float duration = 180f)
|
||||
@@ -272,7 +307,6 @@ namespace Soundings
|
||||
float sine = Mathf.Sin(headingRadians);
|
||||
float cosine = Mathf.Cos(headingRadians);
|
||||
var center = new Vector2(routePosition.x, -routePosition.y) * passageDistance;
|
||||
seaCenter = center;
|
||||
if (vessel != null && delta > 0)
|
||||
{
|
||||
// Use wave heights along the waterline to drive a damped heave, pitch and roll.
|
||||
@@ -312,7 +346,6 @@ namespace Soundings
|
||||
public void EndPassage()
|
||||
{
|
||||
sailing = false;
|
||||
seaCenter = Vector2.zero;
|
||||
waterSurface.localPosition = Vector3.zero;
|
||||
wake.Reset();
|
||||
Shader.SetGlobalFloat("_SoundingsSeaSailing", 0);
|
||||
@@ -340,24 +373,98 @@ namespace Soundings
|
||||
}
|
||||
float easing = 1 - Mathf.Exp(-Time.unscaledDeltaTime * (sailing ? 12f : 11f));
|
||||
if (!sailing && Mathf.Abs(yaw - targetYaw) < .005f && Mathf.Abs(pitch - targetPitch) < .005f &&
|
||||
Mathf.Abs(zoom - targetZoom) < .0001f)
|
||||
Mathf.Abs(zoom - targetZoom) < .0001f && (orbitOffset - targetOrbitOffset).sqrMagnitude < .00001f)
|
||||
return;
|
||||
yaw = Mathf.Repeat(yaw + Mathf.DeltaAngle(yaw, targetYaw) * easing + 180f, 360f) - 180f;
|
||||
pitch = Mathf.Lerp(pitch, targetPitch, easing);
|
||||
zoom = Mathf.Lerp(zoom, targetZoom, easing);
|
||||
orbitOffset = Vector3.Lerp(orbitOffset, targetOrbitOffset, easing);
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
private Vector3 OrbitFocus()
|
||||
private float ViewAspect => Frame == null ? 16f / 9f : (float)Frame.width / Frame.height;
|
||||
private float OverviewZoom => deckIndex >= 0 ? .92f : 1f;
|
||||
private float OverviewFieldOfView => deckIndex >= 0 ? 45f : 38f;
|
||||
private static float HalfFieldOfView(float fieldOfView) => Mathf.Tan(fieldOfView * Mathf.Deg2Rad * .5f);
|
||||
private float OverviewDistance(float aspect) => vesselLength * Mathf.Max(1.29f, 2.16f / aspect);
|
||||
private float OverviewHalfHeight(float aspect) => sailing ? OverviewDistance(aspect) * HalfFieldOfView(OverviewFieldOfView)
|
||||
: Mathf.Max(17.6f, vesselLength * .71f / aspect);
|
||||
private float MinimumZoom(float aspect) => (deckIndex >= 0 ? 2.65f : 3.10f) / OverviewHalfHeight(aspect);
|
||||
|
||||
private Vector3 OverviewFocus()
|
||||
{
|
||||
float level = deckIndex >= 0 && decks != null ? decks.FloorHeight(deckIndex) + .5f : vesselTop * .39f;
|
||||
var center = new Vector3(deckIndex >= 0 ? -2f : 2.1f, level, 0);
|
||||
if (sailing)
|
||||
return new Vector3(deckIndex >= 0 ? -2f : 2.1f, level, 0);
|
||||
}
|
||||
|
||||
private Vector3 OrbitFocus(Vector3 offset)
|
||||
{
|
||||
Vector3 local = OverviewFocus() + offset;
|
||||
return vessel != null ? vessel.TransformPoint(local) : local;
|
||||
}
|
||||
|
||||
private Vector3 FilmToShip(Vector2 filmOffset, float aspect, float height)
|
||||
{
|
||||
Quaternion orbit = Quaternion.Euler(targetPitch, -targetYaw, 0);
|
||||
Vector3 world = orbit * new Vector3(filmOffset.x * aspect, filmOffset.y, 0) * height;
|
||||
if (deckIndex >= 0)
|
||||
{
|
||||
center.x += seaCenter.x;
|
||||
center.z = seaCenter.y;
|
||||
Vector3 back = orbit * Vector3.back;
|
||||
float rise = Vector3.Dot(back, vessel.up);
|
||||
if (rise > .20f)
|
||||
world -= back * (Vector3.Dot(world, vessel.up) / rise);
|
||||
}
|
||||
return center;
|
||||
return vessel.InverseTransformDirection(world);
|
||||
}
|
||||
|
||||
private Vector3 BoundOrbitOffset(Vector3 offset)
|
||||
{
|
||||
Vector3 home = OverviewFocus();
|
||||
Vector3 focus = home + offset;
|
||||
if (deckIndex >= 0)
|
||||
{
|
||||
float length = VesselExterior.DeckLength(vesselLength, deckIndex);
|
||||
float center = VesselExterior.DeckOffset(vesselLength, deckIndex);
|
||||
float halfBeam = VesselExterior.DeckHalfBeam(vesselBeam, deckIndex);
|
||||
focus.x = Mathf.Clamp(focus.x, center - length * .45f, center + length * .40f);
|
||||
focus.y = home.y;
|
||||
focus.z = Mathf.Clamp(focus.z, -halfBeam, halfBeam);
|
||||
}
|
||||
else
|
||||
{
|
||||
focus.x = Mathf.Clamp(focus.x, -vesselLength * .48f, vesselLength * .48f);
|
||||
focus.y = Mathf.Clamp(focus.y, 2.25f, vesselTop - 1.8f);
|
||||
focus.z = Mathf.Clamp(focus.z, -vesselBeam, vesselBeam);
|
||||
}
|
||||
return focus - home;
|
||||
}
|
||||
|
||||
private Bounds OrbitVolume()
|
||||
{
|
||||
if (deckIndex < 0 || decks == null)
|
||||
return hullBounds;
|
||||
Bounds cutaway = hullBounds;
|
||||
Vector3 top = cutaway.max;
|
||||
top.y = decks.FloorHeight(deckIndex) + 2.55f;
|
||||
cutaway.max = top;
|
||||
return cutaway;
|
||||
}
|
||||
|
||||
private float ClearOrbitDistance(Vector3 focus, Vector3 direction)
|
||||
{
|
||||
float waterDistance = (1.96f - focus.y) / Mathf.Max(.01f, direction.y);
|
||||
if (vessel == null)
|
||||
return waterDistance;
|
||||
|
||||
// Cast back toward the focus to find the outer face even when the focus is inside the ship.
|
||||
Bounds volume = OrbitVolume();
|
||||
volume.Expand(OrbitNearClip * 3f);
|
||||
Vector3 local = vessel.InverseTransformPoint(focus);
|
||||
Vector3 outward = vessel.InverseTransformDirection(direction);
|
||||
float probe = volume.size.magnitude + (local - volume.center).magnitude;
|
||||
var ray = new Ray(local + outward * probe, -outward);
|
||||
return volume.IntersectRay(ray, out float hit) ? Mathf.Max(waterDistance, probe - hit + OrbitClearance)
|
||||
: waterDistance;
|
||||
}
|
||||
|
||||
private void ComposeCamera()
|
||||
@@ -365,12 +472,21 @@ namespace Soundings
|
||||
if (cameraMode == CameraMode.Orbit)
|
||||
{
|
||||
viewCamera.orthographic = !sailing;
|
||||
viewCamera.nearClipPlane = .25f;
|
||||
viewCamera.fieldOfView = deckIndex >= 0 ? 45f : 38f;
|
||||
float aspect = Frame == null ? 16f / 9f : (float)Frame.width / Frame.height;
|
||||
float distance = sailing ? vesselLength * Mathf.Max(1.29f, 2.16f / aspect) * zoom : 244f;
|
||||
viewCamera.nearClipPlane = OrbitNearClip;
|
||||
viewCamera.fieldOfView = OverviewFieldOfView;
|
||||
Quaternion orbit = Quaternion.Euler(pitch, -yaw, 0);
|
||||
viewCamera.transform.SetPositionAndRotation(OrbitFocus() + orbit * (Vector3.back * distance), orbit);
|
||||
Vector3 focus = OrbitTarget;
|
||||
Vector3 back = orbit * Vector3.back;
|
||||
float distance = 244f;
|
||||
if (sailing)
|
||||
{
|
||||
float dolly = OverviewDistance(ViewAspect) * zoom;
|
||||
distance = Mathf.Max(dolly, ClearOrbitDistance(focus, back));
|
||||
// When the hull blocks the dolly, keep the same framing with the lens.
|
||||
if (distance > dolly)
|
||||
viewCamera.fieldOfView = Mathf.Atan(OverviewHalfHeight(ViewAspect) * zoom / distance) * 2f * Mathf.Rad2Deg;
|
||||
}
|
||||
viewCamera.transform.SetPositionAndRotation(focus + back * distance, orbit);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -414,18 +530,29 @@ namespace Soundings
|
||||
descriptor.msaaSamples = Mathf.Max(1, SystemInfo.GetRenderTextureSupportedMSAASampleCount(descriptor));
|
||||
Frame = new RenderTexture(descriptor) { name = "Harbor viewport" };
|
||||
viewCamera.targetTexture = Frame;
|
||||
FitViewport();
|
||||
KeepOrbitInRange();
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
private void KeepOrbitInRange()
|
||||
{
|
||||
float minimum = MinimumZoom(ViewAspect);
|
||||
zoom = Mathf.Max(zoom, minimum);
|
||||
targetZoom = Mathf.Max(targetZoom, minimum);
|
||||
orbitOffset = BoundOrbitOffset(orbitOffset);
|
||||
targetOrbitOffset = BoundOrbitOffset(targetOrbitOffset);
|
||||
}
|
||||
|
||||
private void FitViewport()
|
||||
{
|
||||
if (Frame == null)
|
||||
return;
|
||||
float aspect = (float)Frame.width / Frame.height;
|
||||
float aspect = ViewAspect;
|
||||
viewCamera.aspect = aspect;
|
||||
viewCamera.orthographicSize = Mathf.Max(17.6f, vesselLength * .71f / aspect) * zoom;
|
||||
if (viewCamera.orthographic)
|
||||
viewCamera.orthographicSize = OverviewHalfHeight(aspect) * zoom;
|
||||
Transform view = viewCamera.transform;
|
||||
float viewHeight = viewCamera.orthographic ? viewCamera.orthographicSize : Mathf.Tan(viewCamera.fieldOfView * Mathf.Deg2Rad * .5f);
|
||||
float viewHeight = viewCamera.orthographic ? viewCamera.orthographicSize : HalfFieldOfView(viewCamera.fieldOfView);
|
||||
water.SetVector("_ViewOrigin", view.position);
|
||||
water.SetVector("_ViewForward", view.forward);
|
||||
water.SetVector("_ViewRight", view.right);
|
||||
@@ -433,12 +560,15 @@ namespace Soundings
|
||||
water.SetVector("_ViewSize", new Vector4(viewHeight * aspect, viewHeight, viewCamera.orthographic ? 1 : 0, 0));
|
||||
if (backplateRatio <= 0 || sailing)
|
||||
return;
|
||||
float cropX = Mathf.Min(1, aspect / backplateRatio) * .95f;
|
||||
float cropY = Mathf.Min(1, backplateRatio / aspect) * .95f;
|
||||
float backdropScale = Mathf.Lerp(.86f, 1, Mathf.Clamp01(zoom));
|
||||
float cropX = Mathf.Min(1, aspect / backplateRatio) * .95f * backdropScale;
|
||||
float cropY = Mathf.Min(1, backplateRatio / aspect) * .95f * backdropScale;
|
||||
float panoramaYaw = cameraMode == CameraMode.Orbit ? Mathf.DeltaAngle(DefaultYaw, yaw) : Mathf.DeltaAngle(-DefaultYaw, freeYaw);
|
||||
float shiftX = Mathf.Clamp(panoramaYaw * .00225f, -.47f * (1 - cropX), .47f * (1 - cropX));
|
||||
float shiftX = Mathf.Clamp(panoramaYaw * .00225f + Vector3.Dot(view.right, orbitOffset) * .002f,
|
||||
-.47f * (1 - cropX), .47f * (1 - cropX));
|
||||
float panoramaPitch = cameraMode == CameraMode.Orbit ? pitch : freePitch;
|
||||
float shiftY = Mathf.Clamp((panoramaPitch - DefaultPitch) * .002f, -.47f * (1 - cropY), .47f * (1 - cropY));
|
||||
float shiftY = Mathf.Clamp((panoramaPitch - DefaultPitch) * .002f + Vector3.Dot(view.up, orbitOffset) * .002f,
|
||||
-.47f * (1 - cropY), .47f * (1 - cropY));
|
||||
water.SetVector("_BackdropCrop", new Vector4(cropX, cropY, shiftX, shiftY));
|
||||
}
|
||||
|
||||
@@ -481,8 +611,7 @@ namespace Soundings
|
||||
float beam = vesselBeam = ship.Hull.Beam * .5f;
|
||||
vesselTop = VesselExterior.DeckBase + deckCount * VesselExterior.DeckSpacing + 5.95f;
|
||||
water.SetVector("_HullSpan", new Vector4(vesselLength, beam, 0, 0));
|
||||
hullBounds = new Bounds(new Vector3(0, (vesselTop - 2.65f) * .5f, 0), new Vector3(vesselLength, vesselTop + 2.65f, beam * 2.1f));
|
||||
FitViewport();
|
||||
hullBounds = new Bounds(new Vector3(0, (vesselTop - 2.65f) * .5f, 0), new Vector3(vesselLength * 1.05f, vesselTop + 2.65f, beam * 2.26f));
|
||||
|
||||
var exterior = new VesselExterior(ship, vesselLength, beam);
|
||||
foreach (var (paint, batch) in exterior.Meshes())
|
||||
@@ -504,6 +633,8 @@ namespace Soundings
|
||||
deckIndex = -1;
|
||||
decks.Select(-1);
|
||||
}
|
||||
KeepOrbitInRange();
|
||||
ComposeCamera();
|
||||
}
|
||||
|
||||
private void Batch(Mesh mesh, Material material)
|
||||
@@ -535,6 +666,10 @@ namespace Soundings
|
||||
VesselExterior.Paint.LitGlass => ("BB7643", .23f, .25f, .78f, .04f),
|
||||
VesselExterior.Paint.Lounge => ("CC9570", .25f, .20f, .62f, .04f),
|
||||
VesselExterior.Paint.Lifeboat => ("BD542D", .41f, .15f, 0f, .15f),
|
||||
VesselExterior.Paint.Antifouling => ("734938", .76f, .02f, 0f, .42f),
|
||||
VesselExterior.Paint.DeckCanvas => ("315263", .91f, 0f, 0f, .15f),
|
||||
VesselExterior.Paint.PortLight => ("D84738", .25f, .05f, .90f, 0f),
|
||||
VesselExterior.Paint.StarboardLight => ("4DCA8E", .25f, .05f, .90f, 0f),
|
||||
_ => ("318994", .16f, .40f, .05f, .13f)
|
||||
};
|
||||
ColorUtility.TryParseHtmlString("#" + hex, out Color color);
|
||||
@@ -543,6 +678,20 @@ namespace Soundings
|
||||
material.SetFloat("_Metal", metal);
|
||||
material.SetFloat("_Emissive", emissive);
|
||||
material.SetFloat("_Weather", weather);
|
||||
material.SetFloat("_Waterline", paint is VesselExterior.Paint.Navy or VesselExterior.Paint.Antifouling
|
||||
or VesselExterior.Paint.Black ? 1f : 0f);
|
||||
SurfaceFinish finish = paint switch
|
||||
{
|
||||
VesselExterior.Paint.Teak => SurfaceFinish.Timber,
|
||||
VesselExterior.Paint.Glass or VesselExterior.Paint.BlueGlass or VesselExterior.Paint.LitGlass
|
||||
or VesselExterior.Paint.Lounge => SurfaceFinish.Glass,
|
||||
VesselExterior.Paint.DeckCanvas => SurfaceFinish.Fabric,
|
||||
VesselExterior.Paint.Steel or VesselExterior.Paint.Gold => SurfaceFinish.Alloy,
|
||||
VesselExterior.Paint.Navy or VesselExterior.Paint.Antifouling => SurfaceFinish.Hull,
|
||||
VesselExterior.Paint.Pool => SurfaceFinish.Pool,
|
||||
_ => SurfaceFinish.Paint
|
||||
};
|
||||
material.SetFloat("_Finish", (float)finish);
|
||||
var reflected = new Material(reflection) { color = color, name = paint + " reflection" };
|
||||
materials.Add(paint, material);
|
||||
reflections.Add(paint, reflected);
|
||||
|
||||
Reference in New Issue
Block a user