Skip to content

Commit

Permalink
Fix mipmap count too high when image is being resized
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Jan 30, 2024
1 parent 3ad83fb commit df0dc97
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Source/CustomAvatar/Utilities/Protobuf/Texture2DSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void Write(ref ProtoWriter.State state, Texture2D value)
{
int width = value.width;
int height = value.height;
int mipmapCount = value.mipmapCount;
GraphicsFormat graphicsFormat = value.graphicsFormat;
byte[] textureBytes;

Expand All @@ -108,13 +109,13 @@ public void Write(ref ProtoWriter.State state, Texture2D value)
float scale = Mathf.Min(1, (float)kMaxTextureSize / value.width, (float)kMaxTextureSize / value.height);
width = Mathf.RoundToInt(value.width * scale);
height = Mathf.RoundToInt(value.height * scale);

mipmapCount = Math.Min(value.mipmapCount, (int)Math.Ceiling(Log2(Math.Max(width, height))) + 1);
graphicsFormat = SystemInfo.GetCompatibleFormat(value.graphicsFormat, FormatUsage.Render);

RenderTextureDescriptor renderTextureDescriptor = new(width, height)
{
graphicsFormat = graphicsFormat,
mipCount = value.mipmapCount,
mipCount = mipmapCount,
useMipMap = value.mipmapCount > 1,
};

Expand Down Expand Up @@ -146,7 +147,7 @@ public void Write(ref ProtoWriter.State state, Texture2D value)
state.WriteInt32((int)graphicsFormat);

state.WriteFieldHeader(4, WireType.Varint);
state.WriteInt32(value.mipmapCount);
state.WriteInt32(mipmapCount);

state.WriteFieldHeader(5, WireType.String);
state.WriteBytes(textureBytes);
Expand Down Expand Up @@ -187,5 +188,7 @@ private byte[] FetchTextureDataSync(Texture texture)

return textureBytes;
}

private double Log2(double d) => Math.Log(d) / Math.Log(2);
}
}

0 comments on commit df0dc97

Please sign in to comment.