Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tween: Added support to Vector2 and Vector4 #58

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export * from './utils/Stats';
export * from './utils/Utils';
export * from './utils/VectorUtils';

/** @internal */
declare module 'three/src/math/Vector2' {
export interface Vector {
lerpVectors(v1: Vector, v2: Vector, alpha: number): this;
}
}

declare module 'three/src/core/Object3D' {
export interface Object3D extends Object3DExtPrototype { }
}
Expand Down
14 changes: 7 additions & 7 deletions src/tweening/Actions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Color, ColorRepresentation, Euler, MathUtils, Quaternion, Vector3 } from "three";
import { Color, ColorRepresentation, Euler, MathUtils, Quaternion, Vector, Vector2, Vector3, Vector4 } from "three";
import { DEFAULT_EASING, Easing, EasingFunction, Easings } from "./Easings";
import { RunningAction } from "./RunningTween";
import { Tween } from "./Tween";

const easings = new Easings();
export type AllowedTypes = number | Vector3 | Quaternion | Euler | ColorRepresentation;
export type AllowedTypes = number | Vector | Quaternion | Euler | ColorRepresentation;
export type Omitype<T, U> = { [P in keyof T as T[P] extends U ? never : P]: T[P] };
export type PickType<T, U> = { [P in keyof T as T[P] extends U ? P : never]: T[P] };
export type TransformType<T, U, V> = { [P in keyof T]: T[P] extends U ? T[P] | V : T[P] };
export type TransformedTypes<T> = TransformType<TransformType<T, Color, ColorRepresentation>, Vector3, number>;
export type TransformedTypes<T> = TransformType<TransformType<T, Color, ColorRepresentation>, Vector, number>;
export type FilteredType<T> = PickType<Partial<TransformedTypes<T>>, AllowedTypes>;
export type Motion<T> = { [key in keyof FilteredType<T>]: FilteredType<T>[key] };
export type SetMotion<T> = { [key in keyof T]?: T[key] };
Expand Down Expand Up @@ -123,7 +123,7 @@ export class ActionMotion<T> implements IAction<T> {
if (key === "easing") continue;
const actionValue = this.motion[key];
const targetValue = target[key];
const action = this.vector3(key, actionValue as Vector3, targetValue as Vector3)
const action = this.vector(key, actionValue as Vector, targetValue as Vector)
?? this.quaternion(key, actionValue as Quaternion, targetValue as Quaternion)
?? this.euler(key, actionValue as Euler, targetValue as Euler)
?? this.color(key, actionValue as Color, targetValue as Color)
Expand All @@ -140,9 +140,9 @@ export class ActionMotion<T> implements IAction<T> {
return typeof easing === "string" ? (easings[easing].bind(easings) ?? easings.linear) : easing;
}

private vector3(key: string, actionValue: Vector3 | number, targetValue: Vector3): RunningAction<Vector3> {
if (targetValue?.isVector3) {
const value = typeof actionValue === "number" ? new Vector3(actionValue, actionValue, actionValue) : actionValue;
private vector(key: string, actionValue: Vector | number, targetValue: Vector): RunningAction<Vector> {
if ((targetValue as Vector2)?.isVector2 || (targetValue as Vector3)?.isVector3 || (targetValue as Vector4)?.isVector4) {
const value = typeof actionValue === "number" ? targetValue.clone().setScalar(actionValue) : actionValue;
return {
key,
time: this.time,
Expand Down