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

Unable to handle images with width or height that aren't multiples of 4 #286

Open
hostytoasty opened this issue Nov 23, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@hostytoasty
Copy link

hostytoasty commented Nov 23, 2024

I ran into this issue while trying to replace texture2d assets with modified versions of the same image. Obviously, this seems to be an etcpak error which I understand is a dependency issue. I just wanted to ask if I'm missing something obvious that would easily fix this, or if there's not much that can be done without fixing the dependency. Maintaining the image's dimension is key so resizing is not really great.

Code

        env = UnityPy.load(f)
        for obj in env.objects:
            if obj.type.name == 'Texture2D':
                data = obj.read()
                # getting file from somewhere
                texture_path = file.absolute()
                new_texture = Image.open(texture_path)
                # Fails at this line
                data.image = new_texture
                data.save()

Error

Traceback (most recent call last):
  File "...\asset_replace.py", line 63, in main
    data.image = new_texture
  File "Python\Python310\lib\site-packages\UnityPy\classes\legacy_patch\Texture2D.py", line 28, in _Texture2d_set_image
    img_data, tex_format = Texture2DConverter.image_to_texture2d(img, target_format)
  File "Python\Python310\lib\site-packages\UnityPy\export\Texture2DConverter.py", line 68, in image_to_texture2d
    enc_img = etcpak.compress_etc2_rgba(raw_img, img.width, img.height)
ValueError: width or height not multiple of 4

Bug
UnityPy unable to work with images of height or width that aren't multiples of 4

To Reproduce

  • Test Image is 506 x 434
  • following data:
    • Python 3.10
    • UnityPy 1.20.16
@hostytoasty hostytoasty added the bug Something isn't working label Nov 23, 2024
@K0lb3
Copy link
Owner

K0lb3 commented Nov 26, 2024

Nearly all compressed texture formats use 4x4 blocks. There are some exceptions to this, but just with different block sizes.
Image data passed to a texture compressor has to align by the block size.

So in your case, you have to pad the image to the nearest multiple of 4,
then assign that via .image, and finally adjust the width and height to the original values of the texture2d.

@hostytoasty
Copy link
Author

Interesting. I see now that the images in the failing file are indeed ASTC_RGB while the working one is just RGBA32.

Padding out the image should be easy. It seems like I can just expand out the margins since we already have the image, though I'll need to see whether it matters whether its centered or not. Would adjusting the width and height need to be done via editing the _Texture2d_set_image function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants