summaryrefslogtreecommitdiffstats
path: root/Scripts/Impostors.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Scripts/Impostors.cs')
-rw-r--r--Scripts/Impostors.cs64
1 files changed, 45 insertions, 19 deletions
diff --git a/Scripts/Impostors.cs b/Scripts/Impostors.cs
index b82dc8f..30b06f6 100644
--- a/Scripts/Impostors.cs
+++ b/Scripts/Impostors.cs
@@ -131,13 +131,15 @@ public class Impostors : MonoBehaviour
public string keyword;
public Texture2D atlas;
public bool isDepth;
+ public bool linear;
- public BakePass(string name, string keyword, Texture2D atlas, bool isDepth = false)
+ public BakePass(string name, string keyword, Texture2D atlas, bool isDepth = false, bool linear = false)
{
this.name = name;
this.keyword = keyword;
this.atlas = atlas;
this.isDepth = isDepth;
+ this.linear = linear;
}
}
@@ -196,13 +198,14 @@ public class Impostors : MonoBehaviour
}
}
- void ExecutePassesSequentially(BakePass[] passes, RenderTexture colorRT, RenderTexture depthOnlyRT, Material depthBlitMat, int passIndex = 0)
+ void ExecutePassesSequentially(BakePass[] passes, RenderTexture srgbRT, RenderTexture linearRT, RenderTexture depthOnlyRT, Material depthBlitMat, int passIndex = 0)
{
if (passIndex >= passes.Length)
{
// All passes complete
RenderTexture.active = null;
- RenderTexture.ReleaseTemporary(colorRT);
+ RenderTexture.ReleaseTemporary(srgbRT);
+ RenderTexture.ReleaseTemporary(linearRT);
RenderTexture.ReleaseTemporary(depthOnlyRT);
DestroyImmediate(depthBlitMat);
@@ -216,9 +219,10 @@ public class Impostors : MonoBehaviour
SetMaterialDebugKeyword(pass.keyword, true);
UnityEditor.EditorApplication.delayCall += () => {
+ RenderTexture colorRT = pass.linear ? linearRT : srgbRT;
RenderAtlasPass(pass.atlas, colorRT, depthOnlyRT, pass.isDepth ? depthBlitMat : null);
SetMaterialDebugKeyword(pass.keyword, false);
- ExecutePassesSequentially(passes, colorRT, depthOnlyRT, depthBlitMat, passIndex + 1);
+ ExecutePassesSequentially(passes, srgbRT, linearRT, depthOnlyRT, depthBlitMat, passIndex + 1);
};
}
@@ -235,14 +239,18 @@ public class Impostors : MonoBehaviour
BakePass[] passes = new BakePass[]
{
new BakePass("albedo", "_DEBUG_VIEW_UNLIT", new Texture2D(size, size, TextureFormat.RGBA32, false)),
- new BakePass("normals", "_DEBUG_VIEW_WORLD_SPACE_NORMALS", new Texture2D(size, size, TextureFormat.RGBA32, false)),
- new BakePass("metallic/gloss", "_DEBUG_VIEW_METALLIC_GLOSS", new Texture2D(size, size, TextureFormat.RGBA32, false)),
- new BakePass("depth", "", new Texture2D(size, size, TextureFormat.RFloat, false), true)
+ new BakePass("normals", "_DEBUG_VIEW_WORLD_SPACE_NORMALS", new Texture2D(size, size, TextureFormat.RGBA32, false), linear: true),
+ new BakePass("metallic/gloss", "_DEBUG_VIEW_METALLIC_GLOSS", new Texture2D(size, size, TextureFormat.RGBA32, false), linear: true),
+ new BakePass("depth", "", new Texture2D(size, size, TextureFormat.RFloat, false), isDepth: true)
};
- RenderTextureDescriptor desc = new RenderTextureDescriptor(cameraResolution, cameraResolution, RenderTextureFormat.ARGB32, 24);
- desc.sRGB = true;
- RenderTexture colorRT = RenderTexture.GetTemporary(desc);
+ RenderTextureDescriptor srgbDesc = new RenderTextureDescriptor(cameraResolution, cameraResolution, RenderTextureFormat.ARGB32, 24);
+ srgbDesc.sRGB = true;
+ RenderTexture srgbRT = RenderTexture.GetTemporary(srgbDesc);
+
+ RenderTextureDescriptor linearDesc = new RenderTextureDescriptor(cameraResolution, cameraResolution, RenderTextureFormat.ARGB32, 24);
+ linearDesc.sRGB = false;
+ RenderTexture linearRT = RenderTexture.GetTemporary(linearDesc);
RenderTextureDescriptor depthDesc = new RenderTextureDescriptor(cameraResolution, cameraResolution, RenderTextureFormat.Depth, 24);
RenderTexture depthOnlyRT = RenderTexture.GetTemporary(depthDesc);
@@ -250,7 +258,7 @@ public class Impostors : MonoBehaviour
// Ensure all debug keywords start disabled
foreach (var pass in passes) SetMaterialDebugKeyword(pass.keyword, false);
- ExecutePassesSequentially(passes, colorRT, depthOnlyRT, depthBlitMat);
+ ExecutePassesSequentially(passes, srgbRT, linearRT, depthOnlyRT, depthBlitMat);
}
struct TextureExportSettings
@@ -262,9 +270,11 @@ public class Impostors : MonoBehaviour
public FilterMode filter;
public bool alphaTransparency;
public bool uncompressed;
+ public bool isNormalMap;
public TextureExportSettings(string suffix, bool isEXR = false, bool mipmaps = true, bool sRGB = true,
- FilterMode filter = FilterMode.Trilinear, bool alphaTransparency = false, bool uncompressed = false)
+ FilterMode filter = FilterMode.Trilinear, bool alphaTransparency = false,
+ bool uncompressed = false, bool isNormalMap = false)
{
this.suffix = suffix;
this.isEXR = isEXR;
@@ -273,6 +283,7 @@ public class Impostors : MonoBehaviour
this.filter = filter;
this.alphaTransparency = alphaTransparency;
this.uncompressed = uncompressed;
+ this.isNormalMap = isNormalMap;
}
}
@@ -289,13 +300,16 @@ public class Impostors : MonoBehaviour
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer != null)
{
+ if (settings.isNormalMap) importer.textureType = TextureImporterType.NormalMap;
importer.mipmapEnabled = settings.mipmaps;
importer.sRGBTexture = settings.sRGB;
importer.wrapMode = TextureWrapMode.Clamp;
importer.filterMode = settings.filter;
if (settings.alphaTransparency) importer.alphaIsTransparency = true;
if (settings.uncompressed) importer.textureCompression = TextureImporterCompression.Uncompressed;
+ EditorUtility.SetDirty(importer);
importer.SaveAndReimport();
+ AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport);
}
}
@@ -310,24 +324,36 @@ public class Impostors : MonoBehaviour
string baseName = gameObject.name.Replace(" ", "_");
- var exportSettings = new (Texture2D atlas, TextureExportSettings settings, string materialProp)[]
+ var exportSettings = new (TextureExportSettings settings, string materialProp)[]
{
- (albedoAtlas, new TextureExportSettings("albedo", mipmaps: true, sRGB: true, alphaTransparency: true), "_Impostors_Atlas"),
- (normalAtlas, new TextureExportSettings("normal", mipmaps: true, sRGB: false), "_Impostors_Normal_Atlas"),
- (metallicGlossAtlas, new TextureExportSettings("metallic_gloss", mipmaps: true, sRGB: false), "_Impostors_Metallic_Gloss_Atlas"),
- (depthAtlas, new TextureExportSettings("depth", isEXR: true, mipmaps: false, sRGB: false, filter: FilterMode.Bilinear, uncompressed: true), "_Impostors_Depth_Atlas")
+ (new TextureExportSettings("albedo", mipmaps: true, sRGB: true, alphaTransparency: true), "_Impostors_Atlas"),
+ (new TextureExportSettings("normal", mipmaps: true, sRGB: false), "_Impostors_Normal_Atlas"),
+ (new TextureExportSettings("metallic_gloss", mipmaps: true, sRGB: false), "_Impostors_Metallic_Gloss_Atlas"),
+ (new TextureExportSettings("depth", isEXR: true, mipmaps: false, sRGB: false, filter: FilterMode.Bilinear, uncompressed: true), "_Impostors_Depth_Atlas")
};
+ Texture2D[] atlases = { albedoAtlas, normalAtlas, metallicGlossAtlas, depthAtlas };
string[] paths = new string[exportSettings.Length];
for (int i = 0; i < exportSettings.Length; i++)
- SaveAndConfigureTexture(exportSettings[i].atlas, exportSettings[i].settings, baseName, out paths[i]);
+ SaveAndConfigureTexture(atlases[i], exportSettings[i].settings, baseName, out paths[i]);
AssetDatabase.Refresh();
for (int i = 0; i < paths.Length; i++)
ConfigureTextureImporter(paths[i], exportSettings[i].settings);
- // Create impostor
+ // Wait for shader compiler then create impostor
+ WaitForCompilerThenCreateImpostor(paths, exportSettings, baseName);
+ }
+
+ void WaitForCompilerThenCreateImpostor(string[] paths, (TextureExportSettings settings, string materialProp)[] exportSettings, string baseName)
+ {
+ if (EditorApplication.isCompiling)
+ {
+ EditorApplication.delayCall += () => WaitForCompilerThenCreateImpostor(paths, exportSettings, baseName);
+ return;
+ }
+
Texture2D[] textures = new Texture2D[paths.Length];
for (int i = 0; i < paths.Length; i++)
textures[i] = AssetDatabase.LoadAssetAtPath<Texture2D>(paths[i]);