Skip to content

Commit

Permalink
feat(day 3, Na compat): programmer art support
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv256 committed Oct 14, 2024
1 parent 19de198 commit 233d0b6
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gay.sylv.legacy_landscape.client.util;

import gay.sylv.legacy_landscape.client.HackedRenderSystem;
import gay.sylv.legacy_landscape.mixin.client.Accessor_CompositeRenderType;
import gay.sylv.legacy_landscape.mixin.client.Accessor_CompositeState;
import gay.sylv.legacy_landscape.mixin.client.Accessor_TextureStateShard;
import net.minecraft.client.renderer.RenderType;

public final class RenderUtil {
public static final int WATER_COLOR = 0xFF334FDD;

private RenderUtil() {}

public static int saturateTint(int tint) {
int red = (tint & 0x00FF0000) >> 16;
red += 0x28;
red = Math.clamp(red, 0, 0x98);
int green = (tint & 0x0000FF00) >> 8;
green += 0x28;
green = Math.clamp(green, 0, 0xFF); // prevent overflow/underflow
tint = (tint & 0xFF0000FF) | green << 8 | red << 16; // clear green channel and set new green
return tint;
}

public static void setLegacyTextures(RenderType renderType) {
Accessor_CompositeRenderType compositeRenderType = (Accessor_CompositeRenderType) renderType;
Accessor_CompositeState compositeState = (Accessor_CompositeState) (Object) compositeRenderType.getState();
assert compositeState != null;
Accessor_TextureStateShard textureState = (Accessor_TextureStateShard) (compositeState.getTextureState());
if (textureState.getTexture().isPresent()) {
HackedRenderSystem.setShaderTexture(0, textureState.getTexture().get(), textureState.isBlur(), textureState.isMipmap());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import gay.sylv.legacy_landscape.util.TintUtil;
import gay.sylv.legacy_landscape.client.util.RenderUtil;
import net.minecraft.client.OptionInstance;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.BiomeColors;
Expand Down Expand Up @@ -49,10 +49,10 @@ private int saturateBlockTint(ColorResolver colorResolver, Biome biome, double x
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
if (!colorResolver.equals(BiomeColors.WATER_COLOR_RESOLVER)) {
int tint = original.call(colorResolver, biome, x, z);
tint = TintUtil.saturateTint(tint);
tint = RenderUtil.saturateTint(tint);
return tint;
} else {
return TintUtil.WATER_COLOR;
return RenderUtil.WATER_COLOR;
}
}
return original.call(colorResolver, biome, x, z);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package gay.sylv.legacy_landscape.mixin.client;

import com.llamalad7.mixinextras.sugar.Local;
import gay.sylv.legacy_landscape.client.HackedRenderSystem;
import gay.sylv.legacy_landscape.client.util.RenderUtil;
import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.chunk.LevelChunk;
import org.joml.Matrix4f;
Expand All @@ -27,18 +25,13 @@ private Mixin_LevelRenderer() {}
),
require = 0
)
private void setLegacyTextures(RenderType renderType, double x, double y, double z, Matrix4f frustrumMatrix, Matrix4f projectionMatrix, CallbackInfo ci, @Local ShaderInstance shaderInstance) {
private void setLegacyTextures(RenderType renderType, double x, double y, double z, Matrix4f frustrumMatrix, Matrix4f projectionMatrix, CallbackInfo ci) {
Minecraft client = Minecraft.getInstance();
assert client.level != null;
LevelChunk chunk = client.level.getChunkAt(BlockPos.containing(x, y, z));
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
Accessor_CompositeRenderType compositeRenderType = (Accessor_CompositeRenderType) renderType;
Accessor_CompositeState compositeState = (Accessor_CompositeState) (Object) compositeRenderType.getState();
assert compositeState != null;
Accessor_TextureStateShard textureState = (Accessor_TextureStateShard) (compositeState.getTextureState());
if (textureState.getTexture().isPresent()) {
HackedRenderSystem.setShaderTexture(0, textureState.getTexture().get(), textureState.isBlur(), textureState.isMipmap());
}
RenderUtil.setLegacyTextures(renderType);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gay.sylv.legacy_landscape.mixin.client.sodium;

import net.caffeinemc.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
import net.minecraft.client.renderer.RenderType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(TerrainRenderPass.class)
@Pseudo
public interface Accessor_TerrainRenderPass {
@Accessor
RenderType getRenderType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import gay.sylv.legacy_landscape.util.TintUtil;
import gay.sylv.legacy_landscape.client.util.RenderUtil;
import net.caffeinemc.mods.sodium.client.model.color.ColorProvider;
import net.caffeinemc.mods.sodium.client.model.color.ColorProviderRegistry;
import net.caffeinemc.mods.sodium.client.model.color.DefaultColorProviders;
Expand Down Expand Up @@ -39,13 +39,13 @@ private ColorProvider<BlockState> hijackColorProvider(ColorProviderRegistry inst
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
return (levelSlice, blockPos, mutableBlockPos, blockState, modelQuadView, ints) -> {
if (colorProvider.equals(DefaultColorProviders.GrassColorProvider.BLOCKS)) {
Arrays.fill(ints, TintUtil.saturateTint(BiomeColors.getAverageGrassColor(levelSlice, blockPos)));
Arrays.fill(ints, RenderUtil.saturateTint(BiomeColors.getAverageGrassColor(levelSlice, blockPos)));
} else if (colorProvider.equals(DefaultColorProviders.FoliageColorProvider.BLOCKS)) {
Arrays.fill(ints, TintUtil.saturateTint(BiomeColors.getAverageFoliageColor(levelSlice, blockPos)));
Arrays.fill(ints, RenderUtil.saturateTint(BiomeColors.getAverageFoliageColor(levelSlice, blockPos)));
}
};
} else {
return colorProvider;
return colorProvider;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gay.sylv.legacy_landscape.mixin.client.sodium;

import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import gay.sylv.legacy_landscape.util.TintUtil;
import gay.sylv.legacy_landscape.client.util.RenderUtil;
import net.caffeinemc.mods.sodium.client.model.color.ColorProvider;
import net.caffeinemc.mods.sodium.client.model.color.ColorProviderRegistry;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -44,7 +44,7 @@ private void installWaterOverride(CallbackInfo ci) {
assert client.level != null;
LevelChunk chunk = client.level.getChunkAt(blockPos);
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
Arrays.fill(ints, TintUtil.WATER_COLOR);
Arrays.fill(ints, RenderUtil.WATER_COLOR);
} else {
colorProvider.getColors(levelSlice, blockPos, mutableBlockPos, fluidState, modelQuadView, ints);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gay.sylv.legacy_landscape.mixin.client.sodium;

import gay.sylv.legacy_landscape.client.util.RenderUtil;
import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import net.caffeinemc.mods.sodium.client.gl.device.CommandList;
import net.caffeinemc.mods.sodium.client.gl.device.RenderDevice;
import net.caffeinemc.mods.sodium.client.render.chunk.ChunkRenderMatrices;
import net.caffeinemc.mods.sodium.client.render.chunk.DefaultChunkRenderer;
import net.caffeinemc.mods.sodium.client.render.chunk.ShaderChunkRenderer;
import net.caffeinemc.mods.sodium.client.render.chunk.lists.ChunkRenderListIterable;
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
import net.caffeinemc.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import net.caffeinemc.mods.sodium.client.render.viewport.CameraTransform;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.chunk.LevelChunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(DefaultChunkRenderer.class)
@Pseudo
public abstract class Mixin_DefaultChunkRenderer extends ShaderChunkRenderer {
private Mixin_DefaultChunkRenderer(
RenderDevice device, ChunkVertexType vertexType
) {
super(device, vertexType);
}

@SuppressWarnings("deprecation")
@Inject(
method = "render",
at = @At(
value = "INVOKE",
target = "Lnet/caffeinemc/mods/sodium/client/render/chunk/ShaderChunkRenderer;begin(Lnet/caffeinemc/mods/sodium/client/render/chunk/terrain/TerrainRenderPass;)V",
shift = At.Shift.AFTER
)
)
private void setLegacyTextures(
ChunkRenderMatrices matrices, CommandList commandList, ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform camera,
CallbackInfo ci
) {
Minecraft client = Minecraft.getInstance();
assert client.level != null;
LevelChunk chunk = client.level.getChunkAt(BlockPos.containing(camera.x, camera.y, camera.z));
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
RenderUtil.setLegacyTextures(((Accessor_TerrainRenderPass) renderPass).getRenderType());
this.activeProgram.getInterface().setupState();
}
}
}
18 changes: 0 additions & 18 deletions src/main/java/gay/sylv/legacy_landscape/util/TintUtil.java

This file was deleted.

4 changes: 3 additions & 1 deletion src/main/resources/legacy_landscape.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"client.Mixin_LevelRenderer",
"client.sodium.Mixin_BlockRenderer",
"client.sodium.Mixin_ColorProviderRegistry",
"client.sodium.Mixin_LevelColorCache"
"client.sodium.Mixin_DefaultChunkRenderer",
"client.sodium.Mixin_LevelColorCache",
"client.sodium.Accessor_TerrainRenderPass"
],
"server": [],
"injectors": {
Expand Down

0 comments on commit 233d0b6

Please sign in to comment.