Skip to content

Commit

Permalink
Chunks now render, lighting is a bit off though
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiboo committed Sep 16, 2023
1 parent da93cc4 commit f9bc424
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 47 deletions.
14 changes: 7 additions & 7 deletions src/main/java/io/github/cadiboo/nocubes/client/LightCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ public static LightCache retain(Area area) {
}

@Deprecated
public int get(final int x, final int y, final int z) {
return get(x, y, z, MUTABLE_BLOCK_POS.get());
public int get(final int relativeX, final int relativeY, final int relativeZ) {
return get(relativeX, relativeY, relativeZ, MUTABLE_BLOCK_POS.get());
}

public int get(final int x, final int y, final int z, MutableBlockPos pos) {
return get(x, y, z, this.cache, this.area, pos);
public int get(final int relativeX, final int relativeY, final int relativeZ, MutableBlockPos pos) {
return get(relativeX, relativeY, relativeZ, this.cache, this.area, pos);
}

public static int get(
final int x, final int y, final int z,
final int relativeX, final int relativeY, final int relativeZ,
final int[] cache,
Area area,
MutableBlockPos mutableBlockPos
) {
int index = area.indexIfInsideCache(x, y, z);
int index = area.indexIfInsideCache(relativeX, relativeY, relativeZ);
if (index != -1) {
int packedLight = cache[index];
if (packedLight != -1)
Expand All @@ -73,7 +73,7 @@ public static int get(
IBlockState state = area.getBlockState(index, mutableBlockPos);
int packedLight = state.getPackedLightmapCoords(
area.world,
mutableBlockPos.setPos(area.start).add(x, y, z)
mutableBlockPos.setPos(area.start).add(relativeX, relativeY, relativeZ)
);
if (index != -1)
cache[index] = packedLight;
Expand Down
61 changes: 24 additions & 37 deletions src/main/java/io/github/cadiboo/nocubes/client/LightmapInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.github.cadiboo.nocubes.util.ModProfiler;
import io.github.cadiboo.nocubes.util.Vec;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.BlockPos.PooledMutableBlockPos;
import net.minecraft.util.math.BlockPos.MutableBlockPos;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -46,42 +46,33 @@ public static LightmapInfo generateLightmapInfo(
@Nonnull final Vec v1,
@Nonnull final Vec v2,
final Vec v3,
final int chunkRenderPosX,
final int chunkRenderPosY,
final int chunkRenderPosZ,
@Nonnull final PooledMutableBlockPos pooledMutableBlockPos
@Nonnull final MutableBlockPos pooledMutableBlockPos
) {
try (final ModProfiler ignored = ModProfiler.get().start("generateLightmapInfo")) {
switch (Minecraft.getMinecraft().gameSettings.ambientOcclusion) {
case 0:
return generateLightmapInfoFlat(v0, chunkRenderPosX, chunkRenderPosY, chunkRenderPosZ, lazyPackedLightCache, pooledMutableBlockPos);
return generateLightmapInfoFlat(v0, lazyPackedLightCache, pooledMutableBlockPos);
default:
case 1:
return generateLightmapInfoSmooth(v0, v1, v2, v3, chunkRenderPosX, chunkRenderPosY, chunkRenderPosZ, lazyPackedLightCache, pooledMutableBlockPos);
return generateLightmapInfoSmooth(v0, v1, v2, v3, lazyPackedLightCache, pooledMutableBlockPos);
case 2:
return generateLightmapInfoSmoothAO(v0, v1, v2, v3, chunkRenderPosX, chunkRenderPosY, chunkRenderPosZ, lazyPackedLightCache, pooledMutableBlockPos);
return generateLightmapInfoSmoothAO(v0, v1, v2, v3, lazyPackedLightCache, pooledMutableBlockPos);
}
}
}

private static LightmapInfo generateLightmapInfoSmoothAO(
@Nonnull final Vec v0, @Nonnull final Vec v1, @Nonnull final Vec v2, @Nonnull final Vec v3,
final int chunkRenderPosX,
final int chunkRenderPosY,
final int chunkRenderPosZ,
@Nonnull final LightCache packedLightCache,
@Nonnull final PooledMutableBlockPos pooledMutableBlockPos
@Nonnull final MutableBlockPos pooledMutableBlockPos
) {
return generateLightmapInfoSmooth(v0, v1, v2, v3, chunkRenderPosX, chunkRenderPosY, chunkRenderPosZ, packedLightCache, pooledMutableBlockPos);
return generateLightmapInfoSmooth(v0, v1, v2, v3, packedLightCache, pooledMutableBlockPos);
}

private static LightmapInfo generateLightmapInfoSmooth(
@Nonnull final Vec v0, @Nonnull final Vec v1, @Nonnull final Vec v2, @Nonnull final Vec v3,
final int chunkRenderPosX,
final int chunkRenderPosY,
final int chunkRenderPosZ,
@Nonnull final LightCache light,
@Nonnull final PooledMutableBlockPos pooledMutableBlockPos
@Nonnull final MutableBlockPos pooledMutableBlockPos
) {
// TODO pool these arrays? (I think pooling them is more overhead than its worth)
// 3x3x3 cache
Expand All @@ -91,21 +82,21 @@ private static LightmapInfo generateLightmapInfoSmooth(
final int[] packedLight3 = new int[27];

// TODO offset shouldn't be hardcoded +1 anymore
final int v0XOffset = 1 + clamp(floor(v0.x) - chunkRenderPosX, -1, 16);
final int v0YOffset = 1 + clamp(floor(v0.y) - chunkRenderPosY, -1, 16);
final int v0ZOffset = 1 + clamp(floor(v0.z) - chunkRenderPosZ, -1, 16);
final int v0XOffset = 1 + clamp(floor(v0.x), -1, 16);
final int v0YOffset = 1 + clamp(floor(v0.y), -1, 16);
final int v0ZOffset = 1 + clamp(floor(v0.z), -1, 16);

final int v1XOffset = 1 + clamp(floor(v1.x) - chunkRenderPosX, -1, 16);
final int v1YOffset = 1 + clamp(floor(v1.y) - chunkRenderPosY, -1, 16);
final int v1ZOffset = 1 + clamp(floor(v1.z) - chunkRenderPosZ, -1, 16);
final int v1XOffset = 1 + clamp(floor(v1.x), -1, 16);
final int v1YOffset = 1 + clamp(floor(v1.y), -1, 16);
final int v1ZOffset = 1 + clamp(floor(v1.z), -1, 16);

final int v2XOffset = 1 + clamp(floor(v2.x) - chunkRenderPosX, -1, 16);
final int v2YOffset = 1 + clamp(floor(v2.y) - chunkRenderPosY, -1, 16);
final int v2ZOffset = 1 + clamp(floor(v2.z) - chunkRenderPosZ, -1, 16);
final int v2XOffset = 1 + clamp(floor(v2.x), -1, 16);
final int v2YOffset = 1 + clamp(floor(v2.y), -1, 16);
final int v2ZOffset = 1 + clamp(floor(v2.z), -1, 16);

final int v3XOffset = 1 + clamp(floor(v3.x) - chunkRenderPosX, -1, 16);
final int v3YOffset = 1 + clamp(floor(v3.y) - chunkRenderPosY, -1, 16);
final int v3ZOffset = 1 + clamp(floor(v3.z) - chunkRenderPosZ, -1, 16);
final int v3XOffset = 1 + clamp(floor(v3.x), -1, 16);
final int v3YOffset = 1 + clamp(floor(v3.y), -1, 16);
final int v3ZOffset = 1 + clamp(floor(v3.z), -1, 16);

int index = 0;
// From (-1, -1, -1) to (1, 1, 1), accounting for cache offset
Expand Down Expand Up @@ -150,16 +141,12 @@ private static LightmapInfo generateLightmapInfoSmooth(

private static LightmapInfo generateLightmapInfoFlat(
@Nonnull final Vec v0,
final int chunkRenderPosX,
final int chunkRenderPosY,
final int chunkRenderPosZ,
@Nonnull final LightCache lazyPackedLightCache,
@Nonnull final PooledMutableBlockPos pooledMutableBlockPos
@Nonnull final MutableBlockPos pooledMutableBlockPos
) {

final int v0XOffset = 1 + clamp(floor(v0.x) - chunkRenderPosX, -1, 16);
final int v0YOffset = 1 + clamp(floor(v0.y) - chunkRenderPosY, -1, 16);
final int v0ZOffset = 1 + clamp(floor(v0.z) - chunkRenderPosZ, -1, 16);
final int v0XOffset = 1 + clamp(floor(v0.x), -1, 16);
final int v0YOffset = 1 + clamp(floor(v0.y), -1, 16);
final int v0ZOffset = 1 + clamp(floor(v0.z), -1, 16);

final int[] packedLight0 = new int[27];

Expand Down
Loading

0 comments on commit f9bc424

Please sign in to comment.