Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make compound clouds and 3D objects' lighting depend on the light level #5771

Merged
merged 10 commits into from
Jan 13, 2025
13 changes: 6 additions & 7 deletions src/microbe_stage/MicrobeStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,20 +1034,19 @@ protected override void OnLightLevelUpdate()
if (GameWorld.Map.CurrentPatch == null)
return;

// TODO: it would make more sense for the GameWorld to update its patch map data based on the
// light cycle in it.
patchManager.UpdatePatchBiome(GameWorld.Map.CurrentPatch);
GameWorld.UpdateGlobalLightLevels();
var currentPatch = GameWorld.Map.CurrentPatch;

patchManager.UpdatePatchBiome(currentPatch);
patchManager.UpdateAllPatchLightLevels(currentPatch);

HUD.UpdateEnvironmentalBars(GameWorld.Map.CurrentPatch.Biome);

// Updates the background lighting and does various post-effects
if (templateMaxLightLevel > 0.0f && maxLightLevel > 0.0f)
{
// This might need to be refactored for efficiency but, it works for now
// This might need to be refactored for efficiency, but it works for now
var lightLevel =
GameWorld.Map.CurrentPatch!.Biome.GetCompound(Compound.Sunlight, CompoundAmountType.Current).Ambient *
GameWorld.LightCycle.DayLightFraction;
currentPatch.Biome.GetCompound(Compound.Sunlight, CompoundAmountType.Current).Ambient;

// Normalise by maximum light level in the patch
Camera.LightLevel = lightLevel / maxLightLevel;
Expand Down
28 changes: 20 additions & 8 deletions src/microbe_stage/PatchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ public bool ApplyChangedPatchSettingsIfNeeded(Patch currentPatch, IMicrobeSpawnE
UpdateSpawners(currentPatch, spawnEnvironment);

// Change the lighting
UpdateLight(currentPatch.BiomeTemplate);
compoundCloudBrightness = currentPatch.BiomeTemplate.CompoundCloudBrightness;

UpdateAllPatchLightLevels();
UpdateAllPatchLightLevels(currentPatch);

return patchIsChanged;
}
Expand All @@ -135,7 +133,7 @@ public void UpdatePatchBiome(Patch currentPatch)
processSystem.SetBiome(currentPatch.Biome);
}

public void UpdateAllPatchLightLevels()
public void UpdateAllPatchLightLevels(Patch currentPatch)
{
if (CurrentGame == null)
throw new InvalidOperationException($"{nameof(PatchManager)} doesn't have {nameof(CurrentGame)} set");
Expand All @@ -145,9 +143,23 @@ public void UpdateAllPatchLightLevels()
if (!gameWorld.WorldSettings.DayNightCycleEnabled)
return;

var multiplier = gameWorld.LightCycle.DayLightFraction;
compoundCloudSystem.SetBrightnessModifier(multiplier * (compoundCloudBrightness - 1.0f) + 1.0f);
gameWorld.UpdateGlobalLightLevels();

var maxLightLevel = currentPatch.Biome.GetCompound(Compound.Sunlight, CompoundAmountType.Biome).Ambient;

float multiplier;

if (maxLightLevel > 0.0f)
{
multiplier = currentPatch.Biome.GetCompound(Compound.Sunlight, CompoundAmountType.Current).Ambient;
}
else
{
multiplier = 1.0f;
}

compoundCloudSystem.SetBrightnessModifier(multiplier * (compoundCloudBrightness - 1.0f) + 1.0f);
UpdateWorldLighting(currentPatch.BiomeTemplate, 0.2f + 0.8f * multiplier);
}

public void ApplySaveState(Patch? patch, float brightness)
Expand Down Expand Up @@ -305,15 +317,15 @@ private void HandleSpawnHelper(List<CreatedSpawner> existingSpawners, string ite
}
}

private void UpdateLight(Biome biome)
private void UpdateWorldLighting(Biome biome, float lightLevel)
{
worldLight.Position = new Vector3(0, 0, 0);
worldLight.LookAt(biome.Sunlight.Direction, new Vector3(0, 1, 0));

worldLight.ShadowEnabled = biome.Sunlight.Shadows;

worldLight.LightColor = biome.Sunlight.Colour;
worldLight.LightEnergy = biome.Sunlight.Energy;
worldLight.LightEnergy = biome.Sunlight.Energy * lightLevel;
worldLight.LightSpecular = biome.Sunlight.Specular;
}

Expand Down