-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,91 @@ | ||
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() | ||
Check warning on line 27 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
.setShaderState(RenderStateShard.RENDERTYPE_LINES_SHADER) | ||
Check warning on line 28 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
.setWriteMaskState(RenderStateShard.COLOR_WRITE) | ||
Check warning on line 29 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
.setCullState(RenderStateShard.NO_CULL) | ||
Check warning on line 30 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
.setDepthTestState(RenderStateShard.NO_DEPTH_TEST) | ||
Check warning on line 31 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
.setLayeringState(RenderStateShard.VIEW_OFFSET_Z_LAYERING) | ||
Check warning on line 32 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
.setLineState(new RenderStateShard.LineStateShard(OptionalDouble.of(2))) | ||
Check warning on line 33 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
.createCompositeState(true)); | ||
Check warning on line 34 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
public static final Map<Vector3ic, Long> SUBCHUNKS = new HashMap<>(); | ||
private static Level level = null; | ||
@Getter | ||
private static final Map<BlockPos, Integer> highlight = new ConcurrentHashMap<>(); | ||
|
||
|
||
/** | ||
* 高亮方块 | ||
* | ||
* @param level 维度 | ||
* @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()); | ||
Check warning on line 49 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
} | ||
|
||
/** | ||
* 获取被高亮的方块列表 | ||
* | ||
* @param level 维度 | ||
* @return 被高亮的方块列表 | ||
*/ | ||
public static Map<BlockPos, Integer> getHighlight(Level level) { | ||
if (BlockHighlightUtil.level != level) BlockHighlightUtil.highlight.clear(); | ||
return BlockHighlightUtil.highlight; | ||
@Environment(EnvType.CLIENT) | ||
Check warning on line 51 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java GitHub Actions / checkstyle
|
||
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; | ||
} | ||
} |