From 2242ac909eb6090d0a0ec4f1a43be37c7904deb0 Mon Sep 17 00:00:00 2001 From: Maximilian Stiede Date: Mon, 6 Mar 2023 05:29:58 +0100 Subject: [PATCH 1/5] extract canvas size code from Scene into separate CanvasConfig class --- .../chunky/renderer/DefaultRenderManager.java | 19 +- .../chunky/renderer/PathTracingRenderer.java | 11 +- .../chunky/renderer/PreviewRenderer.java | 11 +- .../chunky/renderer/TileBasedRenderer.java | 6 +- .../renderer/export/PfmExportFormat.java | 2 +- .../renderer/export/PngExportFormat.java | 15 +- .../renderdump/AbstractDumpFormat.java | 18 +- .../renderdump/ClassicDumpFormat.java | 12 +- .../chunky/renderer/scene/CanvasConfig.java | 191 ++++++++++++++++++ .../se/llbit/chunky/renderer/scene/Scene.java | 168 +++------------ .../scene/SynchronousSceneManager.java | 2 +- .../src/java/se/llbit/chunky/ui/ChunkMap.java | 8 +- .../se/llbit/chunky/ui/RenderCanvasFx.java | 6 +- .../ui/controller/ChunkyFxController.java | 2 +- .../chunky/ui/render/tabs/GeneralTab.java | 28 +-- .../llbit/imageformats/pfm/PfmFileWriter.java | 6 +- .../imageformats/tiff/TiffFileWriter.java | 4 +- .../chunky/renderer/BlankRenderTest.java | 5 +- .../renderer/renderdump/RenderDumpTest.java | 5 +- 19 files changed, 300 insertions(+), 219 deletions(-) create mode 100644 chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java diff --git a/chunky/src/java/se/llbit/chunky/renderer/DefaultRenderManager.java b/chunky/src/java/se/llbit/chunky/renderer/DefaultRenderManager.java index aef07453de..24bce4c55c 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/DefaultRenderManager.java +++ b/chunky/src/java/se/llbit/chunky/renderer/DefaultRenderManager.java @@ -379,9 +379,7 @@ private void updateRenderState(Scene scene) { * @return the current rendering speed in samples per second (SPS) */ private int samplesPerSecond() { - int canvasWidth = bufferedScene.canvasWidth(); - int canvasHeight = bufferedScene.canvasHeight(); - long pixelsPerFrame = (long) canvasWidth * canvasHeight; + long pixelsPerFrame = bufferedScene.canvasConfig.getPixelCount(); double renderTime = bufferedScene.renderTime / 1000.0; return (int) ((bufferedScene.spp * pixelsPerFrame) / renderTime); } @@ -423,19 +421,21 @@ protected void finalizeFrame(boolean force) { if (filter instanceof PixelPostProcessingFilter) { PixelPostProcessingFilter pixelFilter = (PixelPostProcessingFilter) filter; - int width = bufferedScene.width; - int height = bufferedScene.height; + int width = bufferedScene.canvasConfig.getWidth(); + int height = bufferedScene.canvasConfig.getHeight(); + int totalPixelCount = bufferedScene.canvasConfig.getPixelCount(); + double[] sampleBuffer = bufferedScene.getSampleBuffer(); double exposure = bufferedScene.getExposure(); // Split up to 10 tasks per thread int tasksPerThread = 10; - int pixelsPerTask = (bufferedScene.width * bufferedScene.height) / (pool.threads * tasksPerThread - 1); + int pixelsPerTask = (totalPixelCount / (pool.threads * tasksPerThread - 1)); ArrayList jobs = new ArrayList<>(pool.threads * tasksPerThread); - for (int i = 0; i < bufferedScene.width * bufferedScene.height; i += pixelsPerTask) { + for (int i = 0; i < totalPixelCount; i += pixelsPerTask) { int start = i; - int end = Math.min(bufferedScene.width * bufferedScene.height, i + pixelsPerTask); + int end = Math.min(totalPixelCount, i + pixelsPerTask); jobs.add(pool.submit(worker -> { double[] pixelbuffer = new double[3]; @@ -572,7 +572,8 @@ public RenderStatus getRenderStatus() { @Override public void withSampleBufferProtected(SampleBufferConsumer consumer) { synchronized (bufferedScene) { - consumer.accept(bufferedScene.getSampleBuffer(), bufferedScene.width, bufferedScene.height); + consumer.accept(bufferedScene.getSampleBuffer(), + bufferedScene.canvasConfig.getWidth(), bufferedScene.canvasConfig.getHeight()); } } diff --git a/chunky/src/java/se/llbit/chunky/renderer/PathTracingRenderer.java b/chunky/src/java/se/llbit/chunky/renderer/PathTracingRenderer.java index 946eb5a92f..73145e746b 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/PathTracingRenderer.java +++ b/chunky/src/java/se/llbit/chunky/renderer/PathTracingRenderer.java @@ -52,13 +52,12 @@ public String getDescription() { public void render(DefaultRenderManager manager) throws InterruptedException { Scene scene = manager.bufferedScene; - int width = scene.width; - int height = scene.height; + int width = scene.canvasConfig.getWidth(); - int fullWidth = scene.getFullWidth(); - int fullHeight = scene.getFullHeight(); - int cropX = scene.getCropX(); - int cropY = scene.getCropY(); + int fullWidth = scene.canvasConfig.getCropWidth(); + int fullHeight = scene.canvasConfig.getCropHeight(); + int cropX = scene.canvasConfig.getCropX(); + int cropY = scene.canvasConfig.getCropY(); int sppPerPass = manager.context.sppPerPass(); Camera cam = scene.camera(); diff --git a/chunky/src/java/se/llbit/chunky/renderer/PreviewRenderer.java b/chunky/src/java/se/llbit/chunky/renderer/PreviewRenderer.java index 9822090996..2ebb242311 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/PreviewRenderer.java +++ b/chunky/src/java/se/llbit/chunky/renderer/PreviewRenderer.java @@ -58,13 +58,12 @@ public void render(DefaultRenderManager manager) throws InterruptedException { Scene scene = manager.bufferedScene; - int width = scene.width; - int height = scene.height; + int width = scene.canvasConfig.getWidth(); - int fullWidth = scene.getFullWidth(); - int fullHeight = scene.getFullHeight(); - int cropX = scene.getCropX(); - int cropY = scene.getCropY(); + int fullWidth = scene.canvasConfig.getCropWidth(); + int fullHeight = scene.canvasConfig.getCropHeight(); + int cropX = scene.canvasConfig.getCropX(); + int cropY = scene.canvasConfig.getCropY(); Camera cam = scene.camera(); double halfWidth = fullWidth / (2.0 * fullHeight); diff --git a/chunky/src/java/se/llbit/chunky/renderer/TileBasedRenderer.java b/chunky/src/java/se/llbit/chunky/renderer/TileBasedRenderer.java index 3750118cc7..f87039e1ae 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/TileBasedRenderer.java +++ b/chunky/src/java/se/llbit/chunky/renderer/TileBasedRenderer.java @@ -87,10 +87,10 @@ protected void submitTiles(DefaultRenderManager manager, BiConsumer= 179 @@ -85,10 +86,10 @@ private void writePanoramaMetaData(Scene scene, PngFileWriter writer) throws IOE "XML:com.adobe.xmp", String.format( PNG_PANORAMA_META_ADOBE_RDF_XML, - scene.canvasHeight(), - scene.canvasWidth(), - scene.canvasHeight(), - scene.canvasWidth() + scene.canvasConfig.getHeight(), + scene.canvasConfig.getWidth(), + scene.canvasConfig.getHeight(), + scene.canvasConfig.getWidth() ) )); } diff --git a/chunky/src/java/se/llbit/chunky/renderer/renderdump/AbstractDumpFormat.java b/chunky/src/java/se/llbit/chunky/renderer/renderdump/AbstractDumpFormat.java index 49a212d57a..818c766617 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/renderdump/AbstractDumpFormat.java +++ b/chunky/src/java/se/llbit/chunky/renderer/renderdump/AbstractDumpFormat.java @@ -65,30 +65,30 @@ public void load(DataInputStream inputStream, Scene scene, TaskTracker taskTrack throws IOException, IllegalStateException { double[] samples = scene.getSampleBuffer(); - try (TaskTracker.Task task = taskTracker.task("Loading render dump", scene.width * scene.height)) { + try (TaskTracker.Task task = taskTracker.task("Loading render dump", scene.canvasConfig.getPixelCount())) { readHeader(inputStream, scene); readSamples(inputStream, scene, (index, r, g, b) -> { int offset = index * 3; samples[offset + 0] = r; samples[offset + 1] = g; samples[offset + 2] = b; - }, i -> task.updateInterval(i, scene.width)); + }, i -> task.updateInterval(i, scene.canvasConfig.getWidth())); } } @Override public void save(DataOutputStream outputStream, Scene scene, TaskTracker taskTracker) throws IOException { - try (TaskTracker.Task task = taskTracker.task("Saving render dump", scene.width * scene.height)) { + try (TaskTracker.Task task = taskTracker.task("Saving render dump", scene.canvasConfig.getPixelCount())) { writeHeader(outputStream, scene); - writeSamples(outputStream, scene, i -> task.updateInterval(i, scene.width)); + writeSamples(outputStream, scene, i -> task.updateInterval(i, scene.canvasConfig.getWidth())); } } @Override public void merge(DataInputStream inputStream, Scene scene, TaskTracker taskTracker) throws IOException, IllegalStateException { - try (TaskTracker.Task task = taskTracker.task("Merging render dump", scene.width * scene.height)) { + try (TaskTracker.Task task = taskTracker.task("Merging render dump", scene.canvasConfig.getPixelCount())) { int sceneSpp = scene.spp; long previousRenderTime = scene.renderTime; @@ -104,7 +104,7 @@ public void merge(DataInputStream inputStream, Scene scene, TaskTracker taskTrac samples[offset + 0] = (samples[offset + 0] * sceneSpp + r * dumpSpp) * sinv; samples[offset + 1] = (samples[offset + 1] * sceneSpp + g * dumpSpp) * sinv; samples[offset + 2] = (samples[offset + 2] * sceneSpp + b * dumpSpp) * sinv; - }, i -> task.updateInterval(i, scene.width)); + }, i -> task.updateInterval(i, scene.canvasConfig.getWidth())); scene.spp += sceneSpp; scene.renderTime += previousRenderTime; @@ -115,7 +115,7 @@ protected void readHeader(DataInputStream inputStream, Scene scene) throws IOExc int width = inputStream.readInt(); int height = inputStream.readInt(); - if (width != scene.canvasWidth() || height != scene.canvasHeight()) { + if (width != scene.canvasConfig.getWidth() || height != scene.canvasConfig.getHeight()) { throw new IllegalStateException("Scene size does not match dump size"); } @@ -124,8 +124,8 @@ protected void readHeader(DataInputStream inputStream, Scene scene) throws IOExc } protected void writeHeader(DataOutputStream outputStream, Scene scene) throws IOException { - outputStream.writeInt(scene.width); - outputStream.writeInt(scene.height); + outputStream.writeInt(scene.canvasConfig.getWidth()); + outputStream.writeInt(scene.canvasConfig.getHeight()); outputStream.writeInt(scene.spp); outputStream.writeLong(scene.renderTime); } diff --git a/chunky/src/java/se/llbit/chunky/renderer/renderdump/ClassicDumpFormat.java b/chunky/src/java/se/llbit/chunky/renderer/renderdump/ClassicDumpFormat.java index 0aba0ebf34..5c156842ad 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/renderdump/ClassicDumpFormat.java +++ b/chunky/src/java/se/llbit/chunky/renderer/renderdump/ClassicDumpFormat.java @@ -69,9 +69,9 @@ protected void readSamples(DataInputStream inputStream, Scene scene, double r, g, b; // Warning: This format writes in column major order - for (int x = 0; x < scene.width; x++) { - for (int y = 0; y < scene.height; y++) { - pixelIndex = (y * scene.width + x); + for (int x = 0; x < scene.canvasConfig.getWidth(); x++) { + for (int y = 0; y < scene.canvasConfig.getHeight(); y++) { + pixelIndex = (y * scene.canvasConfig.getWidth() + x); r = inputStream.readDouble(); g = inputStream.readDouble(); b = inputStream.readDouble(); @@ -90,9 +90,9 @@ protected void writeSamples(DataOutputStream outputStream, Scene scene, int done = 0; // Warning: This format writes in column major order - for (int x = 0; x < scene.width; ++x) { - for (int y = 0; y < scene.height; ++y) { - offset = (y * scene.width + x) * 3; + for (int x = 0; x < scene.canvasConfig.getWidth(); ++x) { + for (int y = 0; y < scene.canvasConfig.getHeight(); ++y) { + offset = (y * scene.canvasConfig.getWidth() + x) * 3; outputStream.writeDouble(samples[offset + 0]); outputStream.writeDouble(samples[offset + 1]); outputStream.writeDouble(samples[offset + 2]); diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java b/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java new file mode 100644 index 0000000000..cfce640c7e --- /dev/null +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java @@ -0,0 +1,191 @@ +package se.llbit.chunky.renderer.scene; + +import se.llbit.json.JsonObject; +import se.llbit.math.QuickMath; + +public class CanvasConfig { + /** + * Minimum canvas width. + */ + public static final int MIN_CANVAS_WIDTH = 20; + + /** + * Minimum canvas height. + */ + public static final int MIN_CANVAS_HEIGHT = 20; + + protected int width; + protected int height; + + protected int cropWidth = 0; + protected int cropHeight = 0; + + protected int cropX = 0; + protected int cropY = 0; + + public CanvasConfig(int width, int height) { + this.width = width; + this.height = height; + } + + public void copyState(CanvasConfig other) { + width = other.width; + height = other.height; + cropWidth = other.cropWidth; + cropHeight = other.cropHeight; + cropX = other.cropX; + cropY = other.cropY; + } + + /** + * @return Canvas width + */ + public int getWidth() { + return width; + } + + /** + * @return Canvas height + */ + public int getHeight() { + return height; + } + + public int getSavedCropWidth() { + return cropWidth; + } + + /** + * @return fullWidth if the canvas is cropped, otherwise width + */ + public int getCropWidth() { + if (isCanvasCropped()) { + return cropWidth; + } + return width; + } + + public int getSavedCropHeight() { + return cropHeight; + } + + /** + * @return fullHeight if the canvas is cropped, otherwise height + */ + public int getCropHeight() { + if (isCanvasCropped()) { + return cropHeight; + } + return height; + } + + public int getSavedCropX() { + return cropX; + } + + /** + * @return cropX if the canvas is cropped, otherwise 0 + */ + public int getCropX() { + if (isCanvasCropped()) { + return cropX; + } + return 0; + } + + /** + * @return cropY if the canvas is cropped, otherwise 0 + */ + public int getSavedCropY() { + return cropY; + } + + public int getCropY() { + if (isCanvasCropped()) { + return cropY; + } + return 0; + } + + public boolean isCanvasCropped() { + return cropWidth != 0 && cropHeight != 0; + } + + /** + * @return true if buffers have to be reinitialized + */ + public synchronized boolean setSize(int width, int height) { + int newWidth = Math.max(MIN_CANVAS_WIDTH, width); + int newHeight = Math.max(MIN_CANVAS_HEIGHT, height); + if (newWidth != this.width || newHeight != this.height) { + this.width = newWidth; + this.height = newHeight; + return true; + } + return false; + } + + /** + * @return true if something changed + */ + public synchronized boolean setCropSize( + int newCropWidth, int newCropHeight, + int newCropX, int newCropY + ) { + if (newCropWidth == 0 || newCropHeight == 0) { + // Crop disabled + newCropWidth = 0; + newCropHeight = 0; + newCropX = 0; + newCropY = 0; + } else { + // Crop enabled + newCropWidth = Math.max(width, newCropWidth); + newCropHeight = Math.max(height, newCropHeight); + newCropX = QuickMath.clamp(newCropX, 0, newCropWidth - width); + newCropY = QuickMath.clamp(newCropY, 0, newCropHeight - height); + } + if (newCropWidth != this.cropWidth || newCropHeight != this.cropHeight || + newCropX != this.cropX || newCropY != this.cropY) { + this.cropWidth = newCropWidth; + this.cropHeight = newCropHeight; + this.cropX = newCropX; + this.cropY = newCropY; + return true; + } + return false; + } + + public int getPixelCount() { + return width * height; + } + + public void writeJsonData(JsonObject json) { + json.add("width", width); + json.add("height", height); + json.add("fullWidth", cropWidth); + json.add("fullHeight", cropHeight); + json.add("cropX", cropX); + json.add("cropY", cropY); + } + + /** + * @return true if buffers have to be reinitialized + */ + public boolean importJsonData(JsonObject json) { + int newWidth = json.get("width").intValue(width); + int newHeight = json.get("height").intValue(height); + + cropWidth = json.get("fullWidth").intValue(cropWidth); + cropHeight = json.get("fullHeight").intValue(cropHeight); + cropX = json.get("cropX").intValue(cropX); + cropY = json.get("cropY").intValue(cropY); + + if (width != newWidth || height != newHeight) { + width = newWidth; + height = newHeight; + return true; + } + return false; + } +} diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java index 3d967dad87..3f4325d76a 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java @@ -88,12 +88,6 @@ public class Scene implements JsonSerializable, Refreshable { protected static final double fSubSurface = 0.3; - /** Minimum canvas width. */ - public static final int MIN_CANVAS_WIDTH = 20; - - /** Minimum canvas height. */ - public static final int MIN_CANVAS_HEIGHT = 20; - /** * Minimum exposure. */ @@ -153,20 +147,10 @@ public class Scene implements JsonSerializable, Refreshable { public int sdfVersion = -1; public String name = "default_" + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()); - /** - * Canvas width. - */ - public int width; - - /** - * Canvas height. - */ - public int height; - - public int fullWidth = 0; - public int fullHeight = 0; - public int cropX = 0; - public int cropY = 0; + public CanvasConfig canvasConfig = new CanvasConfig( + PersistentSettings.get3DCanvasWidth(), + PersistentSettings.get3DCanvasHeight() + ); public PostProcessingFilter postProcessingFilter = DEFAULT_POSTPROCESSING_FILTER; public PictureExportFormat outputMode = PictureExportFormats.PNG; @@ -330,8 +314,6 @@ public class Scene implements JsonSerializable, Refreshable { * fromJson(), or importFromJson(), or by calling initBuffers(). */ public Scene() { - width = PersistentSettings.get3DCanvasWidth(); - height = PersistentSettings.get3DCanvasHeight(); sppTarget = PersistentSettings.getSppTargetDefault(); palette = new BlockPalette(); @@ -370,10 +352,10 @@ public static void exportToZip(File sceneDirectory, String name, File targetFile * scene and after scene canvas size changes. */ public synchronized void initBuffers() { - frontBuffer = new BitmapImage(width, height); - backBuffer = new BitmapImage(width, height); - alphaChannel = new byte[width * height]; - samples = new double[width * height * 3]; + frontBuffer = new BitmapImage(canvasConfig.width, canvasConfig.height); + backBuffer = new BitmapImage(canvasConfig.width, canvasConfig.height); + alphaChannel = new byte[canvasConfig.getPixelCount()]; + samples = new double[canvasConfig.getPixelCount() * 3]; } /** @@ -453,20 +435,14 @@ public synchronized void copyState(Scene other, boolean copyChunks) { finalized = false; + canvasConfig.copyState(other.canvasConfig); if (samples != other.samples) { - width = other.width; - height = other.height; backBuffer = other.backBuffer; frontBuffer = other.frontBuffer; alphaChannel = other.alphaChannel; samples = other.samples; } - fullWidth = other.fullWidth; - fullHeight = other.fullHeight; - cropX = other.cropX; - cropY = other.cropY; - octreeImplementation = other.octreeImplementation; animationTime = other.animationTime; @@ -1909,95 +1885,21 @@ public void setTargetSpp(int value) { * new canvas size is not identical to the current canvas size. */ public synchronized void setCanvasSize(int canvasWidth, int canvasHeight) { - int newWidth = Math.max(MIN_CANVAS_WIDTH, canvasWidth); - int newHeight = Math.max(MIN_CANVAS_HEIGHT, canvasHeight); - if (newWidth != width || newHeight != height) { - width = newWidth; - height = newHeight; + if (canvasConfig.setSize(canvasWidth, canvasHeight)) { initBuffers(); refresh(); } } - public synchronized void setCanvasCropSize(int canvasWidth, int canvasHeight, int fullWidth, int fullHeight, int cropX, int cropY) { - canvasWidth = Math.max(MIN_CANVAS_WIDTH, canvasWidth); - canvasHeight = Math.max(MIN_CANVAS_HEIGHT, canvasHeight); - if (fullWidth == 0 || fullHeight == 0) { - // Crop disabled - fullWidth = 0; - fullHeight = 0; - cropX = 0; - cropY = 0; - } else { - // Crop enabled - fullWidth = Math.max(canvasWidth(), fullWidth); - fullHeight = Math.max(canvasHeight(), fullHeight); - cropX = QuickMath.clamp(cropX, 0, fullWidth-canvasWidth()); - cropY = QuickMath.clamp(cropY, 0, fullHeight-canvasHeight()); - } - boolean changed = false; - if (fullWidth != this.fullWidth || fullHeight != this.fullHeight || cropX != this.cropX || cropY != this.cropY) { - changed = true; - this.fullWidth = fullWidth; - this.fullHeight = fullHeight; - this.cropX = cropX; - this.cropY = cropY; - } - if (canvasWidth != this.width || canvasHeight != this.height) { - changed = true; - this.width = canvasWidth; - this.height = canvasHeight; - initBuffers(); - } - if (changed) { - refresh(); - } - } - - public boolean isCanvasCropped() { - return fullWidth != 0 && fullHeight != 0; - } - - /** - * @return Canvas width - */ - public int canvasWidth() { - return width; - } - - /** - * @return Canvas height - */ - public int canvasHeight() { - return height; - } - - public int getFullWidth() { - if (isCanvasCropped()) { - return fullWidth; - } - return width; - } - - public int getFullHeight() { - if (isCanvasCropped()) { - return fullHeight; - } - return height; - } - - public int getCropX() { - if (isCanvasCropped()) { - return cropX; - } - return 0; - } - - public int getCropY() { - if (isCanvasCropped()) { - return cropY; - } - return 0; + public synchronized void setCanvasCropSize( + int canvasWidth, int canvasHeight, + int fullWidth, int fullHeight, + int cropX, int cropY + ) { + boolean changedBuffers = canvasConfig.setSize(canvasWidth, canvasHeight); + boolean cropChanged = canvasConfig.setCropSize(fullWidth, fullHeight, cropX, cropY); + if(changedBuffers) initBuffers(); + if(changedBuffers || cropChanged) refresh(); } /** @@ -2063,15 +1965,15 @@ private void computeAlpha(TaskTracker taskTracker) { AtomicInteger done = new AtomicInteger(0); Chunky.getCommonThreads().submit(() -> { - IntStream.range(0, width).parallel().forEach(x -> { + IntStream.range(0, canvasConfig.width).parallel().forEach(x -> { WorkerState state = new WorkerState(); state.ray = new Ray(); - for (int y = 0; y < height; y++) { + for (int y = 0; y < canvasConfig.height; y++) { computeAlpha(x, y, state); } - task.update(width, done.incrementAndGet()); + task.update(canvasConfig.width, done.incrementAndGet()); }); }).get(); @@ -2096,7 +1998,7 @@ public void postProcessFrame(TaskTracker.Task task) { filter = PreviewFilter.INSTANCE; } filter.processFrame( - width, height, + canvasConfig.width, canvasConfig.height, samples, backBuffer, exposure, task @@ -2283,8 +2185,8 @@ private boolean tryLoadDump(SceneIOProvider context, String fileName, TaskTracke */ public void computeAlpha(int x, int y, WorkerState state) { Ray ray = state.ray; - double halfWidth = width / (2.0 * height); - double invHeight = 1.0 / height; + double halfWidth = canvasConfig.width / (2.0 * canvasConfig.height); + double invHeight = 1.0 / canvasConfig.height; // Rotated grid supersampling. double[][] offsets = new double[][] { @@ -2309,7 +2211,7 @@ public void computeAlpha(int x, int y, WorkerState state) { occlusion += PreviewRayTracer.skyOcclusion(this, state); } - alphaChannel[y * width + x] = (byte) (255 * occlusion * 0.25 + 0.5); + alphaChannel[y * canvasConfig.width + x] = (byte) (255 * occlusion * 0.25 + 0.5); } /** @@ -2573,12 +2475,7 @@ public void setUseCustomWaterColor(boolean value) { JsonObject json = new JsonObject(); json.add("sdfVersion", SDF_VERSION); json.add("name", name); - json.add("width", width); - json.add("height", height); - json.add("fullWidth", fullWidth); - json.add("fullHeight", fullHeight); - json.add("cropX", cropX); - json.add("cropY", cropY); + canvasConfig.writeJsonData(json); json.add("yClipMin", yClipMin); json.add("yClipMax", yClipMax); json.add("yMin", yMin); @@ -2807,19 +2704,10 @@ public synchronized void importFromJson(JsonObject json) { // TODO: check if we actually need to reset the scene based on changed settings. refresh(); - int newWidth = json.get("width").intValue(width); - int newHeight = json.get("height").intValue(height); - if (width != newWidth || height != newHeight || samples == null) { - width = newWidth; - height = newHeight; + if(canvasConfig.importJsonData(json) || samples == null) { initBuffers(); } - fullWidth = json.get("fullWidth").intValue(fullWidth); - fullHeight = json.get("fullHeight").intValue(fullHeight); - cropX = json.get("cropX").intValue(cropX); - cropY = json.get("cropY").intValue(cropY); - yClipMin = json.get("yClipMin").asInt(yClipMin); yClipMax = json.get("yClipMax").asInt(yClipMax); yMin = json.get("yMin").asInt(Math.max(yClipMin, yMin)); diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/SynchronousSceneManager.java b/chunky/src/java/se/llbit/chunky/renderer/scene/SynchronousSceneManager.java index d881addb31..5b41ed969c 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/SynchronousSceneManager.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/SynchronousSceneManager.java @@ -344,7 +344,7 @@ public void removeChangeListener(BiConsumer listener) { protected void mergeDump(File dumpFile) { synchronized (scene) { renderManager.withSampleBufferProtected((samples, width, height) -> { - if (width != scene.width || height != scene.height) { + if (width != scene.canvasConfig.getWidth() || height != scene.canvasConfig.getHeight()) { throw new Error("Failed to merge render dump - wrong canvas size."); } scene.mergeDump(dumpFile, taskTracker); diff --git a/chunky/src/java/se/llbit/chunky/ui/ChunkMap.java b/chunky/src/java/se/llbit/chunky/ui/ChunkMap.java index 8f93151174..2e37b3d8ae 100644 --- a/chunky/src/java/se/llbit/chunky/ui/ChunkMap.java +++ b/chunky/src/java/se/llbit/chunky/ui/ChunkMap.java @@ -649,8 +649,8 @@ public void redrawMap() { public void selectVisibleChunks(ChunkView cv, se.llbit.chunky.renderer.scene.Scene scene) { Camera camera = scene.camera(); - int width = scene.canvasWidth(); - int height = scene.canvasHeight(); + int width = scene.canvasConfig.getWidth(); + int height = scene.canvasConfig.getHeight(); double halfWidth = width / (2.0 * height); @@ -702,8 +702,8 @@ public void selectVisibleChunks(ChunkView cv, se.llbit.chunky.renderer.scene.Sce public static void drawViewBounds(GraphicsContext gc, ChunkView cv, se.llbit.chunky.renderer.scene.Scene scene) { Camera camera = scene.camera(); - int width = scene.canvasWidth(); - int height = scene.canvasHeight(); + int width = scene.canvasConfig.getWidth(); + int height = scene.canvasConfig.getHeight(); double halfWidth = width / (2.0 * height); diff --git a/chunky/src/java/se/llbit/chunky/ui/RenderCanvasFx.java b/chunky/src/java/se/llbit/chunky/ui/RenderCanvasFx.java index 9a5b84ad7b..d3e07e8ee2 100644 --- a/chunky/src/java/se/llbit/chunky/ui/RenderCanvasFx.java +++ b/chunky/src/java/se/llbit/chunky/ui/RenderCanvasFx.java @@ -98,9 +98,9 @@ public RenderCanvasFx(ChunkyFxController chunkyFxController, canvas = new Canvas(); synchronized (scene) { - canvas.setWidth(scene.width); - canvas.setHeight(scene.height); - image = new WritableImage(scene.width, scene.height); + canvas.setWidth(scene.canvasConfig.getWidth()); + canvas.setHeight(scene.canvasConfig.getHeight()); + image = new WritableImage(scene.canvasConfig.getWidth(), scene.canvasConfig.getHeight()); } canvasPane = new StackPane(canvas); diff --git a/chunky/src/java/se/llbit/chunky/ui/controller/ChunkyFxController.java b/chunky/src/java/se/llbit/chunky/ui/controller/ChunkyFxController.java index 8f6e6effde..c136e4b058 100644 --- a/chunky/src/java/se/llbit/chunky/ui/controller/ChunkyFxController.java +++ b/chunky/src/java/se/llbit/chunky/ui/controller/ChunkyFxController.java @@ -325,7 +325,7 @@ public void exportMapView() { CountDownLatch guiUpdateLatch = new CountDownLatch(1); Platform.runLater(() -> { synchronized (scene) { - canvas.setCanvasSize(scene.width, scene.height); + canvas.setCanvasSize(scene.canvasConfig.getWidth(), scene.canvasConfig.getHeight()); } updateTitle(); refreshSettings(); diff --git a/chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java b/chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java index 9e1a78b422..62af867420 100644 --- a/chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java +++ b/chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java @@ -173,18 +173,18 @@ public GeneralTab() throws IOException { cameraCropX.valueProperty().removeListener(canvasCropListener); cameraCropY.valueProperty().removeListener(canvasCropListener); - cameraCropWidth.valueProperty().set(scene.width); - cameraCropHeight.valueProperty().set(scene.height); - cameraCropX.valueProperty().set(scene.cropX); - cameraCropY.valueProperty().set(scene.cropY); + cameraCropWidth.valueProperty().set(scene.canvasConfig.getWidth()); + cameraCropHeight.valueProperty().set(scene.canvasConfig.getHeight()); + cameraCropX.valueProperty().set(scene.canvasConfig.getSavedCropX()); + cameraCropY.valueProperty().set(scene.canvasConfig.getSavedCropY()); cameraCropWidth.valueProperty().addListener(canvasCropListener); cameraCropHeight.valueProperty().addListener(canvasCropListener); cameraCropX.valueProperty().addListener(canvasCropListener); cameraCropY.valueProperty().addListener(canvasCropListener); - renderRegions.setSelected(scene.isCanvasCropped()); - canvasSizeInput.setSize(scene.getFullWidth(), scene.getFullHeight()); + renderRegions.setSelected(scene.canvasConfig.isCanvasCropped()); + canvasSizeInput.setSize(scene.canvasConfig.getCropWidth(), scene.canvasConfig.getCropHeight()); } @Override public String getTabTitle() { @@ -439,15 +439,15 @@ public GeneralTab() throws IOException { applySize.setOnAction(e -> canvasSizeInput.applyChanges()); makeDefaultSize.setTooltip(new Tooltip("Make the current canvas size the default.")); makeDefaultSize.setOnAction(e -> PersistentSettings - .set3DCanvasSize(scene.canvasWidth(), scene.canvasHeight())); + .set3DCanvasSize(scene.canvasConfig.getWidth(), scene.canvasConfig.getHeight())); renderRegionsArea.visibleProperty().bind(renderRegions.selectedProperty()); renderRegionsArea.managedProperty().bind(renderRegionsArea.visibleProperty()); renderRegions.selectedProperty().addListener((observable, oldValue, newValue) -> { if (newValue) { if (cameraCropWidth.getValue() == 0 || cameraCropHeight.getValue() == 0) { - cameraCropWidth.valueProperty().set(scene.getFullWidth()); - cameraCropHeight.valueProperty().set(scene.getFullHeight()); + cameraCropWidth.valueProperty().set(scene.canvasConfig.getCropWidth()); + cameraCropHeight.valueProperty().set(scene.canvasConfig.getCropHeight()); cameraCropX.valueProperty().set(0); cameraCropY.valueProperty().set(0); } @@ -473,16 +473,16 @@ private void updateCanvasSize(int width, int height) { } if (renderRegions.isSelected()) { scene.setCanvasCropSize(cameraCropWidth.getValue(), cameraCropHeight.getValue(), width, height, cameraCropX.getValue(), cameraCropY.getValue()); - if (cameraCropX.getValue() != scene.cropX) { - cameraCropX.valueProperty().set(scene.getCropX()); + if (cameraCropX.getValue() != scene.canvasConfig.getSavedCropX()) { + cameraCropX.valueProperty().set(scene.canvasConfig.getCropX()); } - if (cameraCropY.getValue() != scene.cropY) { - cameraCropY.valueProperty().set(scene.getCropY()); + if (cameraCropY.getValue() != scene.canvasConfig.getSavedCropY()) { + cameraCropY.valueProperty().set(scene.canvasConfig.getCropY()); } } else { scene.setCanvasCropSize(width, height, 0, 0, 0, 0); } - renderControls.getCanvas().setCanvasSize(scene.width, scene.height); + renderControls.getCanvas().setCanvasSize(scene.canvasConfig.getWidth(), scene.canvasConfig.getHeight()); } private void updateCanvasCrop() { diff --git a/chunky/src/java/se/llbit/imageformats/pfm/PfmFileWriter.java b/chunky/src/java/se/llbit/imageformats/pfm/PfmFileWriter.java index 91727dd859..49a62a4391 100644 --- a/chunky/src/java/se/llbit/imageformats/pfm/PfmFileWriter.java +++ b/chunky/src/java/se/llbit/imageformats/pfm/PfmFileWriter.java @@ -66,7 +66,7 @@ private void writeHeader(Scene scene, ByteOrder byteOrder) throws IOException { out.write(0x0a); // Declare Image Size - out.write((scene.canvasWidth()+" "+scene.canvasHeight()).getBytes(StandardCharsets.US_ASCII)); + out.write((scene.canvasConfig.getWidth()+" "+scene.canvasConfig.getHeight()).getBytes(StandardCharsets.US_ASCII)); out.write(0x0a); // Declare Byte Order @@ -75,8 +75,8 @@ private void writeHeader(Scene scene, ByteOrder byteOrder) throws IOException { } private void writePixelData(Scene scene, ByteOrder byteOrder, TaskTracker.Task task) throws IOException { - int width = scene.canvasWidth(); - int height = scene.canvasHeight(); + int width = scene.canvasConfig.getWidth(); + int height = scene.canvasConfig.getHeight(); double[] sampleBuffer = scene.getSampleBuffer(); diff --git a/chunky/src/java/se/llbit/imageformats/tiff/TiffFileWriter.java b/chunky/src/java/se/llbit/imageformats/tiff/TiffFileWriter.java index 13f7fe08fc..d0a3bd1ae9 100644 --- a/chunky/src/java/se/llbit/imageformats/tiff/TiffFileWriter.java +++ b/chunky/src/java/se/llbit/imageformats/tiff/TiffFileWriter.java @@ -237,8 +237,8 @@ private PixelPostProcessingFilter requirePixelPostProcessingFilter(Scene scene) public void write32(Scene scene, TaskTracker.Task task) throws IOException { PixelPostProcessingFilter filter = requirePixelPostProcessingFilter(scene); - int width = scene.canvasWidth(); - int height = scene.canvasHeight(); + int width = scene.canvasConfig.getWidth(); + int height = scene.canvasConfig.getHeight(); writeHeader(width, height, 4); double[] sampleBuffer = scene.getSampleBuffer(); diff --git a/chunky/src/test/se/llbit/chunky/renderer/BlankRenderTest.java b/chunky/src/test/se/llbit/chunky/renderer/BlankRenderTest.java index 177a91eca1..8fed6ebbf9 100644 --- a/chunky/src/test/se/llbit/chunky/renderer/BlankRenderTest.java +++ b/chunky/src/test/se/llbit/chunky/renderer/BlankRenderTest.java @@ -21,6 +21,7 @@ import se.llbit.chunky.main.Chunky; import se.llbit.chunky.main.ChunkyOptions; import se.llbit.chunky.renderer.projection.ProjectionMode; +import se.llbit.chunky.renderer.scene.CanvasConfig; import se.llbit.chunky.renderer.scene.Scene; import se.llbit.chunky.renderer.scene.Sky; import se.llbit.json.JsonObject; @@ -40,8 +41,8 @@ * only two samples per pixel. */ public class BlankRenderTest { - private static final int WIDTH = Math.max(10, Scene.MIN_CANVAS_WIDTH); - private static final int HEIGHT = Math.max(10, Scene.MIN_CANVAS_HEIGHT); + private static final int WIDTH = Math.max(10, CanvasConfig.MIN_CANVAS_WIDTH); + private static final int HEIGHT = Math.max(10, CanvasConfig.MIN_CANVAS_HEIGHT); /** * Renders one sample per pixel and checks that the color values are diff --git a/chunky/src/test/se/llbit/chunky/renderer/renderdump/RenderDumpTest.java b/chunky/src/test/se/llbit/chunky/renderer/renderdump/RenderDumpTest.java index be9721580a..363508d111 100644 --- a/chunky/src/test/se/llbit/chunky/renderer/renderdump/RenderDumpTest.java +++ b/chunky/src/test/se/llbit/chunky/renderer/renderdump/RenderDumpTest.java @@ -18,6 +18,7 @@ import org.junit.Before; import org.junit.Test; +import se.llbit.chunky.renderer.scene.CanvasConfig; import se.llbit.chunky.renderer.scene.Scene; import se.llbit.util.ProgressListener; import se.llbit.util.TaskTracker; @@ -35,8 +36,8 @@ import static org.junit.Assert.assertTrue; public class RenderDumpTest { - protected static final int testWidth = Scene.MIN_CANVAS_WIDTH; - protected static final int testHeight = Scene.MIN_CANVAS_HEIGHT; + protected static final int testWidth = CanvasConfig.MIN_CANVAS_WIDTH; + protected static final int testHeight = CanvasConfig.MIN_CANVAS_HEIGHT; protected static final int testSPP = 100; protected static final long testRenderTime = 654321L; From 7cd692613e91ed7b1992e273d7d0101577ddc4b5 Mon Sep 17 00:00:00 2001 From: Maximilian Stiede Date: Mon, 6 Mar 2023 20:58:14 +0100 Subject: [PATCH 2/5] Apply suggestion Co-authored-by: Tom Martin --- chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java index 3f4325d76a..64a7e38f77 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java @@ -1898,8 +1898,12 @@ public synchronized void setCanvasCropSize( ) { boolean changedBuffers = canvasConfig.setSize(canvasWidth, canvasHeight); boolean cropChanged = canvasConfig.setCropSize(fullWidth, fullHeight, cropX, cropY); - if(changedBuffers) initBuffers(); - if(changedBuffers || cropChanged) refresh(); + if(changedBuffers) { + initBuffers(); + } + if(changedBuffers || cropChanged) { + refresh(); + } } /** From 3caafe748ecb4eb17761bb530daa18bb3e83bc10 Mon Sep 17 00:00:00 2001 From: Maximilian Stiede Date: Tue, 8 Aug 2023 18:26:48 +0200 Subject: [PATCH 3/5] re-add getters with deprecation, make field access private, fix docs --- .../chunky/renderer/scene/CanvasConfig.java | 28 +++++--- .../se/llbit/chunky/renderer/scene/Scene.java | 71 +++++++++++++++---- 2 files changed, 78 insertions(+), 21 deletions(-) diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java b/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java index cfce640c7e..7ba895fe33 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java @@ -1,5 +1,6 @@ package se.llbit.chunky.renderer.scene; +import se.llbit.chunky.PersistentSettings; import se.llbit.json.JsonObject; import se.llbit.math.QuickMath; @@ -14,15 +15,21 @@ public class CanvasConfig { */ public static final int MIN_CANVAS_HEIGHT = 20; - protected int width; - protected int height; + private int width; + private int height; - protected int cropWidth = 0; - protected int cropHeight = 0; + private int cropWidth = 0; + private int cropHeight = 0; - protected int cropX = 0; - protected int cropY = 0; + private int cropX = 0; + private int cropY = 0; + public CanvasConfig() { + this( + PersistentSettings.get3DCanvasWidth(), + PersistentSettings.get3DCanvasHeight() + ); + } public CanvasConfig(int width, int height) { this.width = width; this.height = height; @@ -56,7 +63,7 @@ public int getSavedCropWidth() { } /** - * @return fullWidth if the canvas is cropped, otherwise width + * @return cropWidth if the canvas is cropped, otherwise width */ public int getCropWidth() { if (isCanvasCropped()) { @@ -70,7 +77,7 @@ public int getSavedCropHeight() { } /** - * @return fullHeight if the canvas is cropped, otherwise height + * @return cropHeight if the canvas is cropped, otherwise height */ public int getCropHeight() { if (isCanvasCropped()) { @@ -163,8 +170,11 @@ public int getPixelCount() { public void writeJsonData(JsonObject json) { json.add("width", width); json.add("height", height); + + // TODO: rename json.add("fullWidth", cropWidth); json.add("fullHeight", cropHeight); + json.add("cropX", cropX); json.add("cropY", cropY); } @@ -176,8 +186,10 @@ public boolean importJsonData(JsonObject json) { int newWidth = json.get("width").intValue(width); int newHeight = json.get("height").intValue(height); + // TODO: rename cropWidth = json.get("fullWidth").intValue(cropWidth); cropHeight = json.get("fullHeight").intValue(cropHeight); + cropX = json.get("cropX").intValue(cropX); cropY = json.get("cropY").intValue(cropY); diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java index 64a7e38f77..7aedce3af9 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java @@ -147,10 +147,7 @@ public class Scene implements JsonSerializable, Refreshable { public int sdfVersion = -1; public String name = "default_" + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()); - public CanvasConfig canvasConfig = new CanvasConfig( - PersistentSettings.get3DCanvasWidth(), - PersistentSettings.get3DCanvasHeight() - ); + public CanvasConfig canvasConfig = new CanvasConfig(); public PostProcessingFilter postProcessingFilter = DEFAULT_POSTPROCESSING_FILTER; public PictureExportFormat outputMode = PictureExportFormats.PNG; @@ -352,8 +349,8 @@ public static void exportToZip(File sceneDirectory, String name, File targetFile * scene and after scene canvas size changes. */ public synchronized void initBuffers() { - frontBuffer = new BitmapImage(canvasConfig.width, canvasConfig.height); - backBuffer = new BitmapImage(canvasConfig.width, canvasConfig.height); + frontBuffer = new BitmapImage(canvasConfig.getWidth(), canvasConfig.getHeight()); + backBuffer = new BitmapImage(canvasConfig.getWidth(), canvasConfig.getHeight()); alphaChannel = new byte[canvasConfig.getPixelCount()]; samples = new double[canvasConfig.getPixelCount() * 3]; } @@ -1906,6 +1903,54 @@ public synchronized void setCanvasCropSize( } } + /** + * @deprecated use {@link CanvasConfig} + */ + @Deprecated + public int canvasWidth() { + return canvasConfig.getWidth(); + } + + /** + * @deprecated use {@link CanvasConfig} + */ + @Deprecated + public int canvasHeight() { + return canvasConfig.getHeight(); + } + + /** + * @deprecated use {@link CanvasConfig} + */ + @Deprecated + public int getFullWidth() { + return canvasConfig.getCropWidth(); + } + + /** + * @deprecated use {@link CanvasConfig} + */ + @Deprecated + public int getFullHeight() { + return canvasConfig.getCropHeight(); + } + + /** + * @deprecated use {@link CanvasConfig} + */ + @Deprecated + public int getCropX() { + return canvasConfig.getCropX(); + } + + /** + * @deprecated use {@link CanvasConfig} + */ + @Deprecated + public int getCropY() { + return canvasConfig.getCropY(); + } + /** * Save a snapshot */ @@ -1969,15 +2014,15 @@ private void computeAlpha(TaskTracker taskTracker) { AtomicInteger done = new AtomicInteger(0); Chunky.getCommonThreads().submit(() -> { - IntStream.range(0, canvasConfig.width).parallel().forEach(x -> { + IntStream.range(0, canvasConfig.getWidth()).parallel().forEach(x -> { WorkerState state = new WorkerState(); state.ray = new Ray(); - for (int y = 0; y < canvasConfig.height; y++) { + for (int y = 0; y < canvasConfig.getHeight(); y++) { computeAlpha(x, y, state); } - task.update(canvasConfig.width, done.incrementAndGet()); + task.update(canvasConfig.getWidth(), done.incrementAndGet()); }); }).get(); @@ -2002,7 +2047,7 @@ public void postProcessFrame(TaskTracker.Task task) { filter = PreviewFilter.INSTANCE; } filter.processFrame( - canvasConfig.width, canvasConfig.height, + canvasConfig.getWidth(), canvasConfig.getHeight(), samples, backBuffer, exposure, task @@ -2189,8 +2234,8 @@ private boolean tryLoadDump(SceneIOProvider context, String fileName, TaskTracke */ public void computeAlpha(int x, int y, WorkerState state) { Ray ray = state.ray; - double halfWidth = canvasConfig.width / (2.0 * canvasConfig.height); - double invHeight = 1.0 / canvasConfig.height; + double halfWidth = canvasConfig.getWidth() / (2.0 * canvasConfig.getHeight()); + double invHeight = 1.0 / canvasConfig.getHeight(); // Rotated grid supersampling. double[][] offsets = new double[][] { @@ -2215,7 +2260,7 @@ public void computeAlpha(int x, int y, WorkerState state) { occlusion += PreviewRayTracer.skyOcclusion(this, state); } - alphaChannel[y * canvasConfig.width + x] = (byte) (255 * occlusion * 0.25 + 0.5); + alphaChannel[y * canvasConfig.getWidth() + x] = (byte) (255 * occlusion * 0.25 + 0.5); } /** From 488ef67cd39690627eeaf9cea79e8a3201d9f60f Mon Sep 17 00:00:00 2001 From: Maximilian Stiede Date: Tue, 8 Aug 2023 18:55:30 +0200 Subject: [PATCH 4/5] fix master rebase code --- chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java b/chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java index 62af867420..feb96f1b80 100644 --- a/chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java +++ b/chunky/src/java/se/llbit/chunky/ui/render/tabs/GeneralTab.java @@ -216,7 +216,7 @@ public GeneralTab() throws IOException { try (JsonParser parser = new JsonParser(new ByteArrayInputStream(text.getBytes()))) { JsonObject json = parser.parse().object(); scene.importFromJson(json); - renderControls.getCanvas().setCanvasSize(scene.width, scene.height); + renderControls.getCanvas().setCanvasSize(scene.canvasConfig.getWidth(), scene.canvasConfig.getHeight()); renderControls.refreshSettings(); } catch (IOException e) { Log.warn("Failed to import scene settings."); From d6ef9365a701e620482d5d5a8a0f5c9d00a1ed5a Mon Sep 17 00:00:00 2001 From: Maximilian Stiede Date: Mon, 6 Nov 2023 17:14:56 +0100 Subject: [PATCH 5/5] use Configurable interface --- .../chunky/renderer/scene/CanvasConfig.java | 31 ++++++++++--------- .../se/llbit/chunky/renderer/scene/Scene.java | 20 +++++++----- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java b/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java index 7ba895fe33..b1abab3091 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/CanvasConfig.java @@ -3,8 +3,9 @@ import se.llbit.chunky.PersistentSettings; import se.llbit.json.JsonObject; import se.llbit.math.QuickMath; +import se.llbit.util.Configurable; -public class CanvasConfig { +public class CanvasConfig implements Configurable { /** * Minimum canvas width. */ @@ -35,6 +36,14 @@ public CanvasConfig(int width, int height) { this.height = height; } + @Override + public void reset() { + cropWidth = 0; + cropHeight = 0; + cropX = 0; + cropY = 0; + } + public void copyState(CanvasConfig other) { width = other.width; height = other.height; @@ -167,7 +176,8 @@ public int getPixelCount() { return width * height; } - public void writeJsonData(JsonObject json) { + @Override + public void storeConfiguration(JsonObject json) { json.add("width", width); json.add("height", height); @@ -179,12 +189,10 @@ public void writeJsonData(JsonObject json) { json.add("cropY", cropY); } - /** - * @return true if buffers have to be reinitialized - */ - public boolean importJsonData(JsonObject json) { - int newWidth = json.get("width").intValue(width); - int newHeight = json.get("height").intValue(height); + @Override + public void loadConfiguration(JsonObject json) { + width = json.get("width").intValue(width); + height = json.get("height").intValue(height); // TODO: rename cropWidth = json.get("fullWidth").intValue(cropWidth); @@ -192,12 +200,5 @@ public boolean importJsonData(JsonObject json) { cropX = json.get("cropX").intValue(cropX); cropY = json.get("cropY").intValue(cropY); - - if (width != newWidth || height != newHeight) { - width = newWidth; - height = newHeight; - return true; - } - return false; } } diff --git a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java index 9a55dc1141..634f333525 100644 --- a/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java +++ b/chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java @@ -1945,7 +1945,7 @@ public synchronized void setCanvasCropSize( /** * @deprecated use {@link CanvasConfig} */ - @Deprecated + @Deprecated(forRemoval = true) public int canvasWidth() { return canvasConfig.getWidth(); } @@ -1953,7 +1953,7 @@ public int canvasWidth() { /** * @deprecated use {@link CanvasConfig} */ - @Deprecated + @Deprecated(forRemoval = true) public int canvasHeight() { return canvasConfig.getHeight(); } @@ -1961,7 +1961,7 @@ public int canvasHeight() { /** * @deprecated use {@link CanvasConfig} */ - @Deprecated + @Deprecated(forRemoval = true) public int getFullWidth() { return canvasConfig.getCropWidth(); } @@ -1969,7 +1969,7 @@ public int getFullWidth() { /** * @deprecated use {@link CanvasConfig} */ - @Deprecated + @Deprecated(forRemoval = true) public int getFullHeight() { return canvasConfig.getCropHeight(); } @@ -1977,7 +1977,7 @@ public int getFullHeight() { /** * @deprecated use {@link CanvasConfig} */ - @Deprecated + @Deprecated(forRemoval = true) public int getCropX() { return canvasConfig.getCropX(); } @@ -1985,7 +1985,7 @@ public int getCropX() { /** * @deprecated use {@link CanvasConfig} */ - @Deprecated + @Deprecated(forRemoval = true) public int getCropY() { return canvasConfig.getCropY(); } @@ -2243,6 +2243,7 @@ private boolean tryLoadDump(SceneIOProvider context, String fileName, TaskTracke /** * Copies a pixel in-buffer. */ + @Deprecated(forRemoval = true) public void copyPixel(int jobId, int offset) { System.arraycopy(samples, jobId * 3, samples, (jobId + offset) * 3, 3); } @@ -2512,7 +2513,7 @@ public void setUseCustomWaterColor(boolean value) { JsonObject json = new JsonObject(); json.add("sdfVersion", SDF_VERSION); json.add("name", name); - canvasConfig.writeJsonData(json); + canvasConfig.storeConfiguration(json); json.add("yClipMin", yClipMin); json.add("yClipMax", yClipMax); json.add("yMin", yMin); @@ -2754,7 +2755,10 @@ public synchronized void importFromJson(JsonObject json) { // TODO: check if we actually need to reset the scene based on changed settings. refresh(); - if(canvasConfig.importJsonData(json) || samples == null) { + int oldWidth = canvasConfig.getWidth(); + int oldHeight = canvasConfig.getHeight(); + canvasConfig.loadConfiguration(json); + if(oldWidth != canvasConfig.getWidth() || oldHeight != canvasConfig.getHeight() || samples == null) { initBuffers(); }