Skip to content

Commit

Permalink
feat(day 2): swap block textures to programmer art in legacy chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv256 committed Oct 13, 2024
1 parent 881915d commit d1d9adb
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ neoForge {
}

// This line is optional. Access Transformers are automatically detected
// accessTransformers = project.files('src/main/resources/META-INF/accesstransformer.cfg')
accessTransformers.from 'src/main/resources/META-INF/accesstransformer.cfg'

// Default run configurations.
// These can be tweaked, removed, or duplicated as needed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gay.sylv.legacy_landscape.client;

import com.mojang.blaze3d.systems.RenderSystem;
import gay.sylv.legacy_landscape.mixin.client.Accessor_RenderSystem;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.resources.ResourceLocation;

public final class HackedRenderSystem {
private HackedRenderSystem() {}

public static void setShaderTexture(int shaderTexture, ResourceLocation textureId, boolean blur, boolean mipmap) {
if (!RenderSystem.isOnRenderThread()) {
RenderSystem.recordRenderCall(() -> _setShaderTexture(shaderTexture, textureId, blur, mipmap));
} else {
_setShaderTexture(shaderTexture, textureId, blur, mipmap);
}
}

private static void _setShaderTexture(int shaderTexture, ResourceLocation textureId, boolean blur, boolean mipmap) {
int[] shaderTextures = Accessor_RenderSystem.getShaderTextures();
if (shaderTexture >= 0 && shaderTexture < shaderTextures.length) {
TextureManager textureManager = LegacyResourceFaker.getTextureManager();
AbstractTexture abstractTexture = textureManager.getTexture(textureId);
abstractTexture.setFilter(blur, mipmap);
shaderTextures[shaderTexture] = abstractTexture.getId();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
modid = LegacyLandscape.MOD_ID,
bus = EventBusSubscriber.Bus.MOD
)
public final class ClientLegacyLandscape {
public final class LegacyResourceFaker {
private static final Logger LOGGER = LogUtils.getLogger();

private static boolean initialized;
private static PackResources programmerArt;
private static ReloadableResourceManager resourceManager;
private static TextureManager textureManager;
Expand All @@ -49,25 +50,35 @@ public final class ClientLegacyLandscape {
private static void registerReloadListeners(RegisterClientReloadListenersEvent event) {
event.registerReloadListener((ResourceManagerReloadListener) resourceManager1 -> {
Minecraft client = Minecraft.getInstance();
try (PackResources programmerArtResources = Objects.requireNonNull(client.getResourcePackRepository().getPack("programmer_art")).open()) {
programmerArt = programmerArtResources;
resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES);
textureManager = new TextureManager(resourceManager);
resourceManager.registerReloadListener(textureManager);
splashManager = new SplashManager(Minecraft.getInstance().getUser());
resourceManager.registerReloadListener(splashManager);
resourceManager.registerReloadListener(new GrassColorReloadListener());
resourceManager.registerReloadListener(new FoliageColorReloadListener());
blockColors = BlockColors.createDefault();
modelManager = new ModelManager(textureManager, blockColors, 0);
resourceManager.registerReloadListener(modelManager);
entityModels = new EntityModelSet();
resourceManager.registerReloadListener(entityModels);
if (!initialized) {
try (PackResources programmerArtResources = Objects.requireNonNull(client.getResourcePackRepository().getPack("programmer_art")).open()) {
programmerArt = programmerArtResources;
resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES);
textureManager = new TextureManager(resourceManager);
resourceManager.registerReloadListener(textureManager);
splashManager = new SplashManager(Minecraft.getInstance().getUser());
resourceManager.registerReloadListener(splashManager);
resourceManager.registerReloadListener(new GrassColorReloadListener());
resourceManager.registerReloadListener(new FoliageColorReloadListener());
blockColors = BlockColors.createDefault();
modelManager = new ModelManager(textureManager, blockColors, client.options.mipmapLevels().get());
resourceManager.registerReloadListener(modelManager);
entityModels = new EntityModelSet();
resourceManager.registerReloadListener(entityModels);
}

initialized = true;
}

modelManager.updateMaxMipLevel(client.options.mipmapLevels().get());

List<PackResources> fullResourcesList = client.getResourceManager().listPacks().collect(Collectors.toCollection(ArrayList::new));
fullResourcesList.add(programmerArt);
resourceManager.createReload(Util.backgroundExecutor(), client, CompletableFuture.completedFuture(Unit.INSTANCE), fullResourcesList).done().thenRun(() -> LOGGER.info("Loaded programmer art resources"));
});
}

public static TextureManager getTextureManager() {
return textureManager;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gay.sylv.legacy_landscape.mixin;
package gay.sylv.legacy_landscape.mixin.client;

import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import net.minecraft.client.color.block.BlockTintCache;
Expand All @@ -8,7 +8,7 @@
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ClientLevel.class)
public interface ClientLevelAccessor {
public interface Accessor_ClientLevel {
@Accessor
Object2ObjectArrayMap<ColorResolver, BlockTintCache> getTintCaches();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gay.sylv.legacy_landscape.mixin.client;

import net.minecraft.client.renderer.RenderType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(RenderType.CompositeRenderType.class)
public interface Accessor_CompositeRenderType {
@Accessor
RenderType.CompositeState getState();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gay.sylv.legacy_landscape.mixin.client;

import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.client.renderer.RenderType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(RenderType.CompositeState.class)
public interface Accessor_CompositeState {
@Accessor
RenderStateShard.EmptyTextureStateShard getTextureState();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gay.sylv.legacy_landscape.mixin.client;

import com.mojang.blaze3d.systems.RenderSystem;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(RenderSystem.class)
public interface Accessor_RenderSystem {
@Accessor
static int[] getShaderTextures() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gay.sylv.legacy_landscape.mixin.client;

import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.resources.ResourceLocation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.Optional;

@Mixin(RenderStateShard.TextureStateShard.class)
public interface Accessor_TextureStateShard {
@Accessor
Optional<ResourceLocation> getTexture();

@Accessor
boolean isBlur();

@Accessor
boolean isMipmap();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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.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;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(LevelRenderer.class)
public final class Mixin_LevelRenderer {
private Mixin_LevelRenderer() {}

@Inject(
method = "renderSectionLayer",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/renderer/ShaderInstance;setDefaultUniforms(Lcom/mojang/blaze3d/vertex/VertexFormat$Mode;Lorg/joml/Matrix4f;Lorg/joml/Matrix4f;Lcom/mojang/blaze3d/platform/Window;)V"
)
)
private void setLegacyTextures(RenderType renderType, double x, double y, double z, Matrix4f frustrumMatrix, Matrix4f projectionMatrix, CallbackInfo ci, @Local ShaderInstance shaderInstance) {
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());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gay.sylv.legacy_landscape.networking;

import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import gay.sylv.legacy_landscape.mixin.ClientLevelAccessor;
import gay.sylv.legacy_landscape.mixin.client.Accessor_ClientLevel;
import gay.sylv.legacy_landscape.networking.client_bound.LegacyChunkPayload;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
Expand All @@ -22,7 +22,7 @@ public static void handleLegacyChunkPayload(final LegacyChunkPayload payload, fi
} else {
chunk.removeData(LegacyAttachments.LEGACY_CHUNK);
}
((ClientLevelAccessor) level).getTintCaches().forEach((colorResolver, blockTintCache) -> blockTintCache.invalidateForChunk(chunk.getPos().x, chunk.getPos().z));
((Accessor_ClientLevel) level).getTintCaches().forEach((colorResolver, blockTintCache) -> blockTintCache.invalidateForChunk(chunk.getPos().x, chunk.getPos().z));
for (int y = level.getMinSection(); y < level.getMaxSection(); y++) {
Minecraft.getInstance().levelRenderer.setSectionDirty(chunk.getPos().x, y, chunk.getPos().z);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ private LegacyNetworking() {}
@SubscribeEvent
public static void register(final RegisterPayloadHandlersEvent event) {
final PayloadRegistrar registrar = event.registrar("1");
registrar.executesOn(HandlerThread.MAIN);
registrar.playToClient(
final PayloadRegistrar mainThreadRegistrar = registrar.executesOn(HandlerThread.MAIN);
mainThreadRegistrar.playToClient(
LegacyChunkPayload.TYPE,
LegacyChunkPayload.STREAM_CODEC,
ClientPayloadHandler::handleLegacyChunkPayload
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public net.minecraft.client.renderer.RenderType$CompositeRenderType
9 changes: 7 additions & 2 deletions src/main/resources/legacy_landscape.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
"package": "gay.sylv.legacy_landscape.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"ClientLevelAccessor"
],
"client": [
"client.Mixin_ClientLevel"
"client.Accessor_ClientLevel",
"client.Accessor_CompositeRenderType",
"client.Accessor_CompositeState",
"client.Accessor_RenderSystem",
"client.Accessor_TextureStateShard",
"client.Mixin_ClientLevel",
"client.Mixin_LevelRenderer"
],
"server": [],
"injectors": {
Expand Down

0 comments on commit d1d9adb

Please sign in to comment.