Implementing Sky Color

This commit is contained in:
2024-12-18 18:24:26 -05:00
parent a656383741
commit 20e60ba613
6 changed files with 138 additions and 54 deletions

View File

@@ -64,6 +64,12 @@ namespace CaveGame::Client
/// Checks for missing, or out-of-date, cached sprites of chunks, and renders them.
void CheckCachedChunkSprites();
Color4 GetSkyColorInterpolatedForTimeOfDay(float time);
Color4 GetSkyColorBaseForTimeOfDay(float time);
void DrawSky();
bool AwaitingChunkAtCell(const Vector2 &cell);
};
}

View File

@@ -78,22 +78,93 @@ namespace CaveGame::Client {
}
}
Color4 LocalWorld::GetSkyColorInterpolatedForTimeOfDay(float time) {
float rounded_low = Math::Floor(time);
float rounded_high = Math::Ceil(time);
float delta = (time - rounded_low);
Color4 color_low = GetSkyColorBaseForTimeOfDay(rounded_low);
Color4 color_high = GetSkyColorBaseForTimeOfDay(rounded_high);
return Color4::Lerp(color_low, color_high, delta);
}
Color4 LocalWorld::GetSkyColorBaseForTimeOfDay(float time) {
if (time >= 23*60)
return Colors::Black;
else if (time >= 22*60)
return Colors::Black;
else if (time >= 21*60)
return Colors::White;
else if (time >= 20*60)
return Colors::White;
else if (time >= 19*60)
return Colors::White;
else if (time >= 18*60)
return Colors::White;
else if (time >= 17*60)
return Colors::White;
else if (time >= 16*60)
return Colors::White;
else if (time >= 15*60)
return Colors::White;
}
void LocalWorld::DrawSky() {
Vector2 viewport_topleft = camera.ScaledViewport().minPoint;
Vector2 viewport_bottomright = camera.ScaledViewport().maxPoint;
Color4 base_bg_color = Colors::Blues::CornflowerBlue;
float space_starts_at = -2000.f;
float depth = camera.Position().y;
// Draw space bg.
if (depth < -space_starts_at) {
float t = Math::Clamp( -((camera.Position().y+space_starts_at) / space_starts_at), 0.f, 1.f);
base_bg_color = base_bg_color.Lerp(Colors::Blues::MidnightBlue, t);
}
float deep_starts_at = 2000.f;
// Draw deep bg.
if (depth > deep_starts_at) {
float t = Math::Clamp((camera.Position().y-deep_starts_at) / deep_starts_at, 0.f, 1.f);
base_bg_color = base_bg_color.Lerp(Colors::Black, t);
}
glClearColor(base_bg_color.RN(), base_bg_color.GN(), base_bg_color.BN(),1.f);
if ()
}
void LocalWorld::Draw() {
CheckCachedChunkSprites();
JGL::J2D::Begin();
glClearColor(0.5f,0.5f,1.f,1.f);
//glClearColor(Colors::Blues::SkyBlue.RN(), Colors::Blues::SkyBlue.GN(), Colors::Blues::SkyBlue.BN(),1.f);
DrawSky();
// Shift the origin to the center of the screen.
glTranslatef(camera.HalfSizeOffset().x, camera.HalfSizeOffset().y, 0);
//DrawSky();
// Apply rotation, zoom, and translation.
glRotatef(camera.Rotation(), 0, 0, 1);
glScalef(camera.Zoom(), camera.Zoom(), 1);
glTranslatef(-camera.Position().x, -camera.Position().y, 0);
// Draw the cached RenderTargets for our chunks.
for (const auto&[chunk_pos, chunk] : loaded_chunks)
{

View File

@@ -23,12 +23,12 @@ namespace CaveGame::Core
static constexpr float TopSoilDepth = 345.f;
static constexpr float CaveInputScale = 800.f;
static constexpr float CaveOutputScale = 1.25f;
static constexpr float CaveErosionRange = 0.035f;
static constexpr float CaveOutputScale = 1.20f;
static constexpr float CaveErosionRange = 0.040f;
static constexpr float CaveAdditiveInputScale = 80.f;
static constexpr float CaveAdditiveOutputScale = 0.25f;
static constexpr float CaveAdditiveOutputScale = 0.45f;
static constexpr float SurfaceCaveShrinkDepth = 100;
static constexpr float SurfaceCaveShrinkFactor = 2.f;
static constexpr float SurfaceCaveShrinkFactor = 1.5f;
static constexpr float ClayVeinHiPassHScale = 200.f;
static constexpr float ClayVeinHiPassVScale = 205.f;
@@ -49,7 +49,10 @@ namespace CaveGame::Core
explicit Generator(int seed);
TileID HeightMap(float depth, int wx, int wy);
float ComputePass(int wx, int wy, float hScale, float vScale, float offset, float outputScale);
float Perlin(int wx, int wy, float hScale, float vScale, float offset, float outputScale);
float Octave(int wx, int wy, float hScale, float vScale, float offset, float outputScale, int octaves);
float ComputeHiLoPass(float hi, float lo, float offset);
TileID HiLoSelect(float pass, float upperBound, float lowerBound, TileID hiSelect, TileID loSelect, TileID fallback);
TileID ComputeTile(int wx, int wy);

View File

@@ -10,7 +10,6 @@ namespace CaveGame::Core
OctaveNoise(int seed) : perlin(seed)
{ }
double Noise(int octaves, double x, double y, double z)
{
double scale = 1.f;

View File

@@ -49,10 +49,28 @@ namespace CaveGame::Core
else { return TileID::STONE; }
}
float Generator::ComputePass(int wx, int wy, float hScale, float vScale, float offset, float outputScale) {
float Generator::Perlin(int wx, int wy, float hScale, float vScale, float offset, float outputScale) {
return perlin.Noise(wx / hScale, wy / vScale, offset) * outputScale;
}
float Generator::Octave(int wx, int wy, float hScale, float vScale, float offset, float outputScale, int octaves) {
float noise = 0;
float frequency = 1;
float factor = 1;
float roughness = 3.f;
float persistance = 0.4f;
for (int i = 0; i < octaves; i++) {
noise = noise + perlin.Noise(wx / hScale * (frequency + i) * 0.72354, wy / vScale * (frequency + i) * 0.72354, offset * (frequency + i) * 0.72354) * factor;
factor *= persistance;
frequency *= roughness;
}
return noise;
}
float Generator::ComputeHiLoPass(float hi, float lo, float offset) {
return hi + lo + ((hi * lo) / offset);
}
@@ -89,11 +107,16 @@ namespace CaveGame::Core
} else { return base; }*/
//float cave_erosion = perlin.Noise(wx / CaveInputScale, wy / CaveInputScale, 0.f) * CaveOutputScale;
float cave_erosion = ComputePass(wx, wy, CaveInputScale, CaveInputScale, 0.f, CaveOutputScale);
float cave_erosion = Perlin(wx, wy, CaveInputScale, CaveInputScale, 0.f, CaveOutputScale);
float rng = GetPrecomputedWhiteNoise2D(wx, wy)*0.002;
//float cave_addition = perlin.Noise(wx / CaveAdditiveInputScale, wy / CaveAdditiveInputScale, 0.f)*CaveAdditiveOutputScale;
float cave_addition = ComputePass(wx, wy, CaveAdditiveInputScale, CaveAdditiveInputScale, 0.f, CaveAdditiveOutputScale);
float cave_addition = Perlin(wx, wy, CaveAdditiveInputScale, CaveAdditiveInputScale, 0.25f, CaveAdditiveOutputScale);
float cave_addition_2 = Perlin(wx, wy, 25, 25, 0.625f, 0.125f);
cave_erosion += cave_addition;
cave_erosion += cave_addition_2;
cave_erosion -= rng;
// Make cave openings more narrow
if (depth < SurfaceCaveShrinkDepth) {
@@ -109,79 +132,57 @@ namespace CaveGame::Core
// Ore computation
//float clay_pass_1 = perlin.Noise(wx / ClayVeinHiPassHScale, wy / ClayVeinHiPassVScale, ClayVeinHiPassOffset) * ClayVeinHiPassOutputScale;
//float clay_pass_1 = ComputePass(wx, wy, ClayVeinHiPassHScale, ClayVeinHiPassVScale, ClayVeinHiPassOffset, ClayVeinHiPassOutputScale);
float clay_pass_1 = Perlin(wx, wy, ClayVeinHiPassHScale, ClayVeinHiPassVScale, ClayVeinHiPassOffset, ClayVeinHiPassOutputScale);
//float clay_pass_2 = perlin.Noise(wx / ClayVeinLoPassHScale, wy / ClayVeinLoPassVScale, ClayVeinLoPassOffset) * ClayVeinLoPassOutputScale;
//float clay_pass_2 = ComputePass(wx, wy, ClayVeinLoPassHScale, ClayVeinLoPassVScale, ClayVeinLoPassOffset, ClayVeinLoPassOutputScale);
float clay_pass_2 = Perlin(wx, wy, ClayVeinLoPassHScale, ClayVeinLoPassVScale, ClayVeinLoPassOffset, ClayVeinLoPassOutputScale);
float rng = GetPrecomputedWhiteNoise2D(wx, wy)*0.175;
//float clay_pass = clay_pass_1 + clay_pass_2 + ((clay_pass_1 * clay_pass_2) / 2.f) - rng;
//float clay_pass = ComputeHiLoPass(clay_pass_1, clay_pass_2, 2.f) - rng;
//std::function<float(float, float, float)>
//auto computestage = [&] (float hScale, float vScale, float offset, float outputScale){ return ComputePass(wx, wy, hScale, vScale, offset, outputScale) ;};
/*
if (clay_pass > 0.85f) {
return TileID::CLAY;
}
if (clay_pass < -0.75f) {
return TileID::DIRT;
}
*/
float clay_pass = ComputeHiLoPass(clay_pass_1, clay_pass_2, 2.f) - rng;
if (clay_pass > 0.85f) { return TileID::CLAY; }
if (clay_pass < -0.75f) { return TileID::DIRT; }
/*
float silt_pass_1 = perlin.Noise(wx / 220.f, wy / 205.f, 5.f) * 2.2f;
float silt_pass_2 = perlin.Noise(wx / 27.f, wy / 25.f, 422.f) * 1.25f;
//float rng = GetPrecomputedWhiteNoise2D(wx, wy)*0.175;
float silt_pass = silt_pass_1 + silt_pass_2 + ((silt_pass_1 * silt_pass_2) / 2.f) - rng;
*/
/*
if (silt_pass > 0.85f) {
return TileID::SILT;
}
if (silt_pass > 0.85f) { return TileID::SILT; }
if (silt_pass < -0.75f) { return TileID::DIRT; }
if (silt_pass < -0.75f) {
return TileID::DIRT;
}
*/
/*
float stone_pass_1 = perlin.Noise(wx / 30.f, wy / 30.f, 666.f) * 2.4f;
float stone_pass_2 = perlin.Noise(wx / 220.f, wy / 220.f, 0.5f) * 1.75f;
float stone_pass = stone_pass_1 + stone_pass_2 + (stone_pass_1 * stone_pass_2 / 1.75f) + (rng*1.5f);
*/
// Chunks of stone
/*
if (stone_pass > 0.7f) {
return TileID::STONE;
}
if (stone_pass < -0.7f) {
return TileID::STONE;
}
*/
//float dirt_pass_1 = perlin.Noise(wx / 30.f, wy / 30.f, 12.f) * 1.35f;
//float dirt_pass_2 = perlin.Noise(wx / 90.f, wy / 90.f, 0.5f) * 0.75f;
if (stone_pass > 0.7f) { return TileID::STONE; }
if (stone_pass < -0.7f) { return TileID::STONE; }
//float dirt_pass = dirt_pass_1 + dirt_pass_2;
float dirt_pass_1 = perlin.Noise(wx / 30.f, wy / 30.f, 12.f) * 1.35f;
float dirt_pass_2 = perlin.Noise(wx / 90.f, wy / 90.f, 0.5f) * 0.75f;
float dirt_pass = dirt_pass_1 + dirt_pass_2;
// Chunks of dirt
//if (-0.65 > dirt_pass && dirt_pass < 0.65f) {
//return TileID::DIRT;
//}
if (-0.65 > dirt_pass && dirt_pass < 0.65f) {
return TileID::DIRT;
}
// Chunks of clay
@@ -190,7 +191,7 @@ namespace CaveGame::Core
// The compiler must be doing some optimization for all the function calls
// It feels faster, but maybe I'm mistaken
// I have also discovered that vines are major source of slowness possibly
return HiLoSelect(
/*return HiLoSelect(
ComputeHiLoPass(
ComputePass(wx, wy, ClayVeinHiPassHScale, ClayVeinHiPassVScale, ClayVeinHiPassOffset, ClayVeinHiPassOutputScale),
ComputePass(wx, wy, ClayVeinLoPassHScale, ClayVeinLoPassVScale, ClayVeinLoPassOffset, ClayVeinLoPassOutputScale),
@@ -205,7 +206,7 @@ namespace CaveGame::Core
ComputePass(wx, wy, 30.f, 30.f, 666.f, 2.4f),
ComputePass(wx, wy, 220.f, 220.f, 0.5f, 1.75f),
1.75) + (rng*1.5f) , 0.7f, -0.7f, TileID::STONE, TileID::STONE, base
)));
)));*/
}
return base;

View File

@@ -12,6 +12,10 @@ namespace CaveGame::Core
{
void CaveGame::Core::World::Update(float elapsed) {
time_of_day += elapsed;
time_of_day = std::fmod(time_of_day, 24*60);
autosave_timer += elapsed;
if (autosave_timer >= PeriodicAutosaveIntervalSeconds)