Anim: Add Sharpness to Ease operator
This PR extends the existing `Ease` operator with a sharpness property. This means it has two parameters: * Curve Bend: Define which key to favor. At 0 it's exactly between the two keys. * Sharpness: Higher values make the change more abrupt. During modal operation it is possible to switch the slider between those two by pressing `TAB`. Pull Request: https://projects.blender.org/blender/blender/pulls/117287
This commit is contained in:
parent
646f9bcebe
commit
62f140e048
|
@ -662,16 +662,24 @@ void smooth_fcurve_segment(FCurve *fcu,
|
|||
}
|
||||
/* ---------------- */
|
||||
|
||||
void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor)
|
||||
static float ease_sigmoid_function(const float x, const float width, const float shift)
|
||||
{
|
||||
const float x_shift = (x - shift) * width;
|
||||
const float y = x_shift / sqrt(1 + pow2f(x_shift));
|
||||
/* Normalize result to 0-1. */
|
||||
return (y + 1) * 0.5f;
|
||||
}
|
||||
|
||||
void ease_fcurve_segment(FCurve *fcu,
|
||||
FCurveSegment *segment,
|
||||
const float factor,
|
||||
const float width)
|
||||
{
|
||||
const BezTriple *left_key = fcurve_segment_start_get(fcu, segment->start_index);
|
||||
const float left_x = left_key->vec[1][0];
|
||||
const float left_y = left_key->vec[1][1];
|
||||
|
||||
const BezTriple *right_key = fcurve_segment_end_get(fcu, segment->start_index + segment->length);
|
||||
|
||||
const float key_x_range = right_key->vec[1][0] - left_x;
|
||||
const float key_y_range = right_key->vec[1][1] - left_y;
|
||||
const float key_x_range = right_key->vec[1][0] - left_key->vec[1][0];
|
||||
const float key_y_range = right_key->vec[1][1] - left_key->vec[1][1];
|
||||
|
||||
/* Happens if there is only 1 key on the FCurve. Needs to be skipped because it
|
||||
* would be a divide by 0. */
|
||||
|
@ -679,24 +687,20 @@ void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor
|
|||
return;
|
||||
}
|
||||
|
||||
/* In order to have a curve that favors the right key, the curve needs to be mirrored in x and y.
|
||||
* Having an exponent that is a fraction of 1 would produce a similar but inferior result. */
|
||||
const bool inverted = factor > 0;
|
||||
const float exponent = 1 + fabs(factor) * 4;
|
||||
/* Using the factor on the xshift we are basicaly moving the curve horizontaly. */
|
||||
const float shift = -factor;
|
||||
const float y_min = ease_sigmoid_function(-1, width, shift);
|
||||
const float y_max = ease_sigmoid_function(1, width, shift);
|
||||
|
||||
for (int i = segment->start_index; i < segment->start_index + segment->length; i++) {
|
||||
/* For easy calculation of the curve, the values are normalized. */
|
||||
const float normalized_x = (fcu->bezt[i].vec[1][0] - left_x) / key_x_range;
|
||||
/* Mapping the x-location of the key within the segment to a -1/1 range. */
|
||||
const float x = ((fcu->bezt[i].vec[1][0] - left_key->vec[1][0]) / key_x_range) * 2 - 1;
|
||||
const float y = ease_sigmoid_function(x, width, shift);
|
||||
/* Normalizing the y value to the min and max to ensure that the keys at the end are not
|
||||
* detached from the rest of the animation. */
|
||||
const float blend = (y - y_min) * (1 / (y_max - y_min));
|
||||
|
||||
float normalized_y = 0;
|
||||
if (inverted) {
|
||||
normalized_y = 1 - pow(1 - normalized_x, exponent);
|
||||
}
|
||||
else {
|
||||
normalized_y = pow(normalized_x, exponent);
|
||||
}
|
||||
|
||||
const float key_y_value = left_y + normalized_y * key_y_range;
|
||||
const float key_y_value = left_key->vec[1][1] + key_y_range * blend;
|
||||
BKE_fcurve_keyframe_move_value_with_handles(&fcu->bezt[i], key_y_value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -470,7 +470,10 @@ void smooth_fcurve_segment(FCurve *fcu,
|
|||
float factor,
|
||||
int kernel_size,
|
||||
double *kernel);
|
||||
void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor);
|
||||
/** Snap the keys on the given FCurve segment to an S-Curve. By modifying the `factor` the part of
|
||||
* the S-Curve that the keys are snapped to is moved on the x-axis.*/
|
||||
void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor, float width);
|
||||
|
||||
enum tShearDirection {
|
||||
SHEAR_FROM_LEFT = 1,
|
||||
SHEAR_FROM_RIGHT,
|
||||
|
|
|
@ -918,22 +918,120 @@ void GRAPH_OT_blend_to_default(wmOperatorType *ot)
|
|||
/** \name Ease Operator
|
||||
* \{ */
|
||||
|
||||
static void ease_graph_keys(bAnimContext *ac, const float factor)
|
||||
static void ease_graph_keys(bAnimContext *ac, const float factor, const float width)
|
||||
{
|
||||
apply_fcu_segment_function(ac, factor, ease_fcurve_segment);
|
||||
ListBase anim_data = {NULL, NULL};
|
||||
|
||||
ANIM_animdata_filter(
|
||||
ac, &anim_data, OPERATOR_DATA_FILTER, ac->data, eAnimCont_Types(ac->datatype));
|
||||
LISTBASE_FOREACH (bAnimListElem *, ale, &anim_data) {
|
||||
FCurve *fcu = (FCurve *)ale->key_data;
|
||||
ListBase segments = find_fcurve_segments(fcu);
|
||||
|
||||
LISTBASE_FOREACH (FCurveSegment *, segment, &segments) {
|
||||
ease_fcurve_segment(fcu, segment, factor, width);
|
||||
}
|
||||
|
||||
ale->update |= ANIM_UPDATE_DEFAULT;
|
||||
BLI_freelistN(&segments);
|
||||
}
|
||||
|
||||
ANIM_animdata_update(ac, &anim_data);
|
||||
ANIM_animdata_freelist(&anim_data);
|
||||
}
|
||||
|
||||
static void ease_draw_status_header(bContext *C, wmOperator *op)
|
||||
{
|
||||
char status_str[UI_MAX_DRAW_STR];
|
||||
char mode_str[32];
|
||||
char slider_string[UI_MAX_DRAW_STR];
|
||||
|
||||
tGraphSliderOp *gso = static_cast<tGraphSliderOp *>(op->customdata);
|
||||
ED_slider_status_string_get(gso->slider, slider_string, UI_MAX_DRAW_STR);
|
||||
|
||||
/* Operator specific functionality that extends beyond the slider. */
|
||||
char op_slider_string[UI_MAX_DRAW_STR];
|
||||
if (strcmp(RNA_property_identifier(gso->factor_prop), "factor") == 0) {
|
||||
SNPRINTF(op_slider_string, "%s | %s", slider_string, IFACE_("[TAB] - Modify Sharpness"));
|
||||
}
|
||||
else {
|
||||
SNPRINTF(op_slider_string, "%s | %s", slider_string, IFACE_("[TAB] - Modify Curve Bend"));
|
||||
}
|
||||
|
||||
STRNCPY(mode_str, IFACE_("Ease Keys"));
|
||||
|
||||
if (hasNumInput(&gso->num)) {
|
||||
char str_ofs[NUM_STR_REP_LEN];
|
||||
|
||||
outputNumInput(&gso->num, str_ofs, &gso->scene->unit);
|
||||
|
||||
SNPRINTF(status_str, "%s: %s", mode_str, str_ofs);
|
||||
}
|
||||
else {
|
||||
SNPRINTF(status_str, "%s: %s", mode_str, op_slider_string);
|
||||
}
|
||||
|
||||
ED_workspace_status_text(C, status_str);
|
||||
}
|
||||
|
||||
static void ease_modal_update(bContext *C, wmOperator *op)
|
||||
{
|
||||
tGraphSliderOp *gso = static_cast<tGraphSliderOp *>(op->customdata);
|
||||
|
||||
common_draw_status_header(C, gso, "Ease Keys");
|
||||
ease_draw_status_header(C, op);
|
||||
|
||||
/* Reset keyframes to the state at invoke. */
|
||||
reset_bezts(gso);
|
||||
const float factor = slider_factor_get_and_remember(op);
|
||||
ease_graph_keys(&gso->ac, factor);
|
||||
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, nullptr);
|
||||
float factor;
|
||||
float width;
|
||||
if (strcmp(RNA_property_identifier(gso->factor_prop), "factor") == 0) {
|
||||
factor = slider_factor_get_and_remember(op);
|
||||
width = RNA_float_get(op->ptr, "sharpness");
|
||||
}
|
||||
else {
|
||||
factor = RNA_float_get(op->ptr, "factor");
|
||||
width = slider_factor_get_and_remember(op);
|
||||
}
|
||||
|
||||
ease_graph_keys(&gso->ac, factor, width);
|
||||
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
|
||||
}
|
||||
|
||||
static int ease_modal(bContext *C, wmOperator *op, const wmEvent *event)
|
||||
{
|
||||
if (event->val != KM_PRESS) {
|
||||
return graph_slider_modal(C, op, event);
|
||||
}
|
||||
|
||||
switch (event->type) {
|
||||
case EVT_TABKEY: {
|
||||
tGraphSliderOp *gso = static_cast<tGraphSliderOp *>(op->customdata);
|
||||
if (strcmp(RNA_property_identifier(gso->factor_prop), "factor") == 0) {
|
||||
/* Switch to sharpness. */
|
||||
ED_slider_allow_overshoot_set(gso->slider, false, true);
|
||||
ED_slider_factor_bounds_set(gso->slider, 0.001f, 10);
|
||||
ED_slider_factor_set(gso->slider, RNA_float_get(op->ptr, "sharpness"));
|
||||
ED_slider_mode_set(gso->slider, SLIDER_MODE_FLOAT);
|
||||
ED_slider_unit_set(gso->slider, "");
|
||||
gso->factor_prop = RNA_struct_find_property(op->ptr, "sharpness");
|
||||
}
|
||||
else {
|
||||
ED_slider_allow_overshoot_set(gso->slider, false, false);
|
||||
ED_slider_factor_bounds_set(gso->slider, -1, 1);
|
||||
ED_slider_factor_set(gso->slider, 0.0f);
|
||||
ED_slider_factor_set(gso->slider, RNA_float_get(op->ptr, "factor"));
|
||||
ED_slider_mode_set(gso->slider, SLIDER_MODE_PERCENT);
|
||||
ED_slider_unit_set(gso->slider, "%");
|
||||
gso->factor_prop = RNA_struct_find_property(op->ptr, "factor");
|
||||
}
|
||||
ease_modal_update(C, op);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
return graph_slider_modal(C, op, event);
|
||||
}
|
||||
return OPERATOR_RUNNING_MODAL;
|
||||
}
|
||||
|
||||
static int ease_invoke(bContext *C, wmOperator *op, const wmEvent *event)
|
||||
|
@ -947,7 +1045,8 @@ static int ease_invoke(bContext *C, wmOperator *op, const wmEvent *event)
|
|||
tGraphSliderOp *gso = static_cast<tGraphSliderOp *>(op->customdata);
|
||||
gso->modal_update = ease_modal_update;
|
||||
gso->factor_prop = RNA_struct_find_property(op->ptr, "factor");
|
||||
common_draw_status_header(C, gso, "Ease Keys");
|
||||
ease_draw_status_header(C, op);
|
||||
ED_slider_allow_overshoot_set(gso->slider, false, false);
|
||||
ED_slider_factor_bounds_set(gso->slider, -1, 1);
|
||||
ED_slider_factor_set(gso->slider, 0.0f);
|
||||
|
||||
|
@ -958,17 +1057,16 @@ static int ease_exec(bContext *C, wmOperator *op)
|
|||
{
|
||||
bAnimContext ac;
|
||||
|
||||
/* Get editor data. */
|
||||
if (ANIM_animdata_get_context(C, &ac) == 0) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
const float factor = RNA_float_get(op->ptr, "factor");
|
||||
const float width = RNA_float_get(op->ptr, "sharpness");
|
||||
|
||||
ease_graph_keys(&ac, factor);
|
||||
ease_graph_keys(&ac, factor, width);
|
||||
|
||||
/* Set notifier that keyframes have changed. */
|
||||
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, nullptr);
|
||||
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
|
||||
|
||||
return OPERATOR_FINISHED;
|
||||
}
|
||||
|
@ -982,7 +1080,7 @@ void GRAPH_OT_ease(wmOperatorType *ot)
|
|||
|
||||
/* API callbacks. */
|
||||
ot->invoke = ease_invoke;
|
||||
ot->modal = graph_slider_modal;
|
||||
ot->modal = ease_modal;
|
||||
ot->exec = ease_exec;
|
||||
ot->poll = graphop_editable_keyframes_poll;
|
||||
|
||||
|
@ -995,9 +1093,19 @@ void GRAPH_OT_ease(wmOperatorType *ot)
|
|||
-FLT_MAX,
|
||||
FLT_MAX,
|
||||
"Curve Bend",
|
||||
"Control the bend of the curve",
|
||||
"Defines if the keys should be aligned on an ease-in or ease-out curve",
|
||||
-1.0f,
|
||||
1.0f);
|
||||
|
||||
RNA_def_float(ot->srna,
|
||||
"sharpness",
|
||||
2.0f,
|
||||
0.001f,
|
||||
FLT_MAX,
|
||||
"Sharpness",
|
||||
"Higher values make the change more abrupt",
|
||||
0.01f,
|
||||
16.0f);
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
@ -2468,3 +2576,5 @@ void GRAPH_OT_scale_from_neighbor(wmOperatorType *ot)
|
|||
"Reference Key",
|
||||
"Which end of the segment to use as a reference to scale from");
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
|
Loading…
Reference in New Issue