Skip to content

Commit

Permalink
Day/night cycle!
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSunCat committed Jun 21, 2020
1 parent 2c7a6b8 commit d0d3404
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/minecraft4k/Minecraft4k.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ public class Minecraft4k
final static int CROSS_SIZE = 32;

final static Vec3 FOG_COLOR = new Vec3(1);
final static Vec3 SUN_LIGHT_COLOR = new Vec3(1, 0.8f, 0.5f);
final static Vec3 AMBIENT_LIGHT_COLOR = new Vec3(0, 0, 0.5f);

final static Vec3 SC_DAY = new Vec3(1);//1, 0.8f, 0.5f);
final static Vec3 AC_DAY = new Vec3(0.5f, 0.5f, 0.5f);

final static Vec3 SC_TWILIGHT = new Vec3(1, 0.5f, 0.01f);
final static Vec3 AC_TWILIGHT = new Vec3(0.6f, 0.5f, 0.5f);

final static Vec3 SC_NIGHT = new Vec3(0.3f, 0.3f, 0.5f);
final static Vec3 AC_NIGHT = new Vec3(0.3f, 0.3f, 0.5f);

static long deltaTime = 0;
static Font font = Font.getFont("Arial");
Expand Down Expand Up @@ -130,7 +137,7 @@ public static void main(String[] args)
int PERLIN_YWRAP = 1 << PERLIN_YWRAPB;
int PERLIN_ZWRAPB = 8;
int PERLIN_ZWRAP = 1 << PERLIN_ZWRAPB;

static float[] perlin = null;

float scaled_cosine(float i) {
return (float) (0.5f * (1.0f - Math.cos(i * Math.PI)));
Expand Down Expand Up @@ -241,7 +248,8 @@ float noise(float x, float y) { // stolen from Processing
static float sinYaw, sinPitch;
static float cosYaw, cosPitch;

static float[] perlin = null;
static Vec3 sunColor = new Vec3(SC_DAY);
static Vec3 ambColor = new Vec3(AC_DAY);

static int[] screenBuffer = ((DataBufferInt) SCREEN.getRaster().getDataBuffer()).getData();
static byte[][][] world = new byte[WORLD_SIZE][WORLD_HEIGHT][WORLD_SIZE];
Expand Down Expand Up @@ -584,8 +592,18 @@ else if (d < 4)
cosPitch = (float)Math.cos(cameraPitch);

lightDirection.x = 0;
lightDirection.y = (float) Math.sin(time / 10000.0d);
lightDirection.z = (float) Math.cos(time / 10000.0d);
lightDirection.y = (float) Math.sin(time / 1000.0d);
lightDirection.z = (float) Math.cos(time / 1000.0d);


if(lightDirection.y < 0f)
{
Vec3.lerp(SC_TWILIGHT, SC_DAY, -lightDirection.y, sunColor);
Vec3.lerp(AC_TWILIGHT, AC_DAY, -lightDirection.y, ambColor);
} else {
Vec3.lerp(SC_TWILIGHT, SC_NIGHT, lightDirection.y, sunColor);
Vec3.lerp(AC_TWILIGHT, AC_NIGHT, lightDirection.y, ambColor);
}

while (System.currentTimeMillis() - startTime > 10L) {
// adjust camera
Expand Down Expand Up @@ -1256,7 +1274,7 @@ else if(!classic) { // shadows
Vec3.lerp(pixelColor, FOG_COLOR, fogIntensity, pixelColor);

Vec3 lightColor = new Vec3();
Vec3.lerp(AMBIENT_LIGHT_COLOR, SUN_LIGHT_COLOR, lightIntensity, lightColor);
Vec3.lerp(ambColor, sunColor, lightIntensity, lightColor);

Vec3.mult(pixelColor, lightColor, pixelColor);
}
Expand Down Expand Up @@ -1301,6 +1319,13 @@ class Vec3
z = val;
}

Vec3(Vec3 copy)
{
x = copy.x;
y = copy.y;
z = copy.z;
}

Vec3()
{
this(0.0f, 0.0f, 0.0f);
Expand Down

0 comments on commit d0d3404

Please sign in to comment.