Skip to content

Commit

Permalink
Make compound clouds and 3D objects' lighting depend on the light lev…
Browse files Browse the repository at this point in the history
…el (#5771)

* Make compound clouds and 3D objects' lighting depend on the light level
I.e. the current light level set by the day-night cycle

* Fix day-night cycle existing in lightless patches

* Fix a wrong comma in a comment

* Remove an obsolete TODO

* Fix a formula
The patch's ambient light amount already updates with the day-night cycle and doesn't need to multiply by DayLightFraction as such

* Improve light level handling

* Complete an accidentally uploaded unfinished commit

* Fix `UpdateAllPatchLightLevels` not to multiply by day light fraction

* Add a minimal patch brightness (20%)

* Update a function's name because it wasn't clear
  • Loading branch information
dligr authored Jan 13, 2025
1 parent 7e8dff5 commit 010763e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
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

0 comments on commit 010763e

Please sign in to comment.