using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; namespace DomeShift { internal sealed class DomeBiomes { private static readonly Vector2[] ServicePoints = { new(23f, -17f), new(-3f, 13f), new(-27f, -15f), new(24f, -9f), new(18f, 11f), new(-7f, -3f), new(0f, -29f), new(0f, -23f) }; private static readonly Rect Lagoon = Rect.MinMaxRect(-11.8f, 15.5f, 11.8f, 29.5f); private static readonly Rect Wetland = Rect.MinMaxRect(16.5f, 15.3f, 40.4f, 29.7f); private static readonly Rect[] WalkDecks = { Rect.MinMaxRect(-12.25f, 14.1f, 12.25f, 16.8f), Rect.MinMaxRect(1.0f, 15.5f, 4.1f, 30.1f), Rect.MinMaxRect(-2.1f, 23.8f, 7.1f, 26.0f), Rect.MinMaxRect(25.8f, 13.75f, 28.6f, 30.4f), Rect.MinMaxRect(15.9f, 21.55f, 40.95f, 24.2f) }; private static readonly Shape Cube = MakeBox(); private static readonly Shape Prism = MakeLathe(new[] { -0.5f, 0.5f }, new[] { 0.5f, 0.5f }, 8, true); private static readonly Shape Cone = MakeLathe(new[] { -0.5f, 0.5f }, new[] { 0.5f, 0f }, 7, true); private static readonly Shape Crown = MakeLathe( new[] { -0.5f, -0.32f, 0f, 0.32f, 0.5f }, new[] { 0f, 0.38f, 0.5f, 0.38f, 0f }, 8, false); private readonly List terrains = new(); private readonly List barriers = new(); private readonly List trunks = new(); private readonly Dictionary surfaces = new(); private readonly DomeEnvironment profile; private readonly Transform root; private readonly Shader shader; private System.Random random; private DomeBiomes(Transform parent, DomeEnvironment profile) { this.profile = profile; shader = Shader.Find("Standard"); root = new GameObject("Biome terrain and furnishings").transform; root.SetParent(parent, false); Rainforest(); Ocean(); Mangroves(); Savanna(); Orchard(); Scrub(); Desert(); Farm(); Habitat(); Technosphere(); foreach (Surface surface in surfaces.Values) surface.Build(root, profile); } public static DomeBiomes Create(Transform parent, DomeEnvironment profile) => new(parent, profile); public bool CanWalk(Vector2 point) { if (!WithinOutline(point) || (IsWater(point) && !IsDeck(point))) return false; foreach (Rect barrier in barriers) if (barrier.Contains(point)) return false; foreach (Trunk trunk in trunks) if ((point - trunk.Center).sqrMagnitude < trunk.RadiusSquared) return false; return true; } public float SurfaceY(Vector2 point) { if (IsDeck(point)) return 0.23f; foreach (Terrain terrain in terrains) if (terrain.Bounds.Contains(point)) return TerrainHeight(terrain, point); return 0f; } private void Rainforest() { Rect bounds = Rect.MinMaxRect(-42f, 12f, -15.1f, 31f); Begin(DomeRegion.Rainforest, bounds, 0.76f, "#4A6446", "#466049", "#546B45"); Trail(new Vector2(-41.4f, 20.7f), new Vector2(-15.6f, 20.7f), "#747D56", 25); Trail(new Vector2(-29f, 12.2f), new Vector2(-29f, 29.9f), "#747D56", 18); Scatter(DomeRegion.Rainforest, bounds, 28, 2.15f, 2f, p => { float height = Range(3.8f, 7.1f); Tree(p, height, Range(1.4f, 2.15f), "#485743", "#346B46", "#3F8051", "#57945B"); for (int i = 0; i < 2; i++) { Vector2 fern = p + new Vector2(Range(-1.45f, 1.45f), Range(-1.1f, 1.1f)); if (WithinOutline(fern)) Rosette(fern, Range(0.55f, 1.08f), "#609A62", "#80A957"); } }); Scatter(DomeRegion.Rainforest, bounds, 50, 1.05f, 1.2f, p => Rosette(p, Range(0.48f, 1.04f), "#4F8652", "#8EB16D")); Vector2[] boulders = { new(-39f, 16.3f), new(-31.6f, 27.2f), new(-20.5f, 25.6f) }; foreach (Vector2 p in boulders) Rock(p, 1.25f, "#6F8061", "#829171", true); } private void Ocean() { Rect bounds = Rect.MinMaxRect(-13.1f, 12f, 13.1f, 31.05f); Begin(DomeRegion.Ocean, bounds, 0f, "#CAB78B", "#D5C398", "#BFB083"); Water(Lagoon, "#4AAEAC", "#238EB1", "#267EAA"); AddBoardwalk(WalkDecks[0], true); AddBoardwalk(WalkDecks[1], false); AddBoardwalk(WalkDecks[2], true); CrossRails(WalkDecks[1], false, 17f, 23.6f); CrossRails(WalkDecks[1], false, 26.1f, 29.2f); Vector2[] palms = { new(-11.9f, 13.2f), new(10.1f, 13.05f), new(-10.2f, 30.33f), new(8.2f, 30.35f) }; int palmsToPlace = Count(DomeRegion.Ocean, palms.Length, palms.Length); for (int i = 0; i < palmsToPlace; i++) Palm(palms[i], Range(3.9f, 5.1f)); for (int i = 0; i < Count(DomeRegion.Ocean, 28, 60); i++) { Vector2 reef = new(Range(-11.3f, 11.3f), Range(17.1f, 29.1f)); if (IsDeck(reef)) continue; string coral = i % 3 == 0 ? "#DD836E" : i % 3 == 1 ? "#BC77A0" : "#D5AA79"; Add(Crown, "#63BCB4", At(reef, 0.215f), new Vector3(1.4f, 0.12f, 1.08f), Spin()); for (int n = 0; n < 3; n++) { Vector3 a = At(reef, 0.22f); Vector3 b = a + new Vector3(Range(-0.21f, 0.21f), Range(0.17f, 0.55f), Range(-0.3f, 0.3f)); Bar(Prism, coral, a, b, 0.115f); } } } private void Mangroves() { Rect bounds = Rect.MinMaxRect(15.1f, 12f, 42f, 31.05f); Begin(DomeRegion.MangroveWetlands, bounds, 0f, "#859262", "#909266", "#819068"); Water(Wetland, "#739D83", "#468B83", "#377F7C"); AddBoardwalk(WalkDecks[3], false); AddBoardwalk(WalkDecks[4], true); CrossRails(WalkDecks[3], false, 15.5f, 21.5f); CrossRails(WalkDecks[3], false, 24.3f, 29.4f); Scatter(DomeRegion.MangroveWetlands, Rect.MinMaxRect(17.35f, 16.4f, 39.4f, 29f), 19, 2.1f, 1.35f, p => { float size = Range(0.76f, 1.28f); Add(Crown, "#78966D", At(p, 0.22f), new Vector3(1.5f, 0.19f, 1.29f) * size, Spin()); Vector3 trunk = At(p, size * 0.97f); for (int i = 0; i < 5; i++) { float angle = i * Mathf.PI * 0.4f; Vector3 foot = At(p + new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * size * 0.75f, 0.17f); Bar(Prism, "#775B4D", foot, trunk, size * 0.17f); } Tree(p, size * 3.2f, size * 1.22f, "#6C5143", "#5D845B", "#709762", "#84A865", 0.27f); }); Scatter(DomeRegion.MangroveWetlands, bounds, 25, 1f, 1f, p => Rosette(p, Range(0.42f, 1.02f), "#65875A", "#9BAC62")); } private void Savanna() { Rect bounds = Rect.MinMaxRect(-42f, -9f, -15.1f, 9f); Begin(DomeRegion.Savanna, bounds, 0.54f, "#A49E5C", "#AB9D61", "#B3A468"); Trail(new Vector2(-40.8f, 0f), new Vector2(-15.6f, 0f), "#BAAC74", 24); Trail(new Vector2(-28.7f, -8.7f), new Vector2(-28.7f, 8.6f), "#BAAC74", 17); Scatter(DomeRegion.Savanna, bounds, 11, 3.4f, 1.7f, p => { float y = SurfaceY(p); float h = Range(2.65f, 4.35f); Vector3 fork = At(p, y + h * 0.69f); Bar(Prism, "#776447", At(p, y), fork, 0.34f); for (int i = -1; i <= 1; i++) { Vector3 top = fork + new Vector3(i * 1.08f, h * 0.28f, i * 0.23f); Bar(Prism, "#776447", fork, top, 0.19f); Add(Crown, i == 0 ? "#79924E" : "#8CA054", top, new Vector3(2.2f, 0.69f, 1.85f), Spin()); } AvoidTrunk(p, 0.3f); }); Scatter(DomeRegion.Savanna, bounds, 88, 0.43f, 1.13f, p => Tuft(p, Range(0.34f, 0.83f), "#A1A059", "#C8BE74")); Rock(new Vector2(-36.8f, 4.55f), 1.44f, "#817B61", "#959175", true); Rock(new Vector2(-19.1f, -5.55f), 0.94f, "#817B61", "#959175", true); } private void Orchard() { Rect bounds = Rect.MinMaxRect(-13.1f, -9f, 13.1f, 9f); Begin(DomeRegion.Orchard, bounds, 0.22f, "#718B59", "#7A925D", "#829863"); Trail(new Vector2(-12.65f, 0f), new Vector2(12.65f, 0f), "#B6AB80", 24); Trail(new Vector2(0f, -8.65f), new Vector2(0f, 8.65f), "#B6AB80", 16); for (int row = -1; row <= 1; row += 2) for (int column = -2; column <= 2; column++) { Vector2 p = new(column * 4.8f, row * 4.95f); if (column == 0 || NearService(p, 3.3f)) continue; float density = Density(DomeRegion.Orchard); if (random.NextDouble() >= Math.Min(1f, density)) continue; float y = SurfaceY(p); Add(Crown, "#648155", At(p, y + 0.01f), new Vector3(2.7f, 0.078f, 2.47f), Spin()); Tree(p, 2.7f, 1.04f, "#6F5A43", "#638E4E", "#78A359", "#87AC52"); for (int i = 0; i < 5; i++) { float angle = 1.2566f * i; Vector2 offset = new(Mathf.Cos(angle) * 0.68f, Mathf.Sin(angle) * 0.68f); Add(Crown, row < 0 ? "#DA9860" : "#D37F59", At(p + offset, y + Range(2.34f, 2.72f)), Vector3.one * Range(0.19f, 0.27f), Spin()); } } Scatter(DomeRegion.Orchard, bounds, 18, 0.8f, 0.9f, p => Rosette(p, Range(0.4f, 0.63f), "#718F54", "#99AC60")); } private void Scrub() { Rect bounds = Rect.MinMaxRect(15.1f, 1.2f, 42f, 9f); Begin(DomeRegion.ThornScrub, bounds, 0.37f, "#A7A078", "#AD9F78", "#9F9B71"); Trail(new Vector2(15.5f, 4.45f), new Vector2(41.5f, 4.45f), "#C0AF88", 19); Scatter(DomeRegion.ThornScrub, bounds, 28, 1.45f, 1.15f, p => { float y = SurfaceY(p); float s = Range(0.5f, 1.06f); for (int i = 0; i < 5; i++) { Vector3 a = At(p, y + 0.04f); Vector3 b = a + new Vector3(Range(-0.55f, 0.55f), Range(0.39f, 1.08f), Range(-0.5f, 0.5f)) * s; Bar(Prism, "#86785F", a, b, 0.064f * s); Add(Crown, i % 2 == 0 ? "#83945F" : "#9DA475", b, new Vector3(0.49f, 0.23f, 0.48f) * s, Spin()); } if (s > 0.87f) AvoidTrunk(p, 0.44f); }, cross: false); } private void Desert() { Rect bounds = Rect.MinMaxRect(15.1f, -9f, 42f, -1.2f); Begin(DomeRegion.FogDesert, bounds, 0.9f, "#CCA475", "#D7B184", "#DDB989"); Scatter(DomeRegion.FogDesert, bounds, 12, 1.6f, 1.15f, p => { if (random.NextDouble() < 0.48) Rock(p, Range(0.52f, 1.05f), "#A78263", "#BC986D", true); else Rosette(p, Range(0.65f, 1.05f), "#638C74", "#84A18A"); }, cross: false); Vector2[] cacti = { new(18.35f, -4.6f), new(30.1f, -6.85f), new(38.7f, -3.55f) }; for (int i = 0; i < Count(DomeRegion.FogDesert, cacti.Length, cacti.Length); i++) { Vector2 p = cacti[i]; float y = SurfaceY(p); float height = Range(1.45f, 2.45f); Add(Prism, "#6D946D", At(p, y + height / 2f), new Vector3(0.52f, height, 0.48f), Spin()); Add(Crown, "#87A275", At(p, y + height), new Vector3(0.49f, 0.55f, 0.47f), Spin()); for (int side = -1; side <= 1; side += 2) { float limb = side < 0 ? 0.44f : 0.68f; Vector3 joint = At(p, y + height * limb); Vector3 bend = joint + new Vector3(side * 0.46f, 0.08f, 0f); Bar(Prism, "#729873", joint, bend, 0.25f); Bar(Prism, "#729873", bend, bend + Vector3.up * 0.49f, 0.22f); } AvoidTrunk(p, 0.31f); } } private void Farm() { Rect bounds = Rect.MinMaxRect(-42f, -31f, -15.1f, -13f); Begin(DomeRegion.Agriculture, bounds, 0f, "#7F8665", "#8A8C69", "#8F936E"); Trail(new Vector2(-40.7f, -22.4f), new Vector2(-15.7f, -22.4f), "#A49D7C", 25); Trail(new Vector2(-27f, -30.2f), new Vector2(-27f, -13.3f), "#A49D7C", 17); for (int column = 0; column < 6; column++) { float x = -39.0f + 3.55f * column + (column > 2 ? 4.80f : 0f); CropBed(x, -18.85f, 5.65f, column); if (WithinOutline(new Vector2(x, -29.8f))) CropBed(x, -26.85f, 5.65f, column + 1); } } private void Habitat() { Rect bounds = Rect.MinMaxRect(-13.1f, -31f, 13.1f, -13f); Begin(DomeRegion.HumanHabitat, bounds, 0f, "#858F79", "#8D9680", "#909685"); Trail(new Vector2(-12.5f, -22.4f), new Vector2(12.5f, -22.4f), "#C6B994", 25); Trail(new Vector2(0f, -30.5f), new Vector2(0f, -13.3f), "#C6B994", 17); Pod(new Vector2(-7.3f, -18.4f), 6.2f, 4.15f, "#D9C795", "#B8B79B", "#446A70"); Pod(new Vector2(7.35f, -18.35f), 6.2f, 4.1f, "#BAC4AF", "#9DAE9C", "#496F77"); Pod(new Vector2(-7.25f, -26.6f), 5.75f, 4.05f, "#D1C198", "#B4B49A", "#4D6A70"); Pod(new Vector2(7.45f, -26.7f), 5.6f, 3.8f, "#C7C1A0", "#AAB19B", "#526E72"); Vector2[] planters = { new(-11.5f, -21.6f), new(11.6f, -21.4f), new(-5.5f, -14.25f), new(5.5f, -14.25f), new(-11.55f, -25.85f), new(11.55f, -25.85f), new(-10.65f, -30.1f) }; int planterCount = Count(DomeRegion.HumanHabitat, 4, planters.Length); for (int i = 0; i < planterCount; i++) { Vector2 p = planters[i]; Add(Prism, "#A6A28A", At(p, 0.35f), new Vector3(1.25f, 0.6f, 1.25f), Quaternion.identity); Add(Crown, "#7E9E6A", At(p, 0.86f), new Vector3(1.3f, 0.81f, 1.3f), Spin()); AvoidTrunk(p, 0.57f); } } private void Technosphere() { Rect bounds = Rect.MinMaxRect(15.1f, -31f, 42f, -13f); Begin(DomeRegion.Technosphere, bounds, 0f, "#637B7A", "#6D8581", "#718783"); Trail(new Vector2(15.75f, -22.3f), new Vector2(41f, -22.3f), "#93A298", 25); Trail(new Vector2(23f, -30.35f), new Vector2(23f, -13.2f), "#93A298", 16); Lung(new Vector2(33.4f, -23.45f)); Vector2[] tanks = { new(29.7f, -16.45f), new(33.35f, -16.45f), new(36.95f, -16.4f), new(18.3f, -27.25f), new(18.0f, -17.55f), new(26.45f, -28.7f) }; int tankCount = Mathf.Clamp(Mathf.RoundToInt(4 * profile.DensityFor(DomeRegion.Technosphere)), 1, tanks.Length); for (int i = 0; i < tankCount; i++) { Vector2 p = tanks[i]; Add(Prism, "#415B5D", At(p, 0.28f), new Vector3(2.84f, 0.46f, 2.76f), Quaternion.identity); Add(Prism, "#9EB3A7", At(p, 1.34f), new Vector3(2.35f, 2.13f, 2.35f), Quaternion.identity); Add(Crown, "#BAC6AB", At(p, 2.25f), new Vector3(2.33f, 1.12f, 2.33f), Quaternion.identity); Add(Prism, "#617C79", At(p, 0.74f), new Vector3(2.42f, 0.24f, 2.42f), Quaternion.identity); Add(Prism, "#688786", At(p, 1.75f), new Vector3(2.42f, 0.13f, 2.42f), Quaternion.identity); AvoidTrunk(p, 1.39f); } Vector3 pipe = new(29.7f, 0.36f, -14.98f); Bar(Prism, "#A79B78", pipe, new Vector3(37.65f, 0.36f, -14.98f), 0.29f); } private void CropBed(float x, float z, float length, int variety) { Vector2 p = new(x, z); Add(Cube, "#9D876A", At(p, 0.26f), new Vector3(1.86f, 0.43f, length), Quaternion.identity); Add(Cube, "#6C5944", At(p, 0.491f), new Vector3(1.64f, 0.08f, length - 0.17f), Quaternion.identity); float density = Density(DomeRegion.Agriculture); int plants = Mathf.Clamp(Mathf.RoundToInt(10 * density), 0, 20); for (int i = 0; i < plants; i++) { float along = ((i + 0.5f) / plants - 0.5f) * (length - 0.24f); float halfHeight = Range(0.24f, 0.415f); Vector2 plant = new(x + (i % 2 == 0 ? -0.25f : 0.25f), z + along); Add(Crown, variety % 2 == 0 ? "#658F4E" : "#7E9F52", At(plant, 0.53f + 0.6f * halfHeight), new Vector3(0.94f, halfHeight * 1.35f, 0.63f), Spin()); Add(Crown, variety % 2 == 0 ? "#AACC6A" : "#A51946", At(plant, 0.53f + halfHeight), new Vector3(0.42f, halfHeight * 0.83f, 0.40f), Spin()); } Block(p, 1.04f, length * 0.5f + 0.12f); } private void Pod(Vector2 p, float width, float depth, string shell, string roof, string glass) { Add(Cube, "#596967", At(p, 0.22f), new Vector3(width + 0.4f, 0.34f, depth + 0.42f), Quaternion.identity); Add(Cube, shell, At(p, 1.36f), new Vector3(width, 2.28f, depth), Quaternion.identity); Add(Cube, roof, At(p, 2.64f), new Vector3(width + 0.28f, 0.38f, depth + 0.28f), Quaternion.identity); Add(Cube, glass, At(p + new Vector2(0f, -depth * 0.5f - 0.03f), 1.66f), new Vector3(width - 0.93f, 0.92f, 0.14f), Quaternion.identity); Add(Cube, "#708C89", At(p + new Vector2(0f, -depth * 0.5f - 0.115f), 1.86f), new Vector3(width - 1.08f, 0.075f, 0.045f), Quaternion.identity); Add(Cube, "#718880", At(p, 2.88f), new Vector3(width * 0.55f, 0.26f, depth * 0.56f), Quaternion.identity); for (int i = -1; i <= 1; i++) Add(Cube, shell, At(p + new Vector2(i * width * 0.21f, -depth * 0.5f - 0.125f), 1.64f), new Vector3(0.1f, 1.12f, 0.097f), Quaternion.identity); Block(p, width / 2f + 0.16f, depth / 2f + 0.17f); } private void Lung(Vector2 p) { Add(Prism, "#4C6667", At(p, 0.27f), new Vector3(9.6f, 0.43f, 8.45f), Quaternion.identity); Add(Prism, "#9CADAA", At(p, 0.72f), new Vector3(8.9f, 0.91f, 7.8f), Quaternion.identity); Add(Prism, "#557275", At(p, 1.09f), new Vector3(9.1f, 0.29f, 8f), Quaternion.identity); Add(Crown, "#B4C5B2", At(p, 1.25f), new Vector3(8.49f, 3.8f, 7.48f), Quaternion.identity); Add(Crown, "#C9D4B8", At(p, 2.19f), new Vector3(5.55f, 2.23f, 4.98f), Quaternion.Euler(0f, 22.5f, 0f)); for (int i = 0; i < 8; i++) { float angle = i * Mathf.PI / 4f; Vector3 clamp = At(p, 1.1f) + new Vector3(Mathf.Cos(angle) * 4.36f, 0f, Mathf.Sin(angle) * 3.8f); Add(Cube, "#617C7A", clamp, new Vector3(0.39f, 0.79f, 0.5f), Quaternion.Euler(0f, -i * 45f, 0f)); Add(Cube, "#D4BE82", clamp - Vector3.up * 0.12f, new Vector3(0.48f, 0.10f, 0.53f), Quaternion.Euler(0f, -i * 45f, 0f)); } Block(p, 4.45f, 3.94f); } private void Begin(DomeRegion region, Rect bounds, float relief, string baseColor, string low, string high) { unchecked { random = new System.Random(profile.Seed * 104729 + ((int)region + 1) * 524287); } float amplitude = Mathf.Clamp(profile.TerrainRelief, 0f, 2f) * relief; Terrain terrain = new(bounds, amplitude, Range(0f, Mathf.PI * 2f), region == DomeRegion.FogDesert); terrains.Add(terrain); int columns = Mathf.CeilToInt(bounds.width / 1.55f); int rows = Mathf.CeilToInt(bounds.height / 1.55f); for (int column = 0; column < columns; column++) for (int row = 0; row < rows; row++) { float x1 = Mathf.Lerp(bounds.xMin, bounds.xMax, (float)column / columns); float x2 = Mathf.Lerp(bounds.xMin, bounds.xMax, (column + 1f) / columns); float z1 = Mathf.Lerp(bounds.yMin, bounds.yMax, (float)row / rows); float z2 = Mathf.Lerp(bounds.yMin, bounds.yMax, (row + 1f) / rows); Rect cell = Rect.MinMaxRect(x1, z1, x2, z2); Vector2 center = cell.center; float color = Mathf.Sin(center.x * 0.68f + terrain.Phase) * Mathf.Cos(center.y * 0.55f - terrain.Phase); Face(cell, color > 0.43f ? high : color < -0.4f ? low : baseColor, p => TerrainHeight(terrain, p)); } } private static float TerrainHeight(Terrain terrain, Vector2 p) { Rect box = terrain.Bounds; float edge = Mathf.Min( Mathf.Min(p.x - box.xMin, box.xMax - p.x), Mathf.Min(p.y - box.yMin, box.yMax - p.y)); float edgeFade = Mathf.SmoothStep(0f, 1f, Mathf.Clamp01(edge / 2.1f)); float waves = terrain.Dunes ? 0.49f + 0.39f * Mathf.Sin(p.x * 0.64f + p.y * 0.47f + terrain.Phase) : 0.39f + 0.27f * Mathf.Sin(p.x * 0.3f + terrain.Phase) * Mathf.Cos(p.y * 0.32f); float nearestSquared = float.PositiveInfinity; foreach (Vector2 service in ServicePoints) nearestSquared = Mathf.Min(nearestSquared, (p - service).sqrMagnitude); float blend = (Mathf.Sqrt(nearestSquared) - 2.4f) / 2.2f; float serviceFade = Mathf.SmoothStep(0f, 1f, Mathf.Clamp01(blend)); return 0.035f + edgeFade * terrain.Relief * waves * serviceFade; } private static bool WithinOutline(Vector2 p) { float x = Mathf.Abs(p.x); float z = Mathf.Abs(p.y); return x <= 43f && z <= 32f && x + z <= 68f; } private static bool NearService(Vector2 p, float distance) { foreach (Vector2 station in ServicePoints) if ((p - station).sqrMagnitude < distance * distance) return true; return false; } private static bool IsWater(Vector2 p) => Lagoon.Contains(p) || Wetland.Contains(p); private static bool IsDeck(Vector2 p) { foreach (Rect deck in WalkDecks) if (deck.Contains(p)) return true; return false; } private void Water(Rect bounds, string shallow, string middle, string deep) { int columns = Mathf.CeilToInt(bounds.width / 1.43f); int rows = Mathf.CeilToInt(bounds.height / 1.3f); for (int x = 0; x < columns; x++) for (int z = 0; z < rows; z++) { Rect cell = Rect.MinMaxRect( Mathf.Lerp(bounds.xMin, bounds.xMax, (float)x / columns), Mathf.Lerp(bounds.yMin, bounds.yMax, (float)z / rows), Mathf.Lerp(bounds.xMin, bounds.xMax, (x + 1f) / columns), Mathf.Lerp(bounds.yMin, bounds.yMax, (z + 1f) / rows)); bool edge = x < 2 || z < 2 || x >= columns - 2 || z >= rows - 2; float stripe = Mathf.Sin(cell.center.x * 0.86f + cell.center.y * 0.79f); string tint = edge ? (stripe < -0.55f ? middle : shallow) : (stripe > 0.38f ? middle : deep); Face(cell, tint, _ => 0.172f); if (!IsDeck(cell.center) && WithinOutline(cell.center) && random.NextDouble() < 0.26) { Add(Cube, shallow, At(cell.center, 0.181f), new Vector3(Range(0.27f, 1.21f), 0.016f, 0.052f), Quaternion.Euler(0f, 15f, 0f)); } } } private void AddBoardwalk(Rect rect, bool across) { Add(Cube, "#746753", At(rect.center, 0.123f), new Vector3(rect.width, 0.205f, rect.height), Quaternion.identity); float length = across ? rect.width : rect.height; int count = Mathf.RoundToInt(length / 0.57f); float stride = length / count; for (int i = 0; i < count; i++) { float off = (i + 0.5f) * stride - length / 2f; Vector2 at = rect.center + (across ? Vector2.right : Vector2.up) * off; Vector3 size = across ? new Vector3(stride - 0.035f, 0.065f, rect.height - 0.08f) : new Vector3(rect.width - 0.08f, 0.065f, stride - 0.035f); Add(Cube, i % 4 == 0 ? "#C4AC7E" : "#AF9871", At(at, 0.1975f), size, Quaternion.identity); } } private void CrossRails(Rect deck, bool across, float from, float to) { float low = across ? deck.yMin : deck.xMin; float high = across ? deck.yMax : deck.xMax; for (int side = -1; side <= 1; side += 2) { float at = side < 0 ? low + 0.12f : high - 0.12f; Vector2 start = across ? new Vector2(from, at) : new Vector2(at, from); Vector2 end = across ? new Vector2(to, at) : new Vector2(at, to); Bar(Cube, "#B7AB87", At(start, 0.97f), At(end, 0.97f), 0.095f); for (int i = 0; i < 4; i++) { Vector2 p = Vector2.Lerp(start, end, i / 3f); Add(Cube, "#9D9276", At(p, 0.58f), new Vector3(0.14f, 0.78f, 0.14f), Quaternion.identity); } } } private void Trail(Vector2 start, Vector2 end, string tint, int count) { bool across = Mathf.Abs(end.x - start.x) > Mathf.Abs(end.y - start.y); for (int i = 0; i < count; i++) { Vector2 at = Vector2.Lerp(start, end, (i + 0.5f) / count); if (!WithinOutline(at) || IsWater(at)) continue; at += (across ? Vector2.up : Vector2.right) * Range(-0.16f, 0.16f); Add(Crown, tint, At(at, SurfaceY(at) + 0.015f), new Vector3(across ? 1.34f : 0.87f, 0.045f, across ? 0.89f : 1.3f), Spin()); } } private void Tree(Vector2 p, float height, float width, string bark, string dark, string middle, string bright, float rootHeight = 0f) { float y = SurfaceY(p) + rootHeight; Vector3 basePoint = At(p, y); Vector3 fork = basePoint + Vector3.up * (height * 0.65f); Bar(Prism, bark, basePoint, fork, Mathf.Clamp(width * 0.226f, 0.2f, 0.53f)); Quaternion rotation = Spin(); for (int i = 0; i < 4; i++) { float angle = i * Mathf.PI / 2f; Vector3 limb = rotation * new Vector3(Mathf.Sin(angle) * width * 0.43f, height * Range(0.13f, 0.29f), Mathf.Cos(angle) * width * 0.43f); Vector3 top = fork + limb; Bar(Prism, bark, fork, top, width * 0.115f); Add(Crown, i == 0 ? dark : middle, top, new Vector3(width * 1.53f, width * 0.92f, width * 1.45f), Spin()); } Add(Crown, bright, fork + Vector3.up * (height * 0.35f), new Vector3(width * 1.52f, width * 0.94f, width * 1.48f), Spin()); AvoidTrunk(p, width * 0.19f + 0.16f); } private void Palm(Vector2 p, float height) { Vector3 a = At(p, SurfaceY(p)); Vector3 b = a + new Vector3(0.21f, height * 0.61f, 0.11f); Vector3 c = a + new Vector3(-0.17f, height, 0.15f); Bar(Prism, "#8C7857", a, b, 0.28f); Bar(Prism, "#A88C66", b, c, 0.23f); for (int i = 0; i < 7; i++) { float angle = i * 2f * Mathf.PI / 7f; Vector3 direction = new(Mathf.Sin(angle), 0f, Mathf.Cos(angle)); Vector3 crown = c + direction * 0.64f + Vector3.up * 0.38f; Vector3 tip = c + direction * Range(1.65f, 2.4f) - Vector3.up * 0.11f; Bar(Crown, i % 2 == 0 ? "#5D995E" : "#73A369", c, crown, 0.55f); Bar(Crown, i % 2 == 0 ? "#69A361" : "#80AC66", crown, tip, 0.48f); } AvoidTrunk(p, 0.32f); } private void Rosette(Vector2 p, float scale, string dark, string bright) { float y = SurfaceY(p); float rotation = Range(0f, 1.4f); for (int i = 0; i < 5; i++) { float angle = 1.2566f * i + rotation; Vector3 axis = new(Mathf.Sin(angle) * scale * 0.57f, scale * Range(0.31f, 0.68f), Mathf.Cos(angle) * scale * 0.57f); Vector3 at = At(p, y + 0.034f) + axis * 0.55f; Add(Crown, i % 2 == 0 ? dark : bright, at, new Vector3(scale * 0.31f, axis.magnitude * 1.24f, scale * 0.12f), Quaternion.FromToRotation(Vector3.up, axis)); } } private void Tuft(Vector2 p, float scale, string dark, string bright) { Vector3 start = At(p, SurfaceY(p)); for (int i = 0; i < 4; i++) { Vector3 end = start + new Vector3(Range(-0.38f, 0.38f), Range(0.38f, 1.04f), Range(-0.31f, 0.31f)) * scale; Bar(Cone, i % 2 == 0 ? dark : bright, start, end, 0.12f * scale); } } private void Rock(Vector2 p, float size, string dark, string bright, bool solid) { float y = SurfaceY(p); Add(Crown, dark, At(p, y + size * 0.23f), new Vector3(size * 1.27f, size * 0.85f, size), Spin()); Add(Crown, bright, At(p + new Vector2(size * 0.31f, -size * 0.18f), y + size * 0.3f), new Vector3(size * 0.8f, size * 0.69f, size * 0.79f), Spin()); if (solid) AvoidTrunk(p, size * 0.59f + 0.17f); } private void Scatter(DomeRegion region, Rect bounds, int normal, float spacing, float edge, Action render, bool cross = true) { int count = Count(region, normal, normal * 3); List used = new(count); for (int attempts = 0; used.Count < count && attempts < count * 16; attempts++) { Vector2 p = new(Range(bounds.xMin + edge, bounds.xMax - edge), Range(bounds.yMin + edge, bounds.yMax - edge)); if (!WithinOutline(p + new Vector2(Mathf.Sign(p.x), Mathf.Sign(p.y)) * 0.85f) || NearService(p, 2.9f) || IsDeck(p)) continue; if (cross && (Mathf.Abs(p.x - bounds.center.x) < 1.48f || Mathf.Abs(p.y - bounds.center.y) < 1.45f)) continue; bool fits = true; foreach (Vector2 prior in used) { if ((p - prior).sqrMagnitude < spacing * spacing) { fits = false; break; } } if (!fits) continue; used.Add(p); render(p); } } private float Density(DomeRegion region) => Mathf.Clamp(profile.VegetationDensity * profile.DensityFor(region), 0f, 3f); private int Count(DomeRegion region, int normal, int max) => Mathf.Clamp(Mathf.RoundToInt(normal * Density(region)), 0, max); private float Range(float min, float max) => Mathf.Lerp(min, max, (float)random.NextDouble()); private Quaternion Spin() => Quaternion.Euler(0f, Range(0f, 360f), 0f); private static Vector3 At(Vector2 point, float y) => new(point.x, y, point.y); private void Block(Vector2 p, float x, float z) => barriers.Add(new Rect(p.x - x, p.y - z, x * 2f, z * 2f)); private void AvoidTrunk(Vector2 p, float radius) => trunks.Add(new Trunk(p, radius * radius)); private Surface GetSurface(string hex) { if (surfaces.TryGetValue(hex, out Surface color)) return color; color = new Surface(hex, shader); surfaces.Add(hex, color); return color; } private void Add(Shape shape, string hex, Vector3 position, Vector3 size, Quaternion rotation) => GetSurface(hex).Add(shape, position, size, rotation); private void Bar(Shape shape, string hex, Vector3 start, Vector3 end, float diameter) { Vector3 axis = end - start; Add(shape, hex, (start + end) * 0.5f, new Vector3(diameter, axis.magnitude, diameter), Quaternion.FromToRotation(Vector3.up, axis)); } private void Face(Rect rect, string hex, Func height) { Vector2 a = new(rect.xMin, rect.yMin); Vector2 b = new(rect.xMin, rect.yMax); Vector2 c = new(rect.xMax, rect.yMax); Vector2 d = new(rect.xMax, rect.yMin); if (WithinOutline(a) && WithinOutline(b) && WithinOutline(c) && WithinOutline(d)) { GetSurface(hex).Quad(At(a, height(a)), At(b, height(b)), At(c, height(c)), At(d, height(d))); return; } List outline = new() { a, b, c, d }; for (int x = -1; x <= 1; x += 2) for (int z = -1; z <= 1; z += 2) outline = CutCorner(outline, new Vector2(x, z)); for (int i = 1; i < outline.Count - 1; i++) GetSurface(hex).Triangle(At(outline[0], height(outline[0])), At(outline[i], height(outline[i])), At(outline[i + 1], height(outline[i + 1]))); } private static List CutCorner(List outline, Vector2 normal) { List clipped = new(); if (outline.Count == 0) return clipped; Vector2 previous = outline[^1]; float prevDistance = Vector2.Dot(previous, normal) - 68f; foreach (Vector2 point in outline) { float distance = Vector2.Dot(point, normal) - 68f; if ((distance <= 0f) != (prevDistance <= 0f)) clipped.Add(Vector2.Lerp(previous, point, prevDistance / (prevDistance - distance))); if (distance <= 0f) clipped.Add(point); previous = point; prevDistance = distance; } return clipped; } private static Shape MakeBox() { Vector3 a = new(-0.5f, -0.5f, -0.5f); Vector3 b = new(-0.5f, -0.5f, 0.5f); Vector3 c = new(0.5f, -0.5f, 0.5f); Vector3 d = new(0.5f, -0.5f, -0.5f); Vector3 e = a + Vector3.up; Vector3 f = b + Vector3.up; Vector3 g = c + Vector3.up; Vector3 h = d + Vector3.up; return new Shape(new[] { e, f, g, h, d, c, b, a, a, b, f, e, b, c, g, f, c, d, h, g, d, a, e, h }, new[] { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23 }); } private static Shape MakeLathe(float[] heights, float[] radii, int sides, bool caps) { List vertices = new(); List indices = new(); Vector3 AtRing(int ring, int junction) { float angle = junction * Mathf.PI * 2 / sides; return new Vector3(Mathf.Cos(angle) * radii[ring], heights[ring], Mathf.Sin(angle) * radii[ring]); } for (int side = 0; side < sides; side++) { for (int ring = 0; ring < heights.Length - 1; ring++) { int index = vertices.Count; vertices.Add(AtRing(ring, side)); vertices.Add(AtRing(ring + 1, side)); vertices.Add(AtRing(ring + 1, side + 1)); vertices.Add(AtRing(ring, side + 1)); indices.AddRange(new[] { index, index + 1, index + 2, index, index + 2, index + 3 }); } if (!caps) continue; int last = heights.Length - 1; int firstVert = vertices.Count; vertices.Add(new Vector3(0f, heights[0], 0f)); vertices.Add(AtRing(0, side)); vertices.Add(AtRing(0, side + 1)); vertices.Add(new Vector3(0f, heights[last], 0f)); vertices.Add(AtRing(last, side + 1)); vertices.Add(AtRing(last, side)); indices.AddRange(new[] { firstVert, firstVert + 1, firstVert + 2, firstVert + 3, firstVert + 4, firstVert + 5 }); } return new Shape(vertices.ToArray(), indices.ToArray()); } private readonly struct Terrain { public readonly Rect Bounds; public readonly float Relief; public readonly float Phase; public readonly bool Dunes; public Terrain(Rect bounds, float relief, float phase, bool dunes) { Bounds = bounds; Relief = relief; Phase = phase; Dunes = dunes; } } private readonly struct Trunk { public readonly Vector2 Center; public readonly float RadiusSquared; public Trunk(Vector2 center, float radiusSquared) { Center = center; RadiusSquared = radiusSquared; } } private readonly struct Shape { public readonly Vector3[] Vertices; public readonly int[] Indices; public Shape(Vector3[] vertices, int[] indices) { Vertices = vertices; Indices = indices; } } private sealed class Surface { private readonly List vertices = new(); private readonly List indices = new(); private readonly string hex; private readonly Shader shader; public Surface(string hex, Shader shader) { this.hex = hex; this.shader = shader; } public void Add(Shape shape, Vector3 position, Vector3 scale, Quaternion rotation) { int first = vertices.Count; foreach (Vector3 vertex in shape.Vertices) vertices.Add(position + rotation * Vector3.Scale(vertex, scale)); foreach (int index in shape.Indices) indices.Add(first + index); } public void Quad(Vector3 a, Vector3 b, Vector3 c, Vector3 d) { int first = vertices.Count; vertices.Add(a); vertices.Add(b); vertices.Add(c); vertices.Add(d); indices.Add(first); indices.Add(first + 1); indices.Add(first + 2); indices.Add(first); indices.Add(first + 2); indices.Add(first + 3); } public void Triangle(Vector3 a, Vector3 b, Vector3 c) { int first = vertices.Count; vertices.Add(a); vertices.Add(b); vertices.Add(c); indices.Add(first); indices.Add(first + 1); indices.Add(first + 2); } public void Build(Transform parent, DomeEnvironment profile) { Mesh mesh = new() { name = "Biome " + hex }; if (vertices.Count > ushort.MaxValue) mesh.indexFormat = IndexFormat.UInt32; mesh.SetVertices(vertices); mesh.SetTriangles(indices, 0); mesh.RecalculateNormals(); mesh.RecalculateBounds(); profile.Own(mesh); ColorUtility.TryParseHtmlString(hex, out Color color); Material material = new(shader) { name = mesh.name, color = color }; material.SetFloat("_Glossiness", 0.18f); profile.Own(material); GameObject part = new(mesh.name); part.transform.SetParent(parent, false); part.AddComponent().sharedMesh = mesh; part.AddComponent().sharedMaterial = material; } } } }