-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolved #293
- Loading branch information
Showing
7 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
common/src/main/java/dev/dubhe/anvilcraft/client/renderer/PowerGridRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package dev.dubhe.anvilcraft.client.renderer; | ||
|
||
import com.mojang.blaze3d.vertex.PoseStack; | ||
import com.mojang.blaze3d.vertex.VertexConsumer; | ||
import dev.dubhe.anvilcraft.api.power.PowerGrid; | ||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.util.Mth; | ||
import net.minecraft.util.RandomSource; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@Environment(EnvType.CLIENT) | ||
public class PowerGridRenderer { | ||
/** | ||
* 渲染 | ||
*/ | ||
public static void render(PoseStack poseStack, VertexConsumer consumer, double camX, double camY, double camZ) { | ||
if (Minecraft.getInstance().level == null) return; | ||
RandomSource random = Minecraft.getInstance().level.random; | ||
for (PowerGrid grid : PowerGrid.GRID_SET) { | ||
random.setSeed(grid.hashCode()); | ||
PowerGridRenderer.renderOutline( | ||
poseStack, consumer, camX, camY, camZ, | ||
grid.getPos(), grid.getRange(), | ||
random.nextFloat(), random.nextFloat(), random.nextFloat(), 0.4f | ||
); | ||
} | ||
} | ||
|
||
@SuppressWarnings("SameParameterValue") | ||
private static void renderOutline( | ||
PoseStack poseStack, VertexConsumer consumer, | ||
double camX, double camY, double camZ, @NotNull BlockPos pos, @NotNull VoxelShape shape, | ||
float red, float green, float blue, float alpha | ||
) { | ||
PowerGridRenderer.renderShape( | ||
poseStack, consumer, shape, | ||
(double) pos.getX() - camX, (double) pos.getY() - camY, (double) pos.getZ() - camZ, | ||
red, green, blue, alpha | ||
); | ||
} | ||
|
||
private static void renderShape( | ||
@NotNull PoseStack poseStack, VertexConsumer consumer, @NotNull VoxelShape shape, | ||
double x, double y, double z, float red, float green, float blue, float alpha | ||
) { | ||
PoseStack.Pose pose = poseStack.last(); | ||
shape.forAllEdges((minX, minY, minZ, maxX, maxY, maxZ) -> { | ||
float k = (float) (maxX - minX); | ||
float l = (float) (maxY - minY); | ||
float m = (float) (maxZ - minZ); | ||
float n = Mth.sqrt(k * k + l * l + m * m); | ||
consumer.vertex(pose.pose(), (float) (minX + x), (float) (minY + y), (float) (minZ + z)) | ||
.color(red, green, blue, alpha) | ||
.normal(pose.normal(), k /= n, l /= n, m /= n) | ||
.endVertex(); | ||
consumer.vertex(pose.pose(), (float) (maxX + x), (float) (maxY + y), (float) (maxZ + z)) | ||
.color(red, green, blue, alpha) | ||
.normal(pose.normal(), k, l, m) | ||
.endVertex(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
common/src/main/java/dev/dubhe/anvilcraft/mixin/LevelRendererMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.dubhe.anvilcraft.mixin; | ||
|
||
import com.mojang.blaze3d.vertex.PoseStack; | ||
import com.mojang.blaze3d.vertex.VertexConsumer; | ||
import dev.dubhe.anvilcraft.client.renderer.PowerGridRenderer; | ||
import dev.dubhe.anvilcraft.init.ModItems; | ||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.minecraft.client.Camera; | ||
import net.minecraft.client.renderer.GameRenderer; | ||
import net.minecraft.client.renderer.LevelRenderer; | ||
import net.minecraft.client.renderer.LightTexture; | ||
import net.minecraft.client.renderer.MultiBufferSource; | ||
import net.minecraft.client.renderer.RenderBuffers; | ||
import net.minecraft.client.renderer.RenderType; | ||
import net.minecraft.util.profiling.ProfilerFiller; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.phys.Vec3; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.joml.Matrix4f; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
@Mixin(LevelRenderer.class) | ||
@Environment(EnvType.CLIENT) | ||
public abstract class LevelRendererMixin { | ||
@Shadow | ||
@Final | ||
private RenderBuffers renderBuffers; | ||
|
||
@Inject( | ||
method = "renderLevel", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/client/renderer/debug/DebugRenderer;render(Lcom/mojang/blaze3d/vertex/PoseStack;" | ||
+ "Lnet/minecraft/client/renderer/MultiBufferSource$BufferSource;DDD)V" | ||
), | ||
locals = LocalCapture.CAPTURE_FAILEXCEPTION | ||
) | ||
private void renderLevel( | ||
PoseStack poseStack, float partialTick, long finishNanoTime, | ||
boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, | ||
LightTexture lightTexture, Matrix4f projectionMatrix, CallbackInfo ci, | ||
@NotNull ProfilerFiller profilerFiller, @NotNull Vec3 vec3 | ||
) { | ||
Entity entity = camera.getEntity(); | ||
boolean bl = true; | ||
for (ItemStack slot : entity.getArmorSlots()) { | ||
if (slot.is(ModItems.ANVIL_HAMMER.get())) { | ||
bl = false; | ||
break; | ||
} | ||
} | ||
if (bl) return; | ||
MultiBufferSource.BufferSource bufferSource = this.renderBuffers.bufferSource(); | ||
VertexConsumer vertexConsumer3 = bufferSource.getBuffer(RenderType.lines()); | ||
double camX = vec3.x(); | ||
double camY = vec3.y(); | ||
double camZ = vec3.z(); | ||
profilerFiller.popPush("grid"); | ||
PowerGridRenderer.render(poseStack, vertexConsumer3, camX, camY, camZ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"SolidBucketItemMixin" | ||
], | ||
"client": [ | ||
"LevelRendererMixin" | ||
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
|