@immugio/three-math-extensions 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Vec3.ts CHANGED
@@ -2,14 +2,28 @@ import { Vector3 } from "three";
2
2
  import { Vec2 } from "./Vec2";
3
3
  import { Point3 } from "./Point3";
4
4
 
5
+ /**
6
+ * Vec3 represents a 2D vector. It extends `Vector3` from the `threejs` library.
7
+ */
5
8
  export class Vec3 extends Vector3 {
6
9
 
7
10
  #target: Vector3;
8
11
 
12
+ /**
13
+ * Creates a new Vec3 instance from an {x, y, z} object.
14
+ * @param point - The {x, y, z} instance.
15
+ * @returns A new Vec3 instance.
16
+ */
9
17
  public static fromPoint(point: Point3): Vec3 {
10
18
  return new Vec3(point.x, point.y, point.z);
11
19
  }
12
20
 
21
+ /**
22
+ * Moves this Vec3 instance towards the target Vec3 by the given amount.
23
+ * @param target - The target Vec3.
24
+ * @param amount - The distance to move.
25
+ * @returns This Vec3 instance.
26
+ */
13
27
  public moveTowards(target: Vector3, amount: number): Vec3 {
14
28
  if (this.#target === undefined) {
15
29
  this.#target = new Vector3();
@@ -21,7 +35,12 @@ export class Vec3 extends Vector3 {
21
35
  return this;
22
36
  }
23
37
 
24
- public centerTowards(target: Vector3): Vec3 {
38
+ /**
39
+ * Moves this Vec3 instance halfway towards the target Vec3 by the given amount.
40
+ * @param target - The target Vec3.
41
+ * @returns This Vec3 instance.
42
+ */
43
+ public moveHalfWayTowards(target: Vector3): Vec3 {
25
44
  if (this.#target === undefined) {
26
45
  this.#target = new Vector3();
27
46
  }
@@ -29,41 +48,61 @@ export class Vec3 extends Vector3 {
29
48
  return this.moveTowards(target, this.distanceTo(target) / 2);
30
49
  }
31
50
 
51
+
52
+ /**
53
+ * Adds y amount to this Vec3 instance and return this
54
+ * @param y
55
+ */
32
56
  public addY(y: number): Vec3 {
33
57
  this.y += y;
34
58
  return this;
35
59
  }
36
60
 
61
+ /**
62
+ * Adds x amount to this Vec3 instance and return this
63
+ * @param x
64
+ */
37
65
  public addX(x: number): Vec3 {
38
66
  this.x += x;
39
67
  return this;
40
68
  }
41
69
 
42
- public scale(p: Vector3): Vec3 {
43
- this.x *= p.x;
44
- this.y *= p.y;
45
- this.z *= p.z;
46
- return this;
47
- }
48
-
70
+ /**
71
+ * Returns a clone of the point closest to this from the given points.
72
+ * @param points
73
+ */
49
74
  public closest(...points: Vector3[]): Vec3 {
50
75
  const withDistances = points.map(p => ({ point: p, distance: this.distanceTo(p) }));
51
76
  const closest = withDistances.reduce((a, b) => a.distance < b.distance ? a : b);
52
77
  return Vec3.fromPoint(closest.point);
53
78
  }
54
79
 
80
+ /**
81
+ * Returns a clone of this Vec3 instance with y and z swapped.
82
+ */
55
83
  public toPointWithFlippedYZ(): Vec3 {
56
84
  return new Vec3(this.x, this.z, this.y);
57
85
  }
58
86
 
87
+ /**
88
+ * Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ignored.
89
+ */
59
90
  public onPlan(): Vec2 {
60
91
  return new Vec2(this.x, this.z);
61
92
  }
62
93
 
94
+ /**
95
+ * Get distance to another vector while ignoring the y-axis.
96
+ * @param point
97
+ */
63
98
  public horizontalDistanceTo(point: Vector3): number {
64
99
  return new Vector3(this.x, 0, this.z).distanceTo(new Vector3(point.x, 0, point.z));
65
100
  }
66
101
 
102
+ /**
103
+ * Determines if this Vec2 instance is near the target Vec2.
104
+ * maxDistance is the maximum distance between the two vectors within which they are considered `near`.
105
+ */
67
106
  public isNear(v: Vector3, maxDistance: number = undefined): boolean {
68
107
  if (maxDistance === undefined) {
69
108
  return this.equals(v);
package/types/Vec2.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Vector2 } from "three";
2
2
  import { Vec3 } from "./Vec3";
3
3
  import { Point2 } from "./Point2";
4
4
  /**
5
- * Vec2 represents a 2D vector. It extends `Vector2` from the `three` library.
5
+ * Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
6
6
  */
7
7
  export declare class Vec2 extends Vector2 {
8
8
  /**
@@ -25,11 +25,11 @@ export declare class Vec2 extends Vector2 {
25
25
  */
26
26
  roundIfCloseToInteger(max?: number): this;
27
27
  /**
28
- * Projects this Vec2 instance to a Vec3 instance in 3D space.
29
- * @param z - The z value of the new Vec3 instance.
28
+ * Projects this Vec2 instance to a Vec3 instance in 3D space. Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
29
+ * @param y - The y value of the new Vec3 instance.
30
30
  * @returns A new Vec3 instance.
31
31
  */
32
- in3DSpace(z?: number): Vec3;
32
+ in3DSpace(y?: number): Vec3;
33
33
  /**
34
34
  * Determines if this Vec2 instance is near the target Vec2.
35
35
  * maxDistance is the maximum distance between the two vectors within which they are considered `near`.
package/types/Vec3.d.ts CHANGED
@@ -1,18 +1,62 @@
1
1
  import { Vector3 } from "three";
2
2
  import { Vec2 } from "./Vec2";
3
3
  import { Point3 } from "./Point3";
4
+ /**
5
+ * Vec3 represents a 2D vector. It extends `Vector3` from the `threejs` library.
6
+ */
4
7
  export declare class Vec3 extends Vector3 {
5
8
  #private;
9
+ /**
10
+ * Creates a new Vec3 instance from an {x, y, z} object.
11
+ * @param point - The {x, y, z} instance.
12
+ * @returns A new Vec3 instance.
13
+ */
6
14
  static fromPoint(point: Point3): Vec3;
15
+ /**
16
+ * Moves this Vec3 instance towards the target Vec3 by the given amount.
17
+ * @param target - The target Vec3.
18
+ * @param amount - The distance to move.
19
+ * @returns This Vec3 instance.
20
+ */
7
21
  moveTowards(target: Vector3, amount: number): Vec3;
8
- centerTowards(target: Vector3): Vec3;
22
+ /**
23
+ * Moves this Vec3 instance halfway towards the target Vec3 by the given amount.
24
+ * @param target - The target Vec3.
25
+ * @returns This Vec3 instance.
26
+ */
27
+ moveHalfWayTowards(target: Vector3): Vec3;
28
+ /**
29
+ * Adds y amount to this Vec3 instance and return this
30
+ * @param y
31
+ */
9
32
  addY(y: number): Vec3;
33
+ /**
34
+ * Adds x amount to this Vec3 instance and return this
35
+ * @param x
36
+ */
10
37
  addX(x: number): Vec3;
11
- scale(p: Vector3): Vec3;
38
+ /**
39
+ * Returns a clone of the point closest to this from the given points.
40
+ * @param points
41
+ */
12
42
  closest(...points: Vector3[]): Vec3;
43
+ /**
44
+ * Returns a clone of this Vec3 instance with y and z swapped.
45
+ */
13
46
  toPointWithFlippedYZ(): Vec3;
47
+ /**
48
+ * Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ignored.
49
+ */
14
50
  onPlan(): Vec2;
51
+ /**
52
+ * Get distance to another vector while ignoring the y-axis.
53
+ * @param point
54
+ */
15
55
  horizontalDistanceTo(point: Vector3): number;
56
+ /**
57
+ * Determines if this Vec2 instance is near the target Vec2.
58
+ * maxDistance is the maximum distance between the two vectors within which they are considered `near`.
59
+ */
16
60
  isNear(v: Vector3, maxDistance?: number): boolean;
17
61
  clone(): this;
18
62
  }