A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Statecraft.Game;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;
/// <summary>Imports the sandbox's vegetation and rock models (EZ-Tree, MIT, generated by asset-staging/tools/eztree/gen.mjs;
/// Poly Haven rocks, CC0, decimated by asset-staging/tools/rocks/decimate.py) into the git-ignored prototype folder and
/// saves Resources/HexSandbox/<species>[_n].prefab for the hex tiles (model ids "sandbox:<species>"). A model folder holds <name>.obj and either
/// bark.jpg + leaves.png (a tree: submesh "bark" opaque, "leaves" alpha-clipped and two-sided) or albedo.jpg (+ normal.jpg)
/// (a rock). Sandbox only: nothing here is tracked or shipped; provenance is in asset-staging/kits/PROVENANCE.md.
/// Unity -batchmode -CI false -GITHUB_ACTIONS false -TF_BUILD false -quit -projectPath unity -executeMethod HexSandboxImport.Run</summary>
public static class HexSandboxImport
{
const string Root = "Assets/Art/Settlements/Prototype/HexSandboxModels", Prefabs = "Assets/Art/Settlements/Prototype/Resources/" + "HexSandbox";
/// <summary>(model folder under a source, species prefab name, leaf tint). Several rocks per species become variants.</summary>
static readonly (string source, string model, string species, Color tint)[] Table =
{
("asset-staging/tools/eztree/out", "tabor_oak", "tabor_oak", new Color(0.78f, 0.86f, 0.70f)),
("asset-staging/tools/eztree/out", "olive", "olive", new Color(0.80f, 0.86f, 0.78f)),
("asset-staging/tools/eztree/out", "aleppo_pine", "aleppo_pine", new Color(0.75f, 0.85f, 0.72f)),
("asset-staging/tools/eztree/out", "acacia", "acacia", new Color(0.95f, 0.92f, 0.70f)),
("asset-staging/tools/eztree/out", "date_palm", "date_palm", Color.white),
// Stand-ins from the same generated trees until each has its own: a dark glossy grove tree, a dark carob, a cypress
// (the pine drawn narrow by its species' width in HexSandboxPlanting).
("asset-staging/tools/eztree/out", "olive", "citrus", new Color(0.45f, 0.62f, 0.40f)),
("asset-staging/tools/eztree/out", "tabor_oak", "carob", new Color(0.55f, 0.66f, 0.48f)),
("asset-staging/tools/eztree/out", "aleppo_pine", "cypress", new Color(0.50f, 0.62f, 0.48f)),
// Lite trees for Civ-scale hex tiles (HexTiles): about a third of the triangles, same silhouettes.
("asset-staging/tools/eztree/out", "tabor_oak_lite", "tabor_oak_lite", new Color(0.78f, 0.86f, 0.70f)),
("asset-staging/tools/eztree/out", "olive_lite", "olive_lite", new Color(0.80f, 0.86f, 0.78f)),
("asset-staging/tools/eztree/out", "aleppo_pine_lite", "aleppo_pine_lite", new Color(0.75f, 0.85f, 0.72f)),
("asset-staging/tools/eztree/out", "acacia_lite", "acacia_lite", new Color(0.95f, 0.92f, 0.70f)),
("asset-staging/tools/eztree/out", "olive_lite", "citrus_lite", new Color(0.45f, 0.62f, 0.40f)),
("asset-staging/tools/eztree/out", "tabor_oak_lite", "carob_lite", new Color(0.55f, 0.66f, 0.48f)),
("asset-staging/tools/eztree/out", "aleppo_pine_lite", "cypress_lite", new Color(0.50f, 0.62f, 0.48f)),
// High-quality White City set for the reference Tel Aviv hex tile (HexTiles): b01 hero ~8k, modules ~2.2k, 2K textures.
("asset-staging/tools/kit/out-hq", "b01", "hq_white_city_hero", Color.white),
("asset-staging/tools/kit/out-hq", "b02a", "hq_white_city", Color.white),
("asset-staging/tools/kit/out-hq", "b02b", "hq_white_city_2", Color.white),
("asset-staging/tools/kit/out-hq", "b13a", "hq_white_city_3", Color.white),
// Building kit per city style: the 13 October Tripo modules (pending rights, local only) decimated to ~700 tris by
// asset-staging/tools/kit/decimate_buildings.py; HexSandboxCity places them lot by lot.
("asset-staging/tools/kit/out", "b02a", "kit_white_city", Color.white),
("asset-staging/tools/kit/out", "b02b", "kit_white_city_2", Color.white),
("asset-staging/tools/kit/out", "b13a", "kit_white_city_3", Color.white),
("asset-staging/tools/kit/out", "b11", "kit_white_city_4", Color.white),
("asset-staging/tools/kit/out", "b17", "kit_jaffa", Color.white),
("asset-staging/tools/kit/out", "b21", "kit_jaffa_2", Color.white),
("asset-staging/tools/kit/out", "b03c", "kit_jaffa_3", Color.white),
("asset-staging/tools/kit/out", "b04a", "kit_haifa", Color.white),
("asset-staging/tools/kit/out", "b04b", "kit_haifa_2", Color.white),
("asset-staging/tools/kit/out", "b05a", "kit_haifa_3", Color.white),
("asset-staging/tools/kit/out", "b03a", "kit_jerusalem", Color.white),
("asset-staging/tools/kit/out", "b03b", "kit_jerusalem_2", Color.white),
("asset-staging/tools/kit/out", "b03c", "kit_jerusalem_3", Color.white),
("asset-staging/tools/kit/out", "b17", "kit_jerusalem_4", Color.white),
("asset-staging/tools/kit/out", "b17", "kit_negev", Color.white),
("asset-staging/tools/kit/out", "b03c", "kit_negev_2", Color.white),
("asset-staging/tools/kit/out", "b21", "kit_negev_3", Color.white),
("asset-staging/tools/kit/out", "b05a", "kit_moshava", Color.white),
("asset-staging/tools/kit/out", "b16", "kit_moshava_2", Color.white),
("asset-staging/tools/kit/out", "b24", "kit_moshava_3", Color.white),
("asset-staging/tools/kit/out", "b09a", "kit_kibbutz", Color.white),
("asset-staging/tools/kit/out", "b07", "kit_kibbutz_2", Color.white),
("asset-staging/tools/kit/out", "b24", "kit_kibbutz_3", Color.white),
("asset-staging/tools/kit/out", "b28", "kit_kibbutz_4", Color.white),
("asset-staging/tools/kit/out", "b17", "kit_stone_village", Color.white),
("asset-staging/tools/kit/out", "b03c", "kit_stone_village_2", Color.white),
("asset-staging/tools/kit/out", "b32", "kit_port", Color.white),
("asset-staging/tools/rocks/out", "rock_07", "limestone", Color.white),
("asset-staging/tools/rocks/out", "rock_09", "limestone_2", Color.white),
("asset-staging/tools/rocks/out", "namaqualand_boulder_04", "desert_rock", Color.white),
("asset-staging/tools/rocks/out", "namaqualand_rocks_01", "desert_rock_2", Color.white),
("asset-staging/tools/rocks/out", "namaqualand_boulders_01", "desert_rock_3", Color.white),
};
[MenuItem("Statecraft/Hex Sandbox Import Models")]
public static void Run()
{
try
{
Directory.CreateDirectory(Path.Combine(Paths.RepoRoot, "unity", Root));
Directory.CreateDirectory(Path.Combine(Paths.RepoRoot, "unity", Prefabs));
foreach (var (source, model, species, tint) in Table) Import(Path.Combine(Paths.RepoRoot, source, model), model, species, tint);
AssetDatabase.SaveAssets();
}
catch (Exception e)
{
Debug.LogError("HEX SANDBOX IMPORT FAILED " + e);
if (Application.isBatchMode) EditorApplication.Exit(1);
else throw;
}
}
/// <summary>Higgsfield props (pending rights, local only): imports just the rows of a manifest, each
/// "<model folder>\t<Resources path>[\ttwosided]" (for example "hf_minaret\tHexTiles/arab_village/minaret"), from the folder that
/// holds it, so a tile's tiles:<tile>/<name> id fills its stand-in with no source edit. The folders come from
/// asset-staging/higgsfield-local/<date>/export.py. -executeMethod HexSandboxImport.RunHiggsfield -manifest <path>
/// [-modelsRoot <Assets path>] [-prefabsRoot <Assets path of a Resources folder>]: by default both go into the
/// git-ignored prototype folder; committed props use Assets/Art/HexTiles/Props and Assets/Art (outside Resources: they
/// stream from their own bundle, HexProps, and join HexProps.Ids).</summary>
public static void RunHiggsfield()
{
try
{
var args = Environment.GetCommandLineArgs();
int i = Array.IndexOf(args, "-manifest");
if (i < 0 || i + 1 >= args.Length) throw new ArgumentException("-manifest <path> is required");
var manifest = Path.GetFullPath(args[i + 1]);
string Opt(string name, string fallback) { int k = Array.IndexOf(args, name); return k >= 0 && k + 1 < args.Length ? args[k + 1] : fallback; }
string modelsRoot = Opt("-modelsRoot", Root), prefabsRoot = Opt("-prefabsRoot", Path.GetDirectoryName(Prefabs)!);
foreach (var line in File.ReadAllLines(manifest))
{
var cols = line.Split('\t');
if (cols.Length < 2 || line.StartsWith("#", StringComparison.Ordinal)) continue;
Import(Path.Combine(Path.GetDirectoryName(manifest)!, cols[0]), cols[0], Path.GetFileName(cols[1]), Color.white,
$"{prefabsRoot}/{cols[1]}.prefab", cols.Length > 2 && cols[2] == "twosided", modelsRoot);
}
AssetDatabase.SaveAssets();
}
catch (Exception e)
{
Debug.LogError("HEX HIGGSFIELD IMPORT FAILED " + e);
if (Application.isBatchMode) EditorApplication.Exit(1);
else throw;
}
}
static void Import(string src, string model, string species, Color tint, string? prefabPath = null, bool twoSided = false, string modelsRoot = Root)
{
if (!File.Exists(Path.Combine(src, model + ".obj"))) throw new FileNotFoundException($"no {model}.obj in {src}");
var dir = $"{modelsRoot}/{model}";
Directory.CreateDirectory(Path.Combine(Paths.RepoRoot, "unity", dir));
foreach (var f in Directory.GetFiles(src)) File.Copy(f, Path.Combine(Paths.RepoRoot, "unity", dir, Path.GetFileName(f)), true);
AssetDatabase.Refresh();
bool tree = File.Exists(Path.Combine(src, "leaves.png"));
Texture2D Tex(string file, bool normal = false, bool alpha = false, int max = 1024)
{
var path = $"{dir}/{file}";
var ti = (TextureImporter)AssetImporter.GetAtPath(path);
ti.textureType = normal ? TextureImporterType.NormalMap : TextureImporterType.Default;
ti.alphaIsTransparency = alpha; ti.maxTextureSize = max; ti.mipmapEnabled = true; ti.sRGBTexture = !normal;
ti.SaveAndReimport();
return AssetDatabase.LoadAssetAtPath<Texture2D>(path);
}
var shader = Shader.Find("Universal Render Pipeline/Lit") ?? throw new InvalidOperationException("URP Lit not found");
Material Mat(string name, Texture2D albedo, Color colour, Texture2D? normal = null, bool clip = false)
{
var m = new Material(shader) { name = name };
m.SetTexture("_BaseMap", albedo); m.SetColor("_BaseColor", colour); m.SetFloat("_Smoothness", 0.05f);
if (normal != null) { m.SetTexture("_BumpMap", normal); m.EnableKeyword("_NORMALMAP"); }
if (clip)
{
m.SetFloat("_AlphaClip", 1f); m.SetFloat("_Cutoff", 0.45f); m.EnableKeyword("_ALPHATEST_ON");
m.SetFloat("_Cull", (float)CullMode.Off); m.renderQueue = (int)RenderQueue.AlphaTest;
}
var path = $"{dir}/{species}-{name}.mat";
AssetDatabase.DeleteAsset(path); AssetDatabase.CreateAsset(m, path);
return m;
}
var go = Object.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>($"{dir}/{model}.obj"));
try
{
go.name = species;
int tris = 0;
// Made once per model: Mat replaces the asset at its path, which would orphan a renderer already using it.
var bark = tree ? Mat("bark", Tex("bark.jpg", max: 512), Color.white) : null;
var leaves = tree ? Mat("leaves", Tex("leaves.png", alpha: true, max: 512), tint, clip: true) : null;
var rock = tree ? null : Mat("rock", Tex("albedo.jpg", max: species.StartsWith("hq_", StringComparison.Ordinal) || prefabPath != null ? 2048 : 1024), Color.white, File.Exists(Path.Combine(src, "normal.jpg")) ? Tex("normal.jpg", normal: true) : null);
foreach (var r in go.GetComponentsInChildren<MeshRenderer>())
{
var mesh = r.GetComponent<MeshFilter>().sharedMesh; tris += mesh.triangles.Length / 3;
r.shadowCastingMode = tree || prefabPath != null || (species.StartsWith("kit_", StringComparison.Ordinal) || species.StartsWith("hq_", StringComparison.Ordinal)) ? ShadowCastingMode.On : ShadowCastingMode.Off;
if (tree)
{
// gen.mjs writes "bark" then "leaves"; Unity names submesh materials after the OBJ's usemtl.
bool leafPart = r.name.Contains("leaves");
r.sharedMaterials = r.sharedMaterials.Select((m, i) => m != null && m.name.Contains("leaves") || leafPart ? leaves!
: m != null && m.name.Contains("bark") ? bark! : i == 0 ? bark! : leaves!).ToArray();
}
else
{
// Generated vegetation (open, single-layer pads and leaves) draws both sides, or the back faces leave holes.
if (twoSided) rock!.SetFloat("_Cull", (float)CullMode.Off);
r.sharedMaterials = Enumerable.Repeat(rock!, r.sharedMaterials.Length).ToArray();
}
}
var prefab = prefabPath ?? $"{Prefabs}/{species}.prefab";
Directory.CreateDirectory(Path.Combine(Paths.RepoRoot, "unity", Path.GetDirectoryName(prefab)!));
PrefabUtility.SaveAsPrefabAsset(go, prefab);
Debug.Log($"HEX SANDBOX MODEL {species} <- {model} tris={tris} tree={tree}");
}
finally { Object.DestroyImmediate(go); }
}
}