linuxgurugamer / ksp_toolbar

Common API for GUI toolbars in KSP
BSD 2-Clause "Simplified" License
13 stars 14 forks source link

Request: Pixel perfect rendering on PNG icon at 24x24 #19

Closed Galenmacil closed 5 years ago

Galenmacil commented 5 years ago

Posting yet again a "problem" with the latest Toolbar version (1.7.20.0) rendering. As always an image is worth a thousand word so here we go:

Use a pixel perfect application to visualize this, i.e: No resampling/correction/smoothing on zoom. I used GIMP.

First Line are actual PNG icon visual. Second line is in-game rendering using Toolbar v1.7.18.0, the last version not using unBlur without scaling. Third line is in-game rendering using Toolbar v1.7.20.0, without unBlur set to 24x24 scaling. Toolbar Renders

Reference calibration PNG icon at 24x24: Calibrate

As you can see, something is not right with the way the new scaling works. The third image on the third line shows that, even set to 24x24 native scaling, some sort of manipulation is performed on the icon image. The red, green and yellow dot are completely lost in the process and the middle shows some heavy distortion.

I think that the Toolbar mod should, when set to 24x24, completely disable any form of image manipulation on the icon, just like it did in the past.

Edit: It looks like the plugin use 23x23 internally when set to 24x24 and so on, and so forth for other factors.

linuxgurugamer commented 5 years ago

What were the UI settings for KSP, and were they being applied during your test?

linuxgurugamer commented 5 years ago

Your suggestion is good, I will be implementing it in the next release

Galenmacil commented 5 years ago

The KSP UI settings were set to normal (100%) scaling. While browsing at the code, I came across this code segment in the "ksp_toolbar/Toolbar/Internal/TextureScale.cs " file, that, in my opinion, does not make much sense:

private static void ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear) { texColors = tex.GetPixels(); newColors = new Color[newWidth * newHeight]; if (useBilinear) { ratioX = 1.0f / ((float)newWidth / (tex.width - 1)); ratioY = 1.0f / ((float)newHeight / (tex.height - 1)); }

[...]

}

So, basically, when using bilinear filtering for the 2D image icon, the scaling is "skewed" by 1 pixel horizontally and 1 pixel vertically from the icon dimensions.

Suppose you have a 24x24 texture. The scaling is set to "24" (so, 24x24 is demanded). Now, do the math above and you will end up with a rescaled texture even though it should not be the case. Ratio for that example should be 1 for X and 1 for Y.

I believe the (tex.width -1) and (text.height-1) should be just (tex.width) and (text.height). Substracting one does not make any sense to me...

linuxgurugamer commented 5 years ago

I just released a beta which totally removes the UnBlur stuff, and also seems to do a better job with the images, can you try it out: https://www.dropbox.com/s/o1xoce5ibxfilfz/000_Toolbar-1.7.21.zip?dl=0

Galenmacil commented 5 years ago

I just gave it a try, the beta version, using the latest Toolbar Controller 0.1.8.1. The unblur.dll was removed from the Gamedata folder as well.

However, results are exactly the same as the previous version: Icon rendering at 24 scaling are still affected by rescaling. Also, there is one button that fails to render, That is the "[x] Science!" one which shows a purple square as well as the following error on the console:

_[ERR 21:53:12.348] [Toolbar] [ERROR] Cannot find texture to load:C:/Games/KSP/1.7.3/KSP_x64_Data/../GameData/ScienceChecklist/ChecklistSmall.png

[ERR 21:53:12.349] [Toolbar] [ERROR] button texture not found: ScienceChecklist/ChecklistSmall.png_

It was working fine in Toolbar version 1.7.19.1...

KSP.log

linuxgurugamer commented 5 years ago

Check the latest release, replaces that beta I mentioned

Galenmacil commented 5 years ago

Tried the latest version of both the Controller and Toolbar mod, without Unblur .DLL present in the GameData folder.

I am a bit confused as nothing mentioned on this page as changed: Texture skewing and [x] Science! purple button are still a problem... I will include the LOG so you can check for yourself the version tested.

KSP.log

However, the error spamming about toolbar registration being too late when entering the title screen are gone! Which is a good thing.

linuxgurugamer commented 5 years ago

I wanted you to test the latest release before continuing, I wasn't sure if the issues would have been touched. As of the latest release, I've totally reverted the UnBlur code.

I don't use [x] Science, and all the other buttons and mods I looked at which did have problems were fixed.

I'll take a look at [x] Science and it's issues in the next day or so. Still not sure about the texture, as of now, if the size is 24x24, it shouldn't be doing anything to it. I did notice on one button that when the resize was to 25x25, it looked better than 24x24, but that was before I reverted everything, after the revert, that button looked ok.

That reference image you provided earlier, any chance you can get me a dds version of it? I'm going to drop both into a test mod and see if I can replicate the blurring

Galenmacil commented 5 years ago

The problem is if I provide you with a DDS, the way the compression work, we will loose 99% of the color values for the Red, Green, Blue and Yellow pixel. There is no workaround that using DDS in particular for low resolution image like 24x24 or even 48x48...

But why a DDS? PNG are much better for small icons. Clearer and sharper. They may load faster (DDS) but, frankly, for such small images, the difference is probably ridiculously small.

Did you take a look at the code portion I mentioned above? I would really like to see what happen when you remove those -1. In my opinion, this will fix the skewing issue...

Calibration Icons.zip

I provided you with a PNG and a DDS. So you can see the difference yourself.

linuxgurugamer commented 5 years ago

I agree with you, but there are other mods which don't. This way I can test both ways Thanks I got the issue with [x]Science fixed, looking into this now

linuxgurugamer commented 5 years ago

I did see the code, but was hesitant to change that code without understanding; it was copied directly from Unity's wiki. Was also working out all the unblur crap

linuxgurugamer commented 5 years ago

Current release fixes both issues. The code you pointed out needed to be the way it is, removing the "-1" gets index out of bounds errors I fixed it 2 ways, first, by changing from bilinear to Point, and second, checking to see if the texture needs to be resized, if not, then it just returns.

Full changelog here: Fixed purple buttons from mods which add textures to the game database at runtime without a file (looking at [x]Science!) Optimized the Texture code a bit to eliminate duplicated code Changed resize from Bilinear to Point Added check for textures being resized to same size and returns if so without doing anything Added check for small icon (24x24) when bigtexture is available, if it is and a small texture is available, use that instead

Galenmacil commented 5 years ago

Excellent work! Pixel perfect icon at 24x24. Scaling work as expected for 48x48 using the calibration icon. Thanks. I suppose you are going to continue down this path for future version, so as far as I am concerned, you can close this "issue".

Thanks again.

Edit: The main menu annoyance about button registration is gone too...

linuxgurugamer commented 5 years ago

Thank you for being patient with the varioius betas