1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
package makamys.lodmod.renderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.EntityFX;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.gen.ChunkProviderServer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.lang3.ArrayUtils;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Matrix4f;
import makamys.lodmod.ducks.IWorldRenderer;
import makamys.lodmod.util.Util;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL14.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
public class LODRenderer {
private boolean hasInited = false;
private boolean[] wasDown = new boolean[256];
private int renderQuads = 0;
public boolean renderWorld = true;
public boolean renderLOD = true;
private static int BUFFER_SIZE = 1024 * 1024 * 1024;
private static int MAX_MESHES = 100000;
private int VAO, VBO, shaderProgram;
private IntBuffer piFirst, piCount;
List<Chunk> myChunks = new ArrayList<Chunk>();
List<LODChunk> pendingLODChunks = new ArrayList<>();
private boolean hasServerInited = false;
private Map<ChunkCoordIntPair, LODRegion> loadedRegionsMap = new HashMap<>();
private List<Mesh> sentMeshes = new ArrayList<>();
// TODO make these packets to make this work on dedicated servers
Queue<Chunk> farChunks = new ConcurrentLinkedQueue<>();
List<ChunkCoordIntPair> serverChunkLoadQueue = new ArrayList<>();
private double lastSortX = Double.NaN;
private double lastSortY = Double.NaN;
private double lastSortZ = Double.NaN;
private long lastGCTime = -1;
private long gcInterval = 60 * 1000;
public LODRenderer(){
hasInited = init();
}
public void beforeRenderTerrain() {
if(hasInited) {
mainLoop();
handleKeyboard();
gcInterval = 10 * 1000;
if(lastGCTime == -1 || (System.currentTimeMillis() - lastGCTime) > gcInterval) {
runGC();
lastGCTime = System.currentTimeMillis();
}
if(renderLOD) {
render();
}
}
}
private void mainLoop() {
while(!farChunks.isEmpty()) {
LODChunk lodChunk = receiveFarChunk(farChunks.remove());
sendChunkToGPU(lodChunk);
}
List<Object> players = Minecraft.getMinecraft().getIntegratedServer().getConfigurationManager().playerEntityList;
if(!players.isEmpty()) {
Entity player = (Entity)players.get(0);
List<ChunkCoordIntPair> newServerChunkLoadQueue = new ArrayList<>();
if(Double.isNaN(lastSortX) || getLastSortDistanceSq(player) > 16 * 16) {
int centerX = (int)Math.floor(player.posX / 16.0);
int centerZ = (int)Math.floor(player.posZ / 16.0);
int range = 64;
for(int x = -range; x <= range; x++) {
for(int z = -range; z <= range; z++) {
int chunkX = centerX + x;
int chunkZ = centerZ + z;
if(getLODChunk(chunkX, chunkZ).chunk == null) {
newServerChunkLoadQueue.add(new ChunkCoordIntPair(chunkX, chunkZ));
}
}
}
Collections.sort(newServerChunkLoadQueue, new ChunkCoordDistanceComparator(player));
setServerChunkLoadQueue(newServerChunkLoadQueue);
lastSortX = player.posX;
lastSortY = player.posY;
lastSortZ = player.posZ;
loadedRegionsMap.forEach((k, v) -> v.tick(player));
}
}
}
private void handleKeyboard() {
if(Keyboard.isKeyDown(Keyboard.KEY_F) && !wasDown[Keyboard.KEY_F]) {
renderLOD = !renderLOD;
}
if(Keyboard.isKeyDown(Keyboard.KEY_V) && !wasDown[Keyboard.KEY_V]) {
renderWorld = !renderWorld;
}
for(int i = 0; i < 256; i++) {
wasDown[i] = Keyboard.isKeyDown(i);
}
}
private void runGC() {
nextMeshOffset = 0;
nextTri = 0;
nextMesh = 0;
piFirst.limit(0);
piCount.limit(0);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
int deletedNum = 0;
int deletedRAM = 0;
for(Iterator<Mesh> it = sentMeshes.iterator(); it.hasNext(); ) {
Mesh mesh = it.next();
if(!mesh.pendingGPUDelete) {
if(mesh.offset != nextMeshOffset) {
glBufferSubData(GL_ARRAY_BUFFER, nextMeshOffset, mesh.buffer);
}
mesh.iFirst = nextTri;
mesh.offset = nextMeshOffset;
nextMeshOffset += mesh.buffer.limit();
nextTri += mesh.quadCount * 6;
piFirst.limit(piFirst.limit() + 1);
piFirst.put(nextMesh, mesh.iFirst);
piCount.limit(piCount.limit() + 1);
piCount.put(nextMesh, mesh.iCount);
nextMesh++;
} else {
mesh.iFirst = mesh.offset = -1;
mesh.visible = false;
mesh.pendingGPUDelete = false;
it.remove();
deletedNum++;
deletedRAM += mesh.buffer.limit();
}
}
System.out.println("Deleted " + deletedNum + " meshes, freeing up " + (deletedRAM / 1024 / 1024) + "MB of VRAM");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
private void render() {
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(shaderProgram);
int u_modelView = glGetUniformLocation(shaderProgram, "modelView");
int u_proj = glGetUniformLocation(shaderProgram, "proj");
int u_playerPos = glGetUniformLocation(shaderProgram, "playerPos");
int u_light = glGetUniformLocation(shaderProgram, "lightTex");
int u_viewport = glGetUniformLocation(shaderProgram, "viewport");
int u_projInv = glGetUniformLocation(shaderProgram, "projInv");
int u_fogColor = glGetUniformLocation(shaderProgram, "fogColor");
int u_fogStartEnd = glGetUniformLocation(shaderProgram, "fogStartEnd");
if(false && (u_modelView == -1 || u_proj == -1 || u_playerPos == -1 || u_light == -1 || u_viewport == -1 || u_projInv == -1 || u_fogColor == -1 || u_fogStartEnd == -1)) {
System.out.println("failed to get the uniform");
} else {
FloatBuffer modelView = BufferUtils.createFloatBuffer(16);
glGetFloat(GL_MODELVIEW_MATRIX, modelView);
FloatBuffer projBuf = BufferUtils.createFloatBuffer(16);
glGetFloat(GL_PROJECTION_MATRIX, projBuf);
IntBuffer viewportBuf = BufferUtils.createIntBuffer(16);
glGetInteger(GL_VIEWPORT, viewportBuf);
viewportBuf.limit(4);
FloatBuffer projInvBuf = BufferUtils.createFloatBuffer(16);
Matrix4f m = new Matrix4f();
m.load(projBuf);
projBuf.flip();
m.invert();
m.store(projInvBuf);
projInvBuf.flip();
FloatBuffer fogColorBuf = BufferUtils.createFloatBuffer(16);
glGetFloat(GL_FOG_COLOR, fogColorBuf);
fogColorBuf.limit(4);
FloatBuffer fogStartEnd = BufferUtils.createFloatBuffer(2);
fogStartEnd.put(glGetFloat(GL_FOG_START));
fogStartEnd.put(glGetFloat(GL_FOG_END));
fogStartEnd.flip();
glUniformMatrix4(u_modelView, false, modelView);
glUniformMatrix4(u_proj, false, projBuf);
glUniformMatrix4(u_projInv, false, projInvBuf);
glUniform4f(u_viewport, viewportBuf.get(0),viewportBuf.get(1),viewportBuf.get(2),viewportBuf.get(3));
glUniform4(u_fogColor, fogColorBuf);
glUniform2(u_fogStartEnd, fogStartEnd);
float originX = 0;
float originY = 0;
float originZ = 0;
glUniform3f(u_playerPos, (float)EntityFX.interpPosX - originX, (float)EntityFX.interpPosY - originY, (float)EntityFX.interpPosZ - originZ);
glUniform1i(u_light, 1);
}
glBindVertexArray(VAO);
glMultiDrawArrays(GL_TRIANGLES, piFirst, piCount);
glBindVertexArray(0);
glUseProgram(0);
GL11.glDepthMask(true);
GL11.glPopAttrib();
}
public boolean init() {
Map<String, TextureAtlasSprite> uploadedSprites = ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites;
int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, Util.readFile("shaders/chunk.vert"));
glCompileShader(vertexShader);
if(glGetShaderi(vertexShader, GL_COMPILE_STATUS) == 0) {
System.out.println("Error compiling vertex shader: " + glGetShaderInfoLog(vertexShader, 256));
}
int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, Util.readFile("shaders/chunk.frag"));
glCompileShader(fragmentShader);
if(glGetShaderi(fragmentShader, GL_COMPILE_STATUS) == 0) {
System.out.println("Error compiling fragment shader: " + glGetShaderInfoLog(fragmentShader, 256));
}
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
if(glGetProgrami(shaderProgram, GL_LINK_STATUS) == 0) {
System.out.println("Error linking shader: " + glGetShaderInfoLog(shaderProgram, 256));
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
VAO = glGenVertexArrays();
glBindVertexArray(VAO);
VBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, BUFFER_SIZE, GL_STATIC_DRAW);
int stride = 7 * 4;
glVertexAttribPointer(0, 3, GL_FLOAT, false, stride, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, false, stride, 3 * 4);
glVertexAttribPointer(2, 2, GL_SHORT, false, stride, 5 * 4);
glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, false, stride, 6 * 4);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
piFirst = BufferUtils.createIntBuffer(MAX_MESHES);
piFirst.flip();
piCount = BufferUtils.createIntBuffer(MAX_MESHES);
piCount.flip();
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return true;
}
public void destroy() {
onSave();
glDeleteProgram(shaderProgram);
glDeleteVertexArrays(VAO);
glDeleteBuffers(VBO);
SimpleChunkMesh.instances = 0;
SimpleChunkMesh.usedRAM = 0;
ChunkMesh.instances = 0;
ChunkMesh.usedRAM = 0;
}
public void onWorldRendererPost(WorldRenderer wr) {
LODChunk lodChunk = getLODChunk(Math.floorDiv(wr.posX, 16), Math.floorDiv(wr.posZ, 16));
lodChunk.putChunkMeshes(Math.floorDiv(wr.posY, 16), ((IWorldRenderer)wr).getChunkMeshes());
//setVisible(lodChunk, false);
}
public void onWorldRendererFrustumChange(WorldRenderer wr, boolean inFrustum) {
if(inFrustum) {
LODChunk lodChunk = getLODChunk(Math.floorDiv(wr.posX, 16), Math.floorDiv(wr.posZ, 16));
setVisible(lodChunk, false);
}
}
public void onDontDraw(WorldRenderer wr) {
int chunkX = Math.floorDiv(wr.posX, 16);
int chunkY = Math.floorDiv(wr.posY, 16);
int chunkZ = Math.floorDiv(wr.posZ, 16);
Entity player = (Entity)Minecraft.getMinecraft().getIntegratedServer().getConfigurationManager().playerEntityList.get(0);
LODChunk lodChunk = getLODChunk(chunkX, chunkZ);
if(lodChunk.hasChunkMeshes()) {
setLOD(lodChunk, 2);//(lodChunk.distSq(player) < (16 * 16 * 16 * 16)) ? 1 : 2);
}
setVisible(lodChunk, true);
}
private double getLastSortDistanceSq(Entity player) {
return Math.pow(lastSortX - player.posX, 2) + Math.pow(lastSortZ - player.posZ, 2);
}
private synchronized void setServerChunkLoadQueue(List<ChunkCoordIntPair> coords) {
serverChunkLoadQueue = coords;
}
private LODChunk receiveFarChunk(Chunk chunk) {
LODRegion region = getRegionContaining(chunk.xPosition, chunk.zPosition);
myChunks.add(chunk);
return region.putChunk(chunk);
}
private LODChunk getLODChunk(int chunkX, int chunkZ) {
return getRegionContaining(chunkX, chunkZ).getChunkAbsolute(chunkX, chunkZ);
}
public void onStopServer() {
}
public synchronized void serverTick() {
int chunkLoadsRemaining = 64;
while(!serverChunkLoadQueue.isEmpty() && chunkLoadsRemaining-- > 0) {
ChunkCoordIntPair coords = serverChunkLoadQueue.remove(0);
ChunkProviderServer chunkProviderServer = Minecraft.getMinecraft().getIntegratedServer().worldServers[0].theChunkProviderServer;
Chunk chunk = chunkProviderServer.currentChunkProvider.provideChunk(coords.chunkXPos, coords.chunkZPos);
farChunks.add(chunk);
}
}
private LODRegion getRegionContaining(int chunkX, int chunkZ) {
ChunkCoordIntPair key = new ChunkCoordIntPair(Math.floorDiv(chunkX , 32), Math.floorDiv(chunkZ, 32));
LODRegion region = loadedRegionsMap.get(key);
if(region == null) {
region = LODRegion.load(getSaveDir(), Math.floorDiv(chunkX , 32), Math.floorDiv(chunkZ , 32));
loadedRegionsMap.put(key, region);
}
return region;
}
private void loadChunk(int chunkX, int chunkZ) {
LODRegion region = getRegionContaining(chunkX, chunkZ);
LODChunk lodChunk = region.getChunkAbsolute(chunkX, chunkZ);
if(lodChunk == null) {
ChunkProviderServer chunkProviderServer = Minecraft.getMinecraft().getIntegratedServer().worldServers[0].theChunkProviderServer;
//Chunk chunk = chunkProviderServer.loadChunk(chunkX, chunkZ);
Chunk chunk = chunkProviderServer.currentChunkProvider.provideChunk(chunkX, chunkZ);
/*Chunk chunk = chunkProviderServer.safeLoadChunk(chunkX, chunkZ);
if(chunk == null) {
chunk = chunkProviderServer.currentChunkProvider.provideChunk(chunkX, chunkZ);
}
if(chunk != null) {
chunk.populateChunk(chunkProviderServer, chunkProviderServer, chunkX, chunkZ);
myChunks.add(chunk);
}*/
if(chunk != null) {
myChunks.add(chunk);
}
//lodChunk = region.putChunk(new LODChunk(chunk));
}
sendChunkToGPU(lodChunk);
}
private void sendChunkToGPU(LODChunk lodChunk) {
lodChunk.simpleMesh = new SimpleChunkMesh(lodChunk.chunk);
Entity player = (Entity) Minecraft.getMinecraft().getIntegratedServer().getConfigurationManager().playerEntityList.get(0);
lodChunk.tick(player);
setVisible(lodChunk, true);
}
public void setLOD(LODChunk lodChunk, int lod) {
if(lod == lodChunk.lod) return;
lodChunk.lod = lod;
lodChunkChanged(lodChunk);
}
public void setVisible(LODChunk lodChunk, boolean visible) {
if(visible == lodChunk.visible) return;
lodChunk.visible = visible;
lodChunkChanged(lodChunk);
}
public void lodChunkChanged(LODChunk lodChunk) {
int newLOD = (!lodChunk.hasChunkMeshes() && lodChunk.lod == 2) ? 1 : lodChunk.lod;
if(lodChunk.simpleMesh != null) {
if(lodChunk.visible && newLOD == 1) {
if(!lodChunk.simpleMesh.visible) {
setMeshVisible(lodChunk.simpleMesh, true);
}
} else {
if(lodChunk.simpleMesh.visible) {
setMeshVisible(lodChunk.simpleMesh, false);
}
}
}
for(ChunkMesh cm : lodChunk.chunkMeshes) {
if(cm != null) {
if(lodChunk.visible && newLOD == 2) {
if(!cm.visible) {
setMeshVisible(cm, true);
}
} else {
if(cm.visible) {
setMeshVisible(cm, false);
}
}
}
}
}
private int nextTri;
private int nextMeshOffset;
private int nextMesh;
protected void setMeshVisible(Mesh mesh, boolean visible) {
if(mesh == null) return;
if(mesh.visible != visible) {
if(!visible) {
if(mesh.iFirst == -1) {
System.out.println("uh");
}
piFirst.position(0);
int[] piFirstArr = new int[piFirst.limit()];
piFirst.get(piFirstArr);
int index = ArrayUtils.indexOf(piFirstArr, mesh.iFirst);
piFirstArr = ArrayUtils.remove(piFirstArr, index);
piFirst.position(0);
piFirst.put(piFirstArr);
piFirst.position(0);
piFirst.limit(piFirst.limit() - 1);
piCount.position(0);
deleteMeshFromGPU(mesh);
int[] piCountArr = new int[piCount.limit()];
piCount.get(piCountArr);
piCountArr = ArrayUtils.remove(piCountArr, index);
piCount.position(0);
piCount.put(piCountArr);
piCount.position(0);
piCount.limit(piCount.limit() - 1);
nextMesh--;
} else if(visible) {
sendMeshToGPU(mesh);
piFirst.limit(piFirst.limit() + 1);
piFirst.put(nextMesh, mesh.iFirst);
piCount.limit(piCount.limit() + 1);
piCount.put(nextMesh, mesh.iCount);
nextMesh++;
}
mesh.visible = visible;
}
}
private void sendMeshToGPU(Mesh mesh) {
if(mesh == null) {
return;
}
if(mesh.pendingGPUDelete) {
mesh.pendingGPUDelete = false;
return;
}
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, nextMeshOffset, mesh.buffer);
mesh.iFirst = nextTri;
mesh.iCount = mesh.quadCount * 6;
mesh.offset = nextMeshOffset;
nextTri += mesh.quadCount * 6;
nextMeshOffset += mesh.buffer.limit();
sentMeshes.add(mesh);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
private void deleteMeshFromGPU(Mesh mesh) {
if(mesh == null) {
return;
}
mesh.pendingGPUDelete = true;
}
public Chunk getChunkFromChunkCoords(int x, int z) {
for(Chunk chunk : myChunks) {
if(chunk.xPosition == x && chunk.zPosition == z) {
return chunk;
}
}
return null;
}
public List<String> getDebugText() {
return Arrays.asList(
"VRAM: " + (nextMeshOffset / 1024 / 1024) + "MB / " + (BUFFER_SIZE / 1024 / 1024) + "MB",
"Simple meshes: " + SimpleChunkMesh.instances + " (" + SimpleChunkMesh.usedRAM / 1024 / 1024 + "MB)",
"Full meshes: " + ChunkMesh.instances + " (" + ChunkMesh.usedRAM / 1024 / 1024 + "MB)",
"Total RAM used: " + ((SimpleChunkMesh.usedRAM + ChunkMesh.usedRAM) / 1024 / 1024) + " MB"
);
}
public void onSave() {
loadedRegionsMap.forEach((k, v) -> v.save(getSaveDir()));
}
private Path getSaveDir(){
return Minecraft.getMinecraft().mcDataDir.toPath().resolve("lodmod").resolve(Minecraft.getMinecraft().getIntegratedServer().getFolderName());
}
public static class LODChunkComparator implements Comparator<LODChunk> {
Entity player;
public LODChunkComparator(Entity player) {
this.player = player;
}
@Override
public int compare(LODChunk p1, LODChunk p2) {
int distSq1 = distSq(p1);
int distSq2 = distSq(p2);
return distSq1 < distSq2 ? -1 : distSq1 > distSq2 ? 1 : 0;
}
int distSq(LODChunk p) {
return (int)(
Math.pow(((p.x * 16) - player.chunkCoordX), 2) +
Math.pow(((p.z * 16) - player.chunkCoordZ), 2)
);
}
}
public static class ChunkCoordDistanceComparator implements Comparator<ChunkCoordIntPair> {
Entity player;
public ChunkCoordDistanceComparator(Entity player) {
this.player = player;
}
@Override
public int compare(ChunkCoordIntPair p1, ChunkCoordIntPair p2) {
int distSq1 = distSq(p1);
int distSq2 = distSq(p2);
return distSq1 < distSq2 ? -1 : distSq1 > distSq2 ? 1 : 0;
}
int distSq(ChunkCoordIntPair p) {
return (int)(
Math.pow(((p.chunkXPos * 16) - player.posX), 2) +
Math.pow(((p.chunkZPos * 16) - player.posZ), 2)
);
}
}
}
|