diff --git a/build.gradle b/build.gradle index 747ae3e32..a1238367b 100644 --- a/build.gradle +++ b/build.gradle @@ -105,7 +105,7 @@ subprojects { runs { client { property 'item_asset_export.render.namespaces', 'exposure' - vmArgs '-XX:+AllowEnhancedClassRedefinition', '-XX:HotswapAgent=fatjar', '-Dsodium.checks.issue2561=false' + vmArgs '-XX:+.AllowEnhancedClassRedefinition', '-XX:HotswapAgent=fatjar', '-Dsodium.checks.issue2561=false' } } } diff --git a/common/src/main/java/io/github/mortuusars/exposure/Config.java b/common/src/main/java/io/github/mortuusars/exposure/Config.java index 2b60b2b1e..02a2f5748 100644 --- a/common/src/main/java/io/github/mortuusars/exposure/Config.java +++ b/common/src/main/java/io/github/mortuusars/exposure/Config.java @@ -269,6 +269,9 @@ public static class Common { public static class Client { public static final ForgeConfigSpec SPEC; + // Camera + public static final ForgeConfigSpec.BooleanValue USE_FRAME_STACKING; + // UI public static final ForgeConfigSpec.BooleanValue RECIPE_TOOLTIPS_WITHOUT_JEI; public static final ForgeConfigSpec.BooleanValue CAMERA_SHOW_TOOLTIP_DETAILS; @@ -330,6 +333,17 @@ public static class Client { static { ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder(); + { + builder.push("camera"); + + USE_FRAME_STACKING = builder + .comment("Use more realistic frame stacking, instead of just brightening an image." + + "May cause a client-side freeze. Default: false") + .define("use_frame_stacking", false); + + builder.pop(); + } + { builder.push("ui"); diff --git a/common/src/main/java/io/github/mortuusars/exposure/client/capture/action/CaptureAction.java b/common/src/main/java/io/github/mortuusars/exposure/client/capture/action/CaptureAction.java index fc088d9bd..d65f450d1 100644 --- a/common/src/main/java/io/github/mortuusars/exposure/client/capture/action/CaptureAction.java +++ b/common/src/main/java/io/github/mortuusars/exposure/client/capture/action/CaptureAction.java @@ -50,7 +50,9 @@ static CaptureAction forceRegularOrSelfieCamera(@Nullable CameraHolder holder) { } static CaptureAction hideGui() { - return new HideGuiAction(); + // Looks very weird with my code, so I have to remove it + return EMPTY; + //return new HideGuiAction(); } static CaptureAction disablePostEffect() { diff --git a/common/src/main/java/io/github/mortuusars/exposure/client/capture/task/StackedScreenshotCaptureTask.java b/common/src/main/java/io/github/mortuusars/exposure/client/capture/task/StackedScreenshotCaptureTask.java new file mode 100644 index 000000000..303fb9479 --- /dev/null +++ b/common/src/main/java/io/github/mortuusars/exposure/client/capture/task/StackedScreenshotCaptureTask.java @@ -0,0 +1,127 @@ +package io.github.mortuusars.exposure.client.capture.task; + +import com.mojang.blaze3d.platform.NativeImage; +import io.github.mortuusars.exposure.client.image.WrappedNativeImage; +import io.github.mortuusars.exposure.client.util.Minecrft; +import io.github.mortuusars.exposure.util.cycles.task.Result; +import io.github.mortuusars.exposure.util.cycles.task.Task; +import io.github.mortuusars.exposure.client.image.Image; +import net.minecraft.client.Minecraft; +import net.minecraft.client.Screenshot; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class StackedScreenshotCaptureTask extends Task> { + @Nullable + private CompletableFuture> future; + + private final int framesToCapture; + private final float shutterBrightness; + private int framesCaptured = 0; + private final List frames = new ArrayList<>(); + private long lastGameTick = -1; + + public StackedScreenshotCaptureTask(int durationTicks, float shutterBrightness) { + this.framesToCapture = Math.max(1, durationTicks); + this.shutterBrightness = shutterBrightness; + this.lastGameTick = getCurrentGameTick(); + } + + @Override + public @NotNull CompletableFuture> execute() { + if (future == null) { + future = new CompletableFuture<>(); + } + return future; + } + + @Override + public void tick() { + if (future == null || future.isDone()) { + return; + } + if (getCurrentGameTick() == lastGameTick) return; + lastGameTick = getCurrentGameTick(); + + try { + NativeImage img = Screenshot.takeScreenshot(Minecraft.getInstance().getMainRenderTarget()); + frames.add(img); + framesCaptured++; + } catch (Exception e) { + for (NativeImage frame : frames) { + frame.close(); + } + future.completeExceptionally(e); + return; + } + + if (framesCaptured >= framesToCapture && !future.isDone()) { + try { + NativeImage accumulated = accumulateFrames(frames, shutterBrightness); + future.complete(Result.success(new WrappedNativeImage(accumulated))); + } catch (Exception e) { + for (NativeImage frame : frames) { + frame.close(); + } + future.completeExceptionally(e); + } + } + } + + private NativeImage accumulateFrames(List images, float exposureMultiplier) { + if (images.isEmpty()) throw new IllegalArgumentException("Cannot accumulate 0 frames"); + + int w = images.get(0).getWidth(); + int h = images.get(0).getHeight(); + NativeImage result = new NativeImage(NativeImage.Format.RGBA, w, h, false); + + float perFrameWeight = exposureMultiplier / images.size(); + + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + float a = 0, b = 0, g = 0, r = 0; + + for (NativeImage frame : images) { + int pixel = frame.getPixelRGBA(x, y); + float frameA = ((pixel >> 24) & 0xFF) / 255f; + float frameB = ((pixel >> 16) & 0xFF) / 255f; + float frameG = ((pixel >> 8) & 0xFF) / 255f; + float frameR = (pixel & 0xFF) / 255f; + + a += frameA; + b += frameB * perFrameWeight; + g += frameG * perFrameWeight; + r += frameR * perFrameWeight; + } + + // Clamp to valid range [0, 1] + a = Math.min(1f, Math.max(0f, a / images.size())); + b = Math.min(1f, Math.max(0f, b)); + g = Math.min(1f, Math.max(0f, g)); + r = Math.min(1f, Math.max(0f, r)); + + int outA = (int) (a * 255); + int outB = (int) (b * 255); + int outG = (int) (g * 255); + int outR = (int) (r * 255); + + result.setPixelRGBA(x, y, (outA << 24) | (outB << 16) | (outG << 8) | outR); + } + } + + // Close all source frames to prevent memory leaks + for (NativeImage frame : images) { + frame.close(); + } + + return result; + } + + private long getCurrentGameTick() { + return Minecrft.level().getGameTime(); + } +} diff --git a/common/src/main/java/io/github/mortuusars/exposure/client/capture/template/CameraCaptureTemplate.java b/common/src/main/java/io/github/mortuusars/exposure/client/capture/template/CameraCaptureTemplate.java index 4f42a157b..881319010 100644 --- a/common/src/main/java/io/github/mortuusars/exposure/client/capture/template/CameraCaptureTemplate.java +++ b/common/src/main/java/io/github/mortuusars/exposure/client/capture/template/CameraCaptureTemplate.java @@ -6,7 +6,10 @@ import io.github.mortuusars.exposure.client.capture.action.CaptureAction; import io.github.mortuusars.exposure.client.capture.palettizer.Palettizer; import io.github.mortuusars.exposure.client.capture.saving.ExposureUploader; +import io.github.mortuusars.exposure.client.capture.task.StackedScreenshotCaptureTask; +import io.github.mortuusars.exposure.client.image.Image; import io.github.mortuusars.exposure.client.util.Minecrft; +import io.github.mortuusars.exposure.util.cycles.task.Result; import io.github.mortuusars.exposure.world.camera.capture.CaptureParameters; import io.github.mortuusars.exposure.world.camera.capture.Projection; import io.github.mortuusars.exposure.data.ColorPalette; @@ -36,17 +39,26 @@ public Task createTask(CaptureParameters params) { } @Nullable CameraHolder holder = entity instanceof CameraHolder cameraHolder ? cameraHolder : null; - Holder palette = getColorPalette(params); - Task captureTask = Capture.of(Capture.screenshot(), + final boolean USE_FRAME_STACKING = Config.Client.USE_FRAME_STACKING.get(); + Task> screenshotTask = USE_FRAME_STACKING + ? new StackedScreenshotCaptureTask( + params.getShutterSpeed().getDurationTicks(), + // I decrease it because else it looks much brighter than without stacking + params.getShutterSpeed().getBrightness() * 0.5f) + : Capture.screenshot(); + + Task captureTask = Capture.of(screenshotTask, CaptureAction.setCameraEntity(entity), CaptureAction.forceRegularOrSelfieCamera(holder), CaptureAction.optional(params.fov(), CaptureAction::setFov), + // Looks weird with my code + // See: HideGuiAction.java CaptureAction.hideGui(), CaptureAction.optional(!Config.Client.KEEP_POST_EFFECT.get(), CaptureAction::disablePostEffect), CaptureAction.setFilter(params.filter()), - CaptureAction.modifyGamma(params.getShutterSpeed()), + CaptureAction.optional(!USE_FRAME_STACKING, CaptureAction.modifyGamma(params.getShutterSpeed())), CaptureAction.optional(params.getFlash(), () -> CaptureAction.flash(entity))) .handleErrorAndGetResult(printCasualErrorInChat()) .thenAsync(applyEffectsToImage(params)) diff --git a/gradle.properties b/gradle.properties index 9dac8eb35..140d2f017 100644 --- a/gradle.properties +++ b/gradle.properties @@ -41,4 +41,4 @@ create_version_range_reason=Exposure 1.9.12+ needs Create 6.0.7+ for sequenced f create_version = 6.0.8-289 ponder_version = 1.0.91 flywheel_version = 1.0.5 -registrate_version = MC1.20-1.3.3 \ No newline at end of file +registrate_version = MC1.20-1.3.3 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index ccebba771..1b33c55ba 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3c44eb1b6..aaaabb3cb 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.4-bin.zip networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 index 79a61d421..23d15a936 --- a/gradlew +++ b/gradlew @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## # @@ -55,7 +57,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -83,10 +85,8 @@ done # This is normally unused # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -114,7 +114,7 @@ case "$( uname )" in #( NONSTOP* ) nonstop=true ;; esac -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar +CLASSPATH="\\\"\\\"" # Determine the Java command to use to start the JVM. @@ -133,10 +133,13 @@ location of your Java installation." fi else JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. @@ -144,7 +147,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. - # shellcheck disable=SC3045 + # shellcheck disable=SC2039,SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac @@ -152,7 +155,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then '' | soft) :;; #( *) # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. - # shellcheck disable=SC3045 + # shellcheck disable=SC2039,SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -197,16 +200,20 @@ if "$cygwin" || "$msys" ; then done fi -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ "$@" # Stop when "xargs" is not available. diff --git a/gradlew.bat b/gradlew.bat index 93e3f59f1..db3a6ac20 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -13,6 +13,8 @@ @rem See the License for the specific language governing permissions and @rem limitations under the License. @rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem @if "%DEBUG%"=="" @echo off @rem ########################################################################## @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -57,22 +59,22 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail :execute @rem Setup the command line -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar +set CLASSPATH= @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* :end @rem End local scope for the variables with windows NT shell