Anim: Add Sharpness to Ease operator #117287

Merged
Christoph Lendenfeld merged 20 commits from ChrisLend/blender:graph_slider_ease_ease into main 2024-03-22 13:22:45 +01:00
6 changed files with 179 additions and 0 deletions
Showing only changes of commit b5084f2398 - Show all commits

View File

@ -338,6 +338,7 @@ class GRAPH_MT_key_blending(Menu):
layout.operator("graph.blend_to_neighbor", text="Blend to Neighbor")
layout.operator("graph.blend_to_default", text="Blend to Default Value")
layout.operator("graph.ease", text="Ease")
layout.operator("graph.ease_to_ease", text="Ease to Ease")
layout.operator("graph.blend_offset", text="Blend Offset")
layout.operator("graph.blend_to_ease", text="Blend to Ease")
layout.operator("graph.match_slope", text="Match Slope")

View File

@ -702,6 +702,58 @@ void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor
}
}
static float s_curve(
const float x, const float slope, const float width, const float height, const float shift)
{

This comment seems to be out of sync with the parameters.

This comment seems to be out of sync with the parameters.
/* Formula for 'S' curve we use for the "ease" sliders. The shift values move the curve verticaly
* or horizontaly. The range of the curve used is from 0 to 1 on "x" and "y" so we can scale it
* (width and height) and move it (xshift and y yshift) to crop the part of the curve we need.
* Slope determins how curvy the shape is */
float y = height * pow((x - shift), slope) /
(pow((x - shift), slope) + pow((width - (x - shift)), slope));
/* The curve has some noise beyond our margins so we clamp the values */
if (x > shift + width) {
y = height;
}
else if (x < shift) {
y = 0.0f;
}
return y;
}
void ease_to_ease_fcurve_segment(
FCurve *fcu, FCurveSegment *segment, const float factor, const float slope, const float width)
{
const BezTriple *left_key = fcurve_segment_start_get(fcu, segment->start_index);
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_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. */
if (IS_EQF(key_x_range, 0.0f)) {
return;
}
/* These ease values use the entire curve to get the "S" shape. The value 2.0 on the slope
* makes it a bit sharper. */
const float height = 1.0;
/* Using the factor on the xshift we are basicaly moving the curve horizontaly. */
const float shift = -factor;
for (int i = segment->start_index; i < segment->start_index + segment->length; i++) {
const float x = (fcu->bezt[i].vec[1][0] - left_key->vec[1][0]) / key_x_range;
const float ease = s_curve(x, slope, width, height, shift);
/* The ease variable basicaly act as a mask to molde the shape of the curve. */
const float key_y_value = left_key->vec[1][1] + key_y_range * ease;
BKE_fcurve_keyframe_move_value_with_handles(&fcu->bezt[i], key_y_value);
}
}
/* ---------------- */
void blend_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor)

View File

