Files
cruiseSimulator/Assets/Soundings/Runtime/PassageWake.cs

158 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Soundings
{
internal sealed class PassageWake : IDisposable
{
private const int Rows = 58, Columns = 7;
private const float TrackAge = 34f;
private readonly List<Station> trail = new(Rows - 1);
private readonly Vector3[] positions = new Vector3[Rows * Columns];
private readonly List<Vector4> profiles = new(Rows * Columns);
private readonly int[] triangles = new int[(Rows - 1) * (Columns - 1) * 6];
private readonly GameObject root;
private readonly Mesh mesh;
private readonly Material finish;
private readonly struct Station
{
internal readonly Vector2 Center, Forward;
internal readonly float Time, Thrust;
internal Station(Vector2 center, Vector2 forward, float time, float thrust)
{
Center = center;
Forward = forward;
Time = time;
Thrust = thrust;
}
}
internal PassageWake(Transform parent, Texture2D detail)
{
root = new GameObject("Propeller wash", typeof(MeshFilter), typeof(MeshRenderer));
root.layer = 8;
root.transform.SetParent(parent, false);
mesh = new Mesh { name = "World-space wake ribbon" };
mesh.MarkDynamic();
finish = new Material(Resources.Load<Shader>("PassageWake"));
finish.SetTexture("_OceanDetail", detail);
root.GetComponent<MeshFilter>().sharedMesh = mesh;
root.GetComponent<MeshRenderer>().sharedMaterial = finish;
for (int i = 0; i < positions.Length; i++)
profiles.Add(Vector4.zero);
int face = 0;
for (int row = 0; row < Rows - 1; row++)
for (int col = 0; col < Columns - 1; col++)
{
int near = row * Columns + col;
int far = near + Columns;
triangles[face++] = near;
triangles[face++] = far;
triangles[face++] = far + 1;
triangles[face++] = near;
triangles[face++] = far + 1;
triangles[face++] = near + 1;
}
Reset();
}
internal void Reset()
{
trail.Clear();
root.SetActive(false);
}
internal void Update(Vector2 center, Vector2 bow, float speed, float length, float beam, float time, float storm)
{
Vector2 propellers = center - bow * (length * .476f);
float thrust = Mathf.SmoothStep(0, 1, Mathf.InverseLerp(.9f, 14, speed));
var newest = new Station(propellers, bow, time, thrust);
while (trail.Count > 0 && time - trail[0].Time > TrackAge)
trail.RemoveAt(0);
if (trail.Count == 0)
{
if (root.activeSelf)
root.SetActive(false);
if (thrust > 0)
trail.Add(newest);
return;
}
float gap = Vector2.Distance(propellers, trail[^1].Center);
if (gap > Mathf.Max(62, length * .45f))
{
// Snapshots can jump directly to a different part of a passage.
Reset();
if (thrust > 0)
trail.Add(newest);
return;
}
int row = 0;
float distance = 0;
Vector2 prior = propellers;
WriteRow(row++, newest, time, 0, beam);
for (int i = trail.Count - 1; i >= 0; i--)
{
Station sample = trail[i];
float section = Vector2.Distance(prior, sample.Center);
if (section < .19f)
continue;
distance += section;
WriteRow(row++, sample, time, distance, beam);
prior = sample.Center;
}
if (distance < 1)
{
if (root.activeSelf)
root.SetActive(false);
return;
}
mesh.SetVertices(positions, 0, row * Columns);
mesh.SetUVs(0, profiles, 0, row * Columns);
mesh.SetIndices(triangles, 0, (row - 1) * (Columns - 1) * 6, MeshTopology.Triangles, 0, true);
Bounds bounds = mesh.bounds;
bounds.Expand(new Vector3(0, 4, 0));
mesh.bounds = bounds;
finish.SetFloat("_SeaTime", time);
finish.SetFloat("_WeatherSeverity", storm);
if (!root.activeSelf)
root.SetActive(true);
if (gap >= 3f && thrust > 0)
{
if (trail.Count >= Rows - 1)
trail.RemoveAt(0);
trail.Add(newest);
}
}
private void WriteRow(int row, Station station, float time, float distance, float beam)
{
float age = time - station.Time;
float width = beam * .62f + distance * .34f;
Vector2 right = new Vector2(-station.Forward.y, station.Forward.x);
for (int col = 0; col < Columns; col++)
{
float across = 2f * col / (Columns - 1) - 1f;
Vector2 point = station.Center + right * (across * width);
int vertex = row * Columns + col;
positions[vertex] = new Vector3(point.x, .055f, point.y);
profiles[vertex] = new Vector4(across, age, station.Thrust, distance);
}
}
public void Dispose()
{
UnityEngine.Object.Destroy(root);
UnityEngine.Object.Destroy(mesh);
UnityEngine.Object.Destroy(finish);
}
}
}