751 lines
34 KiB
C#
751 lines
34 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Soundings
|
|
{
|
|
public sealed partial class Harbor : MonoBehaviour
|
|
{
|
|
private readonly Dictionary<VesselExterior.Paint, Material> materials = new Dictionary<VesselExterior.Paint, Material>();
|
|
private readonly Dictionary<VesselExterior.Paint, Material> reflections = new Dictionary<VesselExterior.Paint, Material>();
|
|
private readonly Dictionary<string, Texture2D> backplates = new Dictionary<string, Texture2D>();
|
|
private readonly List<Mesh> shipMeshes = new List<Mesh>();
|
|
private Camera viewCamera;
|
|
private Transform vessel;
|
|
private Transform waterSurface;
|
|
private Material water;
|
|
private Mesh waterMesh;
|
|
private PassageWake wake;
|
|
private Shader surface, reflection;
|
|
private float backplateRatio, vesselLength = 72, vesselTop = 16.4f;
|
|
private int lastHull = -2, lastDecks;
|
|
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 Vector3 hullMotion, hullMotionVelocity;
|
|
private bool sailing;
|
|
private Bounds hullBounds;
|
|
private DeckPassengers decks;
|
|
private Transform exteriorRoot;
|
|
private Ship displayedShip;
|
|
private ServiceDeckModel serviceDeck;
|
|
private int deckIndex = -1, serviceArea;
|
|
private float vesselBeam = 6.65f;
|
|
private CameraMode cameraMode;
|
|
private float freeYaw, freePitch, freeFov = 47f;
|
|
private Vector3 freePosition;
|
|
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 bool HasActiveGuest(GuestActivity activity) => decks?.HasActiveGuest(activity) == true;
|
|
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()
|
|
{
|
|
surface = Resources.Load<Shader>("HarborSurface");
|
|
reflection = Resources.Load<Shader>("HarborReflection");
|
|
water = new Material(Resources.Load<Shader>("HarborWater"));
|
|
var oceanDetail = Resources.Load<Texture2D>("Art/OceanDetail");
|
|
water.SetTexture("_OceanDetail", oceanDetail);
|
|
|
|
var cameraObject = new GameObject("Harbor camera", typeof(Camera));
|
|
cameraObject.transform.SetParent(transform, false);
|
|
viewCamera = cameraObject.GetComponent<Camera>();
|
|
viewCamera.orthographic = true;
|
|
viewCamera.clearFlags = CameraClearFlags.SolidColor;
|
|
viewCamera.backgroundColor = new Color(.09f, .17f, .23f);
|
|
viewCamera.nearClipPlane = .25f;
|
|
viewCamera.farClipPlane = 1300;
|
|
viewCamera.cullingMask = 1 << 8;
|
|
viewCamera.enabled = false;
|
|
|
|
ComposeCamera();
|
|
|
|
var waterObject = new GameObject("Harbor backdrop and water", typeof(MeshFilter), typeof(MeshRenderer));
|
|
waterObject.layer = 8;
|
|
waterObject.transform.SetParent(transform, false);
|
|
waterSurface = waterObject.transform;
|
|
var ocean = new HarborMesh();
|
|
ocean.Quad(new Vector3(-2000, 0, -2000), new Vector3(-2000, 0, 2000),
|
|
new Vector3(2000, 0, 2000), new Vector3(2000, 0, -2000));
|
|
waterMesh = ocean.Bake();
|
|
waterMesh.bounds = new Bounds(Vector3.zero, Vector3.one * 4000);
|
|
waterObject.GetComponent<MeshFilter>().sharedMesh = waterMesh;
|
|
waterObject.GetComponent<MeshRenderer>().sharedMaterial = water;
|
|
wake = new PassageWake(transform, oceanDetail);
|
|
}
|
|
|
|
public void SetVisible(bool visible) => viewCamera.enabled = visible;
|
|
|
|
public void Orbit(Vector2 drag)
|
|
{
|
|
if (cameraMode != CameraMode.Orbit)
|
|
return;
|
|
targetYaw -= drag.x * .18f;
|
|
if (sailing)
|
|
targetYaw = Mathf.Repeat(targetYaw + 180f, 360f) - 180f;
|
|
else
|
|
targetYaw = Mathf.Clamp(targetYaw, 3f, 72f);
|
|
targetPitch = Mathf.Clamp(targetPitch + drag.y * .13f, sailing ? 4f : 12f, deckIndex >= 0 ? 79f : 69f);
|
|
}
|
|
|
|
public void RotateView(float degrees)
|
|
{
|
|
if (Mathf.Abs(degrees) < .0001f)
|
|
return;
|
|
if (cameraMode == CameraMode.Orbit)
|
|
Orbit(new Vector2(degrees / .18f, 0));
|
|
else
|
|
freeYaw = Mathf.Repeat(freeYaw + degrees, 360);
|
|
}
|
|
|
|
public void Zoom(float delta, Vector2? viewportPoint = null)
|
|
{
|
|
if (cameraMode != CameraMode.Orbit)
|
|
{
|
|
freeFov = Mathf.Clamp(freeFov * Mathf.Exp(delta * .035f), 25f, 78f);
|
|
return;
|
|
}
|
|
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)
|
|
{
|
|
if (cameraMode == CameraMode.Bridge)
|
|
{
|
|
ViewBridge();
|
|
return;
|
|
}
|
|
if (cameraMode == CameraMode.Service)
|
|
{
|
|
ExploreServiceArea(serviceArea);
|
|
return;
|
|
}
|
|
if (cameraMode == CameraMode.DeckWalk)
|
|
{
|
|
ExploreDeck(deckIndex);
|
|
return;
|
|
}
|
|
ResetOrbit(immediate);
|
|
}
|
|
|
|
private void ResetOrbit(bool immediate)
|
|
{
|
|
cameraMode = CameraMode.Orbit;
|
|
walkSpace = null;
|
|
targetYaw = deckIndex >= 0 ? 17f : sailing ? 24f : DefaultYaw;
|
|
targetPitch = deckIndex >= 0 ? 57f : sailing ? 13f : DefaultPitch;
|
|
targetZoom = OverviewZoom;
|
|
targetOrbitOffset = Vector3.zero;
|
|
if (!immediate)
|
|
return;
|
|
yaw = targetYaw;
|
|
pitch = targetPitch;
|
|
zoom = targetZoom;
|
|
orbitOffset = Vector3.zero;
|
|
ComposeCamera();
|
|
}
|
|
|
|
public void Look(Vector2 drag)
|
|
{
|
|
if (!sailing || drag.sqrMagnitude < .0001f)
|
|
return;
|
|
if (cameraMode == CameraMode.Orbit)
|
|
{
|
|
Orbit(drag);
|
|
return;
|
|
}
|
|
freeYaw = Mathf.Repeat(freeYaw + drag.x * .22f, 360f);
|
|
freePitch = Mathf.Clamp(freePitch + drag.y * .19f, -80f, 84f);
|
|
}
|
|
|
|
public void Walk(Vector2 input, float delta, bool boost)
|
|
{
|
|
if (!sailing || walkSpace == null || input.sqrMagnitude < .0001f || delta <= 0)
|
|
return;
|
|
input = Vector2.ClampMagnitude(input, 1f);
|
|
Quaternion heading = Quaternion.Euler(0, freeYaw, 0);
|
|
Vector3 movement = heading * new Vector3(input.x, 0, input.y);
|
|
float speed = boost ? 2.85f : 1.65f;
|
|
freePosition = walkSpace.Move(freePosition, movement * (speed * Mathf.Min(.10f, delta)));
|
|
}
|
|
|
|
public void ShowDeck(int index)
|
|
{
|
|
if (displayedShip == null || index < -1 || index >= displayedShip.Decks.Count || (index == deckIndex && cameraMode == CameraMode.Orbit))
|
|
return;
|
|
|
|
deckIndex = index;
|
|
if (serviceDeck != null)
|
|
serviceDeck.Root.SetActive(false);
|
|
exteriorRoot.gameObject.SetActive(index < 0);
|
|
decks.Select(index);
|
|
ResetOrbit(true);
|
|
}
|
|
|
|
public void ExploreDeck(int index)
|
|
{
|
|
if (!sailing || displayedShip == null || index < 0 || index >= displayedShip.Decks.Count)
|
|
return;
|
|
ShowDeck(index);
|
|
decks.Explore(index);
|
|
EnterWalk(CameraMode.DeckWalk, decks.WalkSpace(index), decks.WalkOrigin(index), 62f, 1.8f, 90f);
|
|
}
|
|
|
|
public void ViewBridge()
|
|
{
|
|
if (!sailing || decks == null)
|
|
return;
|
|
if (serviceDeck != null)
|
|
serviceDeck.Root.SetActive(false);
|
|
deckIndex = -1;
|
|
exteriorRoot.gameObject.SetActive(false);
|
|
decks.SelectBridge();
|
|
EnterWalk(CameraMode.Bridge, decks.BridgeWalkSpace, decks.BridgeOrigin(), 68f, 1.3f, 90f);
|
|
}
|
|
|
|
public void ExploreServiceArea(int area)
|
|
{
|
|
if (!sailing || vessel == null || area < 0 || area >= ServiceDeckModel.Areas.Length)
|
|
return;
|
|
if (serviceDeck == null)
|
|
serviceDeck = new ServiceDeckModel(vessel, vesselLength, vesselBeam, surface);
|
|
deckIndex = -1;
|
|
serviceArea = area;
|
|
exteriorRoot.gameObject.SetActive(false);
|
|
decks.HideRooms();
|
|
serviceDeck.Root.SetActive(true);
|
|
EnterWalk(CameraMode.Service, serviceDeck.WalkSpace, serviceDeck.Entry(area), 66f, 2f, serviceDeck.Heading(area));
|
|
}
|
|
|
|
private void EnterWalk(CameraMode mode, ShipWalkSpace space, Vector3 entry, float fieldOfView, float lookDown, float heading)
|
|
{
|
|
cameraMode = mode;
|
|
walkSpace = space;
|
|
freeFov = fieldOfView;
|
|
freePitch = lookDown;
|
|
freeYaw = heading;
|
|
freePosition = walkSpace.Entry(entry);
|
|
viewCamera.orthographic = false;
|
|
ComposeCamera();
|
|
}
|
|
|
|
public void UpdatePassengers(OnboardSession session, float delta)
|
|
{
|
|
if ((sailing || PortCinematicActive) && decks != null && cameraMode != CameraMode.Service)
|
|
decks.Tick(session, delta, viewCamera);
|
|
}
|
|
|
|
internal void SetRudder(float degrees) => decks?.SetRudder(degrees);
|
|
|
|
public bool IsShipAt(Vector2 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)
|
|
{
|
|
sailing = true;
|
|
seaSeverity = (int)weather / 2f;
|
|
seaTime = 0;
|
|
float nominalKnots = 17f + 2f * (displayedShip?.HullIndex ?? 0);
|
|
float weatherSlowdown = weather == WeatherCondition.Storm ? 1.32f : weather == WeatherCondition.Swell ? 1.14f : 1f;
|
|
passageDistance = nominalKnots * .514444f * duration / weatherSlowdown;
|
|
hullMotion = hullMotionVelocity = Vector3.zero;
|
|
water.SetFloat("_WeatherSeverity", seaSeverity);
|
|
Shader.SetGlobalFloat("_SoundingsSeaWeather", seaSeverity);
|
|
Shader.SetGlobalFloat("_SoundingsSeaSailing", 1);
|
|
wake.Reset();
|
|
SetPassage(Vector2.zero, 0, 0, 0);
|
|
ResetOrbit(true);
|
|
}
|
|
|
|
public void SetPassage(Vector2 routePosition, float heading, float speed, float delta)
|
|
{
|
|
seaTime += delta;
|
|
float headingRadians = heading * Mathf.Deg2Rad;
|
|
float sine = Mathf.Sin(headingRadians);
|
|
float cosine = Mathf.Cos(headingRadians);
|
|
var center = new Vector2(routePosition.x, -routePosition.y) * passageDistance;
|
|
if (vessel != null && delta > 0)
|
|
{
|
|
// Use wave heights along the waterline to drive a damped heave, pitch and roll.
|
|
Vector2 bow = new Vector2(cosine, -sine) * (vesselLength * .36f);
|
|
Vector2 port = new Vector2(sine, cosine) * (vesselBeam * .86f);
|
|
float fore = WaveHeight(center + bow), aft = WaveHeight(center - bow);
|
|
float left = WaveHeight(center + port), right = WaveHeight(center - port);
|
|
var target = new Vector3((fore + aft + left + right) * .25f,
|
|
Mathf.Atan2(right - left, 2 * port.magnitude) * Mathf.Rad2Deg,
|
|
Mathf.Atan2(fore - aft, 2 * bow.magnitude) * Mathf.Rad2Deg);
|
|
float response = .48f + vesselLength * .005f;
|
|
hullMotion = Vector3.SmoothDamp(hullMotion, target, ref hullMotionVelocity, response, Mathf.Infinity, delta);
|
|
}
|
|
if (vessel != null)
|
|
vessel.SetLocalPositionAndRotation(new Vector3(center.x, hullMotion.x, center.y),
|
|
Quaternion.Euler(hullMotion.y, heading, hullMotion.z));
|
|
Vector4 hull = water.GetVector("_HullSpan");
|
|
hull.z = center.x;
|
|
hull.w = center.y;
|
|
water.SetVector("_HullSpan", hull);
|
|
water.SetVector("_HullCourse", new Vector4(cosine, sine, Mathf.Clamp01(speed / 19f), 0));
|
|
water.SetFloat("_SeaTime", seaTime);
|
|
water.SetFloat("_Underway", 1);
|
|
waterSurface.localPosition = new Vector3(center.x, 0, center.y);
|
|
Shader.SetGlobalFloat("_SoundingsSeaTime", seaTime);
|
|
wake.Update(center, new Vector2(cosine, -sine), speed, vesselLength, vesselBeam, seaTime, seaSeverity);
|
|
}
|
|
|
|
private float WaveHeight(Vector2 point)
|
|
{
|
|
float amplitude = Mathf.Lerp(.35f, 1.46f, seaSeverity * seaSeverity);
|
|
return amplitude * (.65f * Mathf.Sin(.052f * point.x + .115f * point.y - .93f * seaTime)
|
|
+ .25f * Mathf.Sin(-.073f * point.x + .17f * point.y - 1.22f * seaTime + 1.30f)
|
|
+ .10f * Mathf.Sin(.217f * point.x - .136f * point.y - 1.64f * seaTime + 2.9f));
|
|
}
|
|
|
|
public void EndPassage()
|
|
{
|
|
sailing = false;
|
|
waterSurface.localPosition = Vector3.zero;
|
|
wake.Reset();
|
|
Shader.SetGlobalFloat("_SoundingsSeaSailing", 0);
|
|
if (vessel != null)
|
|
vessel.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
|
|
Vector4 hull = water.GetVector("_HullSpan");
|
|
hull.z = hull.w = 0;
|
|
water.SetVector("_HullSpan", hull);
|
|
water.SetVector("_HullCourse", new Vector4(1, 0, 0, 0));
|
|
water.SetFloat("_Underway", 0);
|
|
water.SetFloat("_WeatherSeverity", 0);
|
|
ShowDeck(-1);
|
|
decks?.HidePassengers();
|
|
ResetView(true);
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (!viewCamera.enabled || PortCinematicActive)
|
|
return;
|
|
if (cameraMode != CameraMode.Orbit)
|
|
{
|
|
ComposeCamera();
|
|
return;
|
|
}
|
|
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 && (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 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;
|
|
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)
|
|
{
|
|
Vector3 back = orbit * Vector3.back;
|
|
float rise = Vector3.Dot(back, vessel.up);
|
|
if (rise > .20f)
|
|
world -= back * (Vector3.Dot(world, vessel.up) / rise);
|
|
}
|
|
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()
|
|
{
|
|
if (PortCinematicActive)
|
|
{
|
|
ComposePortCinematic();
|
|
return;
|
|
}
|
|
if (cameraMode == CameraMode.Orbit)
|
|
{
|
|
viewCamera.orthographic = !sailing;
|
|
viewCamera.nearClipPlane = OrbitNearClip;
|
|
viewCamera.fieldOfView = OverviewFieldOfView;
|
|
Quaternion orbit = Quaternion.Euler(pitch, -yaw, 0);
|
|
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
|
|
{
|
|
viewCamera.orthographic = false;
|
|
viewCamera.nearClipPlane = .095f;
|
|
viewCamera.fieldOfView = freeFov;
|
|
viewCamera.transform.SetPositionAndRotation(vessel.TransformPoint(freePosition),
|
|
vessel.rotation * Quaternion.Euler(freePitch, freeYaw, 0));
|
|
}
|
|
FitViewport();
|
|
}
|
|
|
|
private void SetHarbor(string region)
|
|
{
|
|
string backdrop = region switch
|
|
{
|
|
"Caribbean" or "South Pacific" => "IslandHarbor",
|
|
"Europe" or "Azores" or "Mediterranean" => "IberianHarbor",
|
|
_ => "HarborPlate"
|
|
};
|
|
if (backdrop == currentBackplate)
|
|
return;
|
|
if (!backplates.TryGetValue(backdrop, out Texture2D plate))
|
|
{
|
|
plate = Resources.Load<Texture2D>("Art/" + backdrop);
|
|
plate.wrapMode = TextureWrapMode.Clamp;
|
|
backplates.Add(backdrop, plate);
|
|
}
|
|
currentBackplate = backdrop;
|
|
backplateRatio = (float)plate.width / plate.height;
|
|
water.SetTexture("_Backdrop", plate);
|
|
FitViewport();
|
|
}
|
|
|
|
public void Resize(int width, int height)
|
|
{
|
|
if (Frame != null && Frame.width == width && Frame.height == height)
|
|
return;
|
|
ReleaseFrame();
|
|
var descriptor = new RenderTextureDescriptor(width, height, RenderTextureFormat.ARGB32, 24) { msaaSamples = 4 };
|
|
descriptor.msaaSamples = Mathf.Max(1, SystemInfo.GetRenderTextureSupportedMSAASampleCount(descriptor));
|
|
Frame = new RenderTexture(descriptor) { name = "Harbor viewport" };
|
|
viewCamera.targetTexture = Frame;
|
|
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 = ViewAspect;
|
|
viewCamera.aspect = aspect;
|
|
if (viewCamera.orthographic)
|
|
viewCamera.orthographicSize = OverviewHalfHeight(aspect) * zoom;
|
|
Transform view = viewCamera.transform;
|
|
float viewHeight = viewCamera.orthographic ? viewCamera.orthographicSize : HalfFieldOfView(viewCamera.fieldOfView);
|
|
water.SetVector("_ViewOrigin", view.position);
|
|
water.SetVector("_ViewForward", view.forward);
|
|
water.SetVector("_ViewRight", view.right);
|
|
water.SetVector("_ViewUp", view.up);
|
|
water.SetVector("_ViewSize", new Vector4(viewHeight * aspect, viewHeight, viewCamera.orthographic ? 1 : 0, 0));
|
|
if (PortCinematicActive && portMatteAspect > 0)
|
|
FitPortMatte(aspect);
|
|
if (PortCinematicActive || backplateRatio <= 0 || sailing)
|
|
return;
|
|
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 + 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 + Vector3.Dot(view.up, orbitOffset) * .002f,
|
|
-.47f * (1 - cropY), .47f * (1 - cropY));
|
|
water.SetVector("_BackdropCrop", new Vector4(cropX, cropY, shiftX, shiftY));
|
|
}
|
|
|
|
public void Show(Ship ship)
|
|
{
|
|
if (!PortCinematicActive)
|
|
SetHarbor(Market.Ports[ship?.Port ?? 0].Region);
|
|
if (ship == null)
|
|
{
|
|
if (lastHull == -1)
|
|
return;
|
|
lastHull = -1;
|
|
ReleaseShip();
|
|
water.SetVector("_HullSpan", Vector4.zero);
|
|
return;
|
|
}
|
|
|
|
displayedShip = ship;
|
|
int hullIndex = ship.HullIndex;
|
|
int deckCount = ship.Decks.Count;
|
|
ulong layout = 0;
|
|
foreach (Deck deck in ship.Decks)
|
|
foreach (Facility cell in deck.Cells)
|
|
layout = unchecked(layout * 31 + (uint)cell + 1);
|
|
|
|
if (hullIndex == lastHull && deckCount == lastDecks && layout == lastLayout)
|
|
return;
|
|
|
|
lastHull = hullIndex;
|
|
lastDecks = deckCount;
|
|
lastLayout = layout;
|
|
ReleaseShip();
|
|
displayedShip = ship;
|
|
var root = new GameObject("Cruise vessel");
|
|
vessel = root.transform;
|
|
vessel.SetParent(transform, false);
|
|
exteriorRoot = new GameObject("Exterior").transform;
|
|
exteriorRoot.SetParent(vessel, false);
|
|
|
|
vesselLength = ship.Hull.Length;
|
|
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 * 1.05f, vesselTop + 2.65f, beam * 2.26f));
|
|
|
|
var exterior = new VesselExterior(ship, vesselLength, beam);
|
|
foreach (var (paint, batch) in exterior.Meshes())
|
|
{
|
|
Mesh mesh = batch.Bake();
|
|
shipMeshes.Add(mesh);
|
|
(Material finish, Material reflected) = Materials(paint);
|
|
Batch(mesh, finish);
|
|
Batch(mesh, reflected);
|
|
}
|
|
decks = new DeckPassengers(vessel, ship, vesselLength, beam, surface);
|
|
if (deckIndex >= 0 && deckIndex < ship.Decks.Count)
|
|
{
|
|
exteriorRoot.gameObject.SetActive(false);
|
|
decks.Select(deckIndex);
|
|
}
|
|
else
|
|
{
|
|
deckIndex = -1;
|
|
decks.Select(-1);
|
|
}
|
|
KeepOrbitInRange();
|
|
ComposeCamera();
|
|
}
|
|
|
|
private void Batch(Mesh mesh, Material material)
|
|
{
|
|
var draw = new GameObject(material.name, typeof(MeshFilter), typeof(MeshRenderer));
|
|
draw.layer = 8;
|
|
draw.transform.SetParent(exteriorRoot, false);
|
|
draw.GetComponent<MeshFilter>().sharedMesh = mesh;
|
|
draw.GetComponent<MeshRenderer>().sharedMaterial = material;
|
|
}
|
|
|
|
private (Material Surface, Material Reflection) Materials(VesselExterior.Paint paint)
|
|
{
|
|
if (materials.TryGetValue(paint, out Material material))
|
|
return (material, reflections[paint]);
|
|
|
|
var (hex, roughness, metal, emissive, weather) = paint switch
|
|
{
|
|
VesselExterior.Paint.Navy => ("162737", .29f, .26f, 0f, .38f),
|
|
VesselExterior.Paint.Ivory => ("DBD9CE", .43f, .08f, 0f, .15f),
|
|
VesselExterior.Paint.Pearl => ("EDF0E9", .32f, .26f, 0f, .08f),
|
|
VesselExterior.Paint.Gold => ("BB8250", .27f, .66f, .05f, .13f),
|
|
VesselExterior.Paint.Teak => ("AE8766", .78f, 0f, 0f, .58f),
|
|
VesselExterior.Paint.TeakSeam => ("675145", .91f, 0f, 0f, .25f),
|
|
VesselExterior.Paint.Steel => ("8A9DA6", .27f, .76f, 0f, .20f),
|
|
VesselExterior.Paint.Black => ("172029", .43f, .25f, 0f, .28f),
|
|
VesselExterior.Paint.Glass => ("273E49", .18f, .47f, 0f, .08f),
|
|
VesselExterior.Paint.BlueGlass => ("4B6474", .20f, .53f, 0f, .06f),
|
|
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);
|
|
material = new Material(surface) { color = color, name = paint.ToString() };
|
|
material.SetFloat("_Roughness", roughness);
|
|
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);
|
|
return (material, reflected);
|
|
}
|
|
|
|
private void ReleaseFrame()
|
|
{
|
|
if (Frame == null)
|
|
return;
|
|
viewCamera.targetTexture = null;
|
|
Frame.Release();
|
|
Destroy(Frame);
|
|
}
|
|
|
|
private void ReleaseShip()
|
|
{
|
|
decks?.Dispose();
|
|
decks = null;
|
|
serviceDeck?.Release();
|
|
serviceDeck = null;
|
|
displayedShip = null;
|
|
if (vessel != null)
|
|
{
|
|
vessel.gameObject.SetActive(false);
|
|
Destroy(vessel.gameObject);
|
|
}
|
|
foreach (Mesh mesh in shipMeshes)
|
|
Destroy(mesh);
|
|
shipMeshes.Clear();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
ReleaseFrame();
|
|
ReleaseShip();
|
|
ReleasePortQuay();
|
|
wake?.Dispose();
|
|
foreach (Material material in materials.Values)
|
|
Destroy(material);
|
|
foreach (Material material in reflections.Values)
|
|
Destroy(material);
|
|
Destroy(water);
|
|
Destroy(waterMesh);
|
|
}
|
|
}
|
|
}
|