@ -470,6 +470,10 @@ void smooth_fcurve_segment(FCurve *fcu,
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_to_ease_fcurve_segment(
struct FCurve *fcu, struct FCurveSegment *segment, float factor, float slope, float width);
enum tShearDirection {
SHEAR_FROM_LEFT = 1,
SHEAR_FROM_RIGHT,

View File

@ -128,6 +128,7 @@ void GRAPH_OT_clean(struct wmOperatorType *ot);
void GRAPH_OT_blend_to_neighbor(struct wmOperatorType *ot);
void GRAPH_OT_breakdown(struct wmOperatorType *ot);
void GRAPH_OT_ease(struct wmOperatorType *ot);
void GRAPH_OT_ease_to_ease(struct wmOperatorType *ot);
void GRAPH_OT_blend_offset(struct wmOperatorType *ot);
void GRAPH_OT_blend_to_ease(struct wmOperatorType *ot);
void GRAPH_OT_match_slope(struct wmOperatorType *ot);

View File

@ -470,6 +470,7 @@ void graphedit_operatortypes()
WM_operatortype_append(GRAPH_OT_blend_to_neighbor);
WM_operatortype_append(GRAPH_OT_breakdown);
WM_operatortype_append(GRAPH_OT_ease);
WM_operatortype_append(GRAPH_OT_ease_to_ease);
WM_operatortype_append(GRAPH_OT_shear);
WM_operatortype_append(GRAPH_OT_scale_average);
WM_operatortype_append(GRAPH_OT_scale_from_neighbor);

View File

@ -2469,3 +2469,123 @@ void GRAPH_OT_scale_from_neighbor(wmOperatorType *ot)
"Reference Key",
"Which end of the segment to use as a reference to scale from");
}
/* -------------------------------------------------------------------- */
/** \name Ease Ease Operator
* \{ */
static void ease_ease_graph_keys(bAnimContext *ac,
const float factor,
const float slope,
const float width)
{
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_to_ease_fcurve_segment(fcu, segment, factor, slope, width);
}
ale->update |= ANIM_UPDATE_DEFAULT;
BLI_freelistN(&segments);
}
ANIM_animdata_update(ac, &anim_data);
ANIM_animdata_freelist(&anim_data);
}
static void ease_ease_draw_status_header(bContext *C, tGraphSliderOp *gso)
{
common_draw_status_header(C, gso, "Ease to Ease");
}
static void ease_ease_modal_update(bContext *C, wmOperator *op)
{
tGraphSliderOp *gso = static_cast<tGraphSliderOp *>(op->customdata);
ease_ease_draw_status_header(C, gso);
/* Reset keyframes to the state at invoke. */
reset_bezts(gso);
const float factor = slider_factor_get_and_remember(op);
const float slope = RNA_float_get(op->ptr, "slope");
const float width = RNA_float_get(op->ptr, "width");
ease_ease_graph_keys(&gso->ac, factor, slope, width);
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
}
static int ease_ease_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
const int invoke_result = graph_slider_invoke(C, op, event);
if (invoke_result == OPERATOR_CANCELLED) {
return invoke_result;
}
tGraphSliderOp *gso = static_cast<tGraphSliderOp *>(op->customdata);
gso->modal_update = ease_ease_modal_update;
gso->factor_prop = RNA_struct_find_property(op->ptr, "factor");
ease_ease_draw_status_header(C, gso);
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);
return invoke_result;
}
static int ease_ease_exec(bContext *C, wmOperator *op)
{
bAnimContext ac;
if (ANIM_animdata_get_context(C, &ac) == 0) {
return OPERATOR_CANCELLED;
}
const float factor = RNA_float_get(op->ptr, "factor");
const float slope = RNA_float_get(op->ptr, "slope");
const float width = RNA_float_get(op->ptr, "width");
ease_ease_graph_keys(&ac, factor, slope, width);
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
return OPERATOR_FINISHED;
}
void GRAPH_OT_ease_to_ease(wmOperatorType *ot)
{
/* Identifiers. */
ot->name = "Ease To Ease";
ot->idname = "GRAPH_OT_ease_to_ease";
ot->description = "Align keyframes on a ease-in or ease-out curve";
/* API callbacks. */
ot->invoke = ease_ease_invoke;
ot->modal = graph_slider_modal;
ot->exec = ease_ease_exec;
ot->poll = graphop_editable_keyframes_poll;
/* Flags. */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_float_factor(ot->srna,
"factor",
0.0f,
-FLT_MAX,
FLT_MAX,
"Curve Bend",
"Defines if the keys should be aligned on an ease-in or ease-out curve",
-1.0f,
1.0f);
RNA_def_float(ot->srna, "slope", 2.0f, 0.0f, FLT_MAX, "Slope", "Foo", 0.0f, 16.0f);
RNA_def_float(ot->srna, "width", 1.0f, -FLT_MAX, FLT_MAX, "Width", "Foo", -16.0f, 16.0f);
}
/** \} */