diff --git a/README.MD b/README.MD index 167a0523c..55609ee77 100644 --- a/README.MD +++ b/README.MD @@ -65,3 +65,6 @@ * FRAPI and Indigo * Model building API heavily inspired by/copied from Fabric's; upstream may use it under the original Apache 2.0 license, see LICENSE in `com.gtnewhorizons.angelica.models` +* Dynamic Lights + * LambdAurora for [LambDynamicLights](https://github.com/LambdAurora/LambDynamicLights) under the MIT license, and the Forge port anthxnymc for [DynamicLightsReforged](https://github.com/anthxnymc/DynamicLightsReforged), also under the MIT license + diff --git a/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java b/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java index c73425afc..fde69e6f1 100644 --- a/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java +++ b/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java @@ -70,7 +70,6 @@ public class AngelicaConfig { @Config.DefaultBoolean(true) public static boolean showSplashMemoryBar; - @Config.Comment("Renders the HUD elements once per tick and reuses the pixels to improve performance. [Experimental]") @Config.DefaultBoolean(true) @Config.RequiresMcRestart @@ -81,6 +80,11 @@ public class AngelicaConfig { @Config.RequiresMcRestart public static boolean enableFontRenderer; + @Config.Comment("Enable Dynamic Lights") + @Config.DefaultBoolean(false) + @Config.RequiresMcRestart + public static boolean enableDynamicLights; + @Config.Comment("Optimize world update light. [From Hodgepodge]") @Config.DefaultBoolean(true) @Config.RequiresMcRestart diff --git a/src/main/java/com/gtnewhorizons/angelica/dynamiclights/DynamicLights.java b/src/main/java/com/gtnewhorizons/angelica/dynamiclights/DynamicLights.java new file mode 100644 index 000000000..634a4261c --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/dynamiclights/DynamicLights.java @@ -0,0 +1,363 @@ +package com.gtnewhorizons.angelica.dynamiclights; + +import com.gtnewhorizons.angelica.api.BlockPos; +import com.gtnewhorizons.angelica.compat.mojang.BlockPosImpl; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import it.unimi.dsi.fastutil.longs.LongOpenHashSet; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer; +import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; +import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.util.MathHelper; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.Predicate; + +public class DynamicLights { + private static DynamicLights instance; + public static DynamicLightsMode Mode = DynamicLightsMode.OFF; + + private static final double MAX_RADIUS = 7.75; + private static final double MAX_RADIUS_SQUARED = MAX_RADIUS * MAX_RADIUS; + private final Set dynamicLightSources = new ObjectOpenHashSet<>(); + private final ReentrantReadWriteLock lightSourcesLock = new ReentrantReadWriteLock(); + private long lastUpdate = System.currentTimeMillis(); + private int lastUpdateCount = 0; + + public static DynamicLights get() { + if (instance == null) + instance = new DynamicLights(); + return instance; + } + + public static boolean isEnabled() { + return Mode.isEnabled() && AngelicaConfig.enableDynamicLights; + } + + /** + * Updates all light sources. + * + * @param renderer the renderer + */ + public void updateAll(@NotNull SodiumWorldRenderer renderer) { + if (!isEnabled()) + return; + + long now = System.currentTimeMillis(); + if (now >= this.lastUpdate + 50) { + this.lastUpdate = now; + this.lastUpdateCount = 0; + + this.lightSourcesLock.readLock().lock(); + for (var lightSource : this.dynamicLightSources) { + if (lightSource.angelica$updateDynamicLight(renderer)) + this.lastUpdateCount++; + } + this.lightSourcesLock.readLock().unlock(); + } + } + + public int getLastUpdateCount() { + return this.lastUpdateCount; + } + + public void addLightSource(IDynamicLightSource lightSource) { + this.lightSourcesLock.writeLock().lock(); + this.dynamicLightSources.add(lightSource); + this.lightSourcesLock.writeLock().unlock(); + } + + /** + * Removes the light source from the tracked light sources. + * + * @param lightSource the light source to remove + */ + public void removeLightSource(@NotNull IDynamicLightSource lightSource) { + this.lightSourcesLock.writeLock().lock(); + + var dynamicLightSources = this.dynamicLightSources.iterator(); + IDynamicLightSource it; + while (dynamicLightSources.hasNext()) { + it = dynamicLightSources.next(); + if (it.equals(lightSource)) { + dynamicLightSources.remove(); + if (SodiumWorldRenderer.getInstance() != null) + lightSource.angelica$scheduleTrackedChunksRebuild(SodiumWorldRenderer.getInstance()); + break; + } + } + + this.lightSourcesLock.writeLock().unlock(); + } + + /** + * Removes light sources if the filter matches. + * + * @param filter the removal filter + */ + public void removeLightSources(@NotNull Predicate filter) { + this.lightSourcesLock.writeLock().lock(); + + var dynamicLightSources = this.dynamicLightSources.iterator(); + IDynamicLightSource it; + while (dynamicLightSources.hasNext()) { + it = dynamicLightSources.next(); + if (filter.test(it)) { + dynamicLightSources.remove(); + if (SodiumWorldRenderer.getInstance() != null) { + if (it.angelica$getLuminance() > 0) + it.angelica$resetDynamicLight(); + it.angelica$scheduleTrackedChunksRebuild(SodiumWorldRenderer.getInstance()); + } + break; + } + } + + this.lightSourcesLock.writeLock().unlock(); + } + + /** + * Clears light sources. + */ + public void clearLightSources() { + this.lightSourcesLock.writeLock().lock(); + + var dynamicLightSources = this.dynamicLightSources.iterator(); + IDynamicLightSource it; + while (dynamicLightSources.hasNext()) { + it = dynamicLightSources.next(); + dynamicLightSources.remove(); + if (SodiumWorldRenderer.getInstance() != null) { + if (it.angelica$getLuminance() > 0) + it.angelica$resetDynamicLight(); + it.angelica$scheduleTrackedChunksRebuild(SodiumWorldRenderer.getInstance()); + } + } + + this.lightSourcesLock.writeLock().unlock(); + } + + /** + * Returns whether the light source is tracked or not. + * + * @param lightSource the light source to check + * @return {@code true} if the light source is tracked, else {@code false} + */ + public boolean containsLightSource(@NotNull IDynamicLightSource lightSource) { + boolean result; + this.lightSourcesLock.readLock().lock(); + result = this.dynamicLightSources.contains(lightSource); + this.lightSourcesLock.readLock().unlock(); + return result; + } + + /** + * Returns the number of dynamic light sources that currently emit lights. + * + * @return the number of dynamic light sources emitting light + */ + public int getLightSourcesCount() { + int result; + + this.lightSourcesLock.readLock().lock(); + result = this.dynamicLightSources.size(); + this.lightSourcesLock.readLock().unlock(); + + return result; + } + + public double getDynamicLightLevel(int x, int y, int z) { + double result = 0; + this.lightSourcesLock.readLock().lock(); + for (var lightSource : this.dynamicLightSources) { + result = maxDynamicLightLevel(x, y, z, lightSource, result); + } + this.lightSourcesLock.readLock().unlock(); + + return MathHelper.clamp_double(result, 0, 15); + } + + public double getDynamicLightLevel(@NotNull BlockPosImpl pos) { + return this.getDynamicLightLevel(pos.getX(), pos.getY(), pos.getZ()); + } + + public static double maxDynamicLightLevel(int x, int y, int z, @NotNull IDynamicLightSource lightSource, double currentLightLevel) { + int luminance = lightSource.angelica$getLuminance(); + if (luminance > 0) { + // Can't use Entity#squaredDistanceTo because of eye Y coordinate. + double dx = x - lightSource.angelica$getDynamicLightX() + 0.5; + double dy = y - lightSource.angelica$getDynamicLightY() + 0.5; + double dz = z - lightSource.angelica$getDynamicLightZ() + 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 <= MAX_RADIUS_SQUARED) { + double multiplier = 1.0 - Math.sqrt(distanceSquared) / MAX_RADIUS; + double lightLevel = multiplier * (double) luminance; + if (lightLevel > currentLightLevel) { + return lightLevel; + } + } + } + return currentLightLevel; + } + + /** + * Returns the dynamic light level generated by the light source at the specified position. + * + * @param pos the position + * @param lightSource the light source + * @param currentLightLevel the current surrounding dynamic light level + * @return the dynamic light level at the specified position + */ + public static double maxDynamicLightLevel(@NotNull BlockPosImpl pos, @NotNull IDynamicLightSource lightSource, double currentLightLevel) { + return maxDynamicLightLevel(pos.getX(), pos.getY(), pos.getZ(), lightSource, currentLightLevel); + } + + public int getLightmapWithDynamicLight(int x, int y, int z, int lightmap) { + return this.getLightmapWithDynamicLight(this.getDynamicLightLevel(x, y, z), lightmap); + } + + /** + * Returns the lightmap with combined light levels. + * + * @param dynamicLightLevel the dynamic light level + * @param lightmap the vanilla lightmap coordinates + * @return the modified lightmap coordinates + */ + public int getLightmapWithDynamicLight(double dynamicLightLevel, int lightmap) { + if (dynamicLightLevel > 0) { + // lightmap is (skyLevel << 20 | blockLevel << 4) + + // Get vanilla block light level. + int blockLevel = (lightmap & 0xFFFF) >> 4; + if (dynamicLightLevel > blockLevel) { + // Equivalent to a << 4 bitshift with a little quirk: this one ensure more precision (more decimals are saved). + int luminance = (int) (dynamicLightLevel * 16.0); + lightmap &= 0xfff00000; + lightmap |= luminance & 0x000fffff; + } + } + + return lightmap; + } + + /** + * Schedules a chunk rebuild at the specified chunk position. + * + * @param renderer the renderer + * @param chunkPos the chunk position + */ + public static void scheduleChunkRebuild(@NotNull SodiumWorldRenderer renderer, @NotNull BlockPosImpl chunkPos) { + scheduleChunkRebuild(renderer, chunkPos.getX(), chunkPos.getY(), chunkPos.getZ()); + } + + /** + * Schedules a chunk rebuild at the specified chunk position. + * + * @param renderer the renderer + * @param chunkPos the packed chunk position + */ + public static void scheduleChunkRebuild(@NotNull SodiumWorldRenderer renderer, long chunkPos) { + scheduleChunkRebuild(renderer, BlockPos.unpackLongX(chunkPos), BlockPos.unpackLongY(chunkPos), BlockPos.unpackLongZ(chunkPos)); + } + + public static void scheduleChunkRebuild(@NotNull SodiumWorldRenderer renderer, int x, int y, int z) { + renderer.scheduleRebuildForChunk(x, y, z, false); + } + + /** + * Updates the tracked chunk sets. + * + * @param chunkPos the packed chunk position + * @param old the set of old chunk coordinates to remove this chunk from it + * @param newPos the set of new chunk coordinates to add this chunk to it + */ + public static void updateTrackedChunks(@NotNull BlockPosImpl chunkPos, @Nullable LongOpenHashSet old, @Nullable LongOpenHashSet newPos) { + if (old != null || newPos != null) { + long pos = chunkPos.asLong(); + if (old != null) + old.remove(pos); + if (newPos != null) + newPos.add(pos); + } + } + + /** + * Updates the dynamic lights tracking. + * + * @param lightSource the light source + */ + public static void updateTracking(@NotNull IDynamicLightSource lightSource) { + boolean enabled = lightSource.angelica$isDynamicLightEnabled(); + int luminance = lightSource.angelica$getLuminance(); + + if (!enabled && luminance > 0) { + lightSource.angelica$setDynamicLightEnabled(true); + } else if (enabled && luminance < 1) { + lightSource.angelica$setDynamicLightEnabled(false); + } + } + + /** + * Returns the luminance from an item stack. + * + * @param stack the item stack + * @param submergedInWater {@code true} if the stack is submerged in water, else {@code false} + * @return the luminance of the item + */ + public static int getLuminanceFromItemStack(@NotNull ItemStack stack, boolean submergedInWater) { + Item item = stack.getItem(); + if (item instanceof ItemBlock itemBlock) { + Block block = itemBlock.field_150939_a; + if (block != null) { + return block.getLightValue(); + } + } + + if (item == Items.lava_bucket) return Blocks.lava.getLightValue(); + + return 0; + } + + public static int getLuminanceFromEntity(@NotNull Entity entity) { + + if (entity.fire > 0) return 15; + + if (entity instanceof EntityItem item) { + return getLuminanceFromItemStack(item.getEntityItem(), item.isInWater()); + } + + if (entity instanceof EntityLivingBase living) { + int luminance = 0; + + boolean inWater = living.isInWater(); + + // check equipment + hand for light + ItemStack itemStack; + for (int i = 0; i < 5; i++) { + if ((itemStack = living.getEquipmentInSlot(i)) != null) { + luminance = Math.max(luminance, getLuminanceFromItemStack(itemStack, inWater)); + } + } + + return luminance; + } + + // TODO: Creepers? TNT? Dusts? + + return 0; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/dynamiclights/DynamicLightsMode.java b/src/main/java/com/gtnewhorizons/angelica/dynamiclights/DynamicLightsMode.java new file mode 100644 index 000000000..4c04d7fdd --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/dynamiclights/DynamicLightsMode.java @@ -0,0 +1,35 @@ +package com.gtnewhorizons.angelica.dynamiclights; + +import java.util.Objects; + +public enum DynamicLightsMode +{ + OFF("Off", 0), + FASTEST("Fastest", 500), + FAST("Fast", 250), + FANCY("Fancy", 0); + + private final String name; + private final int delay; + + public boolean hasDelay() { + return delay > 0; + } + + public int getDelay(){ + return delay; + } + + public String getName() { + return name; + } + + DynamicLightsMode(String name, int delay) { + this.name = name; + this.delay = delay; + } + + public boolean isEnabled() { + return !Objects.equals(this, DynamicLightsMode.OFF); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/dynamiclights/IDynamicLightSource.java b/src/main/java/com/gtnewhorizons/angelica/dynamiclights/IDynamicLightSource.java new file mode 100644 index 000000000..f513706af --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/dynamiclights/IDynamicLightSource.java @@ -0,0 +1,86 @@ +package com.gtnewhorizons.angelica.dynamiclights; + +import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +public interface IDynamicLightSource { + /** + * Returns the dynamic light source X coordinate. + * + * @return the X coordinate + */ + double angelica$getDynamicLightX(); + + /** + * Returns the dynamic light source Y coordinate. + * + * @return the Y coordinate + */ + double angelica$getDynamicLightY(); + + /** + * Returns the dynamic light source Z coordinate. + * + * @return the Z coordinate + */ + double angelica$getDynamicLightZ(); + + /** + * Returns the dynamic light source world. + * + * @return the world instance + */ + SodiumWorldRenderer angelica$getDynamicLightWorld(); + + /** + * Returns whether the dynamic light is enabled or not. + * + * @return {@code true} if the dynamic light is enabled, else {@code false} + */ + default boolean angelica$isDynamicLightEnabled() { + return DynamicLights.isEnabled() && DynamicLights.get().containsLightSource(this); + } + + /** + * Sets whether the dynamic light is enabled or not. + *

