1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 09:09:47 +00:00

Use texelFetch instead of texture for indexed mode shaders

Fixes various weird issues when palettes have empty slots, and removes unnecessary calculations.
This commit is contained in:
Emmanouil Papadeas 2024-11-22 20:47:05 +02:00
parent d580523c6e
commit 1dcb696c35
4 changed files with 8 additions and 9 deletions

View file

@ -14,7 +14,7 @@ vec4 swap_color(vec4 color) {
int n_of_colors = textureSize(palette_texture, 0).x;
int color_index = find_index(color, n_of_colors, palette_texture);
return texture(palette_texture, vec2(float(color_index) / float(n_of_colors), 0.0));
return texelFetch(palette_texture, ivec2(color_index, 0), 0);
}
void fragment() {

View file

@ -2,8 +2,7 @@ int find_index(vec4 color, int n_of_colors, sampler2D palette_texture) {
int color_index = 0;
float smaller_distance = distance(color, texture(palette_texture, vec2(0.0)));
for (int i = 0; i <= n_of_colors; i++) {
vec2 uv = vec2(float(i) / float(n_of_colors), 0.0);
vec4 palette_color = texture(palette_texture, uv);
vec4 palette_color = texelFetch(palette_texture, ivec2(i, 0), 0);
float dist = distance(color, palette_color);
if (dist < smaller_distance) {
smaller_distance = dist;

View file

@ -7,16 +7,16 @@ uniform sampler2D palette_texture : filter_nearest;
uniform sampler2D indices_texture : filter_nearest;
void fragment() {
float index = texture(indices_texture, UV).r;
float index = texture(indices_texture, UV).r * 255.0;
if (index <= EPSILON) { // If index is zero, make it transparent
COLOR = vec4(0.0);
}
else {
float n_of_colors = float(textureSize(palette_texture, 0).x);
index -= 1.0 / 255.0;
float index_normalized = ((index * 255.0)) / n_of_colors;
if (index_normalized + EPSILON < 1.0) {
COLOR = texture(palette_texture, vec2(index_normalized + EPSILON, 0.0));
index -= 1.0;
float index_normalized = index / n_of_colors;
if (index < n_of_colors) {
COLOR = texelFetch(palette_texture, ivec2(int(index), 0), 0);
}
else {
// If index is bigger than the size of the palette, make it transparent.

View file

@ -8,7 +8,7 @@ uniform sampler2D palette_texture : filter_nearest;
void fragment() {
vec4 color = texture(rgb_texture, UV);
if (color.a <= 0.01) {
if (color.a <= 0.0001) {
COLOR.r = 0.0;
}
else {