Graphics & UI
FastMath Hardware Acceleration
High-speed trigonometry, vector math, and LUT acceleration via FastMath / System.math.
6. Hardware Math Acceleration: FastMath / System.math
KryonOS features a high-performance Hardware-Accelerated Math Engine exposed globally as FastMath (and also accessible as System.math).
Instead of interpreting floating-point arithmetic loops in JavaScript (Duktape bytecode), FastMath offloads computations directly to:
- ESP32 & ESP32-S3 Hardware FPU (Floating Point Unit): Single-cycle native float calculations.
- ESP32 Hardware TRNG (
esp_random()): High-speed cryptographic true random number generation. - Native C++ 3D & Matrix Batch Pipeline: Transforms hundreds of 3D vertices and calculates matrix multiplications in native machine code, yielding a 300% to 800% FPS boost for 3D games, physics, and animations.
6.1 Constants
FastMath.PI-3.141592653589793FastMath.HALF_PI-1.5707963267948966FastMath.TWO_PI-6.283185307179586FastMath.DEG_TO_RAD-0.01745329251FastMath.RAD_TO_DEG-57.2957795131
6.2 Hardware Trigonometry & LUT
FastMath.sin(rad) / FastMath.cos(rad) / FastMath.tan(rad)
- Parameters:
rad(Float in radians) - Returns:
Float - Description: Direct hardware single-precision FPU trigonometric calculations (
sinf,cosf,tanf).
FastMath.fastSin(deg) / FastMath.fastCos(deg)
- Parameters:
deg(Integer/Float degrees 0-359) - Returns:
Float - Description: Instant lookup table (LUT) trigonometry with zero CPU overhead. Ideal for 60FPS animation and rotation loops.
FastMath.asin(val) / FastMath.acos(val) / FastMath.atan2(y, x)
- Returns:
Float - Description: Native hardware-accelerated arc-trigonometric functions.
FastMath.sqrt(val)
- Returns:
Float - Description: Hardware FPU square root instruction.
FastMath.hypot(dx, dy)
- Returns:
Float - Description: Computes
sqrt(dx^2 + dy^2)natively without intermediate overflow.
6.3 Range, Scalar & Hardware TRNG
FastMath.clamp(val, min, max)
- Returns:
Float(Clamped value between min and max).
FastMath.lerp(start, end, t)
- Returns:
Float(Linear interpolation:start + t * (end - start)).
FastMath.map(val, inMin, inMax, outMin, outMax)
- Returns:
Float(Re-maps a number from one range to another).
FastMath.degToRad(deg) / FastMath.radToDeg(rad)
- Returns:
Float(Fast angle unit conversion).
FastMath.random()
- Returns:
Float(0.0to1.0) - Description: Generates a true random number using the ESP32 internal Hardware True Random Number Generator (TRNG). Up to 10x faster than JS software PRNG.
FastMath.randomRange(min, max)
- Returns:
Float(Random number betweenminandmax).
6.4 2D / 3D Vector Math
FastMath.vec2Distance(x1, y1, x2, y2)
- Returns:
Float(Euclidean distance between two 2D points).
FastMath.vec3Distance(x1, y1, z1, x2, y2, z2)
- Returns:
Float(Euclidean distance between two 3D points).
FastMath.dot2(x1, y1, x2, y2) / FastMath.dot3(x1, y1, z1, x2, y2, z2)
- Returns:
Float(Vector dot product).
FastMath.cross3(ax, ay, az, bx, by, bz)
- Returns:
Object->{ x: Float, y: Float, z: Float }(3D Vector cross product).
FastMath.normalize2(x, y) / FastMath.normalize3(x, y, z)
- Returns:
Object->{ x, y }or{ x, y, z }(Unit length vector).
6.5 3D Matrix & Batch Graphics Acceleration (300%โ800% Speedup)
FastMath.mat4Identity()
- Returns:
Array[16](Standard 4x4 Identity Matrix).
FastMath.mat4Multiply(matA, matB)
- Parameters: Two 16-element arrays.
- Returns:
Array[16](Result of 4x4 matrix multiplicationA x Bcomputed in native C++).
FastMath.mat4Rotate(matrix, angleX, angleY, angleZ)
- Parameters:
matrix(Array[16]),angleX,angleY,angleZ(Euler angles in radians). - Returns:
Array[16](Rotated 4x4 Transformation Matrix).
FastMath.mat4Perspective(fovRad, aspect, near, far)
- Returns:
Array[16](4x4 Perspective Projection Matrix).
FastMath.transformVertices(vertices, matrix, screenWidth, screenHeight, scale, distZ)
- Parameters:
vertices: Array of 3D points (either[{x, y, z}, ...]or flat array[x0, y0, z0, x1, y1, z1, ...]).matrix: 16-element 4x4 transformation matrix.screenWidth(optional, default 240): Screen width for projection centering.screenHeight(optional, default 320): Screen height for projection centering.scale(optional, default 200): Projection field of view scaling factor.distZ(optional, default 3.0): Camera depth offset.
- Returns: Transformed array:
- If input was object array: returns
[{ x, y, z, px, py }, ...]wherepxandpyare projected screen integer pixel coordinates ready for rendering! - If input was flat array: returns
[px0, py0, px1, py1, ...].
- If input was object array: returns
- Performance Note: Performs all 3D vertex rotation, translation, perspective division, and viewport projection in a single native C++ pass. Eliminates hundreds of JavaScript object allocations per frame.
6.6 Array & DSP Batch Processing
FastMath.arraySum(numArray)
- Returns:
Float(Sum of all numerical elements).
FastMath.arrayMinMax(numArray)
- Returns:
Object->{ min: Float, max: Float }(Computes min and max in a single native pass).
FastMath.arrayDot(arrayA, arrayB)
- Returns:
Float(Dot product of two numeric arrays).