From 6eca9d57939cba97f49352f2567a1d52a5f54423 Mon Sep 17 00:00:00 2001 From: Phoupraw <467934387@qq.com> Date: Sat, 13 Apr 2024 05:11:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=99=B6=E6=B4=9E=E9=AB=98=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/dubhe/anvilcraft/item/GeodeItem.java | 8 +- .../anvilcraft/util/BlockHighlightUtil.java | 92 ++++++++++++++----- .../client/fabric/AnvilCraftClient.java | 9 ++ .../anvilcraft/forge/AnvilCraftForge.java | 13 ++- 4 files changed, 94 insertions(+), 28 deletions(-) diff --git a/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java b/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java index d1205bcca..7ff0e6ddb 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java @@ -4,11 +4,7 @@ import dev.dubhe.anvilcraft.util.BlockHighlightUtil; import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; -import net.minecraft.network.chat.ClickEvent; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.ComponentUtils; -import net.minecraft.network.chat.HoverEvent; -import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.*; import net.minecraft.tags.BlockTags; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; @@ -42,7 +38,7 @@ public GeodeItem(Properties properties) { for (int z = -radius; z <= radius; z += interval) { int height = level.getHeight(Heightmap.Types.WORLD_SURFACE, x, z); for (int y = level.getMinBuildHeight(); y <= height; y += interval) { - BlockPos offsetPos = pos.offset(x, y, z); + BlockPos offsetPos = new BlockPos(pos.getX() + x, y, pos.getZ() + z); BlockState state = level.getBlockState(offsetPos); if (!state.is(BlockTags.CRYSTAL_SOUND_BLOCKS)) continue; MutableComponent component = ComponentUtils.wrapInSquareBrackets(Component.translatable( diff --git a/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java b/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java index f7a1f07d2..46efc5cbf 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java @@ -1,17 +1,40 @@ package dev.dubhe.anvilcraft.util; -import lombok.Getter; +import com.mojang.blaze3d.vertex.DefaultVertexFormat; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import com.mojang.blaze3d.vertex.VertexFormat; +import dev.dubhe.anvilcraft.AnvilCraft; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.Camera; +import net.minecraft.client.renderer.LevelRenderer; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderStateShard; +import net.minecraft.client.renderer.RenderType; import net.minecraft.core.BlockPos; import net.minecraft.world.level.Level; +import net.minecraft.world.phys.Vec3; +import org.joml.Vector3i; +import org.joml.Vector3ic; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.OptionalDouble; public class BlockHighlightUtil { + @Environment(EnvType.CLIENT) + public static final RenderType NO_DEPTH = RenderType.create(AnvilCraft.MOD_ID + "_no_depth", DefaultVertexFormat.POSITION_COLOR_NORMAL, VertexFormat.Mode.LINES, 256, true, true, RenderType.CompositeState.builder() + .setShaderState(RenderStateShard.RENDERTYPE_LINES_SHADER) + .setWriteMaskState(RenderStateShard.COLOR_WRITE) + .setCullState(RenderStateShard.NO_CULL) + .setDepthTestState(RenderStateShard.NO_DEPTH_TEST) + .setLayeringState(RenderStateShard.VIEW_OFFSET_Z_LAYERING) + .setLineState(new RenderStateShard.LineStateShard(OptionalDouble.of(2))) + .createCompositeState(true)); + public static final Map SUBCHUNKS = new HashMap<>(); private static Level level = null; - @Getter - private static final Map highlight = new ConcurrentHashMap<>(); - + /** * 高亮方块 * @@ -19,23 +42,50 @@ public class BlockHighlightUtil { * @param pos 位置 */ public static void highlightBlock(Level level, BlockPos pos) { - if (BlockHighlightUtil.level == level) BlockHighlightUtil.highlight.put(pos, 400); - else { - BlockHighlightUtil.level = level; - BlockHighlightUtil.highlight.clear(); - BlockHighlightUtil.highlight.put(pos, 400); + if (BlockHighlightUtil.getLevel() != level) { + BlockHighlightUtil.setLevel(level); + SUBCHUNKS.clear(); } - // TODO 高亮方块 + SUBCHUNKS.put(new Vector3i(Math.floorDiv(pos.getX(), 16), Math.floorDiv(pos.getY(), 16), Math.floorDiv(pos.getZ(), 16)), level.getGameTime()); } - - /** - * 获取被高亮的方块列表 - * - * @param level 维度 - * @return 被高亮的方块列表 - */ - public static Map getHighlight(Level level) { - if (BlockHighlightUtil.level != level) BlockHighlightUtil.highlight.clear(); - return BlockHighlightUtil.highlight; + @Environment(EnvType.CLIENT) + public static void render(Level level, MultiBufferSource consumers, PoseStack poseStack, Camera camera) { + VertexConsumer consumer = consumers.getBuffer(BlockHighlightUtil.NO_DEPTH); + Vec3 cameraPos = camera.getPosition(); + int color = 0xFF8932B8; + poseStack.pushPose(); + poseStack.translate(-cameraPos.x, -cameraPos.y, -cameraPos.z); + for (var iterator = BlockHighlightUtil.SUBCHUNKS.entrySet().iterator(); iterator.hasNext(); ) { + var entry = iterator.next(); + Vector3ic subchunk = entry.getKey(); + Long moment = entry.getValue(); + if (level.getGameTime() > moment + 60 * 20) { + iterator.remove(); + continue; + } + Vector3ic pos1 = subchunk.mul(16, new Vector3i()); + Vector3ic pos2 = pos1.add(16, 16, 16, new Vector3i()); + LevelRenderer.renderLineBox( + poseStack, + consumer, + pos1.x(), + pos1.y(), + pos1.z(), + pos2.x(), + pos2.y(), + pos2.z(), + (color >> 16 & 0xFF) / 255f, + (color >> 8 & 0xFF) / 255f, + (color & 0xFF) / 255f, + (color >> 24) / 255f + ); + } + poseStack.popPose(); + } + public static Level getLevel() { + return level; + } + public static void setLevel(Level level) { + BlockHighlightUtil.level = level; } } diff --git a/fabric/src/main/java/dev/dubhe/anvilcraft/client/fabric/AnvilCraftClient.java b/fabric/src/main/java/dev/dubhe/anvilcraft/client/fabric/AnvilCraftClient.java index aa466a234..ecb137dee 100644 --- a/fabric/src/main/java/dev/dubhe/anvilcraft/client/fabric/AnvilCraftClient.java +++ b/fabric/src/main/java/dev/dubhe/anvilcraft/client/fabric/AnvilCraftClient.java @@ -1,8 +1,11 @@ package dev.dubhe.anvilcraft.client.fabric; import dev.dubhe.anvilcraft.init.ModBlocks; +import dev.dubhe.anvilcraft.util.BlockHighlightUtil; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; +import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.RenderType; public class AnvilCraftClient implements ClientModInitializer { @@ -11,5 +14,11 @@ public void onInitializeClient() { BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.CORRUPTED_BEACON.get(), RenderType.translucent()); BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.RESIN_BLOCK.get(), RenderType.translucent()); BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.AMBER_BLOCK.get(), RenderType.translucent()); + WorldRenderEvents.AFTER_ENTITIES.register(context -> { + if (BlockHighlightUtil.SUBCHUNKS.isEmpty()) return; + MultiBufferSource consumers = context.consumers(); + if (consumers == null) return; + BlockHighlightUtil.render(context.world(), consumers, context.matrixStack(), context.camera()); + }); } } diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java b/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java index d409bd9d6..1a8f03fec 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java @@ -3,8 +3,12 @@ import dev.dubhe.anvilcraft.AnvilCraft; import dev.dubhe.anvilcraft.config.AnvilCraftConfig; import dev.dubhe.anvilcraft.init.ModCommands; +import dev.dubhe.anvilcraft.util.BlockHighlightUtil; import me.shedaniel.autoconfig.AutoConfig; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientLevel; import net.minecraftforge.client.ConfigScreenHandler; +import net.minecraftforge.client.event.RenderLevelStageEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.fml.ModLoadingContext; @@ -19,7 +23,14 @@ public class AnvilCraftForge { public AnvilCraftForge() { AnvilCraft.init(); MinecraftForge.EVENT_BUS.addListener(AnvilCraftForge::registerCommand); - + MinecraftForge.EVENT_BUS.addListener((RenderLevelStageEvent event) -> { + if (event.getStage() != RenderLevelStageEvent.Stage.AFTER_ENTITIES) return; + if (BlockHighlightUtil.SUBCHUNKS.isEmpty()) return; + ClientLevel level = Minecraft.getInstance().level; + if (level == null) return; + BlockHighlightUtil.render(level, Minecraft.getInstance().renderBuffers().bufferSource(), event.getPoseStack(), event.getCamera()); + }); + ModLoadingContext.get().registerExtensionPoint( ConfigScreenHandler.ConfigScreenFactory.class, () -> new ConfigScreenHandler.ConfigScreenFactory( From 4146b00e2604ca9b313712d57a04791065e165b4 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sat, 13 Apr 2024 05:28:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=84=E7=90=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/dubhe/anvilcraft/item/GeodeItem.java | 6 +- .../anvilcraft/util/BlockHighlightUtil.java | 68 ++++++++++++------- .../anvilcraft/forge/AnvilCraftForge.java | 3 +- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java b/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java index 7ff0e6ddb..0173f6514 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java @@ -4,7 +4,11 @@ import dev.dubhe.anvilcraft.util.BlockHighlightUtil; import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; -import net.minecraft.network.chat.*; +import net.minecraft.network.chat.ClickEvent; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.ComponentUtils; +import net.minecraft.network.chat.HoverEvent; +import net.minecraft.network.chat.MutableComponent; import net.minecraft.tags.BlockTags; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; diff --git a/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java b/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java index 46efc5cbf..bc0014476 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java @@ -5,6 +5,7 @@ import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.blaze3d.vertex.VertexFormat; import dev.dubhe.anvilcraft.AnvilCraft; +import lombok.Getter; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Camera; @@ -15,6 +16,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; +import org.jetbrains.annotations.NotNull; import org.joml.Vector3i; import org.joml.Vector3ic; @@ -24,17 +26,21 @@ public class BlockHighlightUtil { @Environment(EnvType.CLIENT) - public static final RenderType NO_DEPTH = RenderType.create(AnvilCraft.MOD_ID + "_no_depth", DefaultVertexFormat.POSITION_COLOR_NORMAL, VertexFormat.Mode.LINES, 256, true, true, RenderType.CompositeState.builder() - .setShaderState(RenderStateShard.RENDERTYPE_LINES_SHADER) - .setWriteMaskState(RenderStateShard.COLOR_WRITE) - .setCullState(RenderStateShard.NO_CULL) - .setDepthTestState(RenderStateShard.NO_DEPTH_TEST) - .setLayeringState(RenderStateShard.VIEW_OFFSET_Z_LAYERING) - .setLineState(new RenderStateShard.LineStateShard(OptionalDouble.of(2))) - .createCompositeState(true)); + public static final RenderType NO_DEPTH = + RenderType.create(AnvilCraft.MOD_ID + "_no_depth", DefaultVertexFormat.POSITION_COLOR_NORMAL, + VertexFormat.Mode.LINES, 256, true, true, RenderType.CompositeState.builder() + .setShaderState(RenderStateShard.RENDERTYPE_LINES_SHADER) + .setWriteMaskState(RenderStateShard.COLOR_WRITE) + .setCullState(RenderStateShard.NO_CULL) + .setDepthTestState(RenderStateShard.NO_DEPTH_TEST) + .setLayeringState(RenderStateShard.VIEW_OFFSET_Z_LAYERING) + .setLineState(new RenderStateShard.LineStateShard(OptionalDouble.of(2))) + .createCompositeState(true)); public static final Map SUBCHUNKS = new HashMap<>(); + + @Getter private static Level level = null; - + /** * 高亮方块 * @@ -46,10 +52,22 @@ public static void highlightBlock(Level level, BlockPos pos) { BlockHighlightUtil.setLevel(level); SUBCHUNKS.clear(); } - SUBCHUNKS.put(new Vector3i(Math.floorDiv(pos.getX(), 16), Math.floorDiv(pos.getY(), 16), Math.floorDiv(pos.getZ(), 16)), level.getGameTime()); + SUBCHUNKS.put(new Vector3i(Math.floorDiv(pos.getX(), 16), Math.floorDiv(pos.getY(), 16), + Math.floorDiv(pos.getZ(), 16)), level.getGameTime()); } + + /** + * 渲染 + * + * @param level 世界 + * @param consumers 消耗 + * @param poseStack 渲染空间 + * @param camera 相机 + */ @Environment(EnvType.CLIENT) - public static void render(Level level, MultiBufferSource consumers, PoseStack poseStack, Camera camera) { + public static void render( + Level level, @NotNull MultiBufferSource consumers, @NotNull PoseStack poseStack, @NotNull Camera camera + ) { VertexConsumer consumer = consumers.getBuffer(BlockHighlightUtil.NO_DEPTH); Vec3 cameraPos = camera.getPosition(); int color = 0xFF8932B8; @@ -66,25 +84,23 @@ public static void render(Level level, MultiBufferSource consumers, PoseStack po Vector3ic pos1 = subchunk.mul(16, new Vector3i()); Vector3ic pos2 = pos1.add(16, 16, 16, new Vector3i()); LevelRenderer.renderLineBox( - poseStack, - consumer, - pos1.x(), - pos1.y(), - pos1.z(), - pos2.x(), - pos2.y(), - pos2.z(), - (color >> 16 & 0xFF) / 255f, - (color >> 8 & 0xFF) / 255f, - (color & 0xFF) / 255f, - (color >> 24) / 255f + poseStack, + consumer, + pos1.x(), + pos1.y(), + pos1.z(), + pos2.x(), + pos2.y(), + pos2.z(), + (color >> 16 & 0xFF) / 255f, + (color >> 8 & 0xFF) / 255f, + (color & 0xFF) / 255f, + (color >> 24) / 255f ); } poseStack.popPose(); } - public static Level getLevel() { - return level; - } + public static void setLevel(Level level) { BlockHighlightUtil.level = level; } diff --git a/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java b/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java index 1a8f03fec..8bf44fcec 100644 --- a/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java +++ b/forge/src/main/java/dev/dubhe/anvilcraft/forge/AnvilCraftForge.java @@ -28,7 +28,8 @@ public AnvilCraftForge() { if (BlockHighlightUtil.SUBCHUNKS.isEmpty()) return; ClientLevel level = Minecraft.getInstance().level; if (level == null) return; - BlockHighlightUtil.render(level, Minecraft.getInstance().renderBuffers().bufferSource(), event.getPoseStack(), event.getCamera()); + BlockHighlightUtil.render(level, Minecraft.getInstance().renderBuffers().bufferSource(), + event.getPoseStack(), event.getCamera()); }); ModLoadingContext.get().registerExtensionPoint(