Skip to main content

Vector4f

APJS Script API reference for the Vector4f class.

TypeNameInterface Description
Variablesw: number

Function: Represents the w component of a four-dimensional vector.

Variablesx: number

Function: Represents the x-coordinate in a four-dimensional vector.

Variablesy: number

Function: Represents the y-coordinate in a four-dimensional vector.

Variablesz: number

Function: Represents the z-coordinate in a four-dimensional vector.

Functionsconstructor()

Functionsconstructor(x?: number, y?: number, z?: number, w?: number)

Parameters

x: - The x component of the vector (optional).

y: - The y component of the vector (optional).

z: - The z component of the vector (optional).

w: - The w component of the vector (optional).

Functionsconstructor(x: Vector3f)

Parameters

x: - A 3-dimensional vector representing the first three components of the new vector. The fourth component is assumed to be 0.

Functionsconstructor(x: Vector4f)

Parameters

x: - An instance of Vector4f to initialize the new object with.

Functionsadd(other: Vector4f): this

Function: Adds the components of other to this vector in place.

Parameters

other: - The Vector4f whose components will be added to this vector.

Returns This Vector4f after addition.

FunctionsclampLength(length: number): this

Function: Clamps the length of this vector to the specified length and returns this vector.

Parameters

length: - The length to which the vector's length should be clamped.

Returns This vector with its length clamped to the specified value.

Functionsclone(): Vector4f

Function: Creates and returns a new Vector4f with the same component values. Use this before calling mutating math methods when the original value must be preserved.

Returns A new Vector4f with the same values as the original.

Functionsdistance(other: Vector4f): number

Function: Calculates and returns the Euclidean distance between this vector and another vector in 4-dimensional space.

Parameters

other: - The target vector to calculate the distance to.

Returns The calculated distance

Functionsdivide(other: Vector4f): this

Function: Divides this vector in place by other component-wise.

Parameters

other: - The vector to divide by.

Returns This vector after division.

Functionsdot(other: Vector4f): number

Function: Returns the dot product of the vector and another vector.

Parameters

other: - The vector to calculate the dot product with.

Returns The dot product.

Functionsequals(other: Vector4f): boolean

Function: Determines if this Vector4f is equal to another Vector4f.

Parameters

other: - The Vector4f to compare with.

Returns True if the two vectors are equal, false otherwise.

Functionsinverse(): this

Function: Replaces each component with its reciprocal in place.

Returns This vector with inverted components.

Functionsmagnitude(): number

Function: Calculates and returns the Euclidean length (magnitude) of the vector.

Returns The length (magnitude) of the vector.

Functionsmultiply(other: Vector4f | number): this

Function: Multiplies this vector in place. If other is a Vector4f, multiplication is performed component-wise. If other is a number, all components are scaled by that number.

Parameters

other: - The vector or scalar to multiply with.

Returns This vector after performing the multiplication.

FunctionsmultiplyScalar(scalar: number): this

Function: Multiplies all components of this vector by scalar in place.

Parameters

scalar: - The number to multiply each component of the vector by.

Returns This vector after performing the multiplication.

Functionsnormalize(): this

Function: Normalizes this vector in place so its magnitude becomes 1. If the magnitude is zero, the vector is left unchanged.

Returns This vector after normalization.

Functionsset(x: number, y: number, z: number, w: number): this

Function: Sets the four-dimensional components of this vector in place.

Parameters

x: - The value for the x component.

y: - The value for the y component.

z: - The value for the z component.

w: - The value for the w component.

Returns This instance with updated components.

FunctionssqrMagnitude(): number

Function: Calculates and returns the squared magnitude of the vector, which is the dot product of the vector with itself.

Returns The squared magnitude (length) of the vector.

Functionssubtract(other: Vector4f): this

Function: Subtracts the components of other from this vector in place.

Parameters

other: - The vector to subtract from this vector.

Returns This vector after subtraction.

FunctionstoString(): string

Function: Returns a string representation of the vector.

Returns A string in the format "Vector4f(x, y, z, w)" where x, y, z, and w are fixed to 5 decimal places.

Static FunctionscompareApproximately(vec1: Vector4f, vec2: Vector4f, dist: number): boolean

Function: Approximate comparison of the two vectors by the value of each component within a specified distance.

Parameters

vec1: - The first vector to compare.

vec2: - The second vector to compare.

dist: - The maximum allowed difference between corresponding components of the vectors for them to be considered approximately equal.

Returns A boolean indicating whether the vectors are approximately equal within the given distance.

Static Functionslerp(vecA: Vector4f, vecB: Vector4f, t: number): Vector4f

Function: Linearly interpolates between the two vectors 'vecA' and 'vecB' by the factor 't'.

Parameters

vecA: - The starting vector.

vecB: - The ending vector.

t: - The interpolation factor, typically ranging from 0 to 1.

Returns A new Vector4f that represents the interpolated position between 'vecA' and 'vecB'.

Static Functionsmax(vecA: Vector4f, vecB: Vector4f): Vector4f

Function: Returns a new vector containing the largest value of each component from the two input vectors.

Parameters

vecA: - The first vector to compare.

vecB: - The second vector to compare.

Returns A new Vector4f with components being the maximum values from vecA and vecB.

Static Functionsmin(vecA: Vector4f, vecB: Vector4f): Vector4f

Function: Returns a new vector containing the smallest value of each component from the two input vectors.

Parameters

vecA: - The first vector to compare.

vecB: - The second vector to compare.

Returns A new Vector4f instance with components set to the minimum values from vecA and vecB.

Examples

constructor()

let obj = new APJS.Vector4f();

constructor(x?: number, y?: number, z?: number, w?: number)

let obj = new APJS.Vector4f();

constructor(x: Vector3f)

let obj = new APJS.Vector4f();

constructor(x: Vector4f)

let obj = new APJS.Vector4f();

compareApproximately(vec1: Vector4f, vec2: Vector4f, dist: number): boolean

let a = new Vector4f(0.0000001, 0.0000001, 0.0000001, 0.0);
let b = new Vector4f(0.0000000, 0.0000000, 0.0, 0.0);
Vector4f.compareApproximately(a, b, 0.0001); // true

Use Case

Change a 3D object's material color at runtime via MeshRenderer.mainMaterial

@component()
export class MaterialColorChange extends APJS.BasicScriptComponent {
@serializeProperty
private targetColor: APJS.Color = new APJS.Color(1, 0, 0, 1);

onStart(): void {
this.applyColor(this.targetColor);
}

private applyColor(color: APJS.Color): void {
const renderer = this.getSceneObject().getComponent("MeshRenderer") as APJS.MeshRenderer;
// Use mainMaterial for single material access
// Use setVector (NOT setVector4 — it doesn't exist)
if (renderer && renderer.mainMaterial) {
renderer.mainMaterial.setVector("_AlbedoColor", new APJS.Vector4f(color.r, color.g, color.b, color.a));
}
}

onDestroy(): void {}
}
Copyright © 2026 TikTok. All rights reserved.
About TikTokHelp CenterCareersContactLegalTerms of ServicePrivacy PolicyCookies