My shader corrupts my image files?
Asked Answered
U

3

0

Hy, I'm new to game developping and currently learning 😛. I'm using godot V4.0.3, and experimenting with shaders and pixel art. When I try to use a sampler2D variable to import an image into my shader code, it somehow corrupts that image?? I have no idea what I'm doing wrong. Here is the shader that I am using:

shader_type canvas_item;

uniform highp sampler2D form : filter_nearest;

void fragment() {
	vec4 f = texture(form, UV);
	COLOR = f;
}

now, when I put this 3x3 image in the sampler2D form var:

My image file gets corrupted and somehow becomes this image:

Which is strange since godot says it is a 3x3 image, but somehow shows a 4x4 pixel image? I don't get it, what am I doing wrong?

Unitary answered 21/8, 2023 at 23:37 Comment(0)
U
1

Ohhhhh yeah!!! found the problem and a workaround! The problem is that when you drag an image into your shader's properties, godot reimports your image file, but this time using lossy compression. I tried several ways to fix this by changing my project settings to these:


But nothing worked. Maybe someone else knows which settings need to be changed?

Anyhow, The workaround I found was to simply drag my image in my shader parameters and then select my image in the FileSystem, go to the import tab and change the compression mode to lossless and mipmaps generate to false, and then reimport my image file.

Hope it helps someone!

Unitary answered 22/8, 2023 at 12:9 Comment(0)
W
1

Standard texture compression on the GPU works by giving you only 4 colours for each 4x4 block of the texture. These are two explicit colours and two colours interpolated at 33% and 66% between the first two colours.
It can definitely look bad when zoomed in and if there's too many different colours near each other (but has performance benefits and uses 1/4 to 1/8 (depending on mode) of the ram).

Wing answered 22/8, 2023 at 13:6 Comment(0)
W
1

I checked Godot 4's options to see what modes it uses.
When using VRAM Compressed, there's three formats available on desktop targets: DXT1, DXT5 and BPTC.
If the texture has no transparency, Godot picks DXT1. If it has transparency, DXT5 is picked. If you enable High Quality, Godot uses BPTC for transparency and no transparency.

DXT1 has the best compression (8:1). It's what I described above. It uses 16bit colour instead of 32bit. (DXT1 does have transparency support, but in exchange for a pure transparent colour you lose one of the interpolated colours. Godot doesn't support DXT1 transparency)
DXT5 has ok compression (4:1), they take twice the vram of DXT1. The colour part is the same as DXT1, but they add better alpha support (8 possible alpha values in each 4x4 block of the texture).
BPTC is one I'm not familiar with. It's similar to DXT5, but each block can pick from 7 different modes to try to get the best quality. It also has a floating point version.

Mobile targets have different modes, but they are similar in concept to the above.

Wing answered 23/8, 2023 at 0:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.