Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions shaders/CompoundCloudPlane.gdshader
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const float CLOUD_MAX_INTENSITY_SHOWN = 1000.f;

float getIntensity(float value)
{
return min(DENSITY_MULTIPLIER * atan(0.006f * CLOUD_MAX_INTENSITY_SHOWN * value), 1.0f) * BrightnessMultiplier;
float baseIntensity = DENSITY_MULTIPLIER * atan(0.006f * CLOUD_MAX_INTENSITY_SHOWN * value);
return min(baseIntensity * BrightnessMultiplier, 1.0f);
}

void fragment() {
Expand Down Expand Up @@ -60,7 +61,7 @@ void fragment() {
colour4 * cloud4;

float adjustedAlpha = min(cloud1 + cloud2 + cloud3 + cloud4, 1.0) *
clamp(min(cloud1 + cloud2 + cloud3 + cloud4, 1.0) * 0.5, 0.5, 0.9);
clamp(min(cloud1 + cloud2 + cloud3 + cloud4, 1.0) * 0.5, 0.5, 0.9);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I counted right this indent is now 3 spaces which is incorrect?


ALPHA = adjustedAlpha;
ALBEDO = colour.rgb;
Expand Down
25 changes: 3 additions & 22 deletions shaders/MicrobeBackground.gdshader
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ shader_type spatial;
render_mode unshaded;

// this is 1 by default to keep the dark BGs looking nice
uniform float lightLevel = 1.0f;
uniform vec3 lightColor = vec3(1.0f, 1.0f, 1.0f);

uniform vec2 repeats = vec2(1.0f, 1.0f);
uniform sampler2D layer0 : source_color;
Expand Down Expand Up @@ -32,25 +32,6 @@ const vec2 speed3 = vec2(1050.0f);
varying vec2 UV3;
varying vec2 UV4;

vec3 LightInfluence(float amount)
{
if (amount > 0.5f)
{
// Day
return mix(vec3(0.75f, 0.5f, 0.5f), vec3(1.0f, 1.0f, 1.0f), 2.0f * amount - 1.0f);
}
else if (amount > 0.25f)
{
// Dawn and Dusk
return mix(vec3(0.25f, 0.25f, 0.25f), vec3(0.75f, 0.5f, 0.5f), 4.0f * amount - 1.0f);
}
else
{
// Night
return mix(vec3(0.052f, 0.05f, 0.17f), vec3(0.25f, 0.25f, 0.25f), 4.0f * amount);
}
}

void vertex(){
vec2 offset = (repeats - 1.0f) / 2.0f;

Expand Down Expand Up @@ -95,8 +76,8 @@ void fragment(){
vec3 mixture1 = mix(colour2.rgb, colour3.rgb * colour3.a, 1.0f);
vec3 mixture2 = mix(mixture0.rgb, mixture1.rgb, 0.5f);
vec3 composition = mix(colour0.rgb, mixture2.rgb, 0.5f);

ALBEDO.rgb = composition.rgb * LightInfluence(lightLevel);
ALBEDO.rgb = composition.rgb * lightColor;

ALPHA = 1.0f;
}
17 changes: 13 additions & 4 deletions src/microbe_stage/BackgroundPlane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public partial class BackgroundPlane : Node3D
private readonly StringName blurAmountParameter = new("blurAmount");
private readonly StringName textureAlbedoParameter = new("textureAlbedo");
private readonly StringName worldPositionParameter = new("worldPos");
private readonly StringName lightLevelParameter = new("lightLevel");
private readonly StringName distortionStrengthParameter = new("distortionFactor");
private readonly StringName lightColorParameter = new("lightColor");

#pragma warning disable CA2213

Expand Down Expand Up @@ -47,6 +47,8 @@ public partial class BackgroundPlane : Node3D

private Vector2 previousWindowSize = new(1280, 720);

private Color previousColor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be initialized to the same value as in the shader (1, 1, 1, 1)?


public float PlaneOffset
{
get => backgroundPlane.Position.Z;
Expand Down Expand Up @@ -163,9 +165,16 @@ public void SetVisibility(bool visible)
OnDisplayBackgroundParticlesChanged(Settings.Instance.DisplayBackgroundParticles);
}

public void UpdateLightLevel(float lightLevel)
public void SetCompoundColoring(Color color)
{
currentBackgroundMaterial.SetShaderParameter(lightLevelParameter, lightLevel);
// Don't do the calculation and sending to gpu again if not needed.
if (previousColor == color)
return;

previousColor = color;

currentBackgroundMaterial.SetShaderParameter(lightColorParameter,
new Vector3(color.R, color.G, color.B));
}

protected override void Dispose(bool disposing)
Expand All @@ -174,9 +183,9 @@ protected override void Dispose(bool disposing)
{
textureAlbedoParameter.Dispose();
blurAmountParameter.Dispose();
lightLevelParameter.Dispose();
distortionStrengthParameter.Dispose();
worldPositionParameter.Dispose();
lightColorParameter.Dispose();
}

base.Dispose(disposing);
Expand Down
6 changes: 3 additions & 3 deletions src/microbe_stage/BackgroundPlane.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
frequency = 0.0035

[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_f6pop"]
seamless = true
noise = SubResource("FastNoiseLite_dor76")
seamless = true

[sub_resource type="FastNoiseLite" id="FastNoiseLite_ve355"]
seed = 2
frequency = 0.0017

[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_tsmw6"]
seamless = true
noise = SubResource("FastNoiseLite_ve355")
seamless = true

[sub_resource type="ShaderMaterial" id="2"]
resource_local_to_scene = true
render_priority = -100
shader = ExtResource("4_5y6kw")
shader_parameter/lightLevel = 1.0
shader_parameter/lightColor = Vector3(1, 1, 1)
shader_parameter/repeats = Vector2(2, 1)
shader_parameter/layer0 = ExtResource("5_waqnb")
shader_parameter/layer1 = ExtResource("6_qx2cp")
Expand Down
41 changes: 5 additions & 36 deletions src/microbe_stage/MicrobeCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ public partial class MicrobeCamera : Camera3D, ISaveLoadedTracked, IGameCamera,
private BackgroundPlane backgroundPlane = null!;
#pragma warning restore CA2213

/// <summary>
/// Used to manually tween the light level to the target value
/// </summary>
private float lastSetLightLevel = 1;

private Vector3 cursorWorldPos = new(0, 0, 0);
private bool cursorDirty = true;

Expand Down Expand Up @@ -179,13 +174,6 @@ public override void _Process(double delta)
{
base._Process(delta);

// Once target is reached the value is set exactly the same
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (lastSetLightLevel != lightLevel)
{
UpdateLightLevel((float)delta);
}

if (AutoProcessWhilePaused && PauseManager.Instance.Paused)
{
UpdateCameraPosition(delta, null);
Expand Down Expand Up @@ -299,6 +287,11 @@ public void SetBackground(Background background)
backgroundPlane.SetBackground(background);
}

public void SetWaterColorFromCompounds(Color color)
{
backgroundPlane.SetCompoundColoring(color);
}

public void WritePropertiesToArchive(ISArchiveWriter writer)
{
writer.Write(ZoomSpeed);
Expand Down Expand Up @@ -405,28 +398,4 @@ private void UpdateBackgroundVisibility()
{
backgroundPlane.SetVisibility(Current);
}

private void UpdateLightLevel(float delta)
{
if (lastSetLightLevel < lightLevel)
{
lastSetLightLevel += LightLevelInterpolateSpeed * delta;

if (lastSetLightLevel > lightLevel)
lastSetLightLevel = lightLevel;
}
else if (lastSetLightLevel > lightLevel)
{
lastSetLightLevel -= LightLevelInterpolateSpeed * delta;

if (lastSetLightLevel < lightLevel)
lastSetLightLevel = lightLevel;
}
else
{
lastSetLightLevel = lightLevel;
}

backgroundPlane.UpdateLightLevel(lastSetLightLevel);
}
}
70 changes: 66 additions & 4 deletions src/microbe_stage/MicrobeStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public sealed partial class MicrobeStage : CreatureStageBase<Entity, MicrobeWorl

private float templateMaxLightLevel;

private Color environmentColor;

private Color compoundColor;

private float previousLightLevel;

private bool appliedPlayerGodMode;

private bool appliedUnlimitGrowthSpeed;
Expand Down Expand Up @@ -1460,6 +1466,8 @@ protected override void UpdatePatchSettings(bool promptPatchNameChange = true)

HUD.UpdateEnvironmentalBars(currentPatch.Biome);

CalculateEnvironmentalLight();

UpdateBackground();

UpdatePatchLightLevelSettings();
Expand Down Expand Up @@ -1500,20 +1508,29 @@ protected override void OnLightLevelUpdate()
var lightLevel =
currentPatch.Biome.GetCompound(Compound.Sunlight, CompoundAmountType.Current).Ambient;

// Don't update the entire lighting pipeline if no need
if (lightLevel == previousLightLevel)
return;

previousLightLevel = lightLevel;

float lightModifier = lightLevel / maxLightLevel;

// Normalise by maximum light level in the patch
Camera.LightLevel = lightModifier;

microbeWorldEnvironment.UpdateAmbientReflection(
currentPatch.BiomeTemplate.EnvironmentColour * lightModifier);
var environmentLightColor = environmentColor * lightModifier;
environmentLightColor = environmentLightColor.Lerp(new Color(0.65f, 0.65f, 0.65f), 0.3f);

microbeWorldEnvironment.UpdateAmbientReflection(environmentLightColor);
Camera.SetWaterColorFromCompounds(compoundColor);
}
else
{
// Don't change lighting for patches without day/night effects
Camera.LightLevel = 1.0f;

microbeWorldEnvironment.UpdateAmbientReflection(currentPatch.BiomeTemplate.EnvironmentColour);
microbeWorldEnvironment.UpdateAmbientReflection(environmentColor);
Camera.SetWaterColorFromCompounds(compoundColor);
}
}

Expand Down Expand Up @@ -1572,6 +1589,51 @@ private void UpdatePatchLightLevelSettings()
.Ambient;
templateMaxLightLevel = GameWorld.Map.CurrentPatch.BiomeTemplate.Conditions
.GetCompound(Compound.Sunlight, CompoundAmountType.Biome).Ambient;

Camera.SetWaterColorFromCompounds(compoundColor);
}

private Color UpdateEnvironmentalCompoundAwareness()
{
GameWorld.Map.CurrentPatch!.Biome.TryGetCompound(Compound.Oxygen, CompoundAmountType.Current, out var oxygen);
var ironAmount = GameWorld.Map.CurrentPatch.GetCompoundAmountInSnapshotForDisplay(
GameWorld.Map.CurrentPatch.CurrentSnapshot, Compound.Iron);

// A lot of magic numbers here, just parameters to tune this.
// When settled on good values I'll formalise it.
var ironNormalized = ironAmount / 4.0f;
var oxygenPercent = oxygen.Ambient;
var oxygenNormalize = MathF.Min(oxygenPercent / 0.4f, 1.0f);
float greenIron = ironNormalized * (1.0f - 0.8f * oxygenNormalize);
float redRust = 0.9f * ironNormalized * oxygenNormalize;

var baseGreen = 0.15f;
var baseRed = 0.1f;
var baseBlue = 0.2f;

var g = Math.Clamp(baseGreen + 0.6f * greenIron, 0.0f, 1.0f);
var r = Math.Clamp(baseRed + 0.8f * redRust, 0.0f, 1.0f);
var b = Math.Clamp(baseBlue - 0.25f * ironNormalized, 0.0f, 1.0f);

return new Color(r, g, b);
}

private void CalculateEnvironmentalLight()
{
compoundColor = UpdateEnvironmentalCompoundAwareness();
var envColor = GameWorld.Map.CurrentPatch!.BiomeTemplate.EnvironmentColour;

var tintStrength = 0.35f;
var envColorTinted = envColor * (1.0f - tintStrength) + compoundColor * tintStrength;
environmentColor = DesaturateColor(envColorTinted, 0.15f);
}

private Color DesaturateColor(Color c, float amount)
{
float grayValue = 0.2126f * c.R + 0.7152f * c.G + 0.0722f * c.B;
var gray = new Color(grayValue, grayValue, grayValue);

return c.Lerp(gray, amount);
}

private void OnFinishLoading()
Expand Down