Skip to content

Commit

Permalink
Support string replacement on texture references
Browse files Browse the repository at this point in the history
This allows glTF models intended for web use to share textures.
  • Loading branch information
tordanik committed May 21, 2023
1 parent 488fea6 commit aade8c5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.function.Function;

import javax.annotation.Nullable;
import javax.imageio.ImageIO;
Expand All @@ -19,9 +20,20 @@
public record ResourceOutputSettings(
ResourceOutputMode modeForStaticResources,
ResourceOutputMode modeForGeneratedResources,
URI textureDirectory
URI textureDirectory,
Function<String, String> textureReferenceMapping
) {

public String buildTextureReference(TextureData texture) {

if (!(texture instanceof ImageFileTexture fileTexture)) {
throw new IllegalArgumentException("Cannot reference runtime-generated textures: " + texture);
}

return textureReferenceMapping.apply(fileTexture.getFile().getAbsolutePath());

}

public enum ResourceOutputMode {
/** store the resource inside the model file */
EMBED,
Expand Down Expand Up @@ -63,7 +75,9 @@ public String storeTexture(TextureData texture, @Nullable URI baseForRelativePat

}

public static ResourceOutputSettings fromConfig(Configuration config, URI defaultTextureDirectory, boolean canEmbed) {
public static ResourceOutputSettings fromConfig(Configuration config, URI textureDirectory, boolean canEmbed) {

/* parse the modes */

ResourceOutputMode modeForStaticResources = canEmbed ? EMBED : REFERENCE;
ResourceOutputMode modeForGeneratedResources = canEmbed ? EMBED : STORE_SEPARATELY_AND_REFERENCE;
Expand All @@ -76,7 +90,16 @@ public static ResourceOutputSettings fromConfig(Configuration config, URI defaul
modeForGeneratedResources = ResourceOutputMode.valueOf(config.getString("generatedResourceOutputMode", ""));
} catch (IllegalArgumentException ignored) { /* keep existing value */ }

URI textureDirectory = defaultTextureDirectory;
/* parse the texture reference mapping */

Function<String, String> textureReferenceMapping = Function.identity();

@Nullable String regex = config.getString("textureReferenceMappingFrom", null);
@Nullable String replacement = config.getString("textureReferenceMappingTo", null);

if (regex != null && replacement != null) {
textureReferenceMapping = (String input) -> input.replaceAll(regex, replacement);
}

/* perform some validation */

Expand All @@ -90,7 +113,7 @@ public static ResourceOutputSettings fromConfig(Configuration config, URI defaul

/* build and return the result */

return new ResourceOutputSettings(modeForStaticResources, modeForGeneratedResources, textureDirectory);
return new ResourceOutputSettings(modeForStaticResources, modeForGeneratedResources, textureDirectory, textureReferenceMapping);

}

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/osm2world/core/target/gltf/GltfTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
import org.osm2world.core.target.common.MeshTarget;
import org.osm2world.core.target.common.MeshTarget.MergeMeshes.MergeOption;
import org.osm2world.core.target.common.ResourceOutputSettings;
import org.osm2world.core.target.common.material.*;
import org.osm2world.core.target.common.material.Material;
import org.osm2world.core.target.common.material.Materials;
import org.osm2world.core.target.common.material.TextureData;
import org.osm2world.core.target.common.material.TextureLayer;
import org.osm2world.core.target.common.mesh.LevelOfDetail;
import org.osm2world.core.target.common.mesh.Mesh;
import org.osm2world.core.target.common.mesh.TriangleGeometry;
Expand Down Expand Up @@ -406,9 +409,9 @@ private int createTexture(TextureData textureData) throws IOException {
ResourceOutputSettings.ResourceOutputMode mode = resourceOutputSettings.modeForTexture(textureData);

String uri = switch (mode) {
case EMBED -> textureData.getDataUri();
case REFERENCE -> ((ImageFileTexture)textureData).getFile().getAbsolutePath();
case REFERENCE -> resourceOutputSettings.buildTextureReference(textureData);
case STORE_SEPARATELY_AND_REFERENCE -> resourceOutputSettings.storeTexture(textureData, outputDir.toURI());
case EMBED -> textureData.getDataUri();
};

int imageIndex = imageIndexMap.containsKey(uri) ? imageIndexMap.get(uri) : createImage(uri);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/osm2world/core/target/obj/ObjTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
import org.osm2world.core.math.VectorXZ;
import org.osm2world.core.target.common.FaceTarget;
import org.osm2world.core.target.common.ResourceOutputSettings;
import org.osm2world.core.target.common.material.*;
import org.osm2world.core.target.common.material.Material;
import org.osm2world.core.target.common.material.Material.Transparency;
import org.osm2world.core.target.common.material.Materials;
import org.osm2world.core.target.common.material.TextureData;
import org.osm2world.core.target.common.material.TextureData.Wrap;
import org.osm2world.core.target.common.material.TextureLayer;
import org.osm2world.core.world.data.WorldObject;

public class ObjTarget extends FaceTarget {
Expand Down Expand Up @@ -212,9 +215,9 @@ private String textureToPath(TextureData texture) throws IOException {
ResourceOutputSettings resourceOutputSettings = ResourceOutputSettings.fromConfig(config, textureDirectory.toURI(), false);

String path = switch (resourceOutputSettings.modeForTexture(texture)) {
case REFERENCE -> ((ImageFileTexture)texture).getFile().getAbsolutePath();
case REFERENCE -> resourceOutputSettings.buildTextureReference(texture);
case STORE_SEPARATELY_AND_REFERENCE -> resourceOutputSettings.storeTexture(texture, objDirectory.toURI());
default -> throw new UnsupportedOperationException("unsupported output mode");
case EMBED -> throw new UnsupportedOperationException("unsupported output mode");
};

textureMap.put(texture, path);
Expand Down

0 comments on commit aade8c5

Please sign in to comment.