This commit is contained in:
josh
2026-12-04 18:19:26 -05:00
parent 0178b39cc3
commit 42be6d67c1
46 changed files with 6014 additions and 0 deletions

View File

@@ -0,0 +1,294 @@
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace DomeShift
{
public enum DomeRegion
{
Rainforest,
Ocean,
MangroveWetlands,
Savanna,
Orchard,
ThornScrub,
FogDesert,
Agriculture,
HumanHabitat,
Technosphere
}
[DisallowMultipleComponent]
[ExecuteAlways]
public sealed class DomeEnvironment : MonoBehaviour
{
private const string GeneratedName = "Generated Bio-Dome";
internal const float HalfWidth = 43f;
internal const float HalfDepth = 32f;
internal static readonly Vector2 Spawn = new(0f, -23f);
private static readonly Vector2[] Sites =
{
new(23f, -17f), new(-3f, 13f), new(-27f, -15f),
new(24f, -9f), new(18f, 11f), new(-7f, -3f), new(0f, -29f)
};
[Header("Footprint and roof")]
[SerializeField, Range(68f, 126f)] private float width = 86f;
[SerializeField, Range(52f, 96f)] private float depth = 64f;
[SerializeField, Range(10f, 22f)] private float roofHeight = 13f;
[SerializeField, Range(4, 12)] private int roofBays = 7;
[SerializeField] private bool glazing = true;
[SerializeField, Range(0.06f, 0.4f)] private float glazingOpacity = 0.16f;
[Header("Landscape")]
[SerializeField] private int seed = 1996;
[SerializeField, Range(0.3f, 1.5f)] private float vegetationDensity = 1f;
[SerializeField, Range(0f, 1.6f)] private float terrainRelief = 1f;
[Header("Region density")]
[SerializeField, Range(0.35f, 1.55f)] private float rainforest = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float tropicalLagoon = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float mangroveWetlands = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float savanna = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float orchard = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float thornScrub = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float greatDesert = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float farm = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float humanHabitat = 1f;
[SerializeField, Range(0.35f, 1.55f)] private float technosphere = 1f;
[Header("Editor preview")]
[SerializeField] private bool showScenePreview = true;
[SerializeField] private bool sceneCutaway;
private readonly List<Object> owned = new();
private Transform generated;
private DomeArchitecture architecture;
private DomeBiomes biomes;
public float Width => width;
public float Depth => depth;
public float RoofHeight => roofHeight;
public int RoofBays => roofBays;
public bool Glazing => glazing;
public float GlazingOpacity => glazingOpacity;
public int Seed => seed;
public float VegetationDensity => vegetationDensity;
public float TerrainRelief => terrainRelief;
public float DensityFor(DomeRegion region) => region switch
{
DomeRegion.Rainforest => rainforest,
DomeRegion.Ocean => tropicalLagoon,
DomeRegion.MangroveWetlands => mangroveWetlands,
DomeRegion.Savanna => savanna,
DomeRegion.Orchard => orchard,
DomeRegion.ThornScrub => thornScrub,
DomeRegion.FogDesert => greatDesert,
DomeRegion.Agriculture => farm,
DomeRegion.HumanHabitat => humanHabitat,
DomeRegion.Technosphere => technosphere,
_ => 1f
};
public static string RegionName(DomeRegion region) => region switch
{
DomeRegion.Rainforest => "Rainforest",
DomeRegion.Ocean => "Tropical Lagoon",
DomeRegion.MangroveWetlands => "Mangrove Wetlands",
DomeRegion.Savanna => "Savanna",
DomeRegion.Orchard => "Orchard",
DomeRegion.ThornScrub => "Thorn Scrub",
DomeRegion.FogDesert => "Great Desert",
DomeRegion.Agriculture => "Farm",
DomeRegion.HumanHabitat => "Human Habitat",
DomeRegion.Technosphere => "Technosphere",
_ => ""
};
internal static DomeRegion RegionAt(Vector2 p)
{
const float division = 14f;
if (p.y >= 10.5f)
{
if (p.x < -division) return DomeRegion.Rainforest;
return p.x > division ? DomeRegion.MangroveWetlands : DomeRegion.Ocean;
}
if (p.y <= -11f)
{
if (p.x < -division) return DomeRegion.Agriculture;
return p.x > division ? DomeRegion.Technosphere : DomeRegion.HumanHabitat;
}
if (p.x < -division) return DomeRegion.Savanna;
if (p.x <= division) return DomeRegion.Orchard;
return p.y > 0f ? DomeRegion.ThornScrub : DomeRegion.FogDesert;
}
internal static Vector2 MapCoordinates(Vector2 p)
{
return new Vector2(Mathf.InverseLerp(-HalfWidth, HalfWidth, p.x), Mathf.InverseLerp(-HalfDepth, HalfDepth, p.y));
}
internal static Vector2 Site(int index) => Sites[index];
public T Own<T>(T asset) where T : Object
{
asset.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild;
owned.Add(asset);
return asset;
}
internal Transform BuildForGame()
{
#if UNITY_EDITOR
EditorApplication.delayCall -= RebuildEditorPreview;
#endif
return Build(false);
}
internal bool CanWalk(Vector2 point)
{
return Mathf.Abs(point.x) <= HalfWidth - 0.8f
&& Mathf.Abs(point.y) <= HalfDepth - 0.8f
&& Mathf.Abs(point.x) + Mathf.Abs(point.y) <= 67.1f
&& biomes.CanWalk(point);
}
internal float SurfaceY(Vector2 point) => biomes.SurfaceY(point);
internal void SetCutaway(bool cutaway) => architecture.SetCutaway(cutaway);
private Transform Build(bool preview)
{
Clear();
GameObject root = new(GeneratedName);
if (preview)
root.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild | HideFlags.NotEditable;
generated = root.transform;
generated.SetParent(transform, false);
generated.localScale = new Vector3(Width / (HalfWidth * 2f), 1f, Depth / (HalfDepth * 2f));
architecture = DomeArchitecture.Create(generated, this);
biomes = DomeBiomes.Create(generated, this);
if (preview)
{
architecture.SetCutaway(sceneCutaway);
GameObject lightObject = new("Preview sunlight");
lightObject.transform.SetParent(generated, false);
lightObject.transform.localRotation = Quaternion.Euler(49f, -33f, 0f);
Light light = lightObject.AddComponent<Light>();
light.type = LightType.Directional;
light.color = new Color32(255, 236, 205, 255);
light.intensity = 1.05f;
light.shadows = LightShadows.Soft;
}
return generated;
}
private void Clear()
{
Transform previous = generated != null ? generated : transform.Find(GeneratedName);
if (previous != null)
{
if (Application.IsPlaying(gameObject))
{
previous.gameObject.SetActive(false);
Destroy(previous.gameObject);
}
else
DestroyImmediate(previous.gameObject);
}
foreach (Object asset in owned)
{
if (asset == null) continue;
if (Application.IsPlaying(gameObject))
Destroy(asset);
else
DestroyImmediate(asset);
}
owned.Clear();
generated = null;
}
private void OnDestroy()
{
#if UNITY_EDITOR
EditorApplication.delayCall -= RebuildEditorPreview;
#endif
Clear();
}
#if UNITY_EDITOR
private void OnEnable()
{
QueueEditorPreview();
}
private void OnValidate()
{
QueueEditorPreview();
}
private void OnDisable()
{
EditorApplication.delayCall -= RebuildEditorPreview;
if (!Application.IsPlaying(gameObject))
Clear();
}
private void QueueEditorPreview()
{
EditorApplication.delayCall -= RebuildEditorPreview;
if (!Application.isBatchMode && !EditorApplication.isPlayingOrWillChangePlaymode)
EditorApplication.delayCall += RebuildEditorPreview;
}
internal void RebuildEditorPreview()
{
EditorApplication.delayCall -= RebuildEditorPreview;
if (this == null || Application.isBatchMode || EditorApplication.isPlayingOrWillChangePlaymode
|| BuildPipeline.isBuildingPlayer || !gameObject.scene.IsValid() || EditorUtility.IsPersistent(this))
return;
if (showScenePreview && isActiveAndEnabled)
Build(true);
else
Clear();
SceneView.RepaintAll();
}
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(DomeEnvironment))]
internal sealed class DomeEnvironmentInspector : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
DomeEnvironment environment = (DomeEnvironment)target;
using (new EditorGUI.DisabledScope(EditorApplication.isPlayingOrWillChangePlaymode))
{
if (GUILayout.Button("Rebuild scene preview"))
environment.RebuildEditorPreview();
if (GUILayout.Button("Frame the Bio-Dome") && SceneView.lastActiveSceneView != null)
{
Bounds bounds = new(environment.transform.position + Vector3.up * (environment.RoofHeight / 2f),
new Vector3(environment.Width + 8f, environment.RoofHeight, environment.Depth + 8f));
SceneView.lastActiveSceneView.Frame(bounds, false);
}
}
}
}
#endif
}