diff --git a/projects/common/src/testMod/java/dan200/computercraft/export/Exporter.java b/projects/common/src/testMod/java/dan200/computercraft/export/Exporter.java index ebec60496..d852c8774 100644 --- a/projects/common/src/testMod/java/dan200/computercraft/export/Exporter.java +++ b/projects/common/src/testMod/java/dan200/computercraft/export/Exporter.java @@ -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; @@ -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; @@ -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 } command which exports icons and recipes for all ComputerCraft items. @@ -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 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 @@ 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); @@ -132,11 +124,13 @@ private static void export(Path root, ImageRenderer renderer) throws IOException 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); diff --git a/projects/common/src/testMod/java/dan200/computercraft/export/ImageRenderer.java b/projects/common/src/testMod/java/dan200/computercraft/export/ImageRenderer.java index 032fc7bb7..ea3d8bc7e 100644 --- a/projects/common/src/testMod/java/dan200/computercraft/export/ImageRenderer.java +++ b/projects/common/src/testMod/java/dan200/computercraft/export/ImageRenderer.java @@ -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(); - } - - 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();