1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-04-05 18:26:58 +00:00

Update item image exporter to work on 1.20.x

This commit is contained in:
Jonathan Coates 2024-11-12 08:11:09 +00:00
parent b742745854
commit ba6da3bc6c
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 29 additions and 32 deletions

View File

@ -9,7 +9,6 @@ import com.google.common.io.RecursiveDeleteOption;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
@ -19,8 +18,8 @@ import dan200.computercraft.data.PrettyJsonWriter;
import dan200.computercraft.gametest.core.TestHooks;
import dan200.computercraft.shared.platform.RegistryWrappers;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.ShapedRecipe;
@ -32,9 +31,8 @@ import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Provides a {@literal /ccexport <path>} command which exports icons and recipes for all ComputerCraft items.
@ -72,12 +70,10 @@ public class Exporter {
private static void export(Path root, ImageRenderer renderer) throws IOException {
var dump = new JsonDump();
Set<Item> items = new HashSet<>();
// First find all CC items
for (var item : RegistryWrappers.ITEMS) {
if (RegistryWrappers.ITEMS.getKey(item).getNamespace().equals(ComputerCraftAPI.MOD_ID)) items.add(item);
}
var items = RegistryWrappers.ITEMS.stream()
.filter(x -> RegistryWrappers.ITEMS.getKey(x).getNamespace().equals(ComputerCraftAPI.MOD_ID))
.collect(Collectors.toSet());
// Now find all CC recipes.
var level = Objects.requireNonNull(Minecraft.getInstance().level);
@ -121,10 +117,6 @@ public class Exporter {
var itemDir = root.resolve("items");
if (Files.exists(itemDir)) MoreFiles.deleteRecursively(itemDir, RecursiveDeleteOption.ALLOW_INSECURE);
renderer.setupState();
var transform = new PoseStack();
transform.setIdentity();
for (var item : items) {
var stack = new ItemStack(item);
var location = RegistryWrappers.ITEMS.getKey(item);
@ -132,11 +124,13 @@ public class Exporter {
dump.itemNames.put(location.toString(), stack.getHoverName().getString());
renderer.captureRender(itemDir.resolve(location.getNamespace()).resolve(location.getPath() + ".png"),
() -> {
// TODO: Minecraft.getInstance().getItemRenderer().ren(transform, stack, 0, 0)
var graphics = new GuiGraphics(Minecraft.getInstance(), Minecraft.getInstance().renderBuffers().bufferSource());
graphics.renderItem(stack, 0, 0);
graphics.flush();
}
);
}
renderer.clearState();
try (Writer writer = Files.newBufferedWriter(root.resolve("index.json")); var jsonWriter = new PrettyJsonWriter(writer)) {
GSON.toJson(dump, JsonDump.class, jsonWriter);

View File

@ -5,15 +5,14 @@
package dan200.computercraft.export;
import com.mojang.blaze3d.pipeline.TextureTarget;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.VertexSorting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.FogRenderer;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL12;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@ -28,39 +27,43 @@ public class ImageRenderer implements AutoCloseable {
private final TextureTarget framebuffer = new TextureTarget(WIDTH, HEIGHT, true, Minecraft.ON_OSX);
private final NativeImage image = new NativeImage(WIDTH, HEIGHT, Minecraft.ON_OSX);
private @Nullable Matrix4f projectionMatrix;
public ImageRenderer() {
framebuffer.setClearColor(0, 0, 0, 0);
framebuffer.clear(Minecraft.ON_OSX);
}
public void setupState() {
projectionMatrix = RenderSystem.getProjectionMatrix();
RenderSystem.setProjectionMatrix(new Matrix4f().identity().ortho(0, 16, 0, 16, 1000, 3000), VertexSorting.DISTANCE_TO_ORIGIN);
public void captureRender(Path output, Runnable render) throws IOException {
Files.createDirectories(output.getParent());
framebuffer.setClearColor(0, 0, 0, 0);
framebuffer.clear(Minecraft.ON_OSX);
framebuffer.bindWrite(true);
// Setup rendering state
var projectionMatrix = RenderSystem.getProjectionMatrix();
RenderSystem.setProjectionMatrix(new Matrix4f().identity().ortho(0, 16, 16, 0, 1000, 3000), VertexSorting.ORTHOGRAPHIC_Z);
var transform = RenderSystem.getModelViewStack();
transform.pushPose();
transform.setIdentity();
transform.translate(0.0f, 0.0f, -2000.0f);
RenderSystem.applyModelViewMatrix();
Lighting.setupFor3DItems();
FogRenderer.setupNoFog();
}
public void clearState() {
if (projectionMatrix == null) throw new IllegalStateException("Not currently rendering");
// Render
render.run();
// Restore rendering state
RenderSystem.setProjectionMatrix(projectionMatrix, VertexSorting.DISTANCE_TO_ORIGIN);
RenderSystem.getModelViewStack().popPose();
}
RenderSystem.applyModelViewMatrix();
public void captureRender(Path output, Runnable render) throws IOException {
Files.createDirectories(output.getParent());
framebuffer.bindWrite(true);
RenderSystem.clear(GL12.GL_COLOR_BUFFER_BIT | GL12.GL_DEPTH_BUFFER_BIT, Minecraft.ON_OSX);
render.run();
framebuffer.unbindWrite();
Minecraft.getInstance().getMainRenderTarget().bindWrite(true);
// And save the image
framebuffer.bindRead();
image.downloadTexture(0, false);
image.flipY();