diff --git a/Obj2Tiles.Library/Common.cs b/Obj2Tiles.Library/Common.cs index f458779..33bd5a6 100644 --- a/Obj2Tiles.Library/Common.cs +++ b/Obj2Tiles.Library/Common.cs @@ -12,17 +12,30 @@ public static class Common public static void CopyImage(Image sourceImage, Image 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];// } } }); diff --git a/Obj2Tiles.Library/Geometry/MeshT.cs b/Obj2Tiles.Library/Geometry/MeshT.cs index cf5dd62..773367d 100644 --- a/Obj2Tiles.Library/Geometry/MeshT.cs +++ b/Obj2Tiles.Library/Geometry/MeshT.cs @@ -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)) diff --git a/Obj2Tiles.Library/Geometry/MeshUtils.cs b/Obj2Tiles.Library/Geometry/MeshUtils.cs index b697fc8..272c206 100644 --- a/Obj2Tiles.Library/Geometry/MeshUtils.cs +++ b/Obj2Tiles.Library/Geometry/MeshUtils.cs @@ -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);