22
33import com .mojang .blaze3d .vertex .PoseStack ;
44import com .mojang .blaze3d .vertex .VertexConsumer ;
5+ import net .minecraft .core .BlockPos ;
56import net .minecraft .client .renderer .MultiBufferSource ;
67import net .minecraft .client .renderer .RenderType ;
78import net .minecraft .client .renderer .blockentity .BlockEntityRenderer ;
89import net .minecraft .client .renderer .blockentity .BlockEntityRendererProvider ;
910import net .minecraft .util .Mth ;
11+ import net .minecraft .world .level .Level ;
1012import net .minecraft .world .level .block .state .BlockState ;
1113import org .joml .Matrix4f ;
1214
@@ -24,26 +26,49 @@ public void render(PortalFrameBlockEntity blockEntity,
2426 int packedLight ,
2527 int packedOverlay ) {
2628 BlockState state = blockEntity .getBlockState ();
27- if (!state .hasProperty (PortalFrameBlock .ACTIVE ) || !state .getValue (PortalFrameBlock .ACTIVE )) return ;
28- if (blockEntity .getLevel () == null ) return ;
29- if (state .hasProperty (PortalFrameBlock .BLINK_ON ) && !state .getValue (PortalFrameBlock .BLINK_ON )) return ;
29+ Level level = blockEntity .getLevel ();
30+ if (level == null ) return ;
31+ if (!state .hasProperty (PortalFrameBlock .ACTIVE )) return ;
32+
33+ boolean active = state .getValue (PortalFrameBlock .ACTIVE );
34+ if (active && state .hasProperty (PortalFrameBlock .BLINK_ON ) && !state .getValue (PortalFrameBlock .BLINK_ON )) return ;
35+
36+ if (!active && !isPartOfCompletedPortal (level , blockEntity .getBlockPos ())) return ;
3037
3138 boolean unstable = state .hasProperty (PortalFrameBlock .UNSTABLE ) && state .getValue (PortalFrameBlock .UNSTABLE );
32- float time = blockEntity . getLevel () .getGameTime () + partialTick ;
39+ float time = level .getGameTime () + partialTick ;
3340 float pulse ;
3441 float half ;
3542 int alpha ;
36- if (unstable ) {
43+ int red ;
44+ int green ;
45+ int blue ;
46+
47+ if (!active ) {
48+ float phase = (blockEntity .getBlockPos ().asLong () & 31L ) * 0.21F ;
49+ pulse = 0.45F + 0.55F * Mth .sin (time * 0.1F + phase );
50+ half = 0.045F + 0.012F * pulse ;
51+ alpha = (int ) (36 + 42 * pulse );
52+ red = 110 ;
53+ green = 220 ;
54+ blue = 255 ;
55+ } else if (unstable ) {
3756 float phase = (blockEntity .getBlockPos ().asLong () & 15L ) * 0.47F ;
3857 float strobe = Mth .sin (time * 4.1F + phase ) > 0.1F ? 1.0F : 0.25F ;
3958 float wave = 0.5F + 0.5F * Mth .sin (time * 1.35F + phase * 0.6F );
4059 pulse = Mth .clamp (strobe * (0.55F + 0.45F * wave ), 0.0F , 1.0F );
4160 half = 0.06F + 0.045F * pulse ;
4261 alpha = (int ) (120 + 125 * pulse );
62+ red = 255 ;
63+ green = 166 ;
64+ blue = 108 ;
4365 } else {
4466 pulse = 0.72F + 0.28F * Mth .sin (time * 0.25F );
4567 half = 0.06F + 0.02F * pulse ;
4668 alpha = (int ) (110 + 80 * pulse );
69+ red = 150 ;
70+ green = 235 ;
71+ blue = 255 ;
4772 }
4873
4974 VertexConsumer consumer = buffer .getBuffer (RenderType .lightning ());
@@ -54,42 +79,67 @@ public void render(PortalFrameBlockEntity blockEntity,
5479 0.5F + half , 0.5F - half , -FACE_EPSILON ,
5580 0.5F + half , 0.5F + half , -FACE_EPSILON ,
5681 0.5F - half , 0.5F + half , -FACE_EPSILON ,
57- alpha );
82+ red , green , blue , alpha );
5883
5984 addQuad (consumer , matrix ,
6085 0.5F - half , 0.5F - half , 1.0F + FACE_EPSILON ,
6186 0.5F - half , 0.5F + half , 1.0F + FACE_EPSILON ,
6287 0.5F + half , 0.5F + half , 1.0F + FACE_EPSILON ,
6388 0.5F + half , 0.5F - half , 1.0F + FACE_EPSILON ,
64- alpha );
89+ red , green , blue , alpha );
6590
6691 addQuad (consumer , matrix ,
6792 -FACE_EPSILON , 0.5F - half , 0.5F - half ,
6893 -FACE_EPSILON , 0.5F - half , 0.5F + half ,
6994 -FACE_EPSILON , 0.5F + half , 0.5F + half ,
7095 -FACE_EPSILON , 0.5F + half , 0.5F - half ,
71- alpha );
96+ red , green , blue , alpha );
7297
7398 addQuad (consumer , matrix ,
7499 1.0F + FACE_EPSILON , 0.5F - half , 0.5F - half ,
75100 1.0F + FACE_EPSILON , 0.5F + half , 0.5F - half ,
76101 1.0F + FACE_EPSILON , 0.5F + half , 0.5F + half ,
77102 1.0F + FACE_EPSILON , 0.5F - half , 0.5F + half ,
78- alpha );
103+ red , green , blue , alpha );
79104
80105 addQuad (consumer , matrix ,
81106 0.5F - half , -FACE_EPSILON , 0.5F - half ,
82107 0.5F - half , -FACE_EPSILON , 0.5F + half ,
83108 0.5F + half , -FACE_EPSILON , 0.5F + half ,
84109 0.5F + half , -FACE_EPSILON , 0.5F - half ,
85- alpha );
110+ red , green , blue , alpha );
86111
87112 addQuad (consumer , matrix ,
88113 0.5F - half , 1.0F + FACE_EPSILON , 0.5F - half ,
89114 0.5F + half , 1.0F + FACE_EPSILON , 0.5F - half ,
90115 0.5F + half , 1.0F + FACE_EPSILON , 0.5F + half ,
91116 0.5F - half , 1.0F + FACE_EPSILON , 0.5F + half ,
92- alpha );
117+ red , green , blue , alpha );
118+ }
119+
120+ private static boolean isPartOfCompletedPortal (Level level , BlockPos framePos ) {
121+ BlockPos corePos = findCoreNear (level , framePos , 4 , 5 );
122+ if (corePos == null ) return false ;
123+
124+ var match = PortalFrameDetector .find (level , corePos );
125+ if (match .isEmpty ()) return false ;
126+
127+ for (BlockPos pos : PortalFrameHelper .collectFrame (match .get (), corePos )) {
128+ if (pos .equals (framePos )) return true ;
129+ }
130+ return false ;
131+ }
132+
133+ private static BlockPos findCoreNear (Level level , BlockPos center , int rXZ , int rY ) {
134+ for (int dy = -rY ; dy <= rY ; dy ++) {
135+ for (int dx = -rXZ ; dx <= rXZ ; dx ++) {
136+ for (int dz = -rXZ ; dz <= rXZ ; dz ++) {
137+ BlockPos p = center .offset (dx , dy , dz );
138+ if (level .getBlockState (p ).is (ModBlocks .PORTAL_CORE )) return p ;
139+ }
140+ }
141+ }
142+ return null ;
93143 }
94144
95145 private static void addQuad (VertexConsumer consumer ,
@@ -106,15 +156,18 @@ private static void addQuad(VertexConsumer consumer,
106156 float x4 ,
107157 float y4 ,
108158 float z4 ,
159+ int red ,
160+ int green ,
161+ int blue ,
109162 int alpha ) {
110- consumer .addVertex (matrix , x1 , y1 , z1 ).setColor (255 , 255 , 255 , alpha );
111- consumer .addVertex (matrix , x2 , y2 , z2 ).setColor (255 , 255 , 255 , alpha );
112- consumer .addVertex (matrix , x3 , y3 , z3 ).setColor (255 , 255 , 255 , alpha );
113- consumer .addVertex (matrix , x4 , y4 , z4 ).setColor (255 , 255 , 255 , alpha );
114-
115- consumer .addVertex (matrix , x4 , y4 , z4 ).setColor (255 , 255 , 255 , alpha );
116- consumer .addVertex (matrix , x3 , y3 , z3 ).setColor (255 , 255 , 255 , alpha );
117- consumer .addVertex (matrix , x2 , y2 , z2 ).setColor (255 , 255 , 255 , alpha );
118- consumer .addVertex (matrix , x1 , y1 , z1 ).setColor (255 , 255 , 255 , alpha );
163+ consumer .addVertex (matrix , x1 , y1 , z1 ).setColor (red , green , blue , alpha );
164+ consumer .addVertex (matrix , x2 , y2 , z2 ).setColor (red , green , blue , alpha );
165+ consumer .addVertex (matrix , x3 , y3 , z3 ).setColor (red , green , blue , alpha );
166+ consumer .addVertex (matrix , x4 , y4 , z4 ).setColor (red , green , blue , alpha );
167+
168+ consumer .addVertex (matrix , x4 , y4 , z4 ).setColor (red , green , blue , alpha );
169+ consumer .addVertex (matrix , x3 , y3 , z3 ).setColor (red , green , blue , alpha );
170+ consumer .addVertex (matrix , x2 , y2 , z2 ).setColor (red , green , blue , alpha );
171+ consumer .addVertex (matrix , x1 , y1 , z1 ).setColor (red , green , blue , alpha );
119172 }
120173}
0 commit comments