1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-02-26 15:33:13 +00:00
Pixelorama/src/Shaders/Gradients/LinearDithering.gdshader

63 lines
2.1 KiB
Text
Raw Normal View History

Shader-based gradients (#677) * Add 6 shader-based gradient types * Shaders now respect selection * Fix step gradient * Remove comments * Disable step and dithering shaders in Web version * Fixed a weird bug with dithering shaders, selection and GLES2 Having a selection, applying a dithering gradient, removing the selection and then going to the gradient dialog again causes the dithering shaders to "remember" the previous selection, even if there is no selection currently. This only happens with the two dithering shaders and only with the GLES2 renderer. Removing `uniform sampler2D dither_texture;` from the shader code seems to fix the issue, but that's obviously isn't what we want so a "proper" fix is included in this commit. * Format & lint * Removed old gradient code * Change how centers work on radial step and dithering * Made angle, center and radius option a bit more clear * Rename bayer-matrices directory to dither-matrices * Use DitherMatrix class * Create dithering types programmatically * Remove unneeded code in shaders * Rewrite the step shader without a for loop More optimized and works on the Web version with GLES2 * Rewrite radial step and dithering shaders without for loop Now all shaders work on the Web version and have been optimized. * Fix Linear & Radial size range and remove some unneeded lines * Added size uniform to Radial Step and Radial Dither * Swap colors in the Linear gradient * Make size a percentage * Make the preview look the same as the result Didn't change the dithering shaders because they seemed to give different results. * Remove ratio uniform and divide uvs by the radius instead This makes more sense because the smaller the number, the smaller the radius. * Fix linear gradient * Change Position to percentage and "Size" to "Transition size" * Mix gradients with original color, if the gradient colors have transparency
2022-05-02 16:12:00 +03:00
shader_type canvas_item;
render_mode unshaded;
uniform sampler2D gradient_texture;
Shader-based gradients (#677) * Add 6 shader-based gradient types * Shaders now respect selection * Fix step gradient * Remove comments * Disable step and dithering shaders in Web version * Fixed a weird bug with dithering shaders, selection and GLES2 Having a selection, applying a dithering gradient, removing the selection and then going to the gradient dialog again causes the dithering shaders to "remember" the previous selection, even if there is no selection currently. This only happens with the two dithering shaders and only with the GLES2 renderer. Removing `uniform sampler2D dither_texture;` from the shader code seems to fix the issue, but that's obviously isn't what we want so a "proper" fix is included in this commit. * Format & lint * Removed old gradient code * Change how centers work on radial step and dithering * Made angle, center and radius option a bit more clear * Rename bayer-matrices directory to dither-matrices * Use DitherMatrix class * Create dithering types programmatically * Remove unneeded code in shaders * Rewrite the step shader without a for loop More optimized and works on the Web version with GLES2 * Rewrite radial step and dithering shaders without for loop Now all shaders work on the Web version and have been optimized. * Fix Linear & Radial size range and remove some unneeded lines * Added size uniform to Radial Step and Radial Dither * Swap colors in the Linear gradient * Make size a percentage * Make the preview look the same as the result Didn't change the dithering shaders because they seemed to give different results. * Remove ratio uniform and divide uvs by the radius instead This makes more sense because the smaller the number, the smaller the radius. * Fix linear gradient * Change Position to percentage and "Size" to "Transition size" * Mix gradients with original color, if the gradient colors have transparency
2022-05-02 16:12:00 +03:00
uniform sampler2D dither_texture;
uniform sampler2D offset_texture;
Shader-based gradients (#677) * Add 6 shader-based gradient types * Shaders now respect selection * Fix step gradient * Remove comments * Disable step and dithering shaders in Web version * Fixed a weird bug with dithering shaders, selection and GLES2 Having a selection, applying a dithering gradient, removing the selection and then going to the gradient dialog again causes the dithering shaders to "remember" the previous selection, even if there is no selection currently. This only happens with the two dithering shaders and only with the GLES2 renderer. Removing `uniform sampler2D dither_texture;` from the shader code seems to fix the issue, but that's obviously isn't what we want so a "proper" fix is included in this commit. * Format & lint * Removed old gradient code * Change how centers work on radial step and dithering * Made angle, center and radius option a bit more clear * Rename bayer-matrices directory to dither-matrices * Use DitherMatrix class * Create dithering types programmatically * Remove unneeded code in shaders * Rewrite the step shader without a for loop More optimized and works on the Web version with GLES2 * Rewrite radial step and dithering shaders without for loop Now all shaders work on the Web version and have been optimized. * Fix Linear & Radial size range and remove some unneeded lines * Added size uniform to Radial Step and Radial Dither * Swap colors in the Linear gradient * Make size a percentage * Make the preview look the same as the result Didn't change the dithering shaders because they seemed to give different results. * Remove ratio uniform and divide uvs by the radius instead This makes more sense because the smaller the number, the smaller the radius. * Fix linear gradient * Change Position to percentage and "Size" to "Transition size" * Mix gradients with original color, if the gradient colors have transparency
2022-05-02 16:12:00 +03:00
uniform sampler2D selection;
uniform vec2 image_size = vec2(64.0);
uniform float position : hint_range(-0.5, 0.5) = 0.0;
uniform float angle : hint_range(0.0, 360.0) = 0.0;
uniform int pixel_size : hint_range(2, 16) = 2;
uniform int n_of_colors = 2;
// Logic taken from https://godotshaders.com/shader/dither-gradient-shader/
float dither(vec2 uv, float modified_uv) {
float uv_lower = floor(modified_uv);
float uv_upper = (floor(modified_uv) + 1.0);
float threshold = texture(dither_texture, uv * (image_size / float(pixel_size))).r;
float ramp_val = modified_uv < threshold ? 0.0 : 1.0;
// sample at the lower bound colour if ramp_val is 0.0, upper bound color if 1.0
float col_sample = mix(uv_lower, uv_upper, ramp_val);
return col_sample;
}
Shader-based gradients (#677) * Add 6 shader-based gradient types * Shaders now respect selection * Fix step gradient * Remove comments * Disable step and dithering shaders in Web version * Fixed a weird bug with dithering shaders, selection and GLES2 Having a selection, applying a dithering gradient, removing the selection and then going to the gradient dialog again causes the dithering shaders to "remember" the previous selection, even if there is no selection currently. This only happens with the two dithering shaders and only with the GLES2 renderer. Removing `uniform sampler2D dither_texture;` from the shader code seems to fix the issue, but that's obviously isn't what we want so a "proper" fix is included in this commit. * Format & lint * Removed old gradient code * Change how centers work on radial step and dithering * Made angle, center and radius option a bit more clear * Rename bayer-matrices directory to dither-matrices * Use DitherMatrix class * Create dithering types programmatically * Remove unneeded code in shaders * Rewrite the step shader without a for loop More optimized and works on the Web version with GLES2 * Rewrite radial step and dithering shaders without for loop Now all shaders work on the Web version and have been optimized. * Fix Linear & Radial size range and remove some unneeded lines * Added size uniform to Radial Step and Radial Dither * Swap colors in the Linear gradient * Make size a percentage * Make the preview look the same as the result Didn't change the dithering shaders because they seemed to give different results. * Remove ratio uniform and divide uvs by the radius instead This makes more sense because the smaller the number, the smaller the radius. * Fix linear gradient * Change Position to percentage and "Size" to "Transition size" * Mix gradients with original color, if the gradient colors have transparency
2022-05-02 16:12:00 +03:00
void fragment() {
vec4 original_color = texture(TEXTURE, UV);
vec4 selection_color = texture(selection, UV);
vec2 uv = UV - 0.5;
float angle_cos = cos(radians(angle));
float angle_sin = sin(radians(angle));
float rotated_uv = uv.x * angle_cos - uv.y * angle_sin;
rotated_uv /= abs(angle_cos) + abs(angle_sin);
rotated_uv -= position - 0.5;
Shader-based gradients (#677) * Add 6 shader-based gradient types * Shaders now respect selection * Fix step gradient * Remove comments * Disable step and dithering shaders in Web version * Fixed a weird bug with dithering shaders, selection and GLES2 Having a selection, applying a dithering gradient, removing the selection and then going to the gradient dialog again causes the dithering shaders to "remember" the previous selection, even if there is no selection currently. This only happens with the two dithering shaders and only with the GLES2 renderer. Removing `uniform sampler2D dither_texture;` from the shader code seems to fix the issue, but that's obviously isn't what we want so a "proper" fix is included in this commit. * Format & lint * Removed old gradient code * Change how centers work on radial step and dithering * Made angle, center and radius option a bit more clear * Rename bayer-matrices directory to dither-matrices * Use DitherMatrix class * Create dithering types programmatically * Remove unneeded code in shaders * Rewrite the step shader without a for loop More optimized and works on the Web version with GLES2 * Rewrite radial step and dithering shaders without for loop Now all shaders work on the Web version and have been optimized. * Fix Linear & Radial size range and remove some unneeded lines * Added size uniform to Radial Step and Radial Dither * Swap colors in the Linear gradient * Make size a percentage * Make the preview look the same as the result Didn't change the dithering shaders because they seemed to give different results. * Remove ratio uniform and divide uvs by the radius instead This makes more sense because the smaller the number, the smaller the radius. * Fix linear gradient * Change Position to percentage and "Size" to "Transition size" * Mix gradients with original color, if the gradient colors have transparency
2022-05-02 16:12:00 +03:00
vec4 dither_value = texture(dither_texture, uv * (image_size / float(pixel_size)));
vec4 output;
for (int i = 1; i <= n_of_colors; i++) {
float colors_minus = float(n_of_colors - 1);
float off = texture(offset_texture, vec2(float(i) / colors_minus)).r;
float off_prev = texture(offset_texture, vec2(float(i - 1) / colors_minus)).r;
vec4 first = texture(gradient_texture, vec2(float((i - 1)) / colors_minus));
vec4 second = texture(gradient_texture, vec2(float(i) / colors_minus));
if (rotated_uv < off_prev) {
if (i == 1) {
output = first;
}
continue;
}
if (rotated_uv > off) {
if (i == n_of_colors) {
output = second;
}
continue;
}
float uvt = (rotated_uv - off_prev) / (off - off_prev);
float col_sample = dither(UV, uvt);
output = mix(first, second, col_sample);
}
Shader-based gradients (#677) * Add 6 shader-based gradient types * Shaders now respect selection * Fix step gradient * Remove comments * Disable step and dithering shaders in Web version * Fixed a weird bug with dithering shaders, selection and GLES2 Having a selection, applying a dithering gradient, removing the selection and then going to the gradient dialog again causes the dithering shaders to "remember" the previous selection, even if there is no selection currently. This only happens with the two dithering shaders and only with the GLES2 renderer. Removing `uniform sampler2D dither_texture;` from the shader code seems to fix the issue, but that's obviously isn't what we want so a "proper" fix is included in this commit. * Format & lint * Removed old gradient code * Change how centers work on radial step and dithering * Made angle, center and radius option a bit more clear * Rename bayer-matrices directory to dither-matrices * Use DitherMatrix class * Create dithering types programmatically * Remove unneeded code in shaders * Rewrite the step shader without a for loop More optimized and works on the Web version with GLES2 * Rewrite radial step and dithering shaders without for loop Now all shaders work on the Web version and have been optimized. * Fix Linear & Radial size range and remove some unneeded lines * Added size uniform to Radial Step and Radial Dither * Swap colors in the Linear gradient * Make size a percentage * Make the preview look the same as the result Didn't change the dithering shaders because they seemed to give different results. * Remove ratio uniform and divide uvs by the radius instead This makes more sense because the smaller the number, the smaller the radius. * Fix linear gradient * Change Position to percentage and "Size" to "Transition size" * Mix gradients with original color, if the gradient colors have transparency
2022-05-02 16:12:00 +03:00
COLOR = mix(original_color, output, selection_color.a);
}