diff options
| author | yum <yum.food.vr@gmail.com> | 2026-01-15 00:02:57 -0800 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2026-01-15 00:02:57 -0800 |
| commit | 2e99a2275ce3d0d9eef7892cf7485a5f278cacb4 (patch) | |
| tree | a416e6dead11fab97d87bd9d0a8cef7c4d8d4480 /Scripts | |
| parent | d01a5d14765cb106327f805c642bc150f5414b3f (diff) | |
Impostors: dilate baked textures
Diffstat (limited to 'Scripts')
| -rw-r--r-- | Scripts/Impostors.cs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Scripts/Impostors.cs b/Scripts/Impostors.cs index 30b06f6..ffd32b4 100644 --- a/Scripts/Impostors.cs +++ b/Scripts/Impostors.cs @@ -287,6 +287,56 @@ public class Impostors : MonoBehaviour } } + void DilateTexture(Texture2D tex, Texture2D alphaSource, bool preserveAlpha = false, int iterations = 8) + { + int w = tex.width, h = tex.height; + Color[] pixels = tex.GetPixels(); + Color[] alpha = alphaSource.GetPixels(); + Color[] origAlpha = preserveAlpha ? (Color[])alpha.Clone() : null; + Color[] buffer = (Color[])pixels.Clone(); + + int[] dx = { -1, 1, 0, 0 }; + int[] dy = { 0, 0, -1, 1 }; + + for (int iter = 0; iter < iterations; iter++) + { + for (int y = 0; y < h; y++) + { + for (int x = 0; x < w; x++) + { + int i = y * w + x; + if (alpha[i].a > 0.01f) continue; + + Color sum = Color.clear; + int count = 0; + for (int d = 0; d < 4; d++) + { + int nx = x + dx[d], ny = y + dy[d]; + if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; + int ni = ny * w + nx; + if (alpha[ni].a > 0.01f || (iter > 0 && pixels[ni].a > 0)) + { + sum += pixels[ni]; + count++; + } + } + if (count > 0) + { + buffer[i] = sum / count; + buffer[i].a = 1; + } + } + } + var tmp = pixels; pixels = buffer; buffer = tmp; + } + + if (preserveAlpha) + for (int i = 0; i < pixels.Length; i++) pixels[i].a = origAlpha[i].a; + + tex.SetPixels(pixels); + tex.Apply(); + } + void SaveAndConfigureTexture(Texture2D atlas, TextureExportSettings settings, string baseName, out string path) { path = Path.Combine(OutputFolder, $"{baseName}_{settings.suffix}.{(settings.isEXR ? "exr" : "png")}"); @@ -332,6 +382,11 @@ public class Impostors : MonoBehaviour (new TextureExportSettings("depth", isEXR: true, mipmaps: false, sRGB: false, filter: FilterMode.Bilinear, uncompressed: true), "_Impostors_Depth_Atlas") }; + // Dilate RGB to prevent dark halos from texture filtering + DilateTexture(albedoAtlas, albedoAtlas, preserveAlpha: true); + DilateTexture(normalAtlas, albedoAtlas); + DilateTexture(metallicGlossAtlas, albedoAtlas); + Texture2D[] atlases = { albedoAtlas, normalAtlas, metallicGlossAtlas, depthAtlas }; string[] paths = new string[exportSettings.Length]; for (int i = 0; i < exportSettings.Length; i++) |
