using Unity.Netcode; using Unity.Netcode.Transports.UTP; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; using UnityEngine.UIElements; namespace GrassCutter.EditorTools { /// /// The Herbaria-7 scene: sky with the ringed gas giant, the endless overgrown meadow (grass + wild hemp), the hills /// beyond, the deck on its stilts, the static base furniture (supply wall, upgrade console, crew cabin, loading ramp and /// the unloading yard) and the systems (cameras, networking, spawner, UI). /// The deck tiles and everything built on them are spawned at runtime. /// public static partial class ProjectBuilder { static Transform _env; static Transform _dockZone; static void BuildScene() { System.IO.Directory.CreateDirectory("Assets/Scenes"); var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); Lighting(); _env = new GameObject("Environment").transform; World(); RigFurniture(); Systems(); EditorSceneManager.SaveScene(scene, ScenePath); Debug.Log("[Builder] Scene saved"); } // ───────────────────────── lighting ───────────────────────── static readonly Vector3 SunDir = new Vector3(0.3f, 0.55f, -0.6f).normalized; // towards the sun static readonly Color Horizon = new Color(0.8f, 0.9f, 0.86f); static void Lighting() { RenderSettings.ambientMode = AmbientMode.Trilight; RenderSettings.ambientSkyColor = new Color(0.66f, 0.8f, 0.98f) * 1.1f; RenderSettings.ambientEquatorColor = new Color(0.78f, 0.88f, 0.74f); RenderSettings.ambientGroundColor = new Color(0.3f, 0.45f, 0.2f); var sky = Mat("M_Sky"); if (sky) { sky.SetVector("_SunDir", SunDir); sky.SetColor("_HorizonColor", Horizon); EditorUtility.SetDirty(sky); RenderSettings.skybox = sky; } RenderSettings.defaultReflectionMode = DefaultReflectionMode.Skybox; RenderSettings.reflectionIntensity = 1f; RenderSettings.fog = true; RenderSettings.fogMode = FogMode.Linear; RenderSettings.fogColor = Horizon; RenderSettings.fogStartDistance = 30f; RenderSettings.fogEndDistance = 1500f; var sun = new GameObject("Sun"); var l = sun.AddComponent(); l.type = LightType.Directional; l.color = new Color(1f, 0.95f, 0.86f); l.intensity = 1.55f; l.shadows = LightShadows.Soft; l.shadowStrength = 0.75f; sun.transform.rotation = Quaternion.LookRotation(-SunDir); RenderSettings.sun = l; var vol = new GameObject("PostFX").AddComponent(); vol.isGlobal = true; vol.priority = 1; const string profPath = "Assets/Settings/MeadowPost.asset"; if (AssetDatabase.LoadAssetAtPath(profPath)) AssetDatabase.DeleteAsset(profPath); var prof = ScriptableObject.CreateInstance(); AssetDatabase.CreateAsset(prof, profPath); var bloom = prof.Add(true); bloom.intensity.Override(0.7f); bloom.threshold.Override(0.95f); bloom.scatter.Override(0.7f); var tm = prof.Add(true); tm.mode.Override(TonemappingMode.Neutral); var ca = prof.Add(true); ca.postExposure.Override(0.15f); ca.contrast.Override(16f); ca.saturation.Override(12f); var vig = prof.Add(true); vig.intensity.Override(0.22f); vig.smoothness.Override(0.5f); var wb = prof.Add(true); wb.temperature.Override(3f); foreach (var c in prof.components) AssetDatabase.AddObjectToAsset(c, prof); EditorUtility.SetDirty(prof); AssetDatabase.SaveAssets(); vol.sharedProfile = prof; } // ───────────────────────── scene helpers ───────────────────────── static GameObject Place(string model, Vector3 pos, float yaw, Transform parent = null, bool isStatic = true) { var m = Model(model); if (!m) { Debug.LogWarning("[Builder] missing " + model); return null; } // wrapper carries placement (so its forward is the model's front); the model keeps its imported rotation var go = new GameObject(model); go.transform.SetParent(parent ? parent : _env, false); go.transform.position = pos; go.transform.rotation = Quaternion.Euler(0, yaw, 0); var inst = (GameObject)PrefabUtility.InstantiatePrefab(m); inst.transform.SetParent(go.transform, false); if (isStatic) foreach (var t in go.GetComponentsInChildren()) t.gameObject.isStatic = true; return go; } static Transform FindT(GameObject root, string child) { foreach (var t in root.GetComponentsInChildren(true)) if (t.name == child) return t; return null; } static void Blocker(GameObject go, Vector3 center, Vector2 size) { var b = go.AddComponent(); b.center = center; b.size = size; } static BoxCollider SolidAt(string name, Vector3 center, Vector3 size, Transform parent, float yaw = 0f, float pitch = 0f) { var go = new GameObject(name); go.transform.SetParent(parent, false); go.transform.localPosition = center; go.transform.localRotation = Quaternion.Euler(pitch, yaw, 0); var bc = go.AddComponent(); bc.size = size; go.isStatic = true; return bc; } // ───────────────────────── the world ───────────────────────── static void World() { new GameObject("Meadow").AddComponent(); var field = new GameObject("GrassField").AddComponent(); field.furMat = Mat("M_GrassFur"); field.strandMat = Mat("M_GrassStrand"); field.hempMat = Mat("M_Hemp"); field.hempMesh = BakedMesh("SM_Hemp"); field.flowerMat = Mat("M_Flowers"); field.flowerMesh = BakedPlant("SM_Wildflower"); // wheat and oats share their plant: baking one again would throw away the first var ear = BakedPlant("SM_Prop_Plant_Wheat_Optimised_02"); var earFar = BakedPlant("SM_Prop_Plant_Wheat_Optimised_01"); field.crops = new[] { new GrassField.CropLook(), new GrassField.CropLook { mat = Mat("M_CropWheat"), near = ear, far = earFar }, new GrassField.CropLook { mat = Mat("M_CropOats"), near = ear, far = earFar }, new GrassField.CropLook { mat = Mat("M_CropCorn"), near = BakedPlant("SM_Prop_Plant_Corn_01_M"), far = BakedPlant("SM_Crop_Corn_Far") }, new GrassField.CropLook { mat = Mat("M_CropSunflower"), near = BakedPlant("SM_Prop_Sunflower_02"), far = BakedPlant("SM_Crop_Sunflower_Far") }, }; var ground = new GameObject("Ground").AddComponent(); ground.soil = Mat("M_Soil"); ground.spires = new[] { Model("SM_Spire_A"), Model("SM_Spire_B"), Model("SM_Spire_C") }; ground.trees = new[] { Model("SM_Generic_Tree_01"), Model("SM_Generic_Tree_02"), Model("SM_Generic_Tree_03") }; ground.bush = Model("SM_Prop_Plant_Bush_03_L"); var deck = new GameObject("Deck").AddComponent(); deck.tileModel = Model("SM_DeckTile"); deck.ladderModel = Model("SM_Ladder"); deck.gaps = new[] { new Vector3(0f, 0f, -9f) }; // the ramp opening in the south railing } /// Company furniture that every base has: the supply wall, the console, the crew cabin, the ramp and the yard. A garage has to be bought. static void RigFurniture() { var rig = new GameObject("Rig").transform; // the supply wall: tools on a pegboard along the west railing SupplyWallPiece(rig); ConsolePiece(rig, new Vector3(6.2f, 0f, -1.5f)); TravelPiece(rig, new Vector3(6.2f, 0f, 2.2f)); // the two treat machines against the north railing, facing the middle of the deck VendPiece(rig, "SM_VendBeer", TreatId.Beer, new Vector3(3.9f, 0f, 8.2f)); VendPiece(rig, "SM_VendSmoke", TreatId.Cigar, new Vector3(5.9f, 0f, 8.35f)); // crew cabin on the north-west tile var cabin = Place("SM_CrewCabin", new Vector3(-6f, 0f, 7f), 180f, rig); if (cabin) { SolidAt("CabinBox", new Vector3(0f, 1.6f, 0f), new Vector3(5.8f, 3.2f, 3.8f), cabin.transform); Blocker(cabin, Vector3.zero, new Vector2(5.8f, 3.8f)); } // loading ramp off the south edge, down to the meadow (1.2 m over 6 m) float slope = Mathf.Asin(0.2f) * Mathf.Rad2Deg; var dock = Place("SM_Dock", new Vector3(0f, 0f, -9f), 180f, rig); if (dock) { SolidAt("RampDeck", new Vector3(0f, -0.66f, 3.0f), new Vector3(3f, 0.12f, 6.2f), dock.transform, 0f, slope); SolidAt("RampCurbL", new Vector3(-1.44f, -0.5f, 3.0f), new Vector3(0.12f, 0.3f, 6.2f), dock.transform, 0f, slope); SolidAt("RampCurbR", new Vector3(1.44f, -0.5f, 3.0f), new Vector3(0.12f, 0.3f, 6.2f), dock.transform, 0f, slope); var gb = dock.AddComponent(); gb.center = new Vector3(0f, 0f, 3.2f); gb.halfSize = new Vector2(1.7f, 3.4f); } // the unloading yard: packed earth at the foot of the ramp where machines tip what they've cut, bays marked with logs var pad = new GameObject("UnloadYard"); pad.transform.SetParent(rig, false); pad.transform.position = new Vector3(0f, GameDefs.WaterY, -20f); var padBlock = pad.AddComponent(); padBlock.halfSize = new Vector2(9f, 8f); var slab = GameObject.CreatePrimitive(PrimitiveType.Cube); slab.name = "PadSlab"; slab.transform.SetParent(pad.transform, false); slab.transform.localPosition = new Vector3(0f, -0.04f, 0f); slab.transform.localScale = new Vector3(18f, 0.1f, 16f); slab.GetComponent().sharedMaterial = Mat("M_Yard"); slab.isStatic = true; for (int i = -2; i <= 2; i++) { if (i == 0) continue; var line = GameObject.CreatePrimitive(PrimitiveType.Cube); line.name = "BayLine"; Object.DestroyImmediate(line.GetComponent()); line.transform.SetParent(pad.transform, false); line.transform.localPosition = new Vector3(i * 2.6f, 0.05f, -1.0f); line.transform.localScale = new Vector3(0.22f, 0.14f, 8f); line.GetComponent().sharedMaterial = Mat("M_Timber"); line.isStatic = true; } // no shed at the base: bought and called machines park on the yard by the dock (garages players build keep theirs) var zone = new GameObject("DockZone").transform; zone.SetParent(rig, false); zone.position = new Vector3(0f, GameDefs.WaterY, -19f); zone.rotation = Quaternion.Euler(0f, 180f, 0f); _dockZone = zone; // a few props from the company crate Prop(rig, "SM_Decor_Lamp", new Vector3(-2.5f, 0f, -8.5f), 0f, 0.4f, 2.4f, true); Prop(rig, "SM_Decor_Lamp", new Vector3(2.5f, 0f, -8.5f), 0f, 0.4f, 2.4f, true); Prop(rig, "SM_Prop_Crate_01", new Vector3(-8.2f, 0f, -8.2f), 20f, 1f, 1f, false, 0.75f); Prop(rig, "SM_Prop_Barrel_01", new Vector3(8.5f, 0f, 2.5f), 90f, 0.6f, 0.9f, false, 0.75f); Prop(rig, "SM_Prop_Hay_Bale_Square_01", new Vector3(7.8f, 0f, -7.6f), 70f, 1.2f, 0.7f, false, 0.75f); Prop(rig, "SM_Prop_Wheelbarrow_01", new Vector3(4.6f, 0f, -7.9f), 200f, 1f, 0.7f, false, 0.75f); // where the crew spawns: south of the depot, looking north over the deck var sp = new GameObject("SpawnPoints").transform; for (int i = 0; i < NetSession.MaxPlayers; i++) { var p = new GameObject("Spawn" + i).transform; p.SetParent(sp, false); p.position = new Vector3(-6f + (i % 8) * 1.4f, 0.15f, -5f - (i / 8) * 1.4f); p.rotation = Quaternion.Euler(0f, 0f, 0f); } } /// GreenHaul console (the upgrade matrix). static void ConsolePiece(Transform rig, Vector3 pos) { var term = Place("SM_Terminal", pos, -90f, rig, false); if (!term) return; SolidAt("TermBox", new Vector3(0f, 0.9f, 0f), new Vector3(1.2f, 1.8f, 0.9f), term.transform); var st = term.AddComponent(); var scr = FindT(term, "Screen"); st.screen = scr ? scr.GetComponent() : null; foreach (var t in term.GetComponentsInChildren()) t.gameObject.layer = Layers.Build; Blocker(term, Vector3.zero, new Vector2(1.6f, 1.2f)); } /// A vending machine that trades a kilo of grass for a treat: its moving parts found by name and handed to the script. static void VendPiece(Transform rig, string model, TreatId kind, Vector3 pos) { var go = Place(model, pos, 180f, rig, false); if (!go) return; bool beer = kind == TreatId.Beer; SolidAt("VendBox", new Vector3(0f, beer ? 1.07f : 0.95f, 0f), beer ? new Vector3(1.05f, 2.14f, 0.85f) : new Vector3(1.1f, 1.9f, 0.62f), go.transform); var vm = go.AddComponent(); vm.kind = kind; var sign = FindT(go, "Sign"); vm.sign = sign ? sign.GetComponent() : null; var display = FindT(go, "Display"); vm.display = display ? display.GetComponent() : null; // the whole model shudders while it works vm.body = go.transform.childCount > 0 ? go.transform.GetChild(0) : null; vm.button = FindT(go, "Button"); vm.tray = FindT(go, "Tray"); vm.drawer = FindT(go, "Drawer"); vm.hopper = FindT(go, "Hopper"); vm.dropPoint = FindT(go, "DropPoint"); var knobs = new System.Collections.Generic.List(); for (int i = 0; i < 4; i++) { var k = FindT(go, "Knob" + i); if (k) knobs.Add(k); } vm.knobs = knobs.ToArray(); foreach (var t in go.GetComponentsInChildren()) t.gameObject.layer = Layers.Build; Blocker(go, Vector3.zero, new Vector2(1.5f, 1.3f)); } static void TravelPiece(Transform rig, Vector3 pos) { var dish = Place("SM_Decor_Dish", pos, -90f, rig, false); if (!dish) return; SolidAt("TravelBox", new Vector3(0f, 1f, 0f), new Vector3(1.2f, 2f, 1.2f), dish.transform); dish.AddComponent(); foreach (var t in dish.GetComponentsInChildren()) t.gameObject.layer = Layers.Build; Blocker(dish, Vector3.zero, new Vector2(1.6f, 1.6f)); } /// What stands around the depot of a far station: put together at the origin, then kept as a prefab. static GameObject StationRig() { var rig = new GameObject("Station_Rig"); var yard = GameObject.CreatePrimitive(PrimitiveType.Cylinder); yard.name = "Yard"; Object.DestroyImmediate(yard.GetComponent()); yard.transform.SetParent(rig.transform, false); yard.transform.localPosition = Vector3.up * 0.02f; yard.transform.localScale = new Vector3(22f, 0.04f, 22f); yard.GetComponent().sharedMaterial = Mat("M_Yard"); SupplyWallPiece(rig.transform); ConsolePiece(rig.transform, new Vector3(6.2f, 0f, -1.5f)); TravelPiece(rig.transform, new Vector3(6.2f, 0f, 2.2f)); VendPiece(rig.transform, "SM_VendBeer", TreatId.Beer, new Vector3(-2.2f, 0f, 7.6f)); VendPiece(rig.transform, "SM_VendSmoke", TreatId.Cigar, new Vector3(-0.2f, 0f, 7.7f)); var prefab = PrefabUtility.SaveAsPrefabAsset(rig, Prefabs + "Station_Rig.prefab"); Object.DestroyImmediate(rig); return prefab; } static void SupplyWallPiece(Transform rig) { var wall = new GameObject("SupplyWall"); wall.transform.SetParent(rig, false); wall.transform.position = new Vector3(-8.5f, 0f, -1.5f); wall.transform.rotation = Quaternion.Euler(0f, 90f, 0f); // faces the middle of the deck var body = Place("SM_SupplyWall", wall.transform.position, 0f, wall.transform, false); if (body) body.transform.localRotation = Quaternion.identity; SolidAt("WallBox", new Vector3(0f, 1.3f, 0f), new Vector3(4.54f, 2.6f, 0.34f), wall.transform); // the tools hang with their blades up, the cheapest on the left as you face the wall float[] sizes = { 0.5f, 0.8f, 0.24f, 0.46f, 0.52f, 0.6f, 0.7f }; for (int i = 0; i < GameDefs.Tools.Length && i < sizes.Length; i++) { var hung = Place(GameDefs.Tools[i].model, wall.transform.TransformPoint(new Vector3(1.8f - i * 0.6f, 0.9f, 0.14f)), 0f, wall.transform, false); if (!hung) continue; hung.transform.localRotation = Quaternion.Euler(-90f, 0f, 0f); hung.transform.localScale = Vector3.one * sizes[i]; } // the lawn mower is too big to hang: it stands on the deck at the end of the wall, handle toward you var mower = Place(GameDefs.Tool(ToolId.LawnMower).model, wall.transform.TransformPoint(new Vector3(-2.75f, 0.95f, 1.4f)), 0f, wall.transform, false); if (mower) mower.transform.localRotation = Quaternion.Euler(0f, 180f, 0f); wall.AddComponent(); foreach (var t in wall.GetComponentsInChildren()) t.gameObject.layer = Layers.Build; Blocker(wall, new Vector3(0f, 0f, 0.2f), new Vector2(4.8f, 0.8f)); } /// A small piece of base furniture with a box you can't walk through. static void Prop(Transform rig, string model, Vector3 pos, float yaw, float size, float height, bool lamp, float scale = 1f) { var go = Place(model, pos, yaw, rig); if (!go) return; go.transform.localScale *= scale; SolidAt("Solid", new Vector3(0f, height * 0.5f, 0f), new Vector3(size, height, size), go.transform); Blocker(go, Vector3.zero, new Vector2(size + 0.4f, size + 0.4f)); if (!lamp) return; var bulb = FindT(go, "Bulb"); var l = new GameObject("Light").AddComponent(); l.transform.SetParent(bulb ? bulb : go.transform, false); if (!bulb) l.transform.localPosition = new Vector3(0f, 2.2f, 0f); l.type = LightType.Point; l.range = 7f; l.intensity = 1.6f; l.color = new Color(1f, 0.85f, 0.6f); l.shadows = LightShadows.None; } // ───────────────────────── systems ───────────────────────── static void Systems() { new GameObject("Steam").AddComponent(); new GameObject("Boot").AddComponent(); // ambience: wind over the meadow, crickets new GameObject("Soundscape").AddComponent(); // cameras var rig = new GameObject("MenuRig"); var mc = rig.AddComponent(); mc.focus = new Vector3(0f, 3f, 2f); mc.radius = 24f; mc.height = 7f; mc.speed = 0.018f; var camGo = new GameObject("Main Camera"); camGo.tag = "MainCamera"; camGo.transform.SetParent(rig.transform, false); var cam = camGo.AddComponent(); cam.nearClipPlane = 0.05f; cam.farClipPlane = 14000f; cam.fieldOfView = 78f; cam.cullingMask = ~(1 << Layers.ViewModel); camGo.AddComponent(); var data = camGo.AddComponent(); data.renderPostProcessing = true; data.antialiasing = AntialiasingMode.SubpixelMorphologicalAntiAliasing; data.requiresDepthTexture = true; camGo.AddComponent(); mc.cam = camGo.transform; var vmGo = new GameObject("ViewModelCamera"); vmGo.transform.SetParent(camGo.transform, false); var vmCam = vmGo.AddComponent(); vmCam.cullingMask = 1 << Layers.ViewModel; vmCam.fieldOfView = 62f; vmCam.nearClipPlane = 0.01f; vmCam.farClipPlane = 6f; var vmData = vmGo.AddComponent(); vmData.renderType = CameraRenderType.Overlay; data.cameraStack.Add(vmCam); var vm = new GameObject("ViewModel"); vm.transform.SetParent(vmGo.transform, false); vm.layer = Layers.ViewModel; vm.AddComponent(); // networking var netGo = new GameObject("NetworkManager"); var nm = netGo.AddComponent(); var utp = netGo.AddComponent(); netGo.AddComponent(); netGo.AddComponent(); nm.NetworkConfig = new NetworkConfig { NetworkTransport = utp, PlayerPrefab = AssetDatabase.LoadAssetAtPath(Prefabs + "Player.prefab"), ConnectionApproval = true, EnableSceneManagement = false, TickRate = 30, ForceSamePrefabs = true, }; nm.NetworkConfig.Prefabs.NetworkPrefabsLists.Add(AssetDatabase.LoadAssetAtPath(Prefabs + "NetworkPrefabs.asset")); // spawner var sp = new GameObject("Spawner").AddComponent(); NetworkObject P(string n) { if (string.IsNullOrEmpty(n)) return null; var g = AssetDatabase.LoadAssetAtPath(Prefabs + n + ".prefab"); return g ? g.GetComponent() : null; } sp.teamStatePrefab = P("TeamState"); sp.cargoPrefabs = new[] { P("Cargo_Grass"), P("Cargo_Hay"), P("Cargo_Brick") }; sp.vehiclePrefabs = new[] { P("Vehicle_Push"), P("Vehicle_Rider"), P("Vehicle_Pickup"), P("Vehicle_Tractor"), P("Vehicle_Combine"), P("Vehicle_Hauler"), P("Vehicle_Mulcher"), P("Vehicle_Baler") }; sp.piecePrefabs = new NetworkObject[GameDefs.Pieces.Length]; for (int i = 0; i < GameDefs.Pieces.Length; i++) sp.piecePrefabs[i] = P(GameDefs.Pieces[i].prefab); sp.dock = _dockZone; new GameObject("Stations").AddComponent().rig = StationRig(); new GameObject("PowerWires").AddComponent(); new GameObject("BeltSound").AddComponent(); // UI var ui = new GameObject("UI"); var doc = ui.AddComponent(); doc.panelSettings = AssetDatabase.LoadAssetAtPath("Assets/UI/PanelSettings.asset"); var root = ui.AddComponent(); root.style = AssetDatabase.LoadAssetAtPath("Assets/UI/GrassCutter.uss"); } } }