Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(optimize-texture): InvalidCastException with RenderTexture #1344

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
24 changes: 13 additions & 11 deletions Editor/Processors/TraceAndOptimize/OptimizeTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ internal class TextureNode : UnionFindNodeBase
{
public Dictionary<MaterialNode, List<TextureUsageInformation>> 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)
{
Expand All @@ -184,9 +184,9 @@ BuildContext context
return node;
}

var texturs = new Dictionary<Texture2D, TextureNode>();
var texturs = new Dictionary<Texture, TextureNode>();

TextureNode? GetTextureNode(Texture2D? texture)
TextureNode? GetTextureNode(Texture? texture)
{
if (texture == null) return null;
if (!texturs.TryGetValue(texture, out var node))
Expand Down Expand Up @@ -224,22 +224,23 @@ 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)
{
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<Texture2D?>()
.Select(t => (t, (TextureUsageInformation?)null));
textures = material.GetTexturePropertyNames().Select(x => material.GetTexture(x))
.Select((Texture?, TextureUsageInformation?) (t) => (t, null));
}

// merge texture nodes
Expand Down Expand Up @@ -374,15 +375,16 @@ 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;

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)
Expand Down
Loading