Skip to content

Instantly share code, notes, and snippets.

@terence410
Last active June 20, 2019 14:44
Show Gist options
  • Save terence410/c6edcdfe7efaf6454c99f97f32e8ee44 to your computer and use it in GitHub Desktop.
Save terence410/c6edcdfe7efaf6454c99f97f32e8ee44 to your computer and use it in GitHub Desktop.
Tween
class DecayTween{
constructor({currentValue = 1, targetValue = 1, scaleFactor = 0.2, decayFactor = 0} = {}){
this.currentValue = currentValue;
this.targetValue = targetValue;
this.scaleFactor = scaleFactor;
this.decayFactor = decayFactor;
this.decay = 0;
}
update(){
this.decay = this.decay * this.decayFactor + (this.targetValue - this.currentValue) * this.scaleFactor;
}
}
class Vector2DecayTween{
constructor({start = THREE.Vector2(), end = THREE.Vector2(), scaleFactor = 0.2, decayFactor = 0} = {}){
/** @type {Vector2} */
this.start = start;
/** @type {Vector2} */
this.end = end;
/** @type {Number} */
this.scaleFactor = scaleFactor;
/** @type {Number} */
this.decayFactor = decayFactor;
/** @private */
this._currentValue = 0;
/** @private */
this._targetValue = 0;
/** @private */
this._decay = 0;
this._updateInternal();
}
/** @private */
_updateInternal(){
this._currentValue = 0;
let vector = this.end.clone().sub(this.start);
this._targetValue = vector.length();
}
getCurrentPoint(){
let direction = this.end.clone().sub(this.start).normalize();
let point = this.start.clone();
return point.add(direction.multiplyScalar(this._currentValue));
}
/**
* @param {Vector2} value
* @returns {Vector2DecayTween}
*/
setStart(value){
this.start = value;
this._updateInternal();
return this;
}
/**
* @param {Vector2} value
* @returns {Vector2DecayTween}
*/
setEnd(value){
this.start = this.getCurrentPoint();
this.end = value;
this._updateInternal();
return this;
}
/**
* @returns {Vector2DecayTween}
*/
clearMomentum(){
this._decay = 0;
return this;
}
update(){
this._decay = this._decay * this.decayFactor + (this._targetValue - this._currentValue) * this.scaleFactor;
this._currentValue += this._decay;
}
}
using UnityEngine;
namespace DecayTweens
{
class Vector3DecayTween
{
public Vector3 Start;
public Vector3 End;
public float ScaleFactor;
public float DecayFactor;
private float _currentValue;
private float _targetValue;
private float _decay;
public Vector3DecayTween(Vector3 start, Vector3 end, float scaleFactor = 0.2f, float decayFactor = 0)
{
Start = start;
End = end;
ScaleFactor = scaleFactor;
DecayFactor = decayFactor;
_updateInternal();
}
public void SetStart(Vector3 value){
Start = value;
_updateInternal();
}
public void SetEnd(Vector3 value){
Start = GetPosition();
End = value;
_updateInternal();
}
public Vector3 GetPosition()
{
var direction = (End - Start).normalized;
return Start + direction * _currentValue;
}
public void ClearMomentum(){
_decay = 0;
}
public void Update(){
_decay = _decay * DecayFactor + (_targetValue - _currentValue) * ScaleFactor;
_currentValue += _decay;
}
private void _updateInternal(){
_currentValue = 0;
var vector = End - Start;
_targetValue = vector.magnitude;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment