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 Vector3
import { 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

SignatureDescription
new Vector3(x, y, z)Construct from three numeric components.
new Vector3(v)Fill all three components with the same scalar value.

Properties

PropertyTypeDescription
xnumberThe X component. Readable and writable.
ynumberThe Y component. Readable and writable.
znumberThe Z component. Readable and writable.
lengthnumber (read-only)Euclidean length: sqrt(x² + y² + z²).
length2number (read-only)Squared length: x² + y² + z². Cheaper than length when only comparing magnitudes.
zeroVector3 (read-only)Returns a new Vector3(0, 0, 0).
oneVector3 (read-only)Returns a new Vector3(1, 1, 1).
unitXVector3 (read-only)Returns a new Vector3(1, 0, 0).
unitYVector3 (read-only)Returns a new Vector3(0, 1, 0).
unitZVector3 (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): void

dot(rhs)

Returns the scalar dot product of this vector and rhs.

dot(rhs: Vector3): number

Examples

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): Vector3

Examples

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(): Vector3

minus(v)

Returns a new Vector3 equal to this - v.

minus(v: Vector3): Vector3

times(scalar)

Returns a new Vector3 equal to this * scalar.

times(scalar: number): Vector3

angleBetween(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): number

sin()

Returns a new Vector3 with Math.sin applied component-wise.

sin(): Vector3

cos()

Returns a new Vector3 with Math.cos applied component-wise.

cos(): Vector3

equals(other)

Returns true if all three components are strictly equal.

equals(other: Vector3): boolean

compareTo(other)

Returns -1, 0, or 1 using lexicographic ordering on (x, y, z).

compareTo(other: Vector3): number

static parse(input)

Parses a whitespace-separated string of three numbers into a Vector3. Throws if the format is invalid.

static parse(input: string): Vector3

Examples

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