Sprite Editor Origin Fixes (#3648)

- PreviewSprite and SpriteEditor.Preview no longer improperly offsets the image by the origin when the aspect ratio of the sprite isn't 1:1

https://files.facepunch.com/CarsonKompon/2025/December/19_08-28-ScrawnyHoopoe.mp4
This commit is contained in:
Carson Kompon
2025-12-22 10:51:24 -05:00
committed by GitHub
parent 8c67542b13
commit 39d80352c7
2 changed files with 29 additions and 5 deletions

View File

@@ -45,8 +45,19 @@ class PreviewSprite : AssetPreview
public override void UpdateScene( float cycle, float timeStep )
{
var texture = spriteRenderer.Texture;
if ( texture.Width == 0 || texture.Height == 0 )
return;
var ratio = (float)texture.Width / texture.Height;
var pivotOffset = new Vector2( 0.5f, 0.5f ) - (CurrentAnimation?.Origin ?? new Vector2( 0.5f, 0.5f ));
PrimaryObject.WorldPosition = new Vector3( 0, pivotOffset.x, pivotOffset.y ) * 16;
if ( ratio > 1 )
{
PrimaryObject.WorldPosition = new Vector3( 0, pivotOffset.x, pivotOffset.y / ratio ) * 16;
}
else
{
PrimaryObject.WorldPosition = new Vector3( 0, pivotOffset.x * ratio, pivotOffset.y ) * 16;
}
spriteRenderer.TextureFilter = FilterMode;

View File

@@ -94,10 +94,23 @@ public class Preview : Widget
private void ScenePreFrame()
{
var originOffset = new Vector2( 0.5f, 0.5f ) - (SpriteEditor?.SelectedAnimation?.Origin ?? new Vector2( 0.5f, 0.5f ));
Renderer.WorldPosition = Renderer.WorldPosition
.WithY( originOffset.x * 100 )
.WithZ( originOffset.y * 100 );
var texture = Renderer.Texture;
if ( texture.Width == 0 || texture.Height == 0 )
return;
var ratio = (float)texture.Width / texture.Height;
var pivotOffset = new Vector2( 0.5f, 0.5f ) - (SpriteEditor?.SelectedAnimation?.Origin ?? new Vector2( 0.5f, 0.5f ));
if ( ratio > 1 )
{
Renderer.WorldPosition = Renderer.WorldPosition
.WithY( pivotOffset.x * 100 )
.WithZ( pivotOffset.y * 100 / ratio );
}
else
{
Renderer.WorldPosition = Renderer.WorldPosition
.WithY( pivotOffset.x * 100 * ratio )
.WithZ( pivotOffset.y * 100 );
}
switch ( SpriteEditor?.Antialiasing )
{