@@ -27,11 +27,15 @@ public class RenderRegion implements BlockAndTintGetter {
27
27
public static final int WIDTH = 3 ;
28
28
public static final int SIZE = WIDTH * WIDTH * WIDTH ;
29
29
30
- public static final int REGION_WIDTH = 16 + 2 ;
31
- public static final int BLOCKS = REGION_WIDTH * REGION_WIDTH * REGION_WIDTH ;
30
+ public static final int BOUNDARY_BLOCK_WIDTH = 2 ;
31
+ public static final int REGION_BLOCK_WIDTH = 16 + BOUNDARY_BLOCK_WIDTH * 2 ;
32
+ public static final int BLOCK_COUNT = REGION_BLOCK_WIDTH * REGION_BLOCK_WIDTH * REGION_BLOCK_WIDTH ;
33
+
34
+ public static final BlockState AIR_BLOCK_STATE = Blocks .AIR .defaultBlockState ();
32
35
33
36
private final int minSecX , minSecY , minSecZ ;
34
37
private final int minX , minY , minZ ;
38
+ private final int maxX , maxY , maxZ ;
35
39
private final Level level ;
36
40
private final int blendRadius ;
37
41
@@ -51,49 +55,48 @@ public class RenderRegion implements BlockAndTintGetter {
51
55
this .minSecX = x - 1 ;
52
56
this .minSecY = y - 1 ;
53
57
this .minSecZ = z - 1 ;
54
- this .minX = (minSecX << 4 ) + 15 ;
55
- this .minZ = (minSecZ << 4 ) + 15 ;
56
- this .minY = (minSecY << 4 ) + 15 ;
58
+ this .minX = (minSecX << 4 ) + 16 - BOUNDARY_BLOCK_WIDTH ;
59
+ this .minZ = (minSecZ << 4 ) + 16 - BOUNDARY_BLOCK_WIDTH ;
60
+ this .minY = (minSecY << 4 ) + 16 - BOUNDARY_BLOCK_WIDTH ;
61
+ this .maxX = minX + REGION_BLOCK_WIDTH ;
62
+ this .maxZ = minZ + REGION_BLOCK_WIDTH ;
63
+ this .maxY = minY + REGION_BLOCK_WIDTH ;
57
64
58
65
this .blockDataContainers = blockData ;
59
66
this .lightData = lightData ;
60
67
this .blockEntityMap = blockEntityMap ;
61
68
62
- this .blockData = new BlockState [BLOCKS ];
69
+ this .blockData = new BlockState [BLOCK_COUNT ];
63
70
64
71
this .blockStateGetter = level .isDebug () ? this ::debugBlockState : this ::defaultBlockState ;
65
72
66
73
this .blendRadius = Minecraft .getInstance ().options .biomeBlendRadius ().get ();
67
74
}
68
75
69
76
public void loadBlockStates () {
70
- int maxSecX = minSecX + 2 ;
71
- int maxSecY = minSecY + 2 ;
72
- int maxSecZ = minSecZ + 2 ;
73
-
74
- int maxX = (maxSecX << 4 );
75
- int maxZ = (maxSecZ << 4 );
76
- int maxY = (maxSecY << 4 );
77
-
78
77
Arrays .fill (blockData , Blocks .AIR .defaultBlockState ());
79
78
80
- for (int x = minSecX ; x <= maxSecX ; ++x ) {
81
- for (int z = minSecZ ; z <= maxSecZ ; ++z ) {
82
- for (int y = minSecY ; y <= maxSecY ; ++y ) {
83
- final int idx = getSectionIdx (( x - minSecX ), ( y - minSecY ), ( z - minSecZ ) );
79
+ for (int x = 0 ; x <= 2 ; ++x ) {
80
+ for (int z = 0 ; z <= 2 ; ++z ) {
81
+ for (int y = 0 ; y <= 2 ; ++y ) {
82
+ final int idx = getSectionIdx (x , y , z );
84
83
85
84
PalettedContainer <BlockState > container = blockDataContainers [idx ];
86
85
87
86
if (container == null )
88
87
continue ;
89
88
90
- int tMinX = Math . max ( minX , x << 4 ) ;
91
- int tMinY = Math . max ( minY , y << 4 ) ;
92
- int tMinZ = Math . max ( minZ , z << 4 ) ;
89
+ int absBlockX = ( x + minSecX ) << 4 ;
90
+ int absBlockY = ( y + minSecY ) << 4 ;
91
+ int absBlockZ = ( z + minSecZ ) << 4 ;
93
92
94
- int tMaxX = Math .min (maxX , (x << 4 ) + 15 );
95
- int tMaxY = Math .min (maxY , (y << 4 ) + 15 );
96
- int tMaxZ = Math .min (maxZ , (z << 4 ) + 15 );
93
+ int tMinX = Math .max (minX , absBlockX );
94
+ int tMinY = Math .max (minY , absBlockY );
95
+ int tMinZ = Math .max (minZ , absBlockZ );
96
+
97
+ int tMaxX = Math .min (maxX , absBlockX + 16 );
98
+ int tMaxY = Math .min (maxY , absBlockY + 16 );
99
+ int tMaxZ = Math .min (maxZ , absBlockZ + 16 );
97
100
98
101
loadSectionBlockStates (container , blockData ,
99
102
tMinX , tMinY , tMinZ , tMaxX , tMaxY , tMaxZ );
@@ -106,10 +109,10 @@ public void loadBlockStates() {
106
109
void loadSectionBlockStates (PalettedContainer <BlockState > container , BlockState [] blockStates ,
107
110
int minX , int minY , int minZ , int maxX , int maxY , int maxZ ) {
108
111
109
- for (int x = minX ; x <= maxX ; ++x ) {
110
- for (int z = minZ ; z <= maxZ ; ++z ) {
111
- for (int y = minY ; y <= maxY ; ++y ) {
112
- final int idx = getBlockIdx (x , y , z );
112
+ for (int y = minY ; y < maxY ; ++y ) {
113
+ for (int z = minZ ; z < maxZ ; ++z ) {
114
+ for (int x = minX ; x < maxX ; ++x ) {
115
+ final int idx = getBlockIdx (x - this . minX , y - this . minY , z - this . minZ );
113
116
114
117
blockStates [idx ] = container != null ?
115
118
container .get (x & 15 , y & 15 , z & 15 )
@@ -129,7 +132,7 @@ public BlockState getBlockState(BlockPos blockPos) {
129
132
}
130
133
131
134
public FluidState getFluidState (BlockPos blockPos ) {
132
- return blockStateGetter . apply (blockPos ).getFluidState ();
135
+ return this . getBlockState (blockPos ).getFluidState ();
133
136
}
134
137
135
138
public float getShade (Direction direction , boolean bl ) {
@@ -141,16 +144,23 @@ public LevelLightEngine getLightEngine() {
141
144
}
142
145
143
146
public int getBrightness (LightLayer lightLayer , BlockPos blockPos ) {
147
+ if (outsideRegion (blockPos .getX (), blockPos .getY (), blockPos .getZ ())) {
148
+ return 0 ;
149
+ }
150
+
144
151
int secX = SectionPos .blockToSectionCoord (blockPos .getX ()) - this .minSecX ;
145
152
int secY = SectionPos .blockToSectionCoord (blockPos .getY ()) - this .minSecY ;
146
153
int secZ = SectionPos .blockToSectionCoord (blockPos .getZ ()) - this .minSecZ ;
147
154
148
155
DataLayer dataLayer = this .lightData [getSectionIdx (secX , secY , secZ )][lightLayer .ordinal ()];
149
156
return dataLayer == null ? 0 : dataLayer .get (blockPos .getX () & 15 , blockPos .getY () & 15 , blockPos .getZ () & 15 );
150
-
151
157
}
152
158
153
159
public int getRawBrightness (BlockPos blockPos , int i ) {
160
+ if (outsideRegion (blockPos .getX (), blockPos .getY (), blockPos .getZ ())) {
161
+ return 0 ;
162
+ }
163
+
154
164
int secX = SectionPos .blockToSectionCoord (blockPos .getX ()) - this .minSecX ;
155
165
int secY = SectionPos .blockToSectionCoord (blockPos .getY ()) - this .minSecY ;
156
166
int secZ = SectionPos .blockToSectionCoord (blockPos .getZ ()) - this .minSecZ ;
@@ -190,14 +200,27 @@ public int getSectionIdx(int secX, int secY, int secZ) {
190
200
}
191
201
192
202
public int getBlockIdx (int x , int y , int z ) {
203
+ return REGION_BLOCK_WIDTH * ((REGION_BLOCK_WIDTH * y ) + z ) + x ;
204
+ }
205
+
206
+ public boolean outsideRegion (int x , int y , int z ) {
207
+ return x < minX || x >= maxX || y < minY || y >= maxY || z < minZ || z >= maxZ ;
208
+ }
209
+
210
+ public BlockState defaultBlockState (BlockPos blockPos ) {
211
+ int x = blockPos .getX ();
212
+ int y = blockPos .getY ();
213
+ int z = blockPos .getZ ();
214
+
215
+ if (outsideRegion (x , y , z )) {
216
+ return AIR_BLOCK_STATE ;
217
+ }
218
+
193
219
x -= minX ;
194
220
y -= minY ;
195
221
z -= minZ ;
196
- return REGION_WIDTH * ((REGION_WIDTH * y ) + z ) + x ;
197
- }
198
222
199
- public BlockState defaultBlockState (BlockPos blockPos ) {
200
- return blockData [getBlockIdx (blockPos .getX (), blockPos .getY (), blockPos .getZ ())];
223
+ return blockData [getBlockIdx (x , y , z )];
201
224
}
202
225
203
226
public BlockState debugBlockState (BlockPos blockPos ) {
0 commit comments