Skip to content

Commit

Permalink
Improvements to light setting tracking.
Browse files Browse the repository at this point in the history
Fixes for additional lights in main game.
  • Loading branch information
OrangeSpork committed Oct 25, 2021
1 parent db226ee commit 7eb6115
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Graphics/AIGraphics/AIGraphics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>D:\AI-Syoujyo\BepInEx\plugins\gps\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;AI</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
2 changes: 1 addition & 1 deletion Graphics/Shared/Graphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class Graphics : BaseUnityPlugin
{
public const string GUID = "ore.graphics";
public const string PluginName = "Graphics";
public const string Version = "0.4.4";
public const string Version = "0.4.5";

public static ConfigEntry<KeyCode> ConfigShortcut { get; private set; }
public static ConfigEntry<string> ConfigCubeMapPath { get; private set; }
Expand Down
8 changes: 8 additions & 0 deletions Graphics/Shared/LightManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ internal static List<LightObject> GetLights()

private bool IsStudioLight { get => Graphics.Instance.IsStudio() && null == _light && null != _ociLight; }

internal OCILight ociLight
{
get
{
return _ociLight;
}
}

internal Light light
{
get
Expand Down
10 changes: 9 additions & 1 deletion Graphics/Shared/MapLightPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,24 @@ public void UpdateParameters()

public void ApplyParameters()
{
#if DEBUG
Graphics.Instance.Log.LogInfo($"Loading Reflection Probes.");
#endif
if (reflectionProbes != null && reflectionProbes.Length > 0)
SceneController.ApplyReflectionProbeSettings(reflectionProbes);

#if DEBUG
Graphics.Instance.Log.LogInfo($"Loaded Light Settings.");
#endif
if (lights != null && lights.Length > 0)
SceneController.ApplyLightSettings(lights);
}

public void Load(byte[] bytes)
{
Deserialize(bytes);
#if DEBUG
Graphics.Instance.Log.LogInfo($"Loaded map preset, applying.");
#endif
ApplyParameters();
}

Expand Down
54 changes: 50 additions & 4 deletions Graphics/Shared/SceneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,48 @@ private PluginData DoSave()
return pluginData;
}


// Using a mix of hierarchy and object id key allows us to handle both Object lights and Scene lights
private static PerLightSettings FindLightSettingsForLight(LightObject light, PerLightSettings[] settings)
{
#if DEBUG
Graphics.Instance.Log.LogInfo($"Light: {light?.ociLight} {light?.ociLight?.lightInfo?.dicKey} {light?.light?.gameObject?.transform}");
#endif

PerLightSettings setting = null;
if (light.ociLight != null)
{
// Try by dic key AND path
setting = settings.FirstOrDefault(s => s.LightId == light?.ociLight?.lightInfo.dicKey && s.HierarchyPath != null && s.HierarchyPath.Matches(light.light.gameObject.transform));
if (setting != null)
{
#if DEBUG
Graphics.Instance.Log.LogInfo($"Found by dic key and hierarchy: {setting.LightId}:{setting.HierarchyPath}");
#endif
return setting;
}

// Try by dic key
setting = settings.FirstOrDefault(s => s.LightId == light.ociLight.lightInfo.dicKey);
if (setting != null)
{
#if DEBUG
Graphics.Instance.Log.LogInfo($"Found by dic key only: {setting.LightId}:{setting.HierarchyPath}");
#endif
return setting;
}
}
// Try by path
setting = settings.FirstOrDefault(s => s.HierarchyPath.Matches(light.light.gameObject.transform));
#if DEBUG
if (setting != null)
Graphics.Instance.Log.LogInfo($"Found by hierarchy only: {setting.LightId}:{setting.HierarchyPath}");
else
Graphics.Instance.Log.LogInfo($"New Light");
#endif
return setting;
}

public static void ApplyLightSettings(PerLightSettings[] settings)
{
LightManager lightManager = Graphics.Instance.LightManager;
Expand All @@ -98,7 +140,7 @@ public static void ApplyLightSettings(PerLightSettings[] settings)
{
foreach (LightObject light in lightManager.DirectionalLights)
{
PerLightSettings setting = settings.FirstOrDefault(s => s.HierarchyPath.Matches(light.light.gameObject.transform));
PerLightSettings setting = FindLightSettingsForLight(light, settings);
if (setting != null)
{
setting.ApplySettings(light);
Expand All @@ -108,14 +150,14 @@ public static void ApplyLightSettings(PerLightSettings[] settings)

foreach (LightObject light in lightManager.PointLights)
{
PerLightSettings setting = settings.FirstOrDefault(s => s.HierarchyPath.Matches(light.light.gameObject.transform));
PerLightSettings setting = FindLightSettingsForLight(light, settings);
if (setting != null)
setting.ApplySettings(light);
}

foreach (LightObject light in lightManager.SpotLights)
{
PerLightSettings setting = settings.FirstOrDefault(s => s.HierarchyPath.Matches(light.light.gameObject.transform));
PerLightSettings setting = FindLightSettingsForLight(light, settings);
if (setting != null)
setting.ApplySettings(light);
}
Expand Down Expand Up @@ -149,14 +191,18 @@ public static void ApplyLightSettings(PerLightSettings[] settings)
// Add extra lights if requested
foreach (PerLightSettings setting in newDirectionalLights)
{
#if DEBUG
Graphics.Instance.Log.LogInfo($"Adding Additional Light {setting.LightName}");
#endif

GameObject lightGameObject = new GameObject(setting.LightName);
Light lightComp = lightGameObject.AddComponent<Light>();
lightGameObject.GetComponent<Light>().type = LightType.Directional;
}
lightManager.Light();
foreach (LightObject light in lightManager.DirectionalLights)
{
PerLightSettings setting = settings.FirstOrDefault(s => s.HierarchyPath.Matches(light.light.gameObject.transform));
PerLightSettings setting = FindLightSettingsForLight(light, settings);
if (setting != null)
{
setting.ApplySettings(light);
Expand Down
23 changes: 19 additions & 4 deletions Graphics/Shared/Setting/PerLightSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ public class PerLightSettings
public int RenderMode { get; set; }
public int CullingMask { get; set; }
public PathElement HierarchyPath { get; set; }
public int LightId { get; set; }

public int Type { get; set; }

internal void ApplySettings(LightObject lightObject)
{
{
lightObject.enabled = !Disabled;

Graphics.Instance.LightManager.UseAlloyLight = UseAlloyLight;
Expand All @@ -47,23 +48,26 @@ internal void ApplySettings(LightObject lightObject)
{
SetAlias(lightObject.light, LightName);
}

lightObject.color = Color;
lightObject.light.colorTemperature = ColorTemperature;


lightObject.shadows = (LightShadows)ShadowType;
lightObject.light.shadowStrength = ShadowStrength;
if (LightType.Directional == lightObject.type && Graphics.Instance.Settings.UsePCSS)
lightObject.ShadowCustomResolution = ShadowResolutionCustom;
else
lightObject.light.shadowResolution = (LightShadowResolution)ShadowResolutionType;


lightObject.light.shadowBias = ShadowBias;
lightObject.light.shadowNormalBias = ShadowNormalBias;
lightObject.light.shadowNearPlane = ShadowNearPlane;

lightObject.intensity = LightIntensity;
lightObject.light.bounceIntensity = IndirectMultiplier;

if (SegiSun)
{
if (null != Graphics.Instance.CameraSettings.MainCamera)
Expand All @@ -82,7 +86,6 @@ internal void ApplySettings(LightObject lightObject)
else if (!KKAPI.Studio.StudioAPI.InsideStudio && lightObject.light.name.StartsWith("(Graphics)"))
lightObject.rotation = Rotation;


lightObject.range = Range;
lightObject.spotAngle = SpotAngle;
if (Graphics.Instance.LightManager.UseAlloyLight)
Expand Down Expand Up @@ -111,6 +114,10 @@ internal void FillSettings(LightObject lightObject)
LightName = NameForLight(lightObject.light);
Graphics.Instance.Log.LogInfo($"Storing Light Alias {LightName}");
}
else if (GraphicsAddedLight(lightObject.light))
{
LightName = lightObject.light.gameObject.name;
}

Color = lightObject.color;
ColorTemperature = lightObject.light.colorTemperature;
Expand Down Expand Up @@ -166,6 +173,14 @@ internal void FillSettings(LightObject lightObject)
CullingMask = lightObject.light.cullingMask;

HierarchyPath = PathElement.Build(lightObject.light.gameObject.transform);

if (lightObject.ociLight != null)
LightId = lightObject.ociLight.lightInfo.dicKey;
}

internal static bool GraphicsAddedLight(Light light)
{
return light.gameObject.name.StartsWith("(Graphics)");
}

internal static Dictionary<WeakReference<Light>, string> LightNameAliases = new Dictionary<WeakReference<Light>, string>();
Expand Down

0 comments on commit 7eb6115

Please sign in to comment.