Vector4f
APJS Script API reference for the Vector4f class.
| Type | Name | Interface Description |
|---|---|---|
| Variables | w: number | • Function: Represents the w component of a four-dimensional vector. |
| Variables | x: number | • Function: Represents the x-coordinate in a four-dimensional vector. |
| Variables | y: number | • Function: Represents the y-coordinate in a four-dimensional vector. |
| Variables | z: number | • Function: Represents the z-coordinate in a four-dimensional vector. |
| Functions | constructor() | |
| Functions | constructor(x?: number, y?: number, z?: number, w?: number) | Parameters • • • • |
| Functions | constructor(x: Vector3f) | Parameters • |
| Functions | constructor(x: Vector4f) | Parameters • |
| Functions | add(other: Vector4f): this | • Function: Adds the components of Parameters • Returns This Vector4f after addition. |
| Functions | clampLength(length: number): this | • Function: Clamps the length of this vector to the specified Parameters • Returns This vector with its length clamped to the specified value. |
| Functions | clone(): 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. |
| Functions | distance(other: Vector4f): number | • Function: Calculates and returns the Euclidean distance between this vector and another vector in 4-dimensional space. Parameters • Returns The calculated distance |
| Functions | divide(other: Vector4f): this | • Function: Divides this vector in place by Parameters • Returns This vector after division. |
| Functions | dot(other: Vector4f): number | • Function: Returns the dot product of the vector and another vector. Parameters • Returns The dot product. |
| Functions | equals(other: Vector4f): boolean | • Function: Determines if this Vector4f is equal to another Vector4f. Parameters • Returns True if the two vectors are equal, false otherwise. |
| Functions | inverse(): this | • Function: Replaces each component with its reciprocal in place. Returns This vector with inverted components. |
| Functions | magnitude(): number | • Function: Calculates and returns the Euclidean length (magnitude) of the vector. Returns The length (magnitude) of the vector. |
| Functions | multiply(other: Vector4f | number): this | • Function: Multiplies this vector in place. If Parameters • Returns This vector after performing the multiplication. |
| Functions | multiplyScalar(scalar: number): this | • Function: Multiplies all components of this vector by Parameters • Returns This vector after performing the multiplication. |
| Functions | normalize(): 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. |
| Functions | set(x: number, y: number, z: number, w: number): this | • Function: Sets the four-dimensional components of this vector in place. Parameters • • • • Returns This instance with updated components. |
| Functions | sqrMagnitude(): 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. |
| Functions | subtract(other: Vector4f): this | • Function: Subtracts the components of Parameters • Returns This vector after subtraction. |
| Functions | toString(): 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 Functions | compareApproximately(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 • • • Returns A boolean indicating whether the vectors are approximately equal within the given distance. |
| Static Functions | lerp(vecA: Vector4f, vecB: Vector4f, t: number): Vector4f | • Function: Linearly interpolates between the two vectors 'vecA' and 'vecB' by the factor 't'. Parameters • • • Returns A new Vector4f that represents the interpolated position between 'vecA' and 'vecB'. |
| Static Functions | max(vecA: Vector4f, vecB: Vector4f): Vector4f | • Function: Returns a new vector containing the largest value of each component from the two input vectors. Parameters • • Returns A new Vector4f with components being the maximum values from vecA and vecB. |
| Static Functions | min(vecA: Vector4f, vecB: Vector4f): Vector4f | • Function: Returns a new vector containing the smallest value of each component from the two input vectors. Parameters • • 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 {}
}