From 04409f538cdff37d5bea15359757978b3cc4fac2 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Thu, 25 Jul 2024 11:57:19 +0900 Subject: [PATCH 1/2] fix: RemoveMeshByMask is broken with negative uv --- Editor/EditModePreview/RemoveMeshPreviewController.cs | 4 ++-- Editor/Processors/SkinnedMeshes/RemoveMeshByMaskProcessor.cs | 5 +++-- Internal/Utils/Utils.Float.cs | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Editor/EditModePreview/RemoveMeshPreviewController.cs b/Editor/EditModePreview/RemoveMeshPreviewController.cs index 6788b7806..354fffdc1 100644 --- a/Editor/EditModePreview/RemoveMeshPreviewController.cs +++ b/Editor/EditModePreview/RemoveMeshPreviewController.cs @@ -453,8 +453,8 @@ private bool TestBlendShape(int movementBase, int index) => private int GetValue(Vector2 uv) { - var x = Mathf.FloorToInt(uv.x % 1 * MaskWidth); - var y = Mathf.FloorToInt(uv.y % 1 * MaskHeight); + var x = Mathf.FloorToInt(Utils.Modulo(uv.x, 1) * MaskWidth); + var y = Mathf.FloorToInt(Utils.Modulo(uv.y, 1) * MaskHeight); var color = Mask[x + y * MaskWidth]; return Mathf.Max(Mathf.Max(color.r, color.g), color.b); } diff --git a/Editor/Processors/SkinnedMeshes/RemoveMeshByMaskProcessor.cs b/Editor/Processors/SkinnedMeshes/RemoveMeshByMaskProcessor.cs index bcfdf2e5a..bba44efa3 100644 --- a/Editor/Processors/SkinnedMeshes/RemoveMeshByMaskProcessor.cs +++ b/Editor/Processors/SkinnedMeshes/RemoveMeshByMaskProcessor.cs @@ -75,8 +75,9 @@ void AutoFix() int GetValue(float u, float v) { - var x = Mathf.FloorToInt(u % 1 * textureWidth); - var y = Mathf.FloorToInt(v % 1 * textureHeight); + var x = Mathf.FloorToInt(Utils.Modulo(u, 1) * textureWidth); + var y = Mathf.FloorToInt(Utils.Modulo(v, 1) * textureHeight); + if (y * textureWidth + x < 0 || y * textureWidth + x >= pixels.Length) throw new IndexOutOfRangeException($"x: {x}, y: {y}, u: {u}, v: {v}, w: {textureWidth}, h: {textureHeight}, l: {pixels.Length}"); var pixel = pixels[y * textureWidth + x]; return Mathf.Max(Mathf.Max(pixel.r, pixel.g), pixel.b); } diff --git a/Internal/Utils/Utils.Float.cs b/Internal/Utils/Utils.Float.cs index 7a4e63b0c..eb9959f84 100644 --- a/Internal/Utils/Utils.Float.cs +++ b/Internal/Utils/Utils.Float.cs @@ -1,5 +1,6 @@ using System; +using UnityEngine; namespace Anatawa12.AvatarOptimizer { @@ -91,5 +92,6 @@ public static float PreviousFloat(float x) #endif public static bool IsFinite(float x) => !float.IsNaN(x) && !float.IsInfinity(x); + public static float Modulo(float x, float y) => x - y * Mathf.Floor(x / y); } } From 3f4b37265b2da6e029d1d10a84f9ac41fd3d8361 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Thu, 25 Jul 2024 12:03:33 +0900 Subject: [PATCH 2/2] docs(changelog): Index out of bounds error with remove mesh by mask with negative UV --- CHANGELOG-PRERELEASE.md | 1 + CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index 6c487487e..104033e49 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog]. ### Removed ### Fixed +- Index out of bounds error with remove mesh by mask with negative UV `#1123` ### Security diff --git a/CHANGELOG.md b/CHANGELOG.md index e03a69583..2cc7ca41c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog]. ### Removed ### Fixed +- Index out of bounds error with remove mesh by mask with negative UV `#1123` ### Security