Class Vector3
Package: @aspose/3d (v24.12.0)
Vector3 is the primary math type used throughout the @aspose/3d API for positions, directions, Euler angles, and scale factors. It is exported from the @aspose/3d/utilities sub-path.
export class Vector3import { Vector3 } from '@aspose/3d/utilities';
// or via the main entry point when used with Transform/GlobalTransform
import { Scene } from '@aspose/3d';Examples
Compute the midpoint between two positions and normalize a direction vector.
import { Vector3 } from '@aspose/3d/utilities';
const a = new Vector3(1, 0, 0);
const b = new Vector3(0, 0, 4);
// Midpoint: average the components manually
const mid = new Vector3(
(a.x + b.x) * 0.5,
(a.y + b.y) * 0.5,
(a.z + b.z) * 0.5
);
console.log(`Midpoint: ${mid}`);
// Midpoint: Vector3(0.5, 0, 2)
// Normalized direction from a to b
const dir = b.minus(a).normalize();
console.log(`Direction: ${dir}`);
// Direction: Vector3(-0.2425..., 0, 0.9701...)
Constructors
| Signature | Description |
|---|---|
new Vector3(x, y, z) | Construct from three numeric components. |
new Vector3(v) | Fill all three components with the same scalar value. |
Properties
| Property | Type | Description |
|---|---|---|
x | number | The X component. Readable and writable. |
y | number | The Y component. Readable and writable. |
z | number | The Z component. Readable and writable. |
length | number (read-only) | Euclidean length: sqrt(x² + y² + z²). |
length2 | number (read-only) | Squared length: x² + y² + z². Cheaper than length when only comparing magnitudes. |
zero | Vector3 (read-only) | Returns a new Vector3(0, 0, 0). |
one | Vector3 (read-only) | Returns a new Vector3(1, 1, 1). |
unitX | Vector3 (read-only) | Returns a new Vector3(1, 0, 0). |
unitY | Vector3 (read-only) | Returns a new Vector3(0, 1, 0). |
unitZ | Vector3 (read-only) | Returns a new Vector3(0, 0, 1). |
Methods
set(x, y, z)
Mutates the vector in place, setting all three components.
set(newX: number, newY: number, newZ: number): voiddot(rhs)
Returns the scalar dot product of this vector and rhs.
dot(rhs: Vector3): numberExamples
import { Vector3 } from '@aspose/3d/utilities';
const up = new Vector3(0, 1, 0);
const dir = new Vector3(0.5, 0.5, 0).normalize();
const alignment = up.dot(dir);
console.log(`Dot: ${alignment.toFixed(4)}`); // Dot: 0.7071
cross(rhs)
Returns a new Vector3 that is the cross product of this vector and rhs.
cross(rhs: Vector3): Vector3Examples
import { Vector3 } from '@aspose/3d/utilities';
const right = new Vector3(1, 0, 0);
const up = new Vector3(0, 1, 0);
const forward = right.cross(up);
console.log(`Forward: ${forward}`);
// Forward: Vector3(0, 0, -1)
normalize()
Returns a new Vector3 with the same direction but unit length. Returns (0, 0, 0) if this vector has zero length.
normalize(): Vector3minus(v)
Returns a new Vector3 equal to this - v.
minus(v: Vector3): Vector3times(scalar)
Returns a new Vector3 equal to this * scalar.
times(scalar: number): Vector3angleBetween(dir, up?)
Returns the angle in radians between this vector and dir. If up is provided, the angle is computed in the plane perpendicular to up (signed angle in that plane).
angleBetween(dir: Vector3, up?: Vector3): numbersin()
Returns a new Vector3 with Math.sin applied component-wise.
sin(): Vector3cos()
Returns a new Vector3 with Math.cos applied component-wise.
cos(): Vector3equals(other)
Returns true if all three components are strictly equal.
equals(other: Vector3): booleancompareTo(other)
Returns -1, 0, or 1 using lexicographic ordering on (x, y, z).
compareTo(other: Vector3): numberstatic parse(input)
Parses a whitespace-separated string of three numbers into a Vector3. Throws if the format is invalid.
static parse(input: string): Vector3Examples
import { Vector3 } from '@aspose/3d/utilities';
const v = Vector3.parse('1.5 2.0 -3.0');
console.log(`${v}`); // Vector3(1.5, 2, -3)
toString()
Returns a string in the form Vector3(x, y, z).
toString(): string