From cabe78d5f4f8d4b6de37db401b72b6b7fa4da2fd Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Thu, 14 Nov 2024 10:40:54 +0900 Subject: [PATCH 1/2] fix(optimize-texture): InvalidCastException with RenderTexture --- .../TraceAndOptimize/OptimizeTexture.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Editor/Processors/TraceAndOptimize/OptimizeTexture.cs b/Editor/Processors/TraceAndOptimize/OptimizeTexture.cs index 260370c0..1877d081 100644 --- a/Editor/Processors/TraceAndOptimize/OptimizeTexture.cs +++ b/Editor/Processors/TraceAndOptimize/OptimizeTexture.cs @@ -155,9 +155,9 @@ internal class TextureNode : UnionFindNodeBase { public Dictionary> Users { get; } = new(); - public Texture2D Texture { get; } + public Texture Texture { get; } - public TextureNode(Texture2D texture) => Texture = texture; + public TextureNode(Texture texture) => Texture = texture; public void UseTexture(MaterialNode material, TextureUsageInformation? usage) { @@ -184,9 +184,9 @@ BuildContext context return node; } - var texturs = new Dictionary(); + var texturs = new Dictionary(); - TextureNode? GetTextureNode(Texture2D? texture) + TextureNode? GetTextureNode(Texture? texture) { if (texture == null) return null; if (!texturs.TryGetValue(texture, out var node)) @@ -224,7 +224,9 @@ BuildContext context { var materialInformation = context.GetMaterialInformation(material); - IEnumerable<(Texture2D?, TextureUsageInformation?)> textures; + // We use Texture? to not have trouble with RenderTexture + // We check if texture is Texture2D or not in later phase + IEnumerable<(Texture?, TextureUsageInformation?)> textures; // collect texture usage information if (materialInformation?.TextureUsageInformationList is { } informations) @@ -232,14 +234,13 @@ BuildContext context materialNode.TextureUsageInformations = informations.ToList(); materialNode.UserRenderersOnAvatar = materialInformation.UserRenderers.Where(x => x != null).ToList(); - textures = informations.Select(x => - ((Texture2D?)material.GetTexture(x.MaterialPropertyName), (TextureUsageInformation?)x)); + textures = informations.Select((Texture?, TextureUsageInformation?) (x) => (material.GetTexture(x.MaterialPropertyName), x)); } else { // failed to retrive texture information, just link texture node - textures = material.GetTexturePropertyNames().Select(x => material.GetTexture(x)).OfType() - .Select(t => (t, (TextureUsageInformation?)null)); + textures = material.GetTexturePropertyNames().Select(x => material.GetTexture(x)) + .Select((Texture?, TextureUsageInformation?) (t) => (t, null)); } // merge texture nodes @@ -374,7 +375,8 @@ BuildContext context var usageInformations = textureNodes.SelectMany(x => x.Users).SelectMany(x => x.Value); var wrapModeU = usageInformations.Select(x => x.WrapModeU).Aggregate((a, b) => a == b ? a : null); var wrapModeV = usageInformations.Select(x => x.WrapModeV).Aggregate((a, b) => a == b ? a : null); - var textures = textureNodes.Select(x => x.Texture).ToList(); + if (textureNodes.Any(x => x.Texture is not Texture2D)) continue; + var textures = textureNodes.Select(x => (Texture2D)x.Texture).ToList(); var atlasResult = MayAtlasTexture(textures, uvids.backedSet, wrapModeU, wrapModeV); if (atlasResult.IsEmpty()) continue; @@ -382,7 +384,7 @@ BuildContext context atlasResults.Add((uvids, atlasResult)); foreach (var textureNode in textureNodes) { - var newTexture = atlasResult.TextureMapping[textureNode.Texture]; + var newTexture = atlasResult.TextureMapping[(Texture2D)textureNode.Texture]; foreach (var (material, usages) in textureNode.Users) foreach (var usage in usages) From 32d585924c002f6d952af1aabde6abe074a797ea Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Thu, 14 Nov 2024 10:49:21 +0900 Subject: [PATCH 2/2] docs(changelog): InvalidCastException with RenderTexture --- CHANGELOG-PRERELEASE.md | 1 + CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index 05dd1e13..f344edb4 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog]. - Error with nested merge skinned mesh `#1340` - Broken synced Layer support `#1341` - Unpacking prefab might look like some data lost in PrefabSafeUniqueCollection `#1342` +- InvalidCastException with RenderTexture `#1334` ### Security diff --git a/CHANGELOG.md b/CHANGELOG.md index d376f849..58438b8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ The format is based on [Keep a Changelog]. - We may relax some restriction in the future. - Because we have to check for each condition if we use AnyState but we can check for only one (in best case) with entry/exit, this generally reduces cost for checking an parameter in a state. - Combined with Entry / Exit to 1D BlendTree optimization, which is implemented in previous release, your AnyState layer may be optimized to 1D BlendTree. -- Optimize Texture in Trace nad Optimize `#1181` `#1184` `#1193` `#1215` `#1225` `#1235` `#1268` `#1278` `#1313` `#1328` `#1338` +- Optimize Texture in Trace nad Optimize `#1181` `#1184` `#1193` `#1215` `#1225` `#1235` `#1268` `#1278` `#1313` `#1328` `#1338` `#1334` - Avatar Optimizer will pack texture and tries to reduce the VRAM usage. - Currently liltoon is only supported. - `Copy Enablement Animation` to Merge Skinned Mesh `#1173`