diff --git a/NewHorizons/Builder/Atmosphere/AtmosphereBuilder.cs b/NewHorizons/Builder/Atmosphere/AtmosphereBuilder.cs index 04baaeca..6ec91e61 100644 --- a/NewHorizons/Builder/Atmosphere/AtmosphereBuilder.cs +++ b/NewHorizons/Builder/Atmosphere/AtmosphereBuilder.cs @@ -6,7 +6,7 @@ namespace NewHorizons.Atmosphere { static class AtmosphereBuilder { - public static void Make(GameObject body, AtmosphereModule atmosphereModule) + public static void Make(GameObject body, AtmosphereModule atmosphereModule, float surfaceSize) { GameObject atmoGO = new GameObject("Atmosphere"); atmoGO.SetActive(false); @@ -14,66 +14,19 @@ namespace NewHorizons.Atmosphere if (atmosphereModule.HasAtmosphere) { - var mat = GameObject.Find("TimberHearth_Body/Atmosphere_TH/AtmoSphere/Atmosphere_LOD0").GetComponent().material; - - GameObject atmo = GameObject.Instantiate(GameObject.Find("Atmosphere_TH/AtmoSphere")); + GameObject atmo = GameObject.Instantiate(GameObject.Find("TimberHearth_Body/Atmosphere_TH/AtmoSphere")); atmo.transform.parent = atmoGO.transform; atmo.transform.localPosition = Vector3.zero; - atmo.transform.localScale = Vector3.one * atmosphereModule.Size; + atmo.transform.localScale = Vector3.one * atmosphereModule.Size * 1.2f; + foreach(var meshRenderer in atmo.GetComponentsInChildren()) + { + meshRenderer.material.SetFloat("_InnerRadius", atmosphereModule.Cloud != null ? atmosphereModule.Size : surfaceSize); + meshRenderer.material.SetFloat("_OuterRadius", atmosphereModule.Size * 1.2f); + if(atmosphereModule.AtmosphereTint != null) + meshRenderer.material.SetColor("_SkyColor", atmosphereModule.AtmosphereTint.ToColor32()); + } + atmo.SetActive(true); - - /* - GameObject lod0 = new GameObject(); - lod0.transform.parent = atmo.transform; - lod0.transform.localPosition = Vector3.zero; - MeshFilter f0 = lod0.AddComponent(); - f0.mesh = GameObject.Find("Atmosphere_LOD0").GetComponent().mesh; - MeshRenderer r0 = lod0.AddComponent(); - r0.material = mat; - - GameObject lod1 = new GameObject(); - lod1.transform.parent = atmo.transform; - lod1.transform.localPosition = Vector3.zero; - MeshFilter f1 = lod1.AddComponent(); - f1.mesh = GameObject.Find("Atmosphere_LOD1").GetComponent().mesh; - MeshRenderer r1 = lod1.AddComponent(); - r1.material = mat; - - GameObject lod2 = new GameObject(); - lod2.transform.parent = atmo.transform; - lod2.transform.localPosition = Vector3.zero; - MeshFilter f2 = lod2.AddComponent(); - f2.mesh = GameObject.Find("Atmosphere_LOD2").GetComponent().mesh; - MeshRenderer r2 = lod2.AddComponent(); - r2.material = mat; - - GameObject lod3 = new GameObject(); - lod3.transform.parent = atmo.transform; - lod3.transform.localPosition = Vector3.zero; - MeshFilter f3 = lod3.AddComponent(); - f3.mesh = GameObject.Find("Atmosphere_LOD3").GetComponent().mesh; - MeshRenderer r3 = lod3.AddComponent(); - r3.material = mat; - - LODGroup lodg = atmo.AddComponent(); - - LOD[] lodlist = new LOD[4]; - Renderer[] t0 = { r0 }; - Renderer[] t1 = { r1 }; - Renderer[] t2 = { r2 }; - Renderer[] t3 = { r3 }; - LOD one = new LOD(1, t0); - LOD two = new LOD(0.7f, t1); - LOD three = new LOD(0.27f, t2); - LOD four = new LOD(0.08f, t3); - lodlist[0] = one; - lodlist[1] = two; - lodlist[2] = three; - lodlist[3] = four; - - lodg.SetLODs(lodlist); - lodg.fadeMode = LODFadeMode.None; - */ } atmoGO.transform.localPosition = Vector3.zero; diff --git a/NewHorizons/Builder/Body/AsteroidBeltBuilder.cs b/NewHorizons/Builder/Body/AsteroidBeltBuilder.cs index d9789960..813e35d3 100644 --- a/NewHorizons/Builder/Body/AsteroidBeltBuilder.cs +++ b/NewHorizons/Builder/Body/AsteroidBeltBuilder.cs @@ -59,8 +59,6 @@ namespace NewHorizons.Builder.Body } }; - Logger.Log($"{config}"); - var asteroid = new NewHorizonsBody(new PlanetConfig(config), assets); Main.NextPassBodies.Add(asteroid); } diff --git a/NewHorizons/Builder/Body/StarBuilder.cs b/NewHorizons/Builder/Body/StarBuilder.cs index 66e0ef2d..e8d55779 100644 --- a/NewHorizons/Builder/Body/StarBuilder.cs +++ b/NewHorizons/Builder/Body/StarBuilder.cs @@ -14,79 +14,97 @@ namespace NewHorizons.Builder.Body static class StarBuilder { private static Texture2D _colorOverTime; - public static void Make(GameObject body, Sector sector, StarModule starModule) + public static StarController Make(GameObject body, Sector sector, StarModule starModule) { if (_colorOverTime == null) _colorOverTime = Main.Instance.ModHelper.Assets.GetTexture("AssetBundle/StarColorOverTime.png"); - var sunGO = new GameObject("Star"); - sunGO.transform.parent = body.transform; + var starGO = new GameObject("Star"); + starGO.transform.parent = body.transform; - var sunSurface = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Geometry_SUN/Surface"), sunGO.transform); + var sunSurface = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Geometry_SUN/Surface"), starGO.transform); sunSurface.transform.localPosition = Vector3.zero; sunSurface.transform.localScale = Vector3.one; sunSurface.name = "Surface"; //var sunLight = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight"), sunGO.transform); var sunLight = new GameObject(); - sunLight.transform.parent = sunGO.transform; + sunLight.transform.parent = starGO.transform; sunLight.transform.localPosition = Vector3.zero; sunLight.transform.localScale = Vector3.one; sunLight.name = "StarLight"; var light = sunLight.AddComponent(); light.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); - + light.intensity *= starModule.SolarLuminosity; - var solarFlareEmitter = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SolarFlareEmitter"), sunGO.transform); + var faceActiveCamera = sunLight.AddComponent(); + faceActiveCamera.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + var csmTextureCacher = sunLight.AddComponent(); + csmTextureCacher.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + csmTextureCacher._light = light; + var proxyShadowLight = sunLight.AddComponent(); + proxyShadowLight.CopyPropertiesFrom(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent()); + proxyShadowLight._light = light; + + var solarFlareEmitter = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SolarFlareEmitter"), starGO.transform); solarFlareEmitter.transform.localPosition = Vector3.zero; solarFlareEmitter.transform.localScale = Vector3.one; solarFlareEmitter.name = "SolarFlareEmitter"; - var sunAudio = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Audio_SUN"), sunGO.transform); + var sunAudio = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Audio_SUN"), starGO.transform); sunAudio.transform.localPosition = Vector3.zero; sunAudio.transform.localScale = Vector3.one; sunAudio.transform.Find("SurfaceAudio_Sun").GetComponent().maxDistance = starModule.Size * 2f; + var surfaceAudio = sunAudio.GetComponentInChildren(); + surfaceAudio.SetSector(sector); + surfaceAudio._sunController = null; + sunAudio.name = "Audio_Star"; - var sunAtmosphere = GameObject.Instantiate(GameObject.Find("Sun_Body/Atmosphere_SUN"), sunGO.transform); + var sunAtmosphere = GameObject.Instantiate(GameObject.Find("Sun_Body/Atmosphere_SUN"), starGO.transform); sunAtmosphere.transform.localPosition = Vector3.zero; sunAtmosphere.transform.localScale = Vector3.one; sunAtmosphere.name = "Atmosphere_Star"; - var ambientLightGO = GameObject.Instantiate(GameObject.Find("Sun_Body/AmbientLight_SUN"), sunGO.transform); + var ambientLightGO = GameObject.Instantiate(GameObject.Find("Sun_Body/AmbientLight_SUN"), starGO.transform); ambientLightGO.transform.localPosition = Vector3.zero; ambientLightGO.name = "AmbientLight_Star"; - var heatVolume = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Volumes_SUN/HeatVolume"), sunGO.transform); + var heatVolume = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Volumes_SUN/HeatVolume"), starGO.transform); heatVolume.transform.localPosition = Vector3.zero; heatVolume.transform.localScale = Vector3.one; heatVolume.GetComponent().radius = 1f; heatVolume.name = "HeatVolume"; - var deathVolume = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Volumes_SUN/ScaledVolumesRoot/DestructionFluidVolume"), sunGO.transform); + var deathVolume = GameObject.Instantiate(GameObject.Find("Sun_Body/Sector_SUN/Volumes_SUN/ScaledVolumesRoot/DestructionFluidVolume"), starGO.transform); deathVolume.transform.localPosition = Vector3.zero; deathVolume.transform.localScale = Vector3.one; deathVolume.GetComponent().radius = 1f; deathVolume.name = "DestructionVolume"; - - PlanetaryFogController fog = sunAtmosphere.transform.Find("FogSphere").GetComponent(); TessellatedSphereRenderer surface = sunSurface.GetComponent(); Light ambientLight = ambientLightGO.GetComponent(); - //GameObject.Destroy(sunLight.GetComponent()); - //GameObject.Destroy(sunLight.GetComponent()); - //GameObject.Destroy(sunLight.GetComponent()); - //SunLightController sunLightController = sunLight.GetComponent(); - //GameObject.Destroy(sunLight.GetComponent()); - //GameObject.Destroy(sunLightController); + Color lightColour = light.color; + if (starModule.LightTint != null) lightColour = starModule.LightTint.ToColor32(); + if (lightColour == null && starModule.Tint != null) + { + // Lighten it a bit + var r = Mathf.Clamp01(starModule.Tint.R * 1.5f / 255f); + var g = Mathf.Clamp01(starModule.Tint.G * 1.5f / 255f); + var b = Mathf.Clamp01(starModule.Tint.B * 1.5f / 255f); + lightColour = new Color(r, g, b); + } + if (lightColour != null) light.color = (Color)lightColour; + + light.color = lightColour; + ambientLight.color = lightColour; fog.fogRadius = starModule.Size * 1.2f; if(starModule.Tint != null) { var colour = starModule.Tint.ToColor32(); //sunLightController.sunColor = colour; - //ambientLight.color = colour; fog.fogTint = colour; var sun = GameObject.Find("Sun_Body"); @@ -94,8 +112,9 @@ namespace NewHorizons.Builder.Body var giantMaterial = sun.GetComponent().GetValue("_endSurfaceMaterial"); surface.sharedMaterial = new Material(starModule.Size >= 3000 ? giantMaterial : mainSequenceMaterial); - surface.sharedMaterial.color = new Color(colour.r * 8f / 255f, colour.g * 8f / 255f, colour.b * 8f / 255f); - surface.sharedMaterial.SetTexture("_ColorRamp", Utility.ImageUtilities.TintImage(_colorOverTime, colour)); + var mod = 8f * starModule.SolarLuminosity / 255f; + surface.sharedMaterial.color = new Color(colour.r * mod, colour.g * mod, colour.b * mod); + surface.sharedMaterial.SetTexture("_ColorRamp", ImageUtilities.TintImage(_colorOverTime, colour)); sunAtmosphere.transform.Find("AtmoSphere").transform.localScale = Vector3.one * (starModule.Size + 1000)/starModule.Size; foreach (var lod in sunAtmosphere.transform.Find("AtmoSphere").GetComponentsInChildren()) @@ -109,8 +128,19 @@ namespace NewHorizons.Builder.Body if(starModule.SolarFlareTint != null) solarFlareEmitter.GetComponent().tint = starModule.SolarFlareTint.ToColor32(); - sunGO.transform.localPosition = Vector3.zero; - sunGO.transform.localScale = starModule.Size * Vector3.one; + starGO.transform.localPosition = Vector3.zero; + starGO.transform.localScale = starModule.Size * Vector3.one; + + var starController = body.AddComponent(); + starController.Light = light; + starController.AmbientLight = ambientLight; + starController.FaceActiveCamera = faceActiveCamera; + starController.CSMTextureCacher = csmTextureCacher; + starController.ProxyShadowLight = proxyShadowLight; + starController.Intensity = starModule.SolarLuminosity; + starController.SunColor = lightColour; + + return starController; } } } diff --git a/NewHorizons/Builder/General/BaseBuilder.cs b/NewHorizons/Builder/General/BaseBuilder.cs index 98ab3f4f..11243407 100644 --- a/NewHorizons/Builder/General/BaseBuilder.cs +++ b/NewHorizons/Builder/General/BaseBuilder.cs @@ -10,7 +10,7 @@ namespace NewHorizons.Builder.General { static class BaseBuilder { - public static Tuple Make(GameObject body, AstroObject primaryBody, Vector3 positionVector, IPlanetConfig config) + public static Tuple Make(GameObject body, AstroObject primaryBody, IPlanetConfig config) { Rigidbody rigidBody = body.AddComponent(); rigidBody.mass = 10000; diff --git a/NewHorizons/Builder/General/DetectorBuilder.cs b/NewHorizons/Builder/General/DetectorBuilder.cs index c8b05fbf..5e40239a 100644 --- a/NewHorizons/Builder/General/DetectorBuilder.cs +++ b/NewHorizons/Builder/General/DetectorBuilder.cs @@ -181,8 +181,6 @@ namespace NewHorizons.Builder.General secondaryInitialMotion.SetValue("_initLinearDirection", d2); secondaryInitialMotion.SetValue("_initLinearSpeed", -v2); - Logger.Log($"Velocity: {d1}, {v1}, {d2}, {v2}"); - // InitialMotion already set its speed so we overwrite that if (!primaryInitialMotion.GetValue("_isInitVelocityDirty")) { diff --git a/NewHorizons/Builder/General/GravityBuilder.cs b/NewHorizons/Builder/General/GravityBuilder.cs index d7b20f71..97eb0aff 100644 --- a/NewHorizons/Builder/General/GravityBuilder.cs +++ b/NewHorizons/Builder/General/GravityBuilder.cs @@ -8,7 +8,7 @@ namespace NewHorizons.Builder.General { static class GravityBuilder { - public static void Make(GameObject body, AstroObject ao, float surfaceAccel, float sphereOfInfluence, float surface, string falloffType) + public static GravityVolume Make(GameObject body, AstroObject ao, float surfaceAccel, float sphereOfInfluence, float surface, string falloffType) { GameObject gravityGO = new GameObject("GravityWell"); gravityGO.transform.parent = body.transform; @@ -42,6 +42,8 @@ namespace NewHorizons.Builder.General gravityGO.SetActive(true); ao.SetValue("_gravityVolume", GV); + + return GV; } } } diff --git a/NewHorizons/Builder/General/PlanetDestroyer.cs b/NewHorizons/Builder/General/PlanetDestroyer.cs index 35740cae..2291cdda 100644 --- a/NewHorizons/Builder/General/PlanetDestroyer.cs +++ b/NewHorizons/Builder/General/PlanetDestroyer.cs @@ -49,17 +49,17 @@ namespace NewHorizons.Builder.General if (ao.GetAstroObjectName() == AstroObject.Name.CaveTwin || ao.GetAstroObjectName() == AstroObject.Name.TowerTwin) { + if (ao.GetAstroObjectName() == AstroObject.Name.TowerTwin) + GameObject.Find("TimeLoopRing_Body").SetActive(false); var focalBody = GameObject.Find("FocalBody"); if (focalBody != null) focalBody.SetActive(false); } - if (ao.GetAstroObjectName() == AstroObject.Name.MapSatellite) + else if (ao.GetAstroObjectName() == AstroObject.Name.MapSatellite) { var msb = GameObject.Find("MapSatellite_Body"); if (msb != null) msb.SetActive(false); } - if (ao.GetAstroObjectName() == AstroObject.Name.TowerTwin) - GameObject.Find("TimeLoopRing_Body").SetActive(false); - if (ao.GetAstroObjectName() == AstroObject.Name.ProbeCannon) + else if(ao.GetAstroObjectName() == AstroObject.Name.ProbeCannon) { GameObject.Find("NomaiProbe_Body").SetActive(false); GameObject.Find("CannonMuzzle_Body").SetActive(false); @@ -68,11 +68,11 @@ namespace NewHorizons.Builder.General GameObject.Find("FakeCannonBarrel_Body (1)").SetActive(false); GameObject.Find("Debris_Body (1)").SetActive(false); } - if (ao.GetAstroObjectName() == AstroObject.Name.SunStation) + else if(ao.GetAstroObjectName() == AstroObject.Name.SunStation) { GameObject.Find("SS_Debris_Body").SetActive(false); } - if (ao.GetAstroObjectName() == AstroObject.Name.GiantsDeep) + else if(ao.GetAstroObjectName() == AstroObject.Name.GiantsDeep) { GameObject.Find("BrambleIsland_Body").SetActive(false); GameObject.Find("GabbroIsland_Body").SetActive(false); @@ -80,15 +80,41 @@ namespace NewHorizons.Builder.General GameObject.Find("StatueIsland_Body").SetActive(false); GameObject.Find("ConstructionYardIsland_Body").SetActive(false); } - if (ao.GetAstroObjectName() == AstroObject.Name.WhiteHole) + else if(ao.GetAstroObjectName() == AstroObject.Name.WhiteHole) { GameObject.Find("WhiteholeStation_Body").SetActive(false); GameObject.Find("WhiteholeStationSuperstructure_Body").SetActive(false); } - if (ao.GetAstroObjectName() == AstroObject.Name.TimberHearth) + else if(ao.GetAstroObjectName() == AstroObject.Name.TimberHearth) { GameObject.Find("MiningRig_Body").SetActive(false); } + else if(ao.GetAstroObjectName() == AstroObject.Name.Sun) + { + var starController = ao.gameObject.GetComponent(); + Main.Instance.StarLightController.RemoveStar(starController); + GameObject.Destroy(starController); + + var audio = ao.GetComponentInChildren(); + GameObject.Destroy(audio); + + foreach(var owAudioSource in ao.GetComponentsInChildren()) + { + owAudioSource.Stop(); + GameObject.Destroy(owAudioSource); + } + + foreach (var audioSource in ao.GetComponentsInChildren()) + { + audioSource.Stop(); + GameObject.Destroy(audioSource); + } + } + else if(ao.GetAstroObjectName() == AstroObject.Name.DreamWorld) + { + GameObject.Find("BackRaft_Body").SetActive(false); + GameObject.Find("SealRaft_Body").SetActive(false); + } // Deal with proxies foreach (var p in GameObject.FindObjectsOfType()) diff --git a/NewHorizons/Builder/Props/PropBuilder.cs b/NewHorizons/Builder/Props/PropBuilder.cs index 4fa930c7..9f88c56f 100644 --- a/NewHorizons/Builder/Props/PropBuilder.cs +++ b/NewHorizons/Builder/Props/PropBuilder.cs @@ -13,15 +13,15 @@ namespace NewHorizons.Builder.Props { public static class PropBuilder { - public static void Make(GameObject body, string propToClone, Vector3 position, Sector sector) + public static GameObject Make(GameObject body, string propToClone, Vector3 position, Sector sector) { var prefab = GameObject.Find(propToClone); - Make(body, prefab, position, sector); + return Make(body, prefab, position, sector); } - public static void Make(GameObject body, GameObject prefab, Vector3 position, Sector sector) + public static GameObject Make(GameObject body, GameObject prefab, Vector3 position, Sector sector) { - if (prefab == null) return; + if (prefab == null) return null; GameObject prop = GameObject.Instantiate(prefab, sector.transform); prop.transform.localPosition = position; @@ -43,7 +43,6 @@ namespace NewHorizons.Builder.Props sector.OnOccupantEnterSector += ((SectorDetector sd) => StreamingManager.LoadStreamingAssets(assetBundle)); } - /* foreach(var component in prop.GetComponentsInChildren()) { try @@ -61,25 +60,13 @@ namespace NewHorizons.Builder.Props Logger.Log($"Found a _sector field in {component}"); sectorField.SetValue(component, sector); } - else - { - if(component is Campfire) - { - Logger.Log("CAMPFIRE"); - Campfire campfire = component as Campfire; - if (campfire._sector != null) - campfire._sector.OnSectorOccupantsUpdated -= campfire.OnSectorOccupantsUpdated; - - campfire._sector = sector; - campfire._sector.OnSectorOccupantsUpdated += campfire.OnSectorOccupantsUpdated; - } - } } catch (Exception e) { Logger.Log($"{e.Message}, {e.StackTrace}"); } } - */ prop.SetActive(true); + + return prop; } public static void Scatter(GameObject body, PropModule.ScatterInfo[] scatterInfo, float radius, Sector sector) @@ -87,14 +74,16 @@ namespace NewHorizons.Builder.Props var area = 4f * Mathf.PI * radius * radius; var points = FibonacciSphere((int)area); - foreach (var scatterer in scatterInfo) + foreach (var propInfo in scatterInfo) { - var prefab = GameObject.Find(scatterer.path); - for(int i = 0; i < scatterer.count; i++) + var prefab = GameObject.Find(propInfo.path); + for(int i = 0; i < propInfo.count; i++) { var randomInd = (int)Random.Range(0, points.Count); var point = points[randomInd]; - Make(body, prefab, point.normalized * radius, sector); + var prop = Make(body, prefab, point.normalized * radius, sector); + if(propInfo.offset != null) prop.transform.localPosition += prop.transform.TransformVector(propInfo.offset); + if(propInfo.rotation != null) prop.transform.rotation *= Quaternion.Euler(propInfo.rotation); points.RemoveAt(randomInd); if (points.Count == 0) return; } diff --git a/NewHorizons/External/AtmosphereModule.cs b/NewHorizons/External/AtmosphereModule.cs index 5265248d..229f9d5b 100644 --- a/NewHorizons/External/AtmosphereModule.cs +++ b/NewHorizons/External/AtmosphereModule.cs @@ -21,5 +21,6 @@ namespace NewHorizons.External public bool HasSnow { get; set; } public bool HasOxygen { get; set; } public bool HasAtmosphere { get; set; } + public MColor32 AtmosphereTint { get; set; } } } diff --git a/NewHorizons/External/PropModule.cs b/NewHorizons/External/PropModule.cs index 175be630..367485a2 100644 --- a/NewHorizons/External/PropModule.cs +++ b/NewHorizons/External/PropModule.cs @@ -16,6 +16,8 @@ namespace NewHorizons.External { public string path; public int count; + public MVector3 offset; + public MVector3 rotation; } } } diff --git a/NewHorizons/External/StarModule.cs b/NewHorizons/External/StarModule.cs index d4286081..6288367b 100644 --- a/NewHorizons/External/StarModule.cs +++ b/NewHorizons/External/StarModule.cs @@ -9,10 +9,10 @@ namespace NewHorizons.External { public class StarModule : Module { - public float Size { get; set; } - public MColor32 Tint { get; set; } + public float Size { get; set; } = 2000f; + public MColor32 Tint { get; set; } public MColor32 SolarFlareTint { get; set; } public MColor32 LightTint { get; set; } - public float SolarLuminosity { get; set; } + public float SolarLuminosity { get; set; } = 1f; } } diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 053ef20c..05654428 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -32,6 +32,8 @@ namespace NewHorizons public static float FurthestOrbit = 50000f; + public StarLightController StarLightController { get; private set; } + public override object GetApi() { return new NewHorizonsApi(); @@ -51,7 +53,7 @@ namespace NewHorizons { LoadConfigs(this); } - catch(Exception) + catch (Exception) { Logger.LogWarning("Couldn't find planets folder"); } @@ -65,52 +67,92 @@ namespace NewHorizons void OnSceneLoaded(Scene scene, LoadSceneMode mode) { - Logger.Log($"Scene Loaded: {scene} {mode}"); - if (scene.name != "SolarSystem") return; + Logger.Log($"Scene Loaded: {scene.name} {mode}"); + if (scene.name != "SolarSystem") { return; } + + // Need to manage this when there are multiple stars + var sun = GameObject.Find("Sun_Body"); + var starController = sun.AddComponent(); + starController.Light = GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent(); + starController.AmbientLight = GameObject.Find("Sun_Body/AmbientLight_SUN").GetComponent(); + starController.FaceActiveCamera = GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent(); + starController.CSMTextureCacher = GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent(); + starController.ProxyShadowLight = GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/SunLight").GetComponent(); + starController.Intensity = 0.9859f; + starController.SunColor = new Color(1f, 0.8845f, 0.6677f, 1f); + + var starLightGO = GameObject.Instantiate(sun.GetComponentInChildren().gameObject); + foreach(var comp in starLightGO.GetComponents()) + { + if(!(comp is SunLightController) && !(comp is SunLightParamUpdater) && !(comp is Light) && !(comp is Transform)) + { + GameObject.Destroy(comp); + } + } + GameObject.Destroy(starLightGO.GetComponent()); + starLightGO.name = "StarLightController"; + + StarLightController = starLightGO.AddComponent(); + StarLightController.AddStar(starController); + + starLightGO.SetActive(true); + + // TODO: Make this configurable probably Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => Locator.GetPlayerBody().gameObject.AddComponent()); AstroObjectLocator.RefreshList(); - foreach(AstroObject ao in GameObject.FindObjectsOfType()) + foreach (AstroObject ao in GameObject.FindObjectsOfType()) { AstroObjectLocator.AddAstroObject(ao); } - // Stars then planets then moons - var toLoad = BodyList.OrderBy(b => - (b.Config.BuildPriority != -1 ? b.Config.BuildPriority : - (b.Config.FocalPoint != null ? 0 : + // Stars then planets then moons (not necessary but probably speeds things up, maybe) + var toLoad = BodyList.OrderBy(b => + (b.Config.BuildPriority != -1 ? b.Config.BuildPriority : + (b.Config.FocalPoint != null ? 0 : (b.Config.Star != null) ? 0 : (b.Config.Orbit.IsMoon ? 2 : 1) ))).ToList(); - var flagNoneLoadedThisPass = true; - while(toLoad.Count != 0) + var passCount = 0; + while (toLoad.Count != 0) { + Logger.Log($"Starting body loading pass #{++passCount}"); + var flagNoneLoadedThisPass = true; foreach (var body in toLoad) { - if (LoadBody(body)) - flagNoneLoadedThisPass = false; + if (LoadBody(body)) flagNoneLoadedThisPass = false; } if (flagNoneLoadedThisPass) { + Logger.LogWarning("No objects were loaded this pass"); // Try again but default to sun - foreach(var body in toLoad) + foreach (var body in toLoad) { - if (LoadBody(body, true)) - flagNoneLoadedThisPass = false; - } - if(flagNoneLoadedThisPass) - { - // Give up - Logger.Log($"Couldn't finish adding bodies."); - return; + if (LoadBody(body, true)) flagNoneLoadedThisPass = false; } } + if (flagNoneLoadedThisPass) + { + // Give up + Logger.Log($"Couldn't finish adding bodies."); + return; + } + toLoad = NextPassBodies; NextPassBodies = new List(); + + // Infinite loop failsafe + if (passCount > 10) + { + Logger.Log("Something went wrong"); + break; + } } + Logger.Log("Done loading bodies"); + // I don't know what these do but they look really weird from a distance Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => PlanetDestroyer.RemoveDistantProxyClones()); } @@ -155,7 +197,7 @@ namespace NewHorizons { try { - GameObject planetObject = GenerateBody(body); + GameObject planetObject = GenerateBody(body, defaultPrimaryToSun); if (planetObject == null) return false; planetObject.SetActive(true); } @@ -168,7 +210,6 @@ namespace NewHorizons return true; } - public void LoadConfigs(IModBehaviour mod) { var folder = mod.ModHelper.Manifest.ModFolderPath; @@ -187,7 +228,7 @@ namespace NewHorizons } } - public static GameObject UpdateBody(NewHorizonsBody body, AstroObject ao) + public GameObject UpdateBody(NewHorizonsBody body, AstroObject ao) { Logger.Log($"Updating existing AstroObject {ao}"); @@ -200,10 +241,10 @@ namespace NewHorizons return SharedGenerateBody(body, go, sector, rb, ao); } - public static GameObject GenerateBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) + public GameObject GenerateBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) { AstroObject primaryBody; - if(body.Config.Orbit.PrimaryBody != null) + if (body.Config.Orbit.PrimaryBody != null) { primaryBody = AstroObjectLocator.GetAstroObject(body.Config.Orbit.PrimaryBody); if (primaryBody == null) @@ -225,27 +266,25 @@ namespace NewHorizons primaryBody = null; } - Logger.Log($"Begin generation sequence of [{body.Config.Name}]"); var go = new GameObject(body.Config.Name.Replace(" ", "").Replace("'", "") + "_Body"); go.SetActive(false); - if(body.Config.Base.GroundSize != 0) GeometryBuilder.Make(go, body.Config.Base.GroundSize); + if (body.Config.Base.GroundSize != 0) GeometryBuilder.Make(go, body.Config.Base.GroundSize); var atmoSize = body.Config.Atmosphere != null ? body.Config.Atmosphere.Size : 0f; float sphereOfInfluence = Mathf.Max(atmoSize, body.Config.Base.SurfaceSize * 2f); - var positionVector = OrbitalHelper.RotateTo(Vector3.left * body.Config.Orbit.SemiMajorAxis * (1 + body.Config.Orbit.Eccentricity), body.Config.Orbit); - - var outputTuple = BaseBuilder.Make(go, primaryBody, positionVector, body.Config); + var outputTuple = BaseBuilder.Make(go, primaryBody, body.Config); var ao = (AstroObject)outputTuple.Item1; var owRigidBody = (OWRigidbody)outputTuple.Item2; + GravityVolume gv = null; if (body.Config.Base.SurfaceGravity != 0) - GravityBuilder.Make(go, ao, body.Config.Base.SurfaceGravity, sphereOfInfluence * (body.Config.Star != null ? 10f : 1f), body.Config.Base.SurfaceSize, body.Config.Base.GravityFallOff); - - if(body.Config.Base.HasReferenceFrame) + gv = GravityBuilder.Make(go, ao, body.Config.Base.SurfaceGravity, sphereOfInfluence * (body.Config.Star != null ? 10f : 1f), body.Config.Base.SurfaceSize, body.Config.Base.GravityFallOff); + + if (body.Config.Base.HasReferenceFrame) RFVolumeBuilder.Make(go, owRigidBody, sphereOfInfluence); if (body.Config.Base.HasMapMarker) @@ -268,8 +307,7 @@ namespace NewHorizons if (body.Config.Base.BlackHoleSize != 0) BlackHoleBuilder.Make(go, body.Config.Base, sector); - if (body.Config.Star != null) - StarBuilder.Make(go, sector, body.Config.Star); + if (body.Config.Star != null) StarLightController.AddStar(StarBuilder.Make(go, sector, body.Config.Star)); if (body.Config.FocalPoint != null) FocalPointBuilder.Make(go, body.Config.FocalPoint); @@ -281,6 +319,7 @@ namespace NewHorizons // Now that we're done move the planet into place go.transform.parent = Locator.GetRootTransform(); + var positionVector = OrbitalHelper.RotateTo(Vector3.left * body.Config.Orbit.SemiMajorAxis * (1 + body.Config.Orbit.Eccentricity), body.Config.Orbit); go.transform.position = positionVector + (primaryBody == null ? Vector3.zero : primaryBody.transform.position); if (go.transform.position.magnitude > FurthestOrbit) @@ -289,7 +328,7 @@ namespace NewHorizons } // Have to do this after setting position - InitialMotionBuilder.Make(go, primaryBody, owRigidBody, body.Config.Orbit); + var initialMotion = InitialMotionBuilder.Make(go, primaryBody, owRigidBody, body.Config.Orbit); // Spawning on other planets is a bit hacky so we do it last if (body.Config.Spawn != null) @@ -306,7 +345,7 @@ namespace NewHorizons return go; } - private static GameObject SharedGenerateBody(NewHorizonsBody body, GameObject go, Sector sector, OWRigidbody rb, AstroObject ao) + private GameObject SharedGenerateBody(NewHorizonsBody body, GameObject go, Sector sector, OWRigidbody rb, AstroObject ao) { if (body.Config.Ring != null) RingBuilder.Make(go, body.Config.Ring, body.Assets); @@ -316,8 +355,8 @@ namespace NewHorizons if (body.Config.Base.HasCometTail) CometTailBuilder.Make(go, body.Config.Base, go.GetComponent().GetPrimaryBody()); - - if(body.Config.Base != null) + + if (body.Config.Base != null) { if (body.Config.Base.LavaSize != 0) LavaBuilder.Make(go, sector, rb, body.Config.Base.LavaSize); @@ -341,7 +380,7 @@ namespace NewHorizons if (body.Config.Atmosphere.FogSize != 0) FogBuilder.Make(go, sector, body.Config.Atmosphere); - AtmosphereBuilder.Make(go, body.Config.Atmosphere); + AtmosphereBuilder.Make(go, body.Config.Atmosphere, body.Config.Base.SurfaceSize); } if (body.Config.Props != null) diff --git a/NewHorizons/OrbitalPhysics/TrackingOrbitLine.cs b/NewHorizons/OrbitalPhysics/TrackingOrbitLine.cs index 6ea98521..84656b0c 100644 --- a/NewHorizons/OrbitalPhysics/TrackingOrbitLine.cs +++ b/NewHorizons/OrbitalPhysics/TrackingOrbitLine.cs @@ -7,12 +7,12 @@ using UnityEngine; namespace NewHorizons.OrbitalPhysics { - public class TrackingOrbitLine : OrbitLine - { + public class TrackingOrbitLine : OrbitLine + { private Vector3[] _vertices; private float _timer; private bool _firstTimeEnabled = true; - + public float TrailTime = 120f; @@ -44,7 +44,7 @@ namespace NewHorizons.OrbitalPhysics public override void OnEnterMapView() { - if (_firstTimeEnabled) + if (_firstTimeEnabled) { ResetLineVertices(); _firstTimeEnabled = false; @@ -65,17 +65,17 @@ namespace NewHorizons.OrbitalPhysics _timer += Time.deltaTime; var updateTime = (TrailTime / (float)_numVerts); - if(_timer > updateTime) - { + if (_timer > updateTime) + { for (int i = _numVerts - 1; i > 0; i--) { var v = _vertices[i - 1]; _vertices[i] = new Vector3(v.x, v.y, v.z); } - _vertices[0] = transform.parent.position - origin; - _lineRenderer.SetPositions(_vertices); _timer = 0; } + _vertices[0] = transform.parent.position - origin; + _lineRenderer.SetPositions(_vertices); base.transform.position = origin; base.transform.rotation = Quaternion.AngleAxis(0f, Vector3.up); @@ -95,12 +95,12 @@ namespace NewHorizons.OrbitalPhysics var dist1 = Vector3.Distance(point, _vertices[0]); var dist2 = Vector3.Distance(point, _vertices[(int)(_numVerts / 2)]); var dist3 = Vector3.Distance(point, _vertices[_numVerts - 1]); - + return Mathf.Min(new float[] { dist1, dist2, dist3 }); } public void ResetLineVertices() - { + { var primary = _astroObject.GetPrimaryBody(); Vector3 origin = primary == null ? Locator.GetRootTransform().position : primary.transform.position; @@ -114,4 +114,4 @@ namespace NewHorizons.OrbitalPhysics _lineRenderer.SetPositions(_vertices); } } -} +} \ No newline at end of file diff --git a/NewHorizons/Utility/DebugRaycaster.cs b/NewHorizons/Utility/DebugRaycaster.cs index 5742691b..8a265396 100644 --- a/NewHorizons/Utility/DebugRaycaster.cs +++ b/NewHorizons/Utility/DebugRaycaster.cs @@ -22,6 +22,20 @@ namespace NewHorizons.Utility { if (Keyboard.current != null && Keyboard.current[Key.P].wasReleasedThisFrame) { + /* + var soundSources = GameObject.FindObjectsOfType(); + foreach(var s in soundSources) + { + if (s.isPlaying) + { + Logger.Log($"{s.name}, {s.gameObject.name}"); + Logger.LogPath(s.gameObject); + s.loop = false; + s.Stop(); + } + } + */ + // Raycast _rb.DisableCollisionDetection(); int layerMask = OWLayerMask.physicalMask; diff --git a/NewHorizons/Utility/NewHorizonExtensions.cs b/NewHorizons/Utility/NewHorizonExtensions.cs index 7a2e4e27..4a10d5ef 100644 --- a/NewHorizons/Utility/NewHorizonExtensions.cs +++ b/NewHorizons/Utility/NewHorizonExtensions.cs @@ -87,7 +87,14 @@ namespace NewHorizons.Utility continue; } - targetProperty.SetValue(destination, srcProp.GetValue(source, null), null); + try + { + targetProperty.SetValue(destination, srcProp.GetValue(source, null), null); + } catch(Exception) + { + Logger.LogWarning($"Couldn't copy property {targetProperty.Name} from {source} to {destination}"); + } + } } } diff --git a/NewHorizons/Utility/Patches.cs b/NewHorizons/Utility/Patches.cs index d0c94b3b..ece09eb6 100644 --- a/NewHorizons/Utility/Patches.cs +++ b/NewHorizons/Utility/Patches.cs @@ -12,8 +12,13 @@ namespace NewHorizons.Utility { public static void Apply() { + // Prefixes Main.Instance.ModHelper.HarmonyHelper.AddPrefix("GetHUDDisplayName", typeof(Patches), nameof(Patches.GetHUDDisplayName)); Main.Instance.ModHelper.HarmonyHelper.AddPrefix("CheckShipOutsideSolarSystem", typeof(Patches), nameof(Patches.CheckShipOutersideSolarSystem)); + Main.Instance.ModHelper.HarmonyHelper.AddPrefix("LateUpdate", typeof(Patches), nameof(Patches.OnSunLightParamUpdaterLateUpdate)); + Main.Instance.ModHelper.HarmonyHelper.AddPrefix("Update", typeof(Patches), nameof(Patches.OnSunSurfaceAudioControllerUpdate)); + + // Postfixes Main.Instance.ModHelper.HarmonyHelper.AddPostfix("Start", typeof(Patches), nameof(Patches.OnEllipticOrbitLineStart)); Main.Instance.ModHelper.HarmonyHelper.AddPostfix("Awake", typeof(Patches), nameof(Patches.OnMapControllerAwake)); Main.Instance.ModHelper.HarmonyHelper.AddPostfix("Awake", typeof(Patches), nameof(Patches.OnOWCameraAwake)); @@ -56,5 +61,37 @@ namespace NewHorizons.Utility { __instance.farClipPlane *= 4f; } + + public static bool OnSunLightParamUpdaterLateUpdate(SunLightParamUpdater __instance) + { + if (__instance.sunLight) + { + Vector3 position = __instance.transform.position; + float w = 2000f; + if (__instance._sunController != null) + { + w = (__instance._sunController.HasSupernovaStarted() ? __instance._sunController.GetSupernovaRadius() : __instance._sunController.GetSurfaceRadius()); + } + float range = __instance.sunLight.range; + Color color = (__instance._sunLightController != null) ? __instance._sunLightController.sunColor : __instance.sunLight.color; + float w2 = (__instance._sunLightController != null) ? __instance._sunLightController.sunIntensity : __instance.sunLight.intensity; + Shader.SetGlobalVector(__instance._propID_SunPosition, new Vector4(position.x, position.y, position.z, w)); + Shader.SetGlobalVector(__instance._propID_OWSunPositionRange, new Vector4(position.x, position.y, position.z, 1f / (range * range))); + Shader.SetGlobalVector(__instance._propID_OWSunColorIntensity, new Vector4(color.r, color.g, color.b, w2)); + } + + return false; + } + + public static bool OnSunSurfaceAudioControllerUpdate(SunSurfaceAudioController __instance) + { + if (__instance._sunController != null) return true; + + var surfaceRadius = __instance.transform.parent.parent.localScale.magnitude; + float value = Mathf.Max(0f, Vector3.Distance(Locator.GetPlayerCamera().transform.position, __instance.transform.position) - surfaceRadius); + float num = Mathf.InverseLerp(1600f, 100f, value); + __instance._audioSource.SetLocalVolume(num * num * __instance._fade); + return false; + } } } diff --git a/NewHorizons/Utility/StarController.cs b/NewHorizons/Utility/StarController.cs new file mode 100644 index 00000000..143b8ca4 --- /dev/null +++ b/NewHorizons/Utility/StarController.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace NewHorizons.Utility +{ + public class StarController : MonoBehaviour + { + public Light Light; + public Light AmbientLight; + public FaceActiveCamera FaceActiveCamera; + public CSMTextureCacher CSMTextureCacher; + public ProxyShadowLight ProxyShadowLight; + public float Intensity; + public Color SunColor; + + public void Awake() + { + Disable(); + } + + public void Disable() + { + if (FaceActiveCamera != null) FaceActiveCamera.enabled = false; + if (CSMTextureCacher != null) CSMTextureCacher.enabled = false; + if (ProxyShadowLight != null) ProxyShadowLight.enabled = false; + } + + public void Enable() + { + if (FaceActiveCamera != null) FaceActiveCamera.enabled = true; + if (CSMTextureCacher != null) CSMTextureCacher.enabled = true; + if (ProxyShadowLight != null) ProxyShadowLight.enabled = true; + } + } +} diff --git a/NewHorizons/Utility/StarLightController.cs b/NewHorizons/Utility/StarLightController.cs new file mode 100644 index 00000000..5e3f8948 --- /dev/null +++ b/NewHorizons/Utility/StarLightController.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using Logger = NewHorizons.Utility.Logger; + +namespace NewHorizons.Utility +{ + [RequireComponent(typeof(SunLightController))] + [RequireComponent(typeof(SunLightParamUpdater))] + public class StarLightController : MonoBehaviour + { + private List _stars = new List(); + private StarController _activeStar; + + private SunLightController _sunLightController; + private SunLightParamUpdater _sunLightParamUpdater; + + public void Awake() + { + _sunLightController = GetComponent(); + _sunLightController.enabled = true; + _sunLightParamUpdater = GetComponent(); + _sunLightParamUpdater._sunLightController = _sunLightController; + } + + public void AddStar(StarController star) + { + Logger.Log($"Adding new star to list: {star.gameObject.name}"); + _stars.Add(star); + } + + public void RemoveStar(StarController star) + { + if (_stars.Contains(star)) + { + if (_activeStar.Equals(star)) _activeStar = null; + _stars.Remove(star); + } + } + + public void Update() + { + if (_activeStar == null || !_activeStar.gameObject.activeInHierarchy) + { + if (_stars.Contains(_activeStar)) _stars.Remove(_activeStar); + if (_stars.Count > 0) ChangeActiveStar(_stars[0]); + else gameObject.SetActive(false); + + return; + } + + foreach(var star in _stars) + { + if (star == null) continue; + + // Player is always at 0,0,0 more or less so if they arent using the map camera then wtv + var origin = Vector3.zero; + if (PlayerState.InMapView()) + { + origin = Locator.GetActiveCamera().transform.position; + } + + if (star.Intensity * (star.transform.position - origin).sqrMagnitude < star.Intensity * (_activeStar.transform.position - origin).sqrMagnitude) + { + ChangeActiveStar(star); + break; + } + } + } + + private void ChangeActiveStar(StarController star) + { + if (_sunLightController == null || _sunLightParamUpdater == null) return; + + if(_activeStar != null) _activeStar.Disable(); + + Logger.Log($"Switching active star: {star.gameObject.name}"); + + _activeStar = star; + + star.Enable(); + + _sunLightController._sunBaseColor = star.SunColor; + _sunLightController._sunBaseIntensity = star.Intensity; + _sunLightController._sunLight = star.Light; + _sunLightController._ambientLight = star.AmbientLight; + + _sunLightParamUpdater.sunLight = star.Light; + _sunLightParamUpdater._sunController = star.transform.GetComponent(); + _sunLightParamUpdater._propID_SunPosition = Shader.PropertyToID("_SunPosition"); + _sunLightParamUpdater._propID_OWSunPositionRange = Shader.PropertyToID("_OWSunPositionRange"); + _sunLightParamUpdater._propID_OWSunColorIntensity = Shader.PropertyToID("_OWSunColorIntensity"); + + // For the param thing to work it wants this to be on the star idk + this.transform.parent = star.transform; + this.transform.localPosition = Vector3.zero; + } + } +} diff --git a/README.md b/README.md index 467a2007..14a9a243 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ Planets are created using a JSON file format structure, and placed in the `plane - Edit existing planet orbits - Black hole / white hole pairs - Separate solar system scenes accessible via wormhole +- Cloaking field +- Signalscope signal ## How to create your own planets using configs