Skip to content

Commit 1dcb696

Browse files
Use texelFetch instead of texture for indexed mode shaders
Fixes various weird issues when palettes have empty slots, and removes unnecessary calculations.
1 parent d580523 commit 1dcb696

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

src/Shaders/Effects/Palettize.gdshader

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ vec4 swap_color(vec4 color) {
1414

1515
int n_of_colors = textureSize(palette_texture, 0).x;
1616
int color_index = find_index(color, n_of_colors, palette_texture);
17-
return texture(palette_texture, vec2(float(color_index) / float(n_of_colors), 0.0));
17+
return texelFetch(palette_texture, ivec2(color_index, 0), 0);
1818
}
1919

2020
void fragment() {

src/Shaders/FindPaletteColorIndex.gdshaderinc

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ int find_index(vec4 color, int n_of_colors, sampler2D palette_texture) {
22
int color_index = 0;
33
float smaller_distance = distance(color, texture(palette_texture, vec2(0.0)));
44
for (int i = 0; i <= n_of_colors; i++) {
5-
vec2 uv = vec2(float(i) / float(n_of_colors), 0.0);
6-
vec4 palette_color = texture(palette_texture, uv);
5+
vec4 palette_color = texelFetch(palette_texture, ivec2(i, 0), 0);
76
float dist = distance(color, palette_color);
87
if (dist < smaller_distance) {
98
smaller_distance = dist;

src/Shaders/IndexedToRGB.gdshader

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ uniform sampler2D palette_texture : filter_nearest;
77
uniform sampler2D indices_texture : filter_nearest;
88

99
void fragment() {
10-
float index = texture(indices_texture, UV).r;
10+
float index = texture(indices_texture, UV).r * 255.0;
1111
if (index <= EPSILON) { // If index is zero, make it transparent
1212
COLOR = vec4(0.0);
1313
}
1414
else {
1515
float n_of_colors = float(textureSize(palette_texture, 0).x);
16-
index -= 1.0 / 255.0;
17-
float index_normalized = ((index * 255.0)) / n_of_colors;
18-
if (index_normalized + EPSILON < 1.0) {
19-
COLOR = texture(palette_texture, vec2(index_normalized + EPSILON, 0.0));
16+
index -= 1.0;
17+
float index_normalized = index / n_of_colors;
18+
if (index < n_of_colors) {
19+
COLOR = texelFetch(palette_texture, ivec2(int(index), 0), 0);
2020
}
2121
else {
2222
// If index is bigger than the size of the palette, make it transparent.

src/Shaders/SetIndices.gdshader

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ uniform sampler2D palette_texture : filter_nearest;
88

99
void fragment() {
1010
vec4 color = texture(rgb_texture, UV);
11-
if (color.a <= 0.01) {
11+
if (color.a <= 0.0001) {
1212
COLOR.r = 0.0;
1313
}
1414
else {

0 commit comments

Comments
 (0)