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

208 lines
9.5 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace Soundings
{
internal sealed class ServiceDeckModel
{
public static readonly string[] Areas = { "Engine room", "Galley", "Stores", "Maintenance", "Water plant", "Sewage treatment" };
private readonly Dictionary<int, HarborMesh> batches = new Dictionary<int, HarborMesh>();
private readonly List<Mesh> meshes = new List<Mesh>();
private readonly List<Material> materials = new List<Material>();
private readonly float span, width;
public GameObject Root { get; }
public ServiceDeckModel(Transform parent, float length, float beam, Shader surface)
{
span = length * .68f;
width = beam * 1.75f;
Root = new GameObject("Service deck");
Root.layer = 8;
Root.transform.SetParent(parent, false);
BuildEnvelope();
for (int area = 0; area < Areas.Length; area++)
{
float x = AreaCenter(area);
int accent = area switch { 0 => 0xB88E58, 1 => 0xB77661, 2 => 0x9DA775, 3 => 0xCEAC61, 4 => 0x65B3C7, _ => 0x73AB82 };
Box(accent, x, .022f, .82f, span / Areas.Length - .35f, .03f, .14f);
Box(0x263744, x, 2.72f, width * .5f - .15f, 4.7f, .64f, .16f);
Sign(Areas[area].ToUpperInvariant(), x, 2.72f, width * .5f - .26f, new Color32(235, 224, 193, 255));
BuildArea(area, x, accent);
}
foreach (var pair in batches)
{
var game = new GameObject("Service deck finish", typeof(MeshFilter), typeof(MeshRenderer));
game.layer = 8;
game.transform.SetParent(Root.transform, false);
Mesh mesh = pair.Value.Bake();
var material = new Material(surface) { color = ColorOf(pair.Key) };
material.SetFloat("_Roughness", .56f);
material.SetFloat("_Emissive", pair.Key == 0xFFF1C8 ? 1 : .21f);
game.GetComponent<MeshFilter>().sharedMesh = mesh;
game.GetComponent<MeshRenderer>().sharedMaterial = material;
meshes.Add(mesh);
materials.Add(material);
}
batches.Clear();
Root.SetActive(false);
}
public Vector3 Entry(int area) => new Vector3(AreaCenter(area), 1.65f, -.75f);
public float Heading(int area) => 0;
private float AreaCenter(int area) => -span * .5f + span * (Mathf.Clamp(area, 0, Areas.Length - 1) + .5f) / Areas.Length;
private void BuildEnvelope()
{
Box(0x596666, 0, -.13f, 0, span, .24f, width);
Box(0x626B6D, 0, 3.31f, 0, span, .22f, width);
for (int side = -1; side <= 1; side += 2)
{
Box(0x81918C, 0, 1.63f, side * width * .5f, span, 3.35f, .2f);
Box(0x37454D, 0, .32f, side * (width * .5f - .13f), span, .65f, .13f);
Box(0xB8AD82, 0, .018f, side * 1.12f, span - .20f, .024f, .06f);
Box(0x9F6E48, side * span * .5f, 1.62f, 0, .22f, 3.35f, width);
Pipe(0x638FAA, new Vector3(-span * .49f, 2.87f, side * (width * .5f - .43f)),
new Vector3(span * .49f, 2.87f, side * (width * .5f - .43f)), .12f);
}
for (int i = 0; i <= 12; i++)
{
float x = span * (i / 12f - .5f);
Box(0x42545A, x, .002f, 0, .043f, .026f, width);
Box(0x52636B, x, 3.13f, 0, .24f, .14f, width);
Box(0xFFF1C8, x, 3.04f, -.20f, .57f, .038f, 1.05f);
}
for (int i = 1; i < Areas.Length; i++)
{
float x = span * (i / (float)Areas.Length - .5f);
Box(0x778580, x, 1.6f, width * .391f, .19f, 3.2f, width * .235f);
Box(0x263744, x, 1.6f, -width * .414f, .19f, 3.2f, width * .18f);
}
}
private void BuildArea(int area, float x, int accent)
{
float wall = width * .5f;
float z = Mathf.Min(3.25f, wall - 1.35f);
switch (area)
{
case 0:
for (int side = -1; side <= 1; side += 2)
{
Box(0x3D484B, x, .16f, side * z, 5.4f, .33f, 1.75f);
Box(accent, x, .69f, side * z, 4.7f, .92f, 1.35f);
Pipe(0x526A74, new Vector3(x - 3.05f, .69f, side * z), new Vector3(x + 2.35f, .69f, side * z), .18f);
for (int i = -2; i <= 2; i++)
{
Box(0x7B8584, x + i * .86f, 1.27f, side * z, .51f, .30f, .80f);
Pipe(0xAA8170, new Vector3(x + i * .86f, 1.38f, side * z), new Vector3(x + i * .86f, 2.64f, side * z), .079f);
}
}
Panel(x + 2.94f, z, 0x62F0B4);
break;
case 1:
Box(0x92A19F, x, .49f, z, 5.40f, .92f, 1.54f);
Box(0xC7C8B9, x, 1.00f, z, 5.65f, .14f, 1.69f);
for (int i = 0; i < 3; i++)
{
float px = x - 1.65f + i * 1.66f;
Box(0x293943, px, 1.10f, z, 1.12f, .08f, 1.08f);
Pipe(accent, new Vector3(px, 1.12f, z), new Vector3(px, 1.38f, z), .26f);
}
Box(0x657478, x, 2.29f, z + .1f, 5.49f, .42f, 1.55f);
Box(0x839690, x, .61f, -z, 3.8f, 1.22f, 1.55f);
break;
case 2:
for (int shelf = 1; shelf <= 3; shelf++)
{
float level = shelf * .62f;
Box(0x655C48, x, level, z, 5.4f, .10f, 1.40f);
for (int i = -2; i <= 2; i++)
Box(i % 2 == 0 ? accent : 0xAF8F70, x + .91f * i, level + .21f, z, .70f, .36f, .76f);
}
for (int side = -1; side <= 1; side += 2)
Box(0x626B62, x + side * 2.45f, 1.13f, z, .12f, 2.26f, 1.59f);
break;
case 3:
Box(0x656B69, x, .48f, z, 5.30f, .93f, 1.35f);
Box(accent, x, .99f, z, 5.53f, .13f, 1.5f);
Box(0x4D6062, x, 1.83f, z + .65f, 5.30f, 1.01f, .14f);
for (int i = -3; i <= 3; i++)
{
float px = x + .61f * i;
Pipe(0xD2CAAF, new Vector3(px, 1.51f, z + .53f), new Vector3(px, 1.96f, z + .53f), .043f);
Box(i % 2 == 0 ? 0xAC784C : 0x748C84, px, 1.42f, z + .53f, .12f, .18f, .14f);
}
Box(0x705E56, x, .59f, -z, 3.14f, 1.18f, 1.25f);
break;
default:
for (int i = -1; i <= 1; i++)
{
float px = x + 1.49f * i;
Pipe(accent, new Vector3(px, .35f, z), new Vector3(px, 2.40f, z), .56f, 16);
Pipe(0xC3BFA7, new Vector3(px, 2.34f, z), new Vector3(px, 2.72f, z), .105f);
Box(0x3F5358, px, 1.40f, z - .49f, .24f, .48f, .26f);
}
Pipe(accent, new Vector3(x - 2.85f, 2.7f, z), new Vector3(x + 2.85f, 2.7f, z), .12f);
Panel(x + 2.94f, z, accent);
break;
}
}
private void Panel(float x, float z, int signal)
{
Box(0x48595E, x, .66f, z, .96f, 1.32f, .83f);
Box(0x223240, x, 1.32f, z - .16f, 1.02f, .39f, .65f);
for (int i = -1; i <= 1; i++)
Box(signal, x + i * .21f, 1.38f, z - .493f, .08f, .08f, .022f);
}
private void Sign(string words, float x, float y, float z, Color color)
{
var game = new GameObject(words, typeof(TextMesh));
game.layer = 8;
game.transform.SetParent(Root.transform, false);
game.transform.localPosition = new Vector3(x, y, z);
var text = game.GetComponent<TextMesh>();
Font font = Resources.Load<Font>("Interface");
text.font = font;
text.text = words;
text.fontStyle = FontStyle.Bold;
text.fontSize = 52;
text.characterSize = .10f;
text.anchor = TextAnchor.MiddleCenter;
text.color = color;
game.GetComponent<MeshRenderer>().sharedMaterial = font.material;
}
private HarborMesh Paint(int color)
{
if (!batches.TryGetValue(color, out HarborMesh mesh))
{
mesh = new HarborMesh();
batches.Add(color, mesh);
}
return mesh;
}
private void Box(int color, float x, float y, float z, float w, float h, float d) =>
Paint(color).Box(new Vector3(x, y, z), new Vector3(w, h, d));
private void Pipe(int color, Vector3 from, Vector3 to, float radius, int sides = 9) => Paint(color).Rod(from, to, radius, sides);
private static Color32 ColorOf(int rgb) => new Color32((byte)(rgb >> 16), (byte)(rgb >> 8), (byte)rgb, 255);
public void Release()
{
Root.SetActive(false);
UnityEngine.Object.Destroy(Root);
foreach (Mesh mesh in meshes)
UnityEngine.Object.Destroy(mesh);
foreach (Material material in materials)
UnityEngine.Object.Destroy(material);
}
}
}