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

Adjusted Godot Atlas exporter to take margins into account #85

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jdbhartley
Copy link

When using the tool as an alternative to Texture Packer in Godot 4 I noticed that the margins were all set to 0 resulting in either a lot of fixing by hand or being left with sprites unusable for animation.

@@ -84,6 +84,9 @@ function prepareData(data, options) {
let frame = {x: item.frame.x, y: item.frame.y, w: item.frame.w, h: item.frame.h, hw: item.frame.w/2, hh: item.frame.h/2};
let spriteSourceSize = {x: item.spriteSourceSize.x, y: item.spriteSourceSize.y, w: item.spriteSourceSize.w, h: item.spriteSourceSize.h};
let sourceSize = {w: item.sourceSize.w, h: item.sourceSize.h};

//Used when importing to Godot Atlas to retain the original sprite size
let margin = {x: (sourceSize.w - spriteSourceSize.w)/2, y: (sourceSize.h - spriteSourceSize.h)/2, h: sourceSize.h - spriteSourceSize.h, w: sourceSize.w - spriteSourceSize.w}
Copy link

@Gerfalerf Gerfalerf Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think rather than splitting the difference of spriteSourceSize and sourceSize, margin.x and margin.y here should simply be set to spriteSourceSize.x and spriteSourceSize.y. Weird that spriteSourceSize is called a "size" when it has an offset like a rect, but those x/y values appear to be the actual offset of the sprite's subregion that was copied into the atlas.

Otherwise, change looks great. Just ran into this issue myself.

@DmitriiPetukhov
Copy link

I also needed this fix, but I ended up creating a custom template based on built-in godot template that also takes margins into account.

I replaced the margin block with:

"margin": {
  "x": {{frame.w | divide : 2 | add : spriteSourceSize.x}},
  "y": {{frame.h | divide : 2 | add : spriteSourceSize.y}},
  "w": {{sourceSize.w}},
  "h": {{sourceSize.h}}
}

End result:

{
  "textures": [
    {
      "image": "{{config.imageFile}}",
      "size": {
        "w": {{config.imageWidth}},
        "h": {{config.imageHeight}}
      },
      "sprites": [
        {{#rects}}
        {
          "filename": "{{name}}",
          "region": {
            "x": {{frame.x}},
            "y": {{frame.y}},
            "w": {{frame.w}},
            "h": {{frame.h}}
          },
          "margin": {
            "x": {{frame.w | divide : 2 | add : spriteSourceSize.x}},
            "y": {{frame.h | divide : 2 | add : spriteSourceSize.y}},
            "w": {{sourceSize.w}},
            "h": {{sourceSize.h}}
          }
        }{{^last}},{{/last}}
        {{/rects}}
      ]
    }
  ],
  "meta": {
    "app": "{{{appInfo.url}}}",
    "version": "{{appInfo.version}}",
    "format": "{{config.format}}",
    "scale": {{config.scale}}
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants