Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

晶洞高亮 #256

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
package dev.dubhe.anvilcraft.util;

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 lombok.Getter;
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.jetbrains.annotations.NotNull;
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 {
private static Level level = null;
@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<Vector3ic, Long> SUBCHUNKS = new HashMap<>();

@Getter
private static final Map<BlockPos, Integer> highlight = new ConcurrentHashMap<>();
private static Level level = null;

/**
* 高亮方块
Expand All @@ -19,23 +48,60 @@ 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 被高亮的方块列表
* @param level 世界
* @param consumers 消耗
* @param poseStack 渲染空间
* @param camera 相机
*/
public static Map<BlockPos, Integer> getHighlight(Level level) {
if (BlockHighlightUtil.level != level) BlockHighlightUtil.highlight.clear();
return BlockHighlightUtil.highlight;
@Environment(EnvType.CLIENT)
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;
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 void setLevel(Level level) {
BlockHighlightUtil.level = level;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +23,15 @@ 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(
Expand Down
Loading