1212import dev .compactmods .crafting .data .CCAttachments ;
1313import dev .compactmods .crafting .projector .EnumProjectorColorType ;
1414import dev .compactmods .crafting .projector .FieldProjectorEntity ;
15+ import net .minecraft .client .Camera ;
1516import net .minecraft .client .Minecraft ;
1617import net .minecraft .client .renderer .MultiBufferSource ;
1718import net .minecraft .client .renderer .RenderType ;
1819import net .minecraft .core .Direction ;
1920import net .minecraft .util .FastColor ;
20- import net .minecraft .world .level .block . Blocks ;
21+ import net .minecraft .world .level .Level ;
2122import net .minecraft .world .phys .AABB ;
2223import net .minecraft .world .phys .BlockHitResult ;
2324import net .minecraft .world .phys .HitResult ;
2425import net .minecraft .world .phys .Vec3 ;
2526import net .neoforged .neoforge .client .event .RenderLevelStageEvent ;
2627import org .joml .Matrix4f ;
2728
29+ import java .util .Objects ;
30+
2831public class MiniaturizationFieldRenderer {
2932
3033 public static void onRenderStage (RenderLevelStageEvent evt ) {
31- if (evt .getStage () == RenderLevelStageEvent .Stage .AFTER_TRANSLUCENT_BLOCKS ) {
34+ if (evt .getStage () == RenderLevelStageEvent .Stage .AFTER_TRANSLUCENT_BLOCKS ) {
3235
3336 final var mc = Minecraft .getInstance ();
3437 final var level = mc .level ;
3538
39+ if (level == null ) return ;
40+
3641 final var partialTicks = evt .getPartialTick ().getGameTimeDeltaPartialTick (false );
3742 final MultiBufferSource .BufferSource buffers = mc .renderBuffers ().bufferSource ();
3843
3944 level .getExistingData (CCAttachments .ACTIVE_FIELDS ).ifPresent (fields -> {
4045 fields .getFields ().forEach (field -> {
41- render (field , partialTicks , evt .getPoseStack ());
46+ render (level , field , partialTicks , evt .getPoseStack (), buffers );
4247 });
4348 });
4449 }
4550 }
4651
47- public static void render (IMiniaturizationField field , float partialTicks , PoseStack matrixStack ) {
48- GhostRenderer .render (Blocks .GREEN_CONCRETE .defaultBlockState (), field .getCenter (), matrixStack );
52+ public static void render (Level level , IMiniaturizationField field , float partialTicks , PoseStack matrixStack , MultiBufferSource .BufferSource buffers ) {
53+ // GhostRenderer.render(Blocks.GREEN_STAINED_GLASS.defaultBlockState(), field.getCenter(), matrixStack);
54+ final Minecraft mc = Minecraft .getInstance ();
55+ final Camera mainCamera = mc .gameRenderer .getMainCamera ();
56+ Vec3 projectedView = mainCamera .getPosition ();
57+
58+ matrixStack .pushPose ();
59+ {
60+ matrixStack .translate (-projectedView .x , -projectedView .y , -projectedView .z );
61+ field .getProjectorPositions ()
62+ .map (level ::getBlockEntity )
63+ .map (be -> be instanceof FieldProjectorEntity fpe ? fpe : null )
64+ .filter (Objects ::nonNull )
65+ .forEach (fieldProjectorEntity -> {
66+ drawScanLine (fieldProjectorEntity , matrixStack , buffers , field .getBounds (), level .getGameTime ());
67+ drawProjectorArcs (fieldProjectorEntity , matrixStack , buffers , field .getBounds (), level .getGameTime ());
68+ });
69+
70+ buffers .endBatch (RenderType .lines ());
71+ }
72+ matrixStack .popPose ();
4973 }
5074
5175 public static int getProjectionColor (EnumProjectorColorType type ) {
@@ -67,51 +91,34 @@ public static int getProjectionColor(EnumProjectorColorType type) {
6791 private static void drawScanLine (FieldProjectorEntity tile , PoseStack mx , MultiBufferSource buffers , AABB fieldBounds , double gameTime ) {
6892 VertexConsumer builder = buffers .getBuffer (RenderType .lines ());
6993
70- Vec3 tilePos = new Vec3 (
71- tile .getBlockPos ().getX () + 0.5d ,
72- tile .getBlockPos ().getY () + 0.5d ,
73- tile .getBlockPos ().getZ () + 0.5d
74- );
75-
7694 mx .pushPose ();
77- mx .translate (.5 , .5 , .5 );
7895
7996 int colorScanLine = getProjectionColor (EnumProjectorColorType .SCAN_LINE );
8097
8198 Direction face = tile .getProjectorSide ();
82- Vec3 left = CubeRenderHelper .getScanLineLeft (face , fieldBounds , gameTime ).subtract (tilePos );
83- Vec3 right = CubeRenderHelper .getScanLineRight (face , fieldBounds , gameTime ).subtract (tilePos );
84-
85- CubeRenderHelper .addColoredVertex (builder , mx , colorScanLine , left );
86- CubeRenderHelper .addColoredVertex (builder , mx , colorScanLine , right );
99+ Vec3 left = CubeRenderHelper .getScanLineLeft (face , fieldBounds , gameTime );
100+ Vec3 right = CubeRenderHelper .getScanLineRight (face , fieldBounds , gameTime );
87101
102+ CubeRenderHelper .drawLine (builder , mx , colorScanLine , left , right );
88103 mx .popPose ();
89104 }
90105
91106 /**
92107 * Handles drawing the projection arcs that connect the projector blocks to the main projection
93108 * in the center of the crafting area.
94109 */
95- private void drawProjectorArcs (FieldProjectorEntity tile , PoseStack mx , MultiBufferSource buffers , AABB fieldBounds , double gameTime ) {
110+ private static void drawProjectorArcs (FieldProjectorEntity tile , PoseStack mx , MultiBufferSource buffers , AABB fieldBounds , double gameTime ) {
96111
97112 try {
98113
99114 Direction facing = tile .getProjectorSide ();
100115
101- Vec3 tilePos = new Vec3 (
102- tile .getBlockPos ().getX () + 0.5d ,
103- tile .getBlockPos ().getY () + 0.5d ,
104- tile .getBlockPos ().getZ () + 0.5d
105- );
106-
107116 mx .pushPose ();
108117
109- mx .translate (.5 , .5 , .5 );
110-
111118 int colorProjectionArc = getProjectionColor (EnumProjectorColorType .FIELD );
112119
113- Vec3 scanLeft = CubeRenderHelper .getScanLineRight (facing , fieldBounds , gameTime ). subtract ( tilePos ) ;
114- Vec3 scanRight = CubeRenderHelper .getScanLineLeft (facing , fieldBounds , gameTime ). subtract ( tilePos ) ;
120+ Vec3 scanLeft = CubeRenderHelper .getScanLineRight (facing , fieldBounds , gameTime );
121+ Vec3 scanRight = CubeRenderHelper .getScanLineLeft (facing , fieldBounds , gameTime );
115122
116123 // 0, 0, 0 is now the edge of the projector's space
117124 final Matrix4f p = mx .last ().pose ();
@@ -136,9 +143,7 @@ private void drawProjectorArcs(FieldProjectorEntity tile, PoseStack mx, MultiBuf
136143 .setNormal (n , 0 , 0 , 0 );
137144
138145 mx .popPose ();
139- }
140-
141- catch (Exception ex ) {
146+ } catch (Exception ex ) {
142147 CompactCrafting .LOGGER .error (ex );
143148 }
144149 }
0 commit comments