Skip to content

Commit

Permalink
Update item image exporter to work on 1.20.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Nov 12, 2024
1 parent b742745 commit ba6da3b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;
Expand All @@ -19,8 +18,8 @@
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;
Expand All @@ -32,9 +31,8 @@
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.
Expand Down Expand Up @@ -72,12 +70,10 @@ private static void run(String path) {
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);
Expand Down Expand Up @@ -121,22 +117,20 @@ private static void export(Path root, ImageRenderer renderer) throws IOException
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);

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

public void captureRender(Path output, Runnable render) throws IOException {
Files.createDirectories(output.getParent());
RenderSystem.applyModelViewMatrix();

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();
Expand Down

0 comments on commit ba6da3b

Please sign in to comment.