summaryrefslogtreecommitdiffstats
path: root/Scripts
diff options
context:
space:
mode:
Diffstat (limited to 'Scripts')
-rw-r--r--Scripts/Impostors.cs55
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++)