Skip to content

Commit

Permalink
Fix for texture resource coordinates greater than 1 not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
dashenxian committed Feb 22, 2024
1 parent 444c2b2 commit 9520388
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
Binary file removed .vs/Obj2Tiles/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file removed .vs/Obj2Tiles/v17/.suo
Binary file not shown.
19 changes: 16 additions & 3 deletions Obj2Tiles.Library/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,30 @@ public static class Common
public static void CopyImage(Image<Rgba32> sourceImage, Image<Rgba32> dest, int sourceX, int sourceY, int sourceWidth, int sourceHeight, int destX, int destY)
{
var height = sourceHeight;

sourceImage.ProcessPixelRows(dest, (sourceAccessor, targetAccessor) =>
{
var shouldCulaSourceAccessorIndex = sourceY + height > sourceAccessor.Height;
for (var i = 0; i < height; i++)
{
var sourceRow = sourceAccessor.GetRowSpan(sourceY + i);
var sourceAccessorIndex = sourceY + i;
if (shouldCulaSourceAccessorIndex && sourceAccessorIndex >= sourceAccessor.Height)
{
sourceAccessorIndex %= sourceAccessor.Height;
}
var sourceRow = sourceAccessor.GetRowSpan(sourceAccessorIndex);
var targetRow = targetAccessor.GetRowSpan(i + destY);
var shouldCulaSourceRowIndex = sourceX + sourceWidth > sourceRow.Length;
for (var x = 0; x < sourceWidth; x++)
{
targetRow[x + destX] = sourceRow[x + sourceX];
var sourceRowIndex = x + sourceX;
if (shouldCulaSourceRowIndex && sourceRowIndex >= sourceRow.Length)
{
sourceRowIndex %= sourceRow.Length;
}
targetRow[x + destX] = sourceRow[sourceRowIndex];//
}
}
});
Expand Down
9 changes: 6 additions & 3 deletions Obj2Tiles.Library/Geometry/MeshT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,12 @@ private void BinPackTextures(string targetFolder, int materialIndex, IReadOnlyLi
Debug.WriteLine("Found place for cluster at " + newTextureClusterRect);

// Too long to explain this here, but it works
var adjustedSourceY = !(material.Texture == null)
? Math.Max(texture.Height - (clusterY + clusterHeight), 0)
: Math.Max(normalMap.Height - (clusterY + clusterHeight), 0);
var height = material.Texture != null ? texture.Height : normalMap.Height;
var adjustedSourceY = height - (clusterY + clusterHeight);
if (adjustedSourceY < 0)
{
adjustedSourceY = (int)Math.Ceiling((double)(clusterY + clusterHeight) / (double)height) * height - (clusterY + clusterHeight);
}
var adjustedDestY = Math.Max(edgeLength - (newTextureClusterRect.Y + clusterHeight), 0);

if (!(material.Texture == null))
Expand Down
2 changes: 1 addition & 1 deletion Obj2Tiles.Library/Geometry/MeshUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IMesh LoadMesh(string fileName, out string[] dependencies)
double.Parse(segs[1], CultureInfo.InvariantCulture),
double.Parse(segs[2], CultureInfo.InvariantCulture));

if (vtx.X < 0 || vtx.Y < 0 || vtx.X > 1 || vtx.Y > 1)
if (vtx.X < 0 || vtx.Y < 0)
throw new Exception("Invalid texture coordinates: " + vtx);

textureVertices.Add(vtx);
Expand Down

0 comments on commit 9520388

Please sign in to comment.