Skip to content

Commit

Permalink
Split decoration format into its own skybox type
Browse files Browse the repository at this point in the history
  • Loading branch information
AMereBagatelle committed Jun 6, 2024
1 parent a2b805c commit be38af9
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 192 deletions.
6 changes: 1 addition & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,4 @@ Please keep pull requests to one topic. If you notice another issue to fix or fe
create a separate request for it. This helps the maintainers to review your code.

We value your contributions and appreciate your efforts in helping us maintain a high-quality codebase. Thank you for
contributing to our project!

## Testing

See [testing.md](docs/testing.md).
contributing to our project!
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.amerebagatelle.mods.nuit.api.skyboxes;

import io.github.amerebagatelle.mods.nuit.components.Conditions;
import io.github.amerebagatelle.mods.nuit.components.Decorations;
import io.github.amerebagatelle.mods.nuit.components.Properties;

public interface NuitSkybox extends Skybox {
Expand All @@ -12,7 +11,5 @@ public interface NuitSkybox extends Skybox {
Properties getProperties();

Conditions getConditions();

Decorations getDecorations();
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,20 @@ public class Decorations {
ResourceLocation.CODEC.optionalFieldOf("moon", LevelRendererAccessor.getMoonPhases()).forGetter(Decorations::getMoonTexture),
Codec.BOOL.optionalFieldOf("showSun", false).forGetter(Decorations::isSunEnabled),
Codec.BOOL.optionalFieldOf("showMoon", false).forGetter(Decorations::isMoonEnabled),
Codec.BOOL.optionalFieldOf("showStars", false).forGetter(Decorations::isStarsEnabled),
Rotation.CODEC.optionalFieldOf("rotation", Rotation.decorations()).forGetter(Decorations::getRotation),
Blend.CODEC.optionalFieldOf("blend", Blend.decorations()).forGetter(Decorations::getBlend)
Codec.BOOL.optionalFieldOf("showStars", false).forGetter(Decorations::isStarsEnabled)
).apply(instance, Decorations::new));
private final ResourceLocation sunTexture;
private final ResourceLocation moonTexture;
private final boolean sunEnabled;
private final boolean moonEnabled;
private final boolean starsEnabled;
private final Rotation rotation;
private final Blend blend;

public Decorations(ResourceLocation sun, ResourceLocation moon, boolean sunEnabled, boolean moonEnabled, boolean starsEnabled, Rotation rotation, Blend blend) {
public Decorations(ResourceLocation sun, ResourceLocation moon, boolean sunEnabled, boolean moonEnabled, boolean starsEnabled) {
this.sunTexture = sun;
this.moonTexture = moon;
this.sunEnabled = sunEnabled;
this.moonEnabled = moonEnabled;
this.starsEnabled = starsEnabled;
this.rotation = rotation;
this.blend = blend;
}

public ResourceLocation getSunTexture() {
Expand All @@ -58,16 +52,8 @@ public boolean isStarsEnabled() {
return this.starsEnabled;
}

public Rotation getRotation() {
return rotation;
}

public Blend getBlend() {
return blend;
}

public static Decorations of() {
return new Decorations(LevelRendererAccessor.getSun(), LevelRendererAccessor.getMoonPhases(), false, false, false, Rotation.decorations(), Blend.decorations());
return new Decorations(LevelRendererAccessor.getSun(), LevelRendererAccessor.getMoonPhases(), false, false, false);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public Rotation(boolean skyboxRotation, Map<Long, Quaternionf> mapping, Map<Long
this.speed = speed;
}

public void rotateStack(PoseStack matrixStack, long currentTime, ClientLevel world) {
public void rotateStack(PoseStack matrixStack, ClientLevel world) {
long currentTime = world.getDayTime() % duration;
// static
var possibleMappingKeyframes = Utils.findClosestKeyframes(mapping, currentTime);
Quaternionf mappingRot = new Quaternionf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.github.amerebagatelle.mods.nuit.api.skyboxes.NuitSkybox;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.Skybox;
import io.github.amerebagatelle.mods.nuit.components.RGB;
import io.github.amerebagatelle.mods.nuit.skybox.AbstractSkybox;
import io.github.amerebagatelle.mods.nuit.skybox.decorations.DecorationBox;
import io.github.amerebagatelle.mods.nuit.util.Utils;
import net.minecraft.client.Camera;
import net.minecraft.client.multiplayer.ClientLevel;
Expand Down Expand Up @@ -55,15 +55,15 @@ private static void redirectSetShaderFogColor(float red, float green, float blue

@Redirect(method = "setupColor", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/ClientLevel;getTimeOfDay(F)F"))
private static float nuit$redirectSkyAngle(ClientLevel instance, float v) {
if (SkyboxManager.getInstance().isEnabled() && SkyboxManager.getInstance().getActiveSkyboxes().stream().anyMatch(skybox -> skybox instanceof AbstractSkybox abstractSkybox && abstractSkybox.getDecorations().getRotation().getSkyboxRotation())) {
if (SkyboxManager.getInstance().isEnabled() && SkyboxManager.getInstance().getActiveSkyboxes().stream().anyMatch(skybox -> skybox instanceof DecorationBox decorBox && decorBox.getProperties().getRotation().getSkyboxRotation())) {
return Mth.positiveModulo(instance.getDayTime() / 24000F + 0.75F, 1);
}
return instance.getTimeOfDay(v);
}

@Redirect(method = "setupColor", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/ClientLevel;getSunAngle(F)F"))
private static float nuit$redirectSkyAngleRadian(ClientLevel instance, float v) {
if (SkyboxManager.getInstance().isEnabled() && SkyboxManager.getInstance().getActiveSkyboxes().stream().anyMatch(skybox -> skybox instanceof AbstractSkybox abstractSkybox && abstractSkybox.getDecorations().getRotation().getSkyboxRotation())) {
if (SkyboxManager.getInstance().isEnabled() && SkyboxManager.getInstance().getActiveSkyboxes().stream().anyMatch(skybox -> skybox instanceof DecorationBox decorBox && decorBox.getProperties().getRotation().getSkyboxRotation())) {
float skyAngle = Mth.positiveModulo(instance.getDayTime() / 24000F + 0.75F, 1);
return skyAngle * (float) (Math.PI * 2);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package io.github.amerebagatelle.mods.nuit.skybox;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import io.github.amerebagatelle.mods.nuit.NuitClient;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.NuitSkybox;
import io.github.amerebagatelle.mods.nuit.components.Conditions;
import io.github.amerebagatelle.mods.nuit.components.Decorations;
import io.github.amerebagatelle.mods.nuit.components.Properties;
import io.github.amerebagatelle.mods.nuit.components.Weather;
import io.github.amerebagatelle.mods.nuit.mixin.LevelRendererAccessor;
import io.github.amerebagatelle.mods.nuit.util.Utils;
import it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.FogRenderer;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.core.registries.Registries;
import net.minecraft.util.Mth;
import net.minecraft.util.Tuple;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.material.FogType;
import org.joml.Matrix4f;

import java.util.Map;
import java.util.Objects;
Expand All @@ -43,7 +36,6 @@ public abstract class AbstractSkybox implements NuitSkybox {
public transient float alpha;
protected Properties properties = Properties.of();
protected Conditions conditions = Conditions.of();
protected Decorations decorations = Decorations.of();

protected boolean unexpectedConditionTransition = false;
protected long lastTime = -2;
Expand All @@ -59,10 +51,9 @@ public abstract class AbstractSkybox implements NuitSkybox {
protected AbstractSkybox() {
}

protected AbstractSkybox(Properties properties, Conditions conditions, Decorations decorations) {
protected AbstractSkybox(Properties properties, Conditions conditions) {
this.properties = properties;
this.conditions = conditions;
this.decorations = decorations;
}

@Override
Expand Down Expand Up @@ -249,77 +240,6 @@ protected boolean checkWeather() {
return (this.conditions.getWeathers().isExcludes() ^ this.conditions.getWeathers().getEntries().contains(Weather.CLEAR)) && !world.isRaining() && !world.isThundering();
}

public void renderDecorations(LevelRendererAccessor worldRendererAccess, PoseStack matrixStack, Matrix4f projectionMatrix, float tickDelta, BufferBuilder bufferBuilder, float alpha, Runnable fogCallback) {
RenderSystem.enableBlend();
var world = Minecraft.getInstance().level;
assert world != null;
long currentTime = world.getDayTime() % this.decorations.getRotation().getDuration();

// Custom Blender
this.decorations.getBlend().applyBlendFunc(alpha);
matrixStack.pushPose();

// static
this.decorations.getRotation().rotateStack(matrixStack, currentTime, world);

// Iris Compat
//matrixStack.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(IrisCompat.getSunPathRotation()));
//matrixStack.multiply(RotationAxis.POSITIVE_X.rotationDegrees(world.getSkyAngle(tickDelta) * 360.0F * this.decorations.getRotation().getRotationSpeed()));

Matrix4f matrix4f2 = matrixStack.last().pose();
RenderSystem.setShader(GameRenderer::getPositionTexShader);
// Sun
if (this.decorations.isSunEnabled()) {
RenderSystem.setShaderTexture(0, this.decorations.getSunTexture());
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
bufferBuilder.vertex(matrix4f2, -30.0F, 100.0F, -30.0F).uv(0.0F, 0.0F).endVertex();
bufferBuilder.vertex(matrix4f2, 30.0F, 100.0F, -30.0F).uv(1.0F, 0.0F).endVertex();
bufferBuilder.vertex(matrix4f2, 30.0F, 100.0F, 30.0F).uv(1.0F, 1.0F).endVertex();
bufferBuilder.vertex(matrix4f2, -30.0F, 100.0F, 30.0F).uv(0.0F, 1.0F).endVertex();
BufferUploader.drawWithShader(bufferBuilder.end());
}
// Moon
if (this.decorations.isMoonEnabled()) {
RenderSystem.setShaderTexture(0, this.decorations.getMoonTexture());
int moonPhase = world.getMoonPhase();
int xCoord = moonPhase % 4;
int yCoord = moonPhase / 4 % 2;
float startX = xCoord / 4.0F;
float startY = yCoord / 2.0F;
float endX = (xCoord + 1) / 4.0F;
float endY = (yCoord + 1) / 2.0F;
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
bufferBuilder.vertex(matrix4f2, -20.0F, -100.0F, 20.0F).uv(endX, endY).endVertex();
bufferBuilder.vertex(matrix4f2, 20.0F, -100.0F, 20.0F).uv(startX, endY).endVertex();
bufferBuilder.vertex(matrix4f2, 20.0F, -100.0F, -20.0F).uv(startX, startY).endVertex();
bufferBuilder.vertex(matrix4f2, -20.0F, -100.0F, -20.0F).uv(endX, startY).endVertex();
BufferUploader.drawWithShader(bufferBuilder.end());
}
// Stars
if (this.decorations.isStarsEnabled()) {
float i = 1.0F - world.getRainLevel(tickDelta);
float brightness = world.getStarBrightness(tickDelta) * i;
if (brightness > 0.0F) {
RenderSystem.setShaderColor(brightness, brightness, brightness, brightness);
FogRenderer.setupNoFog();
worldRendererAccess.getStarsBuffer().bind();
worldRendererAccess.getStarsBuffer().drawWithShader(matrixStack.last().pose(), projectionMatrix, GameRenderer.getPositionShader());
VertexBuffer.unbind();
fogCallback.run();
}
}
matrixStack.popPose();

RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.disableBlend();
RenderSystem.defaultBlendFunc();
}

@Override
public Decorations getDecorations() {
return this.decorations;
}

@Override
public Properties getProperties() {
return this.properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import com.mojang.math.Axis;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.amerebagatelle.mods.nuit.components.*;
import io.github.amerebagatelle.mods.nuit.components.Blend;
import io.github.amerebagatelle.mods.nuit.components.Conditions;
import io.github.amerebagatelle.mods.nuit.components.Properties;
import io.github.amerebagatelle.mods.nuit.components.RGBA;
import io.github.amerebagatelle.mods.nuit.mixin.LevelRendererAccessor;
import net.minecraft.client.Camera;
import net.minecraft.client.renderer.GameRenderer;
Expand All @@ -15,15 +18,14 @@ public class MonoColorSkybox extends AbstractSkybox {
public static Codec<MonoColorSkybox> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Properties.CODEC.fieldOf("properties").forGetter(AbstractSkybox::getProperties),
Conditions.CODEC.optionalFieldOf("conditions", Conditions.of()).forGetter(AbstractSkybox::getConditions),
Decorations.CODEC.optionalFieldOf("decorations", Decorations.of()).forGetter(AbstractSkybox::getDecorations),
RGBA.CODEC.optionalFieldOf("color", RGBA.of()).forGetter(MonoColorSkybox::getColor),
Blend.CODEC.optionalFieldOf("blend", Blend.normal()).forGetter(MonoColorSkybox::getBlend)
).apply(instance, MonoColorSkybox::new));
public RGBA color;
public Blend blend;

public MonoColorSkybox(Properties properties, Conditions conditions, Decorations decorations, RGBA color, Blend blend) {
super(properties, conditions, decorations);
public MonoColorSkybox(Properties properties, Conditions conditions, RGBA color, Blend blend) {
super(properties, conditions);
this.color = color;
this.blend = blend;
}
Expand Down Expand Up @@ -64,8 +66,6 @@ public void render(LevelRendererAccessor worldRendererAccess, PoseStack matrices
}
BufferUploader.drawWithShader(bufferBuilder.end());

this.renderDecorations(worldRendererAccess, matrices, projectionMatrix, tickDelta, bufferBuilder, this.alpha, fogCallback);

RenderSystem.disableBlend();
RenderSystem.depthMask(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.serialization.Codec;
import io.github.amerebagatelle.mods.nuit.NuitClient;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.Skybox;
import io.github.amerebagatelle.mods.nuit.skybox.decorations.DecorationBox;
import io.github.amerebagatelle.mods.nuit.skybox.textured.MultiTexturedSkybox;
import io.github.amerebagatelle.mods.nuit.skybox.textured.SquareTexturedSkybox;
import io.github.amerebagatelle.mods.nuit.skybox.vanilla.EndSkybox;
Expand All @@ -28,6 +29,8 @@ public class SkyboxType<T extends Skybox> {
public static final SkyboxType<SquareTexturedSkybox> SQUARE_TEXTURED_SKYBOX;
public static final SkyboxType<MultiTexturedSkybox> MULTI_TEXTURED_SKYBOX;

public static final SkyboxType<DecorationBox> DECORATION_BOX;

static {
SKYBOX_ID_CODEC = Codec.STRING.xmap((s) -> {
if (!s.contains(":")) {
Expand All @@ -48,6 +51,8 @@ public class SkyboxType<T extends Skybox> {
MONO_COLOR_SKYBOX = new SkyboxType<>("monocolor", 1, MonoColorSkybox.CODEC);
SQUARE_TEXTURED_SKYBOX = new SkyboxType<>("square-textured", 1, SquareTexturedSkybox.CODEC);
MULTI_TEXTURED_SKYBOX = new SkyboxType<>("multi-textured", 1, MultiTexturedSkybox.CODEC);

DECORATION_BOX = new SkyboxType<>("decorations", 1, DecorationBox.CODEC);
}

private final BiMap<Integer, Codec<T>> codecBiMap;
Expand All @@ -68,6 +73,7 @@ public static void register(Consumer<SkyboxType<?>> function) {
function.accept(MONO_COLOR_SKYBOX);
function.accept(SQUARE_TEXTURED_SKYBOX);
function.accept(MULTI_TEXTURED_SKYBOX);
function.accept(DECORATION_BOX);
}

public String getName() {
Expand Down
Loading

0 comments on commit be38af9

Please sign in to comment.