+ * Note: please do not call this function in your mod or you will break things. + * + * @param enabled {@code true} if the dynamic light is enabled, else {@code false} + */ + @ApiStatus.Internal + default void angelica$setDynamicLightEnabled(boolean enabled) { + this.angelica$resetDynamicLight(); + if (enabled) + DynamicLights.get().addLightSource(this); + else + DynamicLights.get().removeLightSource(this); + } + + void angelica$resetDynamicLight(); + + /** + * Returns the luminance of the light source. + * The maximum is 15, below 1 values are ignored. + * + * @return the luminance of the light source + */ + int angelica$getLuminance(); + + /** + * Executed at each tick. + */ + void angelica$dynamicLightTick(); + + /** + * Returns whether this dynamic light source should update. + * + * @return {@code true} if this dynamic light source should update, else {@code false} + */ + boolean angelica$shouldUpdateDynamicLight(); + + boolean angelica$updateDynamicLight(@NotNull SodiumWorldRenderer renderer); + + void angelica$scheduleTrackedChunksRebuild(@NotNull SodiumWorldRenderer renderer); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java index bc333201d..fa57dd374 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java @@ -53,6 +53,14 @@ public enum Mixins { ,"angelica.debug.MixinTextureManager" ) ), + ANGELICA_DYNAMIC_LIGHTS(new Builder("Angelica Dynamic Lights").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT) + .setPhase(Phase.EARLY).setApplyIf(() -> AngelicaConfig.enableDynamicLights).addMixinClasses( + "angelica.dynamiclights.MixinEntityRenderer" + ,"angelica.dynamiclights.MixinEntity" + ,"angelica.dynamiclights.MixinWorld" + ,"angelica.dynamiclights.MixinItemRenderer" + ) + ), // Not compatible with the lwjgl debug callbacks, so disable if that's enabled ARCHAIC_SPLASH(new Builder("ArchaicFix Splash").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT) diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinEntity.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinEntity.java new file mode 100644 index 000000000..2cf024cb6 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinEntity.java @@ -0,0 +1,171 @@ +package com.gtnewhorizons.angelica.mixins.early.angelica.dynamiclights; + +import com.gtnewhorizons.angelica.compat.mojang.BlockPosImpl; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; +import com.gtnewhorizons.angelica.dynamiclights.IDynamicLightSource; +import it.unimi.dsi.fastutil.longs.LongOpenHashSet; +import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer; +import net.minecraft.entity.Entity; +import net.minecraft.util.MathHelper; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; +import org.jetbrains.annotations.NotNull; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(Entity.class) +public abstract class MixinEntity implements IDynamicLightSource { + @Shadow + public World worldObj; + @Shadow + public double posX; + @Shadow + public double posY; + @Shadow + public double posZ; + @Shadow + public int chunkCoordX; + @Shadow + public int chunkCoordZ; + @Shadow + public abstract float getEyeHeight(); + @Shadow + public boolean isDead; + + @Unique + protected int angelica$luminance = 0; + @Unique + private int angelica$lastLuminance = 0; + @Unique + private static long angelica$lastUpdate = 0; + @Unique + private double angelica$prevX; + @Unique + private double angelica$prevY; + @Unique + private double angelica$prevZ; + @Unique + private LongOpenHashSet angelica$trackedLitChunkPos = new LongOpenHashSet(); + + + @Override + public double angelica$getDynamicLightX() { + return posX; + } + + @Override + public double angelica$getDynamicLightY() { + return posY + getEyeHeight(); + } + + @Override + public double angelica$getDynamicLightZ() { + return posZ; + } + + @Override + public void angelica$resetDynamicLight() { + this.angelica$lastLuminance = 0; + } + + @Override + public boolean angelica$shouldUpdateDynamicLight() { + if (DynamicLights.Mode.hasDelay()) { + long currentTime = System.currentTimeMillis(); + if (currentTime < angelica$lastUpdate + DynamicLights.Mode.getDelay()) + return false; + + angelica$lastUpdate = currentTime; + } + + return true; + } + + + @Inject(method = "onEntityUpdate", at = @At("TAIL")) + public void angelica$onUpdate(CallbackInfo ci) { + if (worldObj != null && worldObj.isRemote) { + if (isDead) { + angelica$setDynamicLightEnabled(false); + } else { + angelica$dynamicLightTick(); + DynamicLights.updateTracking(this); + } + } + } + + @Override + public void angelica$dynamicLightTick() { + this.angelica$luminance = DynamicLights.getLuminanceFromEntity((Entity) (Object) this); + } + + @Override + public int angelica$getLuminance() { + return this.angelica$luminance; + } + + @Override + public boolean angelica$updateDynamicLight(@NotNull SodiumWorldRenderer renderer) { + if (!this.angelica$shouldUpdateDynamicLight()) + return false; + double deltaX = this.posX - this.angelica$prevX; + double deltaY = this.posY - this.angelica$prevY; + double deltaZ = this.posZ - this.angelica$prevZ; + + int luminance = this.angelica$getLuminance(); + + if (Math.abs(deltaX) > 0.1D || Math.abs(deltaY) > 0.1D || Math.abs(deltaZ) > 0.1D || luminance != this.angelica$lastLuminance) { + this.angelica$prevX = this.posX; + this.angelica$prevY = this.posY; + this.angelica$prevZ = this.posZ; + this.angelica$lastLuminance = luminance; + + var newPos = new LongOpenHashSet(); + + if (luminance > 0) { + var chunkPos = new BlockPosImpl(chunkCoordX, MathHelper.floor_double(posY + getEyeHeight()) >> 4, chunkCoordZ); + + DynamicLights.scheduleChunkRebuild(renderer, chunkPos); + DynamicLights.updateTrackedChunks(chunkPos, this.angelica$trackedLitChunkPos, newPos); + + var directionX = ((MathHelper.floor_double(posX) & 15) >= 8) ? ForgeDirection.EAST : ForgeDirection.WEST; + var directionY = ((MathHelper.floor_double(posY + getEyeHeight()) & 15) >= 8) ? ForgeDirection.UP : ForgeDirection.DOWN; + var directionZ = ((MathHelper.floor_double(posZ) & 15) >= 8) ? ForgeDirection.SOUTH : ForgeDirection.NORTH; + + for (int i = 0; i < 7; i++) { + if (i % 4 == 0) { + chunkPos = chunkPos.offset(directionX); // X + } else if (i % 4 == 1) { + chunkPos = chunkPos.offset(directionZ); // XZ + } else if (i % 4 == 2) { + chunkPos = chunkPos.offset(directionX.getOpposite()); // Z + } else { + chunkPos = chunkPos.offset(directionZ.getOpposite()); // origin + chunkPos = chunkPos.offset(directionY); // Y + } + DynamicLights.scheduleChunkRebuild(renderer, chunkPos); + DynamicLights.updateTrackedChunks(chunkPos, this.angelica$trackedLitChunkPos, newPos); + } + } + + // Schedules the rebuild of removed chunks. + this.angelica$scheduleTrackedChunksRebuild(renderer); + // Update tracked lit chunks. + this.angelica$trackedLitChunkPos = newPos; + return true; + } + return false; + } + + @Override + public void angelica$scheduleTrackedChunksRebuild(@NotNull SodiumWorldRenderer renderer) { + for (long pos : this.angelica$trackedLitChunkPos) { + DynamicLights.scheduleChunkRebuild(renderer, pos); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinEntityRenderer.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinEntityRenderer.java new file mode 100644 index 000000000..3bda03c60 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinEntityRenderer.java @@ -0,0 +1,23 @@ +package com.gtnewhorizons.angelica.mixins.early.angelica.dynamiclights; + +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; +import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(EntityRenderer.class) +public class MixinEntityRenderer { + @Shadow + private Minecraft mc; + + @Inject(method = "renderWorld", at = @At("HEAD")) + private void updateDynamicLights(float p_78471_1_, long p_78471_2_, CallbackInfo ci){ + mc.mcProfiler.endStartSection("angelica_dynamic_lighting"); + DynamicLights.get().updateAll(SodiumWorldRenderer.getInstance()); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinItemRenderer.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinItemRenderer.java new file mode 100644 index 000000000..508bc75e4 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinItemRenderer.java @@ -0,0 +1,23 @@ +package com.gtnewhorizons.angelica.mixins.early.angelica.dynamiclights; + +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.client.renderer.ItemRenderer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(ItemRenderer.class) +public class MixinItemRenderer { + + @WrapOperation(method = "renderItemInFirstPerson", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/WorldClient;getLightBrightnessForSkyBlocks(IIII)I")) + private int angelica$dynamiclights_renderItemInFirstPerson(WorldClient theWorld, int posX, int posY, int posZ, int p_72802_4_, Operation original){ + int lightmap = original.call(theWorld, posX, posY, posZ, p_72802_4_); + if (DynamicLights.isEnabled()){ + lightmap = (int)DynamicLights.get().getLightmapWithDynamicLight(posX, posY, posZ, lightmap); + } + + return lightmap; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinWorld.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinWorld.java new file mode 100644 index 000000000..b7e293745 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/dynamiclights/MixinWorld.java @@ -0,0 +1,29 @@ +package com.gtnewhorizons.angelica.mixins.early.angelica.dynamiclights; + +import com.gtnewhorizons.angelica.dynamiclights.IDynamicLightSource; +import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.world.World; +import net.minecraft.world.WorldSettings; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(World.class) +public abstract class MixinWorld { + + @Shadow + protected abstract void initialize(WorldSettings p_72963_1_); + @Shadow + public abstract Block getBlock(int p_147439_1_, int p_147439_2_, int p_147439_3_); + + @Inject(method = "onEntityRemoved", at = @At("HEAD")) + private void angelica$removeEntity(Entity entity, CallbackInfo ci){ + if (entity instanceof IDynamicLightSource lightSource){ + lightSource.angelica$setDynamicLightEnabled(false); + } + } + +} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderManager.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderManager.java index 5484c81a8..ddb5e6c5b 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderManager.java @@ -1,5 +1,6 @@ package com.gtnewhorizons.angelica.mixins.early.sodium; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; import me.jellysquid.mods.sodium.client.gui.options.named.LightingQuality; import me.jellysquid.mods.sodium.client.model.light.EntityLighter; import net.minecraft.client.Minecraft; @@ -17,10 +18,20 @@ public class MixinRenderManager { */ @Redirect(method = "renderEntityStatic", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;getBrightnessForRender(F)I")) private int sodium$getBrightnessForRender(Entity self, float partialTicks) { + int lightmap; if (Minecraft.getMinecraft().gameSettings.ambientOcclusion == LightingQuality.HIGH.getVanilla()) { - return EntityLighter.getBlendedLight(self, partialTicks); + lightmap = EntityLighter.getBlendedLight(self, partialTicks); + }else { + lightmap = self.getBrightnessForRender(partialTicks); } - return self.getBrightnessForRender(partialTicks); + if (DynamicLights.isEnabled()){ + int dynamicLightLevel = (int) DynamicLights.get().getDynamicLightLevel((int)self.posX, (int)self.posY, (int)self.posZ); + if (dynamicLightLevel > 0) { + lightmap = DynamicLights.get().getLightmapWithDynamicLight(dynamicLightLevel, lightmap); + } + } + + return lightmap; } } diff --git a/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java b/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java index 7b961430d..07c3a1136 100644 --- a/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java +++ b/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java @@ -5,6 +5,7 @@ import com.gtnewhorizons.angelica.common.BlockTest; import com.gtnewhorizons.angelica.compat.ModStatus; import com.gtnewhorizons.angelica.compat.bettercrashes.BetterCrashesCompat; import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.debug.OpenGLDebugging; import com.gtnewhorizons.angelica.hudcaching.HUDCaching; @@ -188,6 +189,16 @@ public class ClientProxy extends CommonProxy { event.right.add(Block.blockRegistry.getNameForObject(block)); event.right.add("meta: " + meta); } + if (DynamicLights.isEnabled()){ + var builder = new StringBuilder("Dynamic Light Sources: "); + DynamicLights dl = DynamicLights.get(); + builder.append(dl.getLightSourcesCount()) + .append(" (U: ") + .append(dl.getLastUpdateCount()) + .append(')'); + + event.right.add(builder.toString()); + } if(AngelicaConfig.modernizeF3Screen) { boolean hasReplacedXYZ = false; for(int i = 0; i < event.left.size() - 3; i++) { diff --git a/src/main/java/jss/notfine/core/Settings.java b/src/main/java/jss/notfine/core/Settings.java index 7edd406e9..98c5573e3 100644 --- a/src/main/java/jss/notfine/core/Settings.java +++ b/src/main/java/jss/notfine/core/Settings.java @@ -1,6 +1,8 @@ package jss.notfine.core; import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLightsMode; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import jss.notfine.gui.options.control.NotFineControlValueFormatter; @@ -46,6 +48,12 @@ public enum Settings { } }, DYNAMIC_FOV(new NotFineOptionTickBox(true, null)), + DYNAMIC_LIGHTS(new NotFineOptionCycling(DynamicLightsMode.OFF, OptionImpact.VARIES)){ + @Override + public void applyChanges() { + DynamicLights.Mode = (DynamicLightsMode) DYNAMIC_LIGHTS.option.getStore(); + } + }, GUI_BACKGROUND(new NotFineOptionCycling<>(BackgroundSelect.DEFAULT, null)) { @Override public void applyChanges() { diff --git a/src/main/java/jss/notfine/gui/NotFineGameOptionPages.java b/src/main/java/jss/notfine/gui/NotFineGameOptionPages.java index bb46c9902..d3b1990f8 100644 --- a/src/main/java/jss/notfine/gui/NotFineGameOptionPages.java +++ b/src/main/java/jss/notfine/gui/NotFineGameOptionPages.java @@ -190,6 +190,7 @@ public class NotFineGameOptionPages { .add(Settings.MODE_WATER.option) .add(Settings.DOWNFALL_DISTANCE.option) .add(Settings.MODE_VIGNETTE.option) + .add(Settings.DYNAMIC_LIGHTS.option) .add(Settings.MODE_SHADOWS.option) .add(Settings.VOID_FOG.option) .add(Settings.MODE_DROPPED_ITEMS.option) diff --git a/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptionPages.java b/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptionPages.java index 824c40daa..9acae6c08 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptionPages.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptionPages.java @@ -221,6 +221,7 @@ public class SodiumGameOptionPages { )*/ .add(Settings.MODE_SHADOWS.option) .add(Settings.MODE_VIGNETTE.option) + .add(Settings.DYNAMIC_LIGHTS.option) .build()); diff --git a/src/main/java/me/jellysquid/mods/sodium/client/model/light/data/LightDataAccess.java b/src/main/java/me/jellysquid/mods/sodium/client/model/light/data/LightDataAccess.java index 50dd2ff6a..95d4deb57 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/model/light/data/LightDataAccess.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/model/light/data/LightDataAccess.java @@ -1,6 +1,8 @@ package me.jellysquid.mods.sodium.client.model.light.data; +import com.gtnewhorizons.angelica.api.MutableBlockPos; import com.gtnewhorizons.angelica.compat.mojang.BlockPosImpl; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; import me.jellysquid.mods.sodium.client.util.StateUtil; import me.jellysquid.mods.sodium.client.world.WorldSlice; import net.minecraft.block.Block; @@ -119,6 +121,19 @@ public abstract class LightDataAccess { return aoi * (1.0f / 4096.0f); } + public static int getLightMap(long originWord, BlockPosImpl pos) { + if (!DynamicLights.isEnabled()) { + return unpackLM(originWord); + } + if (unpackFO(originWord)){ + return unpackLM(originWord); + } + + double dynamic = DynamicLights.get().getDynamicLightLevel(pos); + return DynamicLights.get().getLightmapWithDynamicLight(dynamic, unpackLM(originWord)); + + } + public WorldSlice getWorld() { return this.world; } diff --git a/src/main/java/me/jellysquid/mods/sodium/client/model/light/flat/FlatLightPipeline.java b/src/main/java/me/jellysquid/mods/sodium/client/model/light/flat/FlatLightPipeline.java index 2aa628247..dc0b24868 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/model/light/flat/FlatLightPipeline.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/model/light/flat/FlatLightPipeline.java @@ -40,7 +40,7 @@ public class FlatLightPipeline implements LightPipeline { if ((flags & ModelQuadFlags.IS_ALIGNED) != 0 || ((flags & ModelQuadFlags.IS_PARALLEL) != 0 && LightDataAccess.unpackFC(this.lightCache.get(pos)))) { lightmap = getOffsetLightmap(pos, face); } else { - lightmap = LightDataAccess.unpackLM(this.lightCache.get(pos)); + lightmap = LightDataAccess.getLightMap(this.lightCache.get(pos), pos); } } @@ -58,10 +58,10 @@ public class FlatLightPipeline implements LightPipeline { * inconsistent values so this method exists to mirror vanilla behavior as closely as possible. */ private int getOffsetLightmap(BlockPosImpl pos, ForgeDirection face) { - int lightmap = LightDataAccess.unpackLM(this.lightCache.get(pos, face)); + int lightmap = LightDataAccess.getLightMap(this.lightCache.get(pos, face), pos); // If the block light is not 15 (max)... if ((lightmap & 0xF0) != 0xF0) { - int originLightmap = LightDataAccess.unpackLM(this.lightCache.get(pos)); + int originLightmap = LightDataAccess.getLightMap(this.lightCache.get(pos), pos); // ...take the maximum combined block light at the origin and offset positions lightmap = (lightmap & ~0xFF) | Math.max(lightmap & 0xFF, originLightmap & 0xFF); } diff --git a/src/main/java/me/jellysquid/mods/sodium/client/model/light/smooth/AoFaceData.java b/src/main/java/me/jellysquid/mods/sodium/client/model/light/smooth/AoFaceData.java index de5842b00..4347b0bf4 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/model/light/smooth/AoFaceData.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/model/light/smooth/AoFaceData.java @@ -6,8 +6,8 @@ import net.minecraftforge.common.util.ForgeDirection; import static me.jellysquid.mods.sodium.client.model.light.cache.ArrayLightDataCache.unpackAO; import static me.jellysquid.mods.sodium.client.model.light.cache.ArrayLightDataCache.unpackFO; -import static me.jellysquid.mods.sodium.client.model.light.cache.ArrayLightDataCache.unpackLM; import static me.jellysquid.mods.sodium.client.model.light.cache.ArrayLightDataCache.unpackOP; +import static me.jellysquid.mods.sodium.client.model.light.cache.ArrayLightDataCache.getLightMap; class AoFaceData { public final int[] lm = new int[4]; @@ -43,9 +43,9 @@ class AoFaceData { // Use the origin block's light values if the adjacent one is opaque if (offset && unpackFO(adjWord)) { - calm = unpackLM(cache.get(x, y, z)); + calm = getLightMap(cache.get(x, y, z), pos); } else { - calm = unpackLM(adjWord); + calm = getLightMap(adjWord, pos); } final float caao = unpackAO(adjWord); @@ -53,22 +53,22 @@ class AoFaceData { ForgeDirection[] faces = AoNeighborInfo.get(direction).faces; final long e0 = cache.get(adjX, adjY, adjZ, faces[0]); - final int e0lm = unpackLM(e0); + final int e0lm = getLightMap(e0, pos); final float e0ao = unpackAO(e0); final boolean e0op = unpackOP(e0); final long e1 = cache.get(adjX, adjY, adjZ, faces[1]); - final int e1lm = unpackLM(e1); + final int e1lm = getLightMap(e1, pos); final float e1ao = unpackAO(e1); final boolean e1op = unpackOP(e1); final long e2 = cache.get(adjX, adjY, adjZ, faces[2]); - final int e2lm = unpackLM(e2); + final int e2lm = getLightMap(e2, pos); final float e2ao = unpackAO(e2); final boolean e2op = unpackOP(e2); final long e3 = cache.get(adjX, adjY, adjZ, faces[3]); - final int e3lm = unpackLM(e3); + final int e3lm = getLightMap(e3, pos); final float e3ao = unpackAO(e3); final boolean e3op = unpackOP(e3); @@ -81,7 +81,7 @@ class AoFaceData { c0ao = e0ao; } else { long d0 = cache.get(adjX, adjY, adjZ, faces[0], faces[2]); - c0lm = unpackLM(d0); + c0lm = getLightMap(d0, pos); c0ao = unpackAO(d0); } @@ -93,7 +93,7 @@ class AoFaceData { c1ao = e0ao; } else { long d1 = cache.get(adjX, adjY, adjZ, faces[0], faces[3]); - c1lm = unpackLM(d1); + c1lm = getLightMap(d1, pos); c1ao = unpackAO(d1); } @@ -106,7 +106,7 @@ class AoFaceData { c2ao = e1ao; } else { long d2 = cache.get(adjX, adjY, adjZ, faces[1], faces[2]); - c2lm = unpackLM(d2); + c2lm = getLightMap(d2, pos); c2ao = unpackAO(d2); } @@ -119,7 +119,7 @@ class AoFaceData { c3ao = e1ao; } else { long d3 = cache.get(adjX, adjY, adjZ, faces[1], faces[3]); - c3lm = unpackLM(d3); + c3lm = getLightMap(d3, pos); c3ao = unpackAO(d3); } diff --git a/src/main/java/me/jellysquid/mods/sodium/client/render/SodiumWorldRenderer.java b/src/main/java/me/jellysquid/mods/sodium/client/render/SodiumWorldRenderer.java index 74e0240f9..32766182b 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/render/SodiumWorldRenderer.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/render/SodiumWorldRenderer.java @@ -5,6 +5,7 @@ import com.gtnewhorizons.angelica.compat.mojang.ChunkPos; import com.gtnewhorizons.angelica.compat.toremove.MatrixStack; import com.gtnewhorizons.angelica.compat.toremove.RenderLayer; import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; import com.gtnewhorizons.angelica.glsm.GLStateManager; import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import it.unimi.dsi.fastutil.longs.LongSet; @@ -115,6 +116,9 @@ public class SodiumWorldRenderer implements ChunkStatusListener { // If we have a world is already loaded, unload the renderer if (this.world != null) { + if (DynamicLights.isEnabled()){ + DynamicLights.get().clearLightSources(); + } this.unloadWorld(); } @@ -122,6 +126,7 @@ public class SodiumWorldRenderer implements ChunkStatusListener { if (world != null) { this.loadWorld(world); } + } private void loadWorld(WorldClient world) { diff --git a/src/main/java/me/jellysquid/mods/sodium/client/world/WorldSlice.java b/src/main/java/me/jellysquid/mods/sodium/client/world/WorldSlice.java index 9ff7a4913..684fa68c4 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/world/WorldSlice.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/world/WorldSlice.java @@ -4,6 +4,7 @@ import java.util.Arrays; import com.gtnewhorizons.angelica.compat.mojang.ChunkSectionPos; import com.gtnewhorizons.angelica.compat.mojang.CompatMathHelper; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; import lombok.Getter; import me.jellysquid.mods.sodium.client.world.cloned.ChunkRenderContext; import me.jellysquid.mods.sodium.client.world.cloned.ClonedChunkSection; @@ -196,6 +197,10 @@ public class WorldSlice implements IBlockAccess { blockBrightness = min; } + if (DynamicLights.isEnabled() && !getBlock(x, y, z).isOpaqueCube()) { + return DynamicLights.get().getLightmapWithDynamicLight(x, y, z, (skyBrightness << 20 | blockBrightness << 4)); + } + return skyBrightness << 20 | blockBrightness << 4; } diff --git a/src/main/resources/assets/notfine/lang/en_US.lang b/src/main/resources/assets/notfine/lang/en_US.lang index 2d7f63e7f..48aed8602 100644 --- a/src/main/resources/assets/notfine/lang/en_US.lang +++ b/src/main/resources/assets/notfine/lang/en_US.lang @@ -17,6 +17,8 @@ options.guiScale.massive=Massive options.downfall_distance=Weather Effects options.dynamic_fov=Dynamic FOV options.dynamic_fov.tooltip=If enabled, the game's field of view will change based on the player's speed. +options.dynamic_lights=Dynamic Lights +options.dynamic_lights.tooltip=Enables dynamic lights. Modes: OFF - off, FASTEST - 500ms delay, FAST - 250ms delay, FANCY - no delay options.mode_cloud_translucency=Cloud Translucency options.mode_clouds=Clouds options.mode_dropped_items=Dropped Items