-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework spatial lookup, prepare support for light shapes.
- Loading branch information
1 parent
6931a08
commit e58249e
Showing
9 changed files
with
254 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/dev/lambdaurora/lambdynlights/engine/LightCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright © 2024 LambdAurora <email@lambdaurora.dev> | ||
* | ||
* This file is part of LambDynamicLights. | ||
* | ||
* Licensed under the Lambda License. For more information, | ||
* see the LICENSE file. | ||
*/ | ||
|
||
package dev.lambdaurora.lambdynlights.engine; | ||
|
||
import dev.lambdaurora.lambdynlights.engine.lookup.SpatialLookupCollectionEntry; | ||
import it.unimi.dsi.fastutil.bytes.ByteArrayList; | ||
import it.unimi.dsi.fastutil.bytes.ByteList; | ||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; | ||
import it.unimi.dsi.fastutil.longs.LongArrayList; | ||
import it.unimi.dsi.fastutil.longs.LongList; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.stream.Stream; | ||
|
||
public record LightCollection(Collection<LightCollectionEntry> entries) { | ||
public Stream<SpatialLookupCollectionEntry> split() { | ||
record Data(LongList position, ByteList luminance) {} | ||
var cellKeyToData = new Int2ObjectOpenHashMap<Data>(); | ||
|
||
for (var record : this.entries) { | ||
int cellKey = DynamicLightingEngine.hashAt(record.x(), record.y(), record.z()); | ||
|
||
var data = cellKeyToData.computeIfAbsent(cellKey, k -> new Data(new LongArrayList(), new ByteArrayList())); | ||
|
||
data.position.add(record.x()); | ||
data.position.add(record.y()); | ||
data.position.add(record.z()); | ||
data.luminance.add((byte) record.luminance()); | ||
} | ||
|
||
return cellKeyToData.int2ObjectEntrySet() | ||
.stream() | ||
.map(entry -> new SpatialLookupCollectionEntry( | ||
entry.getIntKey(), | ||
entry.getValue().position().toLongArray(), | ||
entry.getValue().luminance().toByteArray() | ||
)); | ||
} | ||
|
||
public static LightCollection cuboid(int startX, int startY, int startZ, int endX, int endY, int endZ, int luminance) { | ||
var entries = new ArrayList<LightCollectionEntry>(); | ||
|
||
for (int x = startX; x <= endX; x++) { | ||
for (int y = startY; y <= endY; y++) { | ||
for (int z = startZ; z <= endZ; z++) { | ||
entries.add(new LightCollectionEntry(x, y, z, luminance)); | ||
} | ||
} | ||
} | ||
|
||
return new LightCollection(entries); | ||
} | ||
|
||
/*public static LightCollection line(int startX, int startY, int startZ, int endX, int endY, int endZ, int luminance) { | ||
HashMap<Vec3i, Double> blocks = new HashMap<>(); | ||
}*/ | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/dev/lambdaurora/lambdynlights/engine/LightCollectionEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright © 2024 LambdAurora <email@lambdaurora.dev> | ||
* | ||
* This file is part of LambDynamicLights. | ||
* | ||
* Licensed under the Lambda License. For more information, | ||
* see the LICENSE file. | ||
*/ | ||
|
||
package dev.lambdaurora.lambdynlights.engine; | ||
|
||
public record LightCollectionEntry(int x, int y, int z, int luminance) { | ||
} |
22 changes: 0 additions & 22 deletions
22
src/main/java/dev/lambdaurora/lambdynlights/engine/SpatialLookupEntry.java
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
src/main/java/dev/lambdaurora/lambdynlights/engine/lookup/SpatialLookupCollectionEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright © 2024 LambdAurora <email@lambdaurora.dev> | ||
* | ||
* This file is part of LambDynamicLights. | ||
* | ||
* Licensed under the Lambda License. For more information, | ||
* see the LICENSE file. | ||
*/ | ||
|
||
package dev.lambdaurora.lambdynlights.engine.lookup; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Represents an entry made of a collection of light sources in a spatial lookup. | ||
* | ||
* @param cellKey the cell key of this entry | ||
* @param positions the positions of each light sources in the current cell | ||
* @param luminance the light values in the current cell | ||
* @author LambdAurora, Akarys | ||
* @version 4.0.0 | ||
* @since 4.0.0 | ||
*/ | ||
public record SpatialLookupCollectionEntry(int cellKey, long[] positions, byte[] luminance) implements SpatialLookupEntry { | ||
@Override | ||
public double getDynamicLightLevel(@NotNull BlockPos pos) { | ||
double maxLightLevel = 0.; | ||
|
||
for (int i = 0; i < this.luminance.length; i++) { | ||
int posIndex = i * 3; | ||
double x = this.positions[posIndex] + 0.5; | ||
double y = this.positions[posIndex + 1] + 0.5; | ||
double z = this.positions[posIndex + 2] + 0.5; | ||
byte luminance = this.luminance[i]; | ||
|
||
double lightLevel = SpatialLookupEntry.lightAtPos(x, y, z, pos, luminance); | ||
|
||
if (lightLevel > maxLightLevel) { | ||
maxLightLevel = lightLevel; | ||
} | ||
} | ||
|
||
return maxLightLevel; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/dev/lambdaurora/lambdynlights/engine/lookup/SpatialLookupEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright © 2024 LambdAurora <email@lambdaurora.dev> | ||
* | ||
* This file is part of LambDynamicLights. | ||
* | ||
* Licensed under the Lambda License. For more information, | ||
* see the LICENSE file. | ||
*/ | ||
|
||
package dev.lambdaurora.lambdynlights.engine.lookup; | ||
|
||
import dev.lambdaurora.lambdynlights.engine.DynamicLightingEngine; | ||
import net.minecraft.core.BlockPos; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Represents an entry in a spatial lookup. | ||
* | ||
* @author LambdAurora, Akarys | ||
* @version 4.0.0 | ||
* @since 3.1.0 | ||
*/ | ||
public interface SpatialLookupEntry { | ||
/** | ||
* {@return the cell key of this entry} | ||
*/ | ||
int cellKey(); | ||
|
||
/** | ||
* Returns the dynamic light level generated by this entry at the specified position. | ||
* | ||
* @param pos the position | ||
* @return the computed dynamic light level at the specified position | ||
*/ | ||
double getDynamicLightLevel(@NotNull BlockPos pos); | ||
|
||
static double lightAtPos(double x, double y, double z, @NotNull BlockPos pos, int luminance) { | ||
// Can't use Entity#squaredDistanceTo because of eye Y coordinate. | ||
double dx = pos.getX() - x + 0.5; | ||
double dy = pos.getY() - y + 0.5; | ||
double dz = pos.getZ() - z + 0.5; | ||
|
||
double distanceSquared = dx * dx + dy * dy + dz * dz; | ||
// 7.75 because else we would have to update more chunks and that's not a good idea. | ||
// 15 (max range for blocks) would be too much and a bit cheaty. | ||
if (distanceSquared <= DynamicLightingEngine.MAX_RADIUS_SQUARED) { | ||
double multiplier = 1.0 - Math.sqrt(distanceSquared) / DynamicLightingEngine.MAX_RADIUS; | ||
return multiplier * (double) luminance; | ||
} | ||
|
||
return 0.; | ||
} | ||
} |
Oops, something went wrong.