From c2203996f24138caf4a97e206373eb8af244b642 Mon Sep 17 00:00:00 2001 From: Ben C Date: Fri, 6 May 2022 20:53:46 -0400 Subject: [PATCH] Added the ability to make custom sky boxes --- .../Builder/StarSystem/SkyboxBuilder.cs | 61 +++++++++++++++++-- .../External/Configs/StarSystemConfig.cs | 9 +++ NewHorizons/Handlers/SystemCreationHandler.cs | 27 ++++---- NewHorizons/star_system_schema.json | 19 ++++++ 4 files changed, 95 insertions(+), 21 deletions(-) diff --git a/NewHorizons/Builder/StarSystem/SkyboxBuilder.cs b/NewHorizons/Builder/StarSystem/SkyboxBuilder.cs index d447fe69..f3557854 100644 --- a/NewHorizons/Builder/StarSystem/SkyboxBuilder.cs +++ b/NewHorizons/Builder/StarSystem/SkyboxBuilder.cs @@ -1,12 +1,63 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using NewHorizons.External.Configs; +using NewHorizons.Utility; +using OWML.Common; +using UnityEngine; +using Logger = NewHorizons.Utility.Logger; +using Object = System.Object; namespace NewHorizons.Builder.StarSystem { - class SkyboxBuilder + internal class SkyboxBuilder { + public static Material LoadMaterial(string assetBundle, string path, string uniqueModName, IModBehaviour mod) + { + string key = uniqueModName + "." + assetBundle; + AssetBundle bundle; + Material cubemap; + + try + { + if (Main.AssetBundles.ContainsKey(key)) bundle = Main.AssetBundles[key]; + else + { + bundle = mod.ModHelper.Assets.LoadBundle(assetBundle); + Main.AssetBundles[key] = bundle; + } + } + catch (Exception e) + { + Logger.LogError($"Couldn't load AssetBundle {assetBundle} : {e.Message}"); + return null; + } + + try + { + cubemap = bundle.LoadAsset(path); + } + catch (Exception e) + { + Logger.Log($"Couldn't load asset {path} from AssetBundle {assetBundle} : {e.Message}"); + return null; + } + + return cubemap; + } + + public static void Make(StarSystemConfig.SkyboxConfig info, IModBehaviour mod) + { + Logger.Log("Building Skybox"); + Material skyBoxMaterial = LoadMaterial(info.assetBundle, info.path, mod.ModHelper.Manifest.UniqueName, mod); + RenderSettings.skybox = skyBoxMaterial; + DynamicGI.UpdateEnvironment(); + Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => + { + foreach (var camera in Resources.FindObjectsOfTypeAll()) + { + camera.clearFlags = CameraClearFlags.Skybox; + } + }); + } + } } diff --git a/NewHorizons/External/Configs/StarSystemConfig.cs b/NewHorizons/External/Configs/StarSystemConfig.cs index 29f117ce..f0c2be3a 100644 --- a/NewHorizons/External/Configs/StarSystemConfig.cs +++ b/NewHorizons/External/Configs/StarSystemConfig.cs @@ -14,6 +14,7 @@ namespace NewHorizons.External.Configs public bool destroyStockPlanets = true; public string factRequiredForWarp; public NomaiCoordinates coords; + public SkyboxConfig skybox; public class NomaiCoordinates { @@ -22,6 +23,14 @@ namespace NewHorizons.External.Configs public int[] z; } + public class SkyboxConfig + { + public string assetBundle = null; + public string path = null; + public bool destroyStarField = false; + } + public StarSystemConfig(Dictionary dict) : base(dict) { } + } } diff --git a/NewHorizons/Handlers/SystemCreationHandler.cs b/NewHorizons/Handlers/SystemCreationHandler.cs index 8465ba9f..ad50a626 100644 --- a/NewHorizons/Handlers/SystemCreationHandler.cs +++ b/NewHorizons/Handlers/SystemCreationHandler.cs @@ -5,7 +5,10 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using NewHorizons.Builder.StarSystem; using UnityEngine; +using Logger = NewHorizons.Utility.Logger; +using Object = UnityEngine.Object; namespace NewHorizons.Handlers { @@ -17,23 +20,15 @@ namespace NewHorizons.Handlers var skybox = GameObject.Find("Skybox/Starfield"); - /* - skybox.GetComponent().material.shader = Shader.Find("Standard"); - */ + if (system.Config.skybox?.destroyStarField ?? false) + { + Object.Destroy(skybox); + } - /* - var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); - GameObject.Destroy(sphere.GetComponent()); - - sphere.transform.parent = skybox.transform; - sphere.transform.localPosition = Vector3.zero; - - var meshFilter = sphere.GetComponent(); - meshFilter.mesh.triangles = meshFilter.mesh.triangles.Reverse().ToArray(); - - var meshRenderer = sphere.GetComponent(); - meshRenderer.material.color = Color.green; - */ + if (system.Config.skybox?.assetBundle != null && system.Config.skybox?.path != null) + { + SkyboxBuilder.Make(system.Config.skybox, system.Mod); + } } } diff --git a/NewHorizons/star_system_schema.json b/NewHorizons/star_system_schema.json index b31179ed..be3573b1 100644 --- a/NewHorizons/star_system_schema.json +++ b/NewHorizons/star_system_schema.json @@ -19,6 +19,25 @@ "destroyStockPlanets": { "type": "bool", "description": "Do you want a clean slate for this star system? Or will it be a modified version of the original." + }, + "skybox": { + "type": "object", + "description": "Options for the skybox of your system", + "properties": { + "destroyStarField": { + "type": "boolean", + "description": "Whether to destroy the star field around the player (always set to true if `assetBundle` and `path` is set)", + "default": false + }, + "assetBundle": { + "type": "string", + "description": "Path to the Unity asset bundle to load the skybox material from" + }, + "path": { + "type": "string", + "description": "Path to the material within the asset bundle specified by `assetBundle` to use for the skybox" + } + } } } }