Skip to content

Commit

Permalink
Add support for relative skymap paths in scene description files. (ch…
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik committed Sep 9, 2023
1 parent 0334a72 commit ba2f58b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ public synchronized void loadScene(RenderContext context, String sceneName, Task
}

// Load the configured skymap file.
sky.loadSkymap();
sky.reloadSkymap(context.getSceneDirectory());

loadedWorld = EmptyWorld.INSTANCE;
if (!worldPath.isEmpty()) {
Expand Down
24 changes: 14 additions & 10 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.commons.math3.util.FastMath;
import se.llbit.chunky.block.Air;
import se.llbit.chunky.renderer.SunSamplingStrategy;
import se.llbit.chunky.resources.HDRTexture;
import se.llbit.chunky.resources.PFMTexture;
import se.llbit.chunky.resources.Texture;
Expand All @@ -36,8 +35,10 @@
import se.llbit.util.JsonSerializable;
import se.llbit.util.JsonUtil;
import se.llbit.util.annotation.NotNull;
import se.llbit.util.annotation.Nullable;

import java.io.File;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -194,18 +195,18 @@ public Sky(Scene sceneDescription) {
/**
* Load the configured skymap file
*/
public void loadSkymap() {
public void reloadSkymap(@Nullable File sceneDirectory) {
switch (mode) {
case SKYMAP_EQUIRECTANGULAR:
case SKYMAP_ANGULAR:
if (!skymapFileName.isEmpty()) {
loadSkymap(skymapFileName);
loadSkymap(skymapFileName, sceneDirectory);
}
break;
case SKYBOX:
for (int i = 0; i < 6; ++i) {
if (!skyboxFileName[i].isEmpty()) {
loadSkyboxTexture(skyboxFileName[i], i);
loadSkyboxTexture(skyboxFileName[i], i, sceneDirectory);
}
}
default:
Expand All @@ -216,9 +217,9 @@ public void loadSkymap() {
/**
* Load a panoramic skymap texture.
*/
public void loadSkymap(String fileName) {
public void loadSkymap(String fileName, @Nullable File sceneDirectory) {
skymapFileName = fileName;
skymap = loadSkyTexture(fileName, skymap);
skymap = loadSkyTexture(fileName, skymap, sceneDirectory);
scene.refresh();
}

Expand Down Expand Up @@ -860,17 +861,20 @@ public static void makeDefaultGradient(Collection<Vector4> gradient) {
gradient.add(new Vector4(0x75 / 255., 0xAA / 255., 0xFF / 255., 1));
}

public void loadSkyboxTexture(String fileName, int index) {
public void loadSkyboxTexture(String fileName, int index, @Nullable File sceneDirectory) {
if (index < 0 || index >= 6) {
throw new IllegalArgumentException();
}
skyboxFileName[index] = fileName;
skybox[index] = loadSkyTexture(fileName, skybox[index]);
skybox[index] = loadSkyTexture(fileName, skybox[index], sceneDirectory);
scene.refresh();
}

private Texture loadSkyTexture(String fileName, Texture prevTexture) {
File textureFile = new File(fileName);
private Texture loadSkyTexture(String fileName, Texture prevTexture, @Nullable File sceneDirectory) {
String resolvedFilename = sceneDirectory == null
? fileName
: Paths.get(sceneDirectory.getAbsolutePath(), fileName).toAbsolutePath().toString();
File textureFile = new File(resolvedFilename);
if (textureFile.exists()) {
try {
Log.info("Loading skymap: " + fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void selectSkyboxTexture(int textureIndex) {
File imageFile = fileChooser.showOpenDialog(getScene().getWindow());
if (imageFile != null) {
skyboxDirectory = imageFile.getParentFile();
scene.sky().loadSkyboxTexture(imageFile.getAbsolutePath(), textureIndex);
scene.sky().loadSkyboxTexture(imageFile.getAbsolutePath(), textureIndex, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setPanoramic(boolean pano) {
File imageFile = fileChooser.showOpenDialog(getScene().getWindow());
if (imageFile != null) {
lastDirectory = imageFile.getParentFile();
scene.sky().loadSkymap(imageFile.getAbsolutePath());
scene.sky().loadSkymap(imageFile.getAbsolutePath(), null);
}
});
skymapYaw.setName("Skymap yaw");
Expand Down

0 comments on commit ba2f58b

Please sign in to comment.