Skip to content

Commit

Permalink
Fix jumps on ActionSequencer transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
fedegratti committed Feb 26, 2022
1 parent 42a6e22 commit 09c00b0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v6.3.4
v6.3.5
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ohzi-core",
"version": "6.3.4",
"version": "6.3.5",
"description": "OHZI Core Library",
"source": "src/index.js",
"module": "build/index.module.js",
Expand Down
58 changes: 45 additions & 13 deletions src/action_sequencer/ActionSequencer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { Math as TMath } from 'three';
import OMath from '../utilities/OMath';

export default class ActionSequencer
{
Expand Down Expand Up @@ -121,9 +122,9 @@ export default class ActionSequencer
this.channels[interpolator.attribute_name].push(keyframe);
}

get_property_target_value(name)
get_current_target_value(name)
{
const keyframe = this.__get_nearest_keyframe(name, this.elapsed_time);
const keyframe = this.__get_current_keyframe(name, this.elapsed_time);

if (this.elapsed_time < keyframe.from)
{
Expand All @@ -133,6 +134,21 @@ export default class ActionSequencer
return keyframe.interpolator.evaluate(1);
}

get_current_starting_value(name)
{
const keyframe = this.__get_current_keyframe(name, this.elapsed_time);

return keyframe.interpolator.evaluate(0);
}

get_current_progress(name)
{
const keyframe = this.__get_current_keyframe(name, this.elapsed_time);
const t = this.__linear_map_01(this.elapsed_time, keyframe.from, keyframe.to);

return keyframe.interpolator.easing_function(t);
}

get_duration()
{
return this.duration;
Expand All @@ -154,14 +170,16 @@ export default class ActionSequencer
for (let i = 0; i < channel_names.length; i++)
{
const name = channel_names[i];
const keyframe = this.__get_nearest_keyframe(name, from);
const keyframe = this.__get_current_keyframe(name, from);

this.context[name] = this.evaluate_keyframe(keyframe, from);
}
}

evaluate_keyframe(keyframe, time)
{
this.tmp_t = this.__linear_map_01(time, keyframe.from, keyframe.to);

return keyframe.interpolator.evaluate(TMath.clamp(this.tmp_t, 0, 1));
}

Expand All @@ -174,26 +192,40 @@ export default class ActionSequencer
from_range_start_value,
from_range_end_value)
{
return ((value - from_range_start_value) / (from_range_end_value - from_range_start_value)) * (1 - 0) + 0;
return OMath.saturate(((value - from_range_start_value) / (from_range_end_value - from_range_start_value)) * (1 - 0) + 0);
}

__get_nearest_keyframe(channel_name, time)
__get_current_keyframe(channel_name, time)
{
let closest = undefined;
let min_time = 9999999;
let current = undefined;

const keyframes = this.channels[channel_name];

for (let i = 0; i < keyframes.length; i++)
{
const keyframe = keyframes[i];
const middle_time = (keyframe.to - keyframe.from) / 2 + keyframe.from;
const distance_to_middle_time = Math.abs(time - middle_time);

if (distance_to_middle_time < min_time)
if (time >= keyframe.from && time <= keyframe.to)
{
min_time = distance_to_middle_time;
closest = keyframe;
current = keyframe;
return current;
}
}
return closest;

let min = Infinity;

for (let i = 0; i < keyframes.length; i++)
{
const keyframe = keyframes[i];
const distance = Math.abs(keyframe.to - time);

if (distance < min)
{
current = keyframe;
min = distance;
}
}

return current;
}
}
4 changes: 3 additions & 1 deletion types/action_sequencer/ActionSequencer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export class ActionSequencer {
is_finished(): boolean;
add_action_event(trigger_time: any, action: any): void;
add_action_interpolator(from: any, to: any, interpolator: any, use_dynamic_from_value?: boolean): void;
get_property_target_value(name: any): any;
get_current_starting_value(name: string): number;
get_current_target_value(name: string): number;
get_current_progress(name: string): number;
get_duration(): number;
__play_clips(from: any, to: any): void;
evaluate_keyframe(keyframe: any, time: any): any;
Expand Down

0 comments on commit 09c00b0

Please sign in to comment.