Pixel-Composer/scripts/gradients_function/gradients_function.gml

119 lines
3.3 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
enum GRADIENT_INTER {
smooth,
2022-11-14 03:16:15 +01:00
none,
hue
2022-01-13 05:24:03 +01:00
}
2023-02-14 05:32:32 +01:00
function gradientKey(time, value) constructor {
self.time = time;
self.value = value;
static clone = function() { return new gradientKey(time, value); }
static serialize = function() {
var m = ds_map_create();
m[? "time"] = time;
m[? "value"] = value;
return m;
}
}
2022-01-13 05:24:03 +01:00
function draw_gradient(_x, _y, _w, _h, _grad, _int = GRADIENT_INTER.smooth) {
static RES = 48;
var _step = _w / RES;
var _ox, _oc;
2022-11-14 03:16:15 +01:00
var uniform_grad_blend = shader_get_uniform(sh_gradient_display, "gradient_blend");
var uniform_grad = shader_get_uniform(sh_gradient_display, "gradient_color");
var uniform_grad_time = shader_get_uniform(sh_gradient_display, "gradient_time");
var uniform_grad_key = shader_get_uniform(sh_gradient_display, "gradient_keys");
var _grad_color = [];
var _grad_time = [];
2023-02-28 09:43:01 +01:00
2023-02-14 05:32:32 +01:00
for(var i = 0; i < array_length(_grad); i++) {
2023-02-23 07:02:19 +01:00
if(_grad[i].value == undefined) return;
2023-02-14 05:32:32 +01:00
_grad_color[i * 4 + 0] = color_get_red(_grad[i].value) / 255;
_grad_color[i * 4 + 1] = color_get_green(_grad[i].value) / 255;
_grad_color[i * 4 + 2] = color_get_blue(_grad[i].value) / 255;
2022-11-14 03:16:15 +01:00
_grad_color[i * 4 + 3] = 1;
2023-02-14 05:32:32 +01:00
_grad_time[i] = _grad[i].time;
2022-11-14 03:16:15 +01:00
}
2023-02-14 05:32:32 +01:00
if(array_length(_grad) == 0) {
2022-11-14 03:16:15 +01:00
draw_sprite_stretched(s_fx_pixel, 0, _x, _y, _w, _h)
} else {
shader_set(sh_gradient_display);
shader_set_uniform_i(uniform_grad_blend, _int);
2023-02-14 05:32:32 +01:00
shader_set_uniform_f_array_safe(uniform_grad, _grad_color);
shader_set_uniform_f_array_safe(uniform_grad_time, _grad_time);
shader_set_uniform_i(uniform_grad_key, array_length(_grad));
2022-11-14 03:16:15 +01:00
draw_sprite_stretched(s_fx_pixel, 0, _x, _y, _w, _h)
shader_reset();
2022-01-13 05:24:03 +01:00
}
}
function gradient_eval(_gradient, _time, _int = GRADIENT_INTER.smooth) {
2023-02-14 05:32:32 +01:00
if(array_length(_gradient) == 0) return c_white;
if(array_length(_gradient) == 1) return _gradient[0].value;
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
for(var i = 0; i < array_length(_gradient); i++) {
var _key = _gradient[i];
2023-01-17 08:11:55 +01:00
if(_key.time < _time) continue;
2023-02-14 05:32:32 +01:00
if(_key.time == _time) return _gradient[i].value;
2023-01-17 08:11:55 +01:00
if(i == 0) //before first color
2023-02-14 05:32:32 +01:00
return _gradient[0].value;
2023-01-17 08:11:55 +01:00
2023-02-14 05:32:32 +01:00
var c0 = _gradient[i - 1].value;
2023-01-17 08:11:55 +01:00
if(_int == GRADIENT_INTER.smooth) {
2023-02-14 05:32:32 +01:00
var rat = (_time - _gradient[i - 1].time) / (_gradient[i].time - _gradient[i - 1].time);
var c1 = _gradient[i].value;
2023-01-17 08:11:55 +01:00
return merge_color(c0, c1, rat);
} else if(_int == GRADIENT_INTER.none) {
return c0;
2022-01-13 05:24:03 +01:00
}
}
2023-02-14 05:32:32 +01:00
return _gradient[array_length(_gradient) - 1].value; //after last color
2022-01-13 05:24:03 +01:00
}
function gradient_add(_gradient, _addkey, _deleteDup) {
2023-02-14 05:32:32 +01:00
if(array_length(_gradient) == 0) {
array_push(_gradient, _addkey);
2022-01-13 05:24:03 +01:00
return;
}
2023-02-14 05:32:32 +01:00
for(var i = 0; i < array_length(_gradient); i++) {
var _key = _gradient[i];
2022-01-13 05:24:03 +01:00
if(_key.time == _addkey.time) {
if(_deleteDup)
_key.value = _addkey.value;
return;
} else if(_key.time > _addkey.time) {
2023-02-14 05:32:32 +01:00
array_insert(_gradient, i, _addkey);
2022-01-13 05:24:03 +01:00
return;
}
}
2023-02-14 05:32:32 +01:00
array_push(_gradient, _addkey);
}
function gradient_to_array(_gradient) {
var _grad_color = [], _grad_time = [];
for(var i = 0; i < array_length(_gradient); i++) {
2023-02-23 07:02:19 +01:00
if(is_undefined(_gradient[i].value)) continue;
2023-02-14 05:32:32 +01:00
_grad_color[i * 4 + 0] = color_get_red(_gradient[i].value) / 255;
_grad_color[i * 4 + 1] = color_get_green(_gradient[i].value) / 255;
_grad_color[i * 4 + 2] = color_get_blue(_gradient[i].value) / 255;
_grad_color[i * 4 + 3] = 1;
_grad_time[i] = _gradient[i].time;
}
return [ _grad_color, _grad_time ];
2022-01-13 05:24:03 +01:00
}