Skip to content

Commit

Permalink
Support padding for pitch textures
Browse files Browse the repository at this point in the history
  • Loading branch information
tordanik committed May 23, 2024
1 parent b447f0c commit 5e1f21f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static final Function<TextureDataDimensions, TexCoordFunction> mirroredHo
* modifies a calculated texture coordinate to account for {@link TextureDataDimensions#padding()}.
* This is helpful when implementing {@link TexCoordFunction}s, not when using them.
*/
static VectorXZ applyPadding(VectorXZ texCoord, TextureDataDimensions dimensions) {
public static VectorXZ applyPadding(VectorXZ texCoord, TextureDataDimensions dimensions) {
double padding = dimensions.padding();
if (padding == 0) {
return texCoord;
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/org/osm2world/core/world/modules/SportsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.osm2world.core.math.VectorXYZ;
import org.osm2world.core.math.VectorXZ;
import org.osm2world.core.target.common.material.Material;
import org.osm2world.core.target.common.material.TextureDataDimensions;
import org.osm2world.core.target.common.material.TextureLayer;
import org.osm2world.core.target.common.mesh.ExtrusionGeometry;
import org.osm2world.core.target.common.mesh.LevelOfDetail;
import org.osm2world.core.target.common.mesh.Mesh;
Expand All @@ -29,6 +31,7 @@
import org.osm2world.core.target.common.model.ModelInstance;
import org.osm2world.core.target.common.texcoord.NamedTexCoordFunction;
import org.osm2world.core.target.common.texcoord.TexCoordFunction;
import org.osm2world.core.target.common.texcoord.TexCoordUtil;
import org.osm2world.core.world.data.AbstractAreaWorldObject;
import org.osm2world.core.world.data.ProceduralWorldObject;
import org.osm2world.core.world.data.TerrainBoundaryWorldObject;
Expand Down Expand Up @@ -97,7 +100,8 @@ public void buildMeshesAndModels(Target target) {

List<TriangleXYZ> triangles = getTriangulation();

TexCoordFunction texFunction = configureTexFunction(area.getOuterPolygon());
List<TextureLayer> layers = getPitchMaterial().getTextureLayers();
TexCoordFunction texFunction = configureTexFunction(area.getOuterPolygon(), layers.get(layers.size() - 1).baseColorTexture.dimensions());

if (texFunction != null) {

Expand All @@ -124,7 +128,8 @@ public void buildMeshesAndModels(Target target) {
* @return the texture coordinate function;
* null if it's not possible to construct a valid pitch
*/
protected PitchTexFunction configureTexFunction(SimplePolygonXZ polygon) {
protected PitchTexFunction configureTexFunction(SimplePolygonXZ polygon,
TextureDataDimensions textureDimensions) {

/* approximate a rectangular shape for the pitch */

Expand Down Expand Up @@ -173,7 +178,7 @@ protected PitchTexFunction configureTexFunction(SimplePolygonXZ polygon) {

/* build the result */

return new PitchTexFunction(origin, longSide, shortSide);
return new PitchTexFunction(origin, longSide, shortSide, textureDimensions);

}

Expand All @@ -185,12 +190,15 @@ static class PitchTexFunction implements TexCoordFunction {
private final VectorXZ origin;
private final VectorXZ longSide;
private final VectorXZ shortSide;
private final TextureDataDimensions textureDimensions;

PitchTexFunction(VectorXZ origin, VectorXZ longSide, VectorXZ shortSide) {
PitchTexFunction(VectorXZ origin, VectorXZ longSide, VectorXZ shortSide,
TextureDataDimensions textureDimensions) {

this.origin = origin;
this.longSide = longSide;
this.shortSide = shortSide;
this.textureDimensions = textureDimensions;

}

Expand Down Expand Up @@ -221,9 +229,11 @@ public List<VectorXZ> apply(List<VectorXYZ> vs) {
double angleShort = VectorXZ.angleBetween(v, shortSide);
double shortSideProjectedLength = v.length() * cos(angleShort);

result.add(new VectorXZ(
shortSideProjectedLength / shortSide.length(),
longSideProjectedLength / longSide.length()));
VectorXZ rawTexCoord = new VectorXZ(
shortSideProjectedLength / shortSide.length(),
longSideProjectedLength / longSide.length());

result.add(TexCoordUtil.applyPadding(rawTexCoord, textureDimensions));

}

Expand Down Expand Up @@ -363,7 +373,8 @@ public void buildMeshesAndModels(Target target) {

/* add a net with posts */

PitchTexFunction texFunction = configureTexFunction(area.getOuterPolygon());
List<TextureLayer> layers = getPitchMaterial().getTextureLayers();
PitchTexFunction texFunction = configureTexFunction(area.getOuterPolygon(), layers.get(layers.size() - 1).baseColorTexture.dimensions());

//TODO: support this feature when elevation is enabled

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;
import org.osm2world.core.math.VectorXYZ;
import org.osm2world.core.math.VectorXZ;
import org.osm2world.core.target.common.material.TextureDataDimensions;
import org.osm2world.core.world.modules.SportsModule.Pitch.PitchTexFunction;


Expand All @@ -16,7 +17,7 @@ public class SportsModuleTest {
public void testPitchTexFunction() {

PitchTexFunction texFunction = new PitchTexFunction(
NULL_VECTOR, new VectorXZ(2, 0), new VectorXZ(0, 1));
NULL_VECTOR, new VectorXZ(2, 0), new VectorXZ(0, 1), new TextureDataDimensions(1, 1));

assertAlmostEquals(0, 0, texFunction.apply(asList(new VectorXYZ(0, 0, 0))).get(0));
assertAlmostEquals(0, 1, texFunction.apply(asList(new VectorXYZ(2, 0, 0))).get(0));
Expand All @@ -26,7 +27,7 @@ public void testPitchTexFunction() {
assertAlmostEquals(2, 2, texFunction.apply(asList(new VectorXYZ(4, 0, 2))).get(0));
assertAlmostEquals(0, -1, texFunction.apply(asList(new VectorXYZ(-2, 0, 0))).get(0));

texFunction = new PitchTexFunction(NULL_VECTOR, new VectorXZ(0, -3), new VectorXZ(2, 0));
texFunction = new PitchTexFunction(NULL_VECTOR, new VectorXZ(0, -3), new VectorXZ(2, 0), new TextureDataDimensions(1, 1));

assertAlmostEquals(0, 0, texFunction.apply(asList(new VectorXYZ(0, 0, 0))).get(0));
assertAlmostEquals(0, 1, texFunction.apply(asList(new VectorXYZ(0, 0, -3))).get(0));
Expand Down

0 comments on commit 5e1f21f

Please sign in to comment.