Files
src
build.gradle
settings.gradle
Code
7 229
src/client/java/com/kodari/starlight/StarlightClient.java
package com.kodari.starlight;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.world.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.world.WorldRenderEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.client.render.RenderLayers;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.Identifier;
public final class StarlightClient implements ClientModInitializer {
private static final Identifier GALAXY_TEXTURE = Identifier.of("starlight", "environment/galaxy");
private static final Identifier MOON_TEXTURE = Identifier.of("starlight", "environment/moon");
private static final Identifier STAR_TEXTURE = Identifier.of("starlight", "environment/star");
private static final List<Star> STARS = createStars();
@Override
public void onInitializeClient() {
WorldRenderEvents.END_MAIN.register(StarlightClient::renderNightSky);
}
private static void renderNightSky(WorldRenderContext context) {
ClientWorld world = MinecraftClient.getInstance().world;
if (world == null || !world.getDimension().hasSkyLight()) {
return;
}
long timeOfDay = Math.floorMod(world.getTimeOfDay(), 24000L);
if (timeOfDay < 13000L || timeOfDay >= 23000L) {
return;
}
Camera camera = context.gameRenderer().getCamera();
double yaw = Math.toRadians(camera.getYaw());
double pitch = Math.toRadians(camera.getPitch());
double sinYaw = Math.sin(yaw);
double cosYaw = Math.cos(yaw);
double sinPitch = Math.sin(pitch);
double cosPitch = Math.cos(pitch);
double rightX = cosYaw;
double rightY = 0.0;
double rightZ = sinYaw;
double upX = -sinYaw * sinPitch;
double upY = cosPitch;
double upZ = -cosYaw * sinPitch;
VertexConsumer galaxy = context.consumers().getBuffer(RenderLayers.entityTranslucent(GALAXY_TEXTURE));
drawBillboard(galaxy, rightX, rightY, rightZ, upX, upY, upZ,
0.0, 0.78, 0.625, 155.0, 124.0, 34.0, -0.18,
255, 255, 255, 232);
double skyRotation = timeOfDay * Math.PI * 2.0 / 24000.0;
double sinRotation = Math.sin(skyRotation);
double cosRotation = Math.cos(skyRotation);
VertexConsumer stars = context.consumers().getBuffer(RenderLayers.entityTranslucent(STAR_TEXTURE));
for (Star star : STARS) {
double y = star.y() * cosRotation - star.z() * sinRotation;
if (y < 0.025) {
continue;
}
double z = star.y() * sinRotation + star.z() * cosRotation;
drawBillboard(stars, rightX, rightY, rightZ, upX, upY, upZ,
star.x(), y, z, 150.0, star.size(), star.size(), 0.0,
star.red(), star.green(), star.blue(), star.alpha());
}
double progress = (timeOfDay - 13000.0) / 10000.0;
double moonY = Math.sin(progress * Math.PI);
double moonZ = Math.cos(progress * Math.PI);
VertexConsumer moon = context.consumers().getBuffer(RenderLayers.entityTranslucent(MOON_TEXTURE));
drawBillboard(moon, rightX, rightY, rightZ, upX, upY, upZ,
0.0, moonY, moonZ, 145.0, 22.0, 22.0, 0.0,
255, 255, 255, 255);
}
private static void drawBillboard(VertexConsumer consumer,
double rightX, double rightY, double rightZ,
double upX, double upY, double upZ,
double directionX, double directionY, double directionZ,
double distance, double width, double height, double roll,
int red, int green, int blue, int alpha) {
double cosRoll = Math.cos(roll);
double sinRoll = Math.sin(roll);
double axisRightX = rightX * cosRoll + upX * sinRoll;
double axisRightY = rightY * cosRoll + upY * sinRoll;
double axisRightZ = rightZ * cosRoll + upZ * sinRoll;
double axisUpX = upX * cosRoll - rightX * sinRoll;
double axisUpY = upY * cosRoll - rightY * sinRoll;
double axisUpZ = upZ * cosRoll - rightZ * sinRoll;
double centerX = directionX * distance;
double centerY = directionY * distance;
double centerZ = directionZ * distance;
double halfWidth = width * 0.5;
double halfHeight = height * 0.5;
float normalX = (float) -directionX;
float normalY = (float) -directionY;
float normalZ = (float) -directionZ;
vertex(consumer, centerX - axisRightX * halfWidth - axisUpX * halfHeight,
centerY - axisRightY * halfWidth - axisUpY * halfHeight,
centerZ - axisRightZ * halfWidth - axisUpZ * halfHeight,
red, green, blue, alpha, 0.0f, 1.0f, normalX, normalY, normalZ);
vertex(consumer, centerX - axisRightX * halfWidth + axisUpX * halfHeight,
centerY - axisRightY * halfWidth + axisUpY * halfHeight,
centerZ - axisRightZ * halfWidth + axisUpZ * halfHeight,
red, green, blue, alpha, 0.0f, 0.0f, normalX, normalY, normalZ);
vertex(consumer, centerX + axisRightX * halfWidth + axisUpX * halfHeight,
centerY + axisRightY * halfWidth + axisUpY * halfHeight,
centerZ + axisRightZ * halfWidth + axisUpZ * halfHeight,
red, green, blue, alpha, 1.0f, 0.0f, normalX, normalY, normalZ);
vertex(consumer, centerX + axisRightX * halfWidth - axisUpX * halfHeight,
centerY + axisRightY * halfWidth - axisUpY * halfHeight,
centerZ + axisRightZ * halfWidth - axisUpZ * halfHeight,
red, green, blue, alpha, 1.0f, 1.0f, normalX, normalY, normalZ);
}
private static void vertex(VertexConsumer consumer, double x, double y, double z,
int red, int green, int blue, int alpha,
float u, float v, float normalX, float normalY, float normalZ) {
consumer.vertex((float) x, (float) y, (float) z)
.color(red, green, blue, alpha)
.texture(u, v)
.overlay(0, 0)
.light(LightmapTextureManager.MAX_LIGHT_COORDINATE)
.normal(normalX, normalY, normalZ);
}
private static List<Star> createStars() {
Random random = new Random(0x51A7L);
List<Star> stars = new ArrayList<>(260);
for (int i = 0; i < 260; i++) {
double y = random.nextDouble();
double angle = random.nextDouble() * Math.PI * 2.0;
double horizontal = Math.sqrt(1.0 - y * y);
int tint = random.nextInt(3);
int red = tint == 1 ? 190 : 255;
int green = tint == 2 ? 210 : 245;
int blue = tint == 0 ? 255 : 230;
stars.add(new Star(horizontal * Math.cos(angle), y, horizontal * Math.sin(angle),
1.0f + random.nextFloat() * 2.2f, red, green, blue,
165 + random.nextInt(91)));
}
return List.copyOf(stars);
}
private record Star(double x, double y, double z, float size,
int red, int green, int blue, int alpha) {
}
}