Make sun appear dimmer on atmo shader when it should

This commit is contained in:
Nick 2022-07-24 22:30:04 -04:00
parent d66f179eb7
commit 3479e60c0d
3 changed files with 45 additions and 6 deletions

View File

@ -1,5 +1,6 @@
using NewHorizons.External.Modules;
using NewHorizons.Utility;
using System.Collections.Generic;
using UnityEngine;
namespace NewHorizons.Builder.Atmosphere
{
@ -9,6 +10,13 @@ namespace NewHorizons.Builder.Atmosphere
private static readonly int OuterRadius = Shader.PropertyToID("_OuterRadius");
private static readonly int SkyColor = Shader.PropertyToID("_SkyColor");
public static readonly List<(GameObject, Material)> Skys = new();
public static void Init()
{
Skys.Clear();
}
public static void Make(GameObject planetGO, Sector sector, AtmosphereModule atmosphereModule, float surfaceSize)
{
GameObject atmoGO = new GameObject("Atmosphere");
@ -20,15 +28,20 @@ namespace NewHorizons.Builder.Atmosphere
GameObject atmo = GameObject.Instantiate(SearchUtilities.Find("TimberHearth_Body/Atmosphere_TH/AtmoSphere"), atmoGO.transform, true);
atmo.transform.position = planetGO.transform.TransformPoint(Vector3.zero);
atmo.transform.localScale = Vector3.one * atmosphereModule.size * 1.2f;
foreach (var meshRenderer in atmo.GetComponentsInChildren<MeshRenderer>())
var renderers = atmo.GetComponentsInChildren<MeshRenderer>();
var sharedMaterial = new Material(renderers[0].material);
foreach (var meshRenderer in renderers)
{
meshRenderer.material.SetFloat(InnerRadius, atmosphereModule.clouds != null ? atmosphereModule.size : surfaceSize);
meshRenderer.material.SetFloat(OuterRadius, atmosphereModule.size * 1.2f);
if (atmosphereModule.atmosphereTint != null)
meshRenderer.material.SetColor(SkyColor, atmosphereModule.atmosphereTint.ToColor());
meshRenderer.sharedMaterial = sharedMaterial;
}
sharedMaterial.SetFloat(InnerRadius, atmosphereModule.clouds != null ? atmosphereModule.size : surfaceSize);
sharedMaterial.SetFloat(OuterRadius, atmosphereModule.size * 1.2f);
if (atmosphereModule.atmosphereTint != null) sharedMaterial.SetColor(SkyColor, atmosphereModule.atmosphereTint.ToColor());
atmo.SetActive(true);
Skys.Add((planetGO, sharedMaterial));
}
atmoGO.transform.position = planetGO.transform.TransformPoint(Vector3.zero);

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using NewHorizons.Builder.Atmosphere;
using System.Collections.Generic;
using UnityEngine;
using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Components
@ -7,6 +8,9 @@ namespace NewHorizons.Components
[RequireComponent(typeof(SunLightParamUpdater))]
public class StarLightController : MonoBehaviour
{
private static readonly int SunIntensity = Shader.PropertyToID("_SunIntensity");
private static readonly float hearthSunDistanceSqr = 8593 * 8593;
public static StarLightController Instance { get; private set; }
private List<StarController> _stars = new List<StarController>();
@ -57,9 +61,27 @@ namespace NewHorizons.Components
if (_stars.Count > 0) ChangeActiveStar(_stars[0]);
else gameObject.SetActive(false);
foreach (var atmo in AtmosphereBuilder.Skys)
{
var shader = atmo.Item2;
shader.SetFloat(SunIntensity, 0);
}
return;
}
// Update atmo shaders
foreach (var atmo in AtmosphereBuilder.Skys)
{
var planet = atmo.Item1;
var shader = atmo.Item2;
var sqrDist = (planet.transform.position - _activeStar.transform.position).sqrMagnitude;
var intensity = Mathf.Min(_activeStar.Light.intensity / (sqrDist / hearthSunDistanceSqr), 2f);
shader.SetFloat(SunIntensity, intensity);
}
foreach (var star in _stars)
{
if (star == null) continue;

View File

@ -1,5 +1,6 @@
using HarmonyLib;
using NewHorizons.AchievementsPlus;
using NewHorizons.Builder.Atmosphere;
using NewHorizons.Builder.Body;
using NewHorizons.Builder.Props;
using NewHorizons.Components;
@ -274,11 +275,14 @@ namespace NewHorizons
IsSystemReady = false;
NewHorizonsData.Load();
// Some builders have to be reset each loop
SignalBuilder.Init();
BrambleDimensionBuilder.Init();
AstroObjectLocator.Init();
StreamingHandler.Init();
AudioTypeHandler.Init();
AtmosphereBuilder.Init();
BrambleNodeBuilder.Init(BodyDict[CurrentStarSystem].Select(x => x.Config).Where(x => x.Bramble?.dimension != null).ToArray());
PlanetCreationHandler.Init(BodyDict[CurrentStarSystem]);