Skip to content

Commit

Permalink
Support padding in all standard TexCoordFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
tordanik committed May 23, 2024
1 parent 28f44aa commit b447f0c
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package org.osm2world.core.target.common.texcoord;

import static org.osm2world.core.target.common.texcoord.TexCoordUtil.applyPadding;

import java.util.ArrayList;
import java.util.List;

import org.osm2world.core.math.AxisAlignedRectangleXZ;
import org.osm2world.core.math.FaceXYZ;
import org.osm2world.core.math.SimplePolygonXZ;
import org.osm2world.core.math.VectorXYZ;
import org.osm2world.core.math.VectorXZ;
import org.osm2world.core.math.*;
import org.osm2world.core.target.common.material.TextureDataDimensions;

/**
* fits an image onto a flat polygon.
* Vertices must represent the vertex loop of a {@link FaceXYZ}
*/
public class FaceFitTexCoordFunction implements TexCoordFunction {
public record FaceFitTexCoordFunction(TextureDataDimensions textureDimensions) implements TexCoordFunction {

@Override
public List<VectorXZ> apply(List<VectorXYZ> vs) {
Expand All @@ -26,7 +25,8 @@ public List<VectorXZ> apply(List<VectorXYZ> vs) {

for (VectorXZ v : faceXZ.vertices()) {
VectorXZ vRelative = v.subtract(faceBbox.bottomLeft());
result.add(new VectorXZ(vRelative.x / faceBbox.sizeX(), vRelative.z / faceBbox.sizeZ()));
VectorXZ rawTexCoord = new VectorXZ(vRelative.x / faceBbox.sizeX(), vRelative.z / faceBbox.sizeZ());
result.add(applyPadding(rawTexCoord, textureDimensions));
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
* like {@link GlobalXZTexCoordFunction}, but uses y instead of z dimension.
* Better suited for certain vertical surfaces.
*/
public class GlobalXYTexCoordFunction implements TexCoordFunction {

public final TextureDataDimensions textureDimensions;

public GlobalXYTexCoordFunction(TextureDataDimensions textureDimensions) {
this.textureDimensions = textureDimensions;
}
public record GlobalXYTexCoordFunction(TextureDataDimensions textureDimensions) implements TexCoordFunction {

@Override
public List<VectorXZ> apply(List<VectorXYZ> vs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@
* to place a texture. This function works for all geometries,
* but steep inclines or even vertical walls produce odd-looking results.
*/
public class GlobalXZTexCoordFunction implements TexCoordFunction {

public final TextureDataDimensions textureDimensions;

public GlobalXZTexCoordFunction(TextureDataDimensions textureDimensions) {
this.textureDimensions = textureDimensions;
}
public record GlobalXZTexCoordFunction(TextureDataDimensions textureDimensions) implements TexCoordFunction {

@Override
public List<VectorXZ> apply(List<VectorXYZ> vs) {

List<VectorXZ> result = new ArrayList<>(vs.size());

for (VectorXYZ v : vs) {
result.add(new VectorXZ(
v.x / textureDimensions.width(),
v.z / textureDimensions.height()));
result.add(TexCoordUtil.applyPadding(new VectorXZ(
v.x / textureDimensions.width(),
v.z / textureDimensions.height()),
this.textureDimensions));
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@
* like {@link GlobalXZTexCoordFunction}, but uses y instead of x dimension.
* Better suited for certain vertical surfaces.
*/
public class GlobalZYTexCoordFunction implements TexCoordFunction {

public final TextureDataDimensions textureDimensions;

public GlobalZYTexCoordFunction(TextureDataDimensions textureDimensions) {
this.textureDimensions = textureDimensions;
}
public record GlobalZYTexCoordFunction(TextureDataDimensions textureDimensions) implements TexCoordFunction {

@Override
public List<VectorXZ> apply(List<VectorXYZ> vs) {

List<VectorXZ> result = new ArrayList<>(vs.size());

for (VectorXYZ v : vs) {
result.add(new VectorXZ(
v.z / textureDimensions.width(),
v.y / textureDimensions.height()));
result.add(TexCoordUtil.applyPadding(new VectorXZ(
v.z / textureDimensions.width(),
v.y / textureDimensions.height()),
textureDimensions));
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public List<VectorXZ> apply(List<VectorXYZ> vs) {
public VectorXZ apply(VectorXYZ v) {
VectorXZ result = texCoordMap.get(v);
if (textureDimensionsForScaling != null) {
result = new VectorXZ(
result.x / textureDimensionsForScaling.width(),
result.z / textureDimensionsForScaling.height());
result = TexCoordUtil.applyPadding(new VectorXZ(
result.x / textureDimensionsForScaling.width(),
result.z / textureDimensionsForScaling.height()),
textureDimensionsForScaling);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.osm2world.core.target.common.texcoord;

import java.util.function.Function;

import org.osm2world.core.math.FaceXYZ;
import org.osm2world.core.target.common.material.TextureDataDimensions;

import java.util.function.Function;

/**
* offers generators for several useful {@link TexCoordFunction} implementations.
* They can be referenced by name in style definition files.
Expand Down Expand Up @@ -82,7 +82,7 @@ public TexCoordFunction apply(TextureDataDimensions dimensions) {
case SLOPED_TRIANGLES -> new SlopedTrianglesTexCoordFunction(dimensions);
case STRIP_WALL, STRIP_FIT_HEIGHT, STRIP_FIT ->
new StripWallTexCoordFunction(dimensions, this == STRIP_FIT, this != STRIP_WALL);
case FACE_FIT -> new FaceFitTexCoordFunction();
case FACE_FIT -> new FaceFitTexCoordFunction(dimensions);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
*
* TODO: introduce face requirement?
*/
public class SlopedTrianglesTexCoordFunction implements TexCoordFunction {

public final TextureDataDimensions textureDimensions;

public SlopedTrianglesTexCoordFunction(TextureDataDimensions textureDimensions) {
this.textureDimensions = textureDimensions;
}
public record SlopedTrianglesTexCoordFunction(TextureDataDimensions textureDimensions) implements TexCoordFunction {

@Override
public List<VectorXZ> apply(List<VectorXYZ> vs) {
Expand All @@ -33,7 +27,7 @@ public List<VectorXZ> apply(List<VectorXYZ> vs) {

List<VectorXZ> result = new ArrayList<>(vs.size());

List<Double> knownAngles = new ArrayList<Double>();
List<Double> knownAngles = new ArrayList<>();

for (int i = 0; i < vs.size() / 3; i++) {

Expand Down Expand Up @@ -69,9 +63,10 @@ public List<VectorXZ> apply(List<VectorXYZ> vs) {

for (VectorXYZ v : triangle.verticesNoDup()) {
VectorXZ baseTexCoord = v.rotateY(-downAngle).xz();
result.add(new VectorXZ(
VectorXZ texCoord = new VectorXZ(
-baseTexCoord.x / textureDimensions.width(),
-baseTexCoord.z / textureDimensions.height()));
-baseTexCoord.z / textureDimensions.height());
result.add(TexCoordUtil.applyPadding(texCoord, textureDimensions));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@
* This only works for vertices forming a triangle strip,
* alternating between upper and lower vertex.
*/
public class StripWallTexCoordFunction implements TexCoordFunction {

public final TextureDataDimensions textureDimensions;
public final boolean fitWidth;
public final boolean fitHeight;

public StripWallTexCoordFunction(TextureDataDimensions textureDimensions, boolean fitWidth, boolean fitHeight) {
this.textureDimensions = textureDimensions;
this.fitWidth = fitWidth;
this.fitHeight = fitHeight;
}
public record StripWallTexCoordFunction(TextureDataDimensions textureDimensions, boolean fitWidth, boolean fitHeight)
implements TexCoordFunction {

@Override
public List<VectorXZ> apply(List<VectorXYZ> vs) {
Expand Down Expand Up @@ -100,7 +91,8 @@ public List<VectorXZ> apply(List<VectorXYZ> vs) {
t = (i % 2 == 0) ? (v.distanceTo(vs.get(i+1))) / height : 0;
}

result.add(new VectorXZ(s, t));
VectorXZ rawTexCoord = new VectorXZ(s, t);
result.add(TexCoordUtil.applyPadding(rawTexCoord, textureDimensions));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.osm2world.core.world.modules;

import static java.lang.Math.min;
import static java.util.stream.Collectors.toList;
import static org.osm2world.core.target.common.material.Materials.*;
import static org.osm2world.core.target.common.texcoord.NamedTexCoordFunction.GLOBAL_X_Z;
import static org.osm2world.core.target.common.texcoord.TexCoordUtil.texCoordLists;
Expand Down Expand Up @@ -107,11 +106,11 @@ public void renderTo(Target target) {

Function<TextureDataDimensions, TexCoordFunction> localXZTexCoordFunction = (TextureDataDimensions textureDimensions) -> {
return (List<VectorXYZ> vs) -> {
TexCoordFunction globalXZ = new GlobalXZTexCoordFunction(textureDimensions);
var globalXZ = new GlobalXZTexCoordFunction(textureDimensions);
VectorXZ center = area.getOuterPolygon().getCentroid();
List<VectorXYZ> localCoords = vs.stream().map(v -> v.subtract(center)).collect(toList());
List<VectorXZ> result = globalXZ.apply(localCoords);
return result.stream().map(v -> v.add(new VectorXZ(0.5, 0.5))).collect(toList());
VectorXZ shift = center.add(-textureDimensions.width() / 2, -textureDimensions.height() / 2);
List<VectorXYZ> localCoords = vs.stream().map(v -> v.subtract(shift)).toList();
return globalXZ.apply(localCoords);
};
};

Expand Down

0 comments on commit b447f0c

Please sign in to comment.