Derived from the cocosair-math skill and its canonical docs/source pointers; reorganized on 2026-10-11. Source annotations below identify repository inputs, not files guaranteed to ship to npm consumers.
Contracts
mathis a namespace object, not a class bag:src/cocos/core/index.ts:28doesimport * as math from './math'and:36exports it, somath.Vec3,math.bits,math.clamp01are the same bindings as the top-level ones. The namespace surface is exactlysrc/cocos/core/math/index.ts:34-49(bits:34, the value types with their factories:35-43,./utils:44,./type-define:45,./math-base:46).easingand the top-levelbitscome from elsewhere:easingissrc/cocos/core/algorithm/index.ts:29, andmath.easingdoes NOT exist.- Every static operation on every value type is
out-first: it writes fields ofoutand returnsout; none of them allocate except theclonefamily (Vec3.add<Out extends IVec3Like>(out, a, b): Out,src/cocos/core/math/vec3.ts:112-117). Passing a frozen constant asoutthrows; passing a live node-owned vector asoutmutates the node's stored data. - Frozen constants are a hard boundary.
freezeVec3(src/cocos/core/math/vec3.ts:48-50) isObject.freeze(new Vec3(...)), module-local and not exported, and backs the nine statics at:57-65(UNIT_X/UNIT_Y/UNIT_Z,RIGHT/UP/FORWARD/ZERO/ONE/NEG_ONE) typedReadonly<Vec3>. Equivalent helpers/inline freezes exist per type:vec2.ts:50-51with five statics (:59-63),vec4.ts:46-47with seven (:55-61),color.ts:45-46with ten (:56-65),Mat4.IDENTITYmat4.ts:63,Mat3.IDENTITYmat3.ts:45,Quat.IDENTITYquat.ts:54,Size.ZERO/ONEsize.ts:36-37.Recthas no frozen statics. Bundled ESM is always strict mode, so bothVec3.UP.x = 5andVec3.UP.set(...)throwTypeErrorin the strict-mode bundle; the same writes in a sloppy script silently do nothing. - Instance fields are plain mutable numbers (
public declare x: number,src/cocos/core/math/vec3.ts:884), so aVec3is only as private as its holder. The scene graph stores its own private_lpos/_lrot/_lscaleand hands them out asReadonly<Vec3>getters; a returnedReadonlyreference to a node's own storage is a TS marker only and is still mutable at runtime. - Color channels are bytes:
Colorstoresprivate _data = new Uint8ClampedArray(4)(src/cocos/core/math/color.ts:340) and ther/g/b/aaccessors index it directly (:346-387), with defaults0/0/0/255inset(:721-743). Writing0.5into a channel is a byte coercion (clamped and rounded), not 50 percent; the 0-1 view is the compatibility accessorsx/y/z/w(:390-397) andColor.toVec4/fromVec4(:113,:131). There is nopremultiplymember onColorin this runtime. - Composition order is documented as "S->R->T applied in order", which in the column-major storage of
Mat4meansout = T * R * S(Mat4.fromSRT,src/cocos/core/math/mat4.ts:964-1000; translation lands inm12/m13/m14).fromRTS(:951) andtoRTS(:865) are deprecated aliases that forward tofromSRT/toSRT- the letters in the name are not an order.Mat4.multiply(out, a, b)isout = a * b(:316), and the manual asserts child world =parent.worldMatrix * childLocal(docs/manual/matrix-transformations.md:7). - Angles: the euler entry points take DEGREES (
Quat.fromEulermultiplies byhalfToRad = 0.5 * PI / 180,src/cocos/core/math/quat.ts:557-560,:934;Node.setRotationFromEuler,src/cocos/scene-graph/node.ts:2506-2514), while every axis-angle,Mat4.rotate*andMat3.toEulerentry point takes radians (quat.ts:475,mat4.ts:441,mat3.ts:649). Unit mixing does not throw - it silently rotates by a factor of about 57. - Degenerate input is answered silently, never by a throw:
Mat4.invertwrites an ALL-ZERO matrix when the determinant is exactly 0 (src/cocos/core/math/mat4.ts:254-255, documented at:229- the old contents are lost),Vec3.normalizewrites(0,0,0)for a zero-length input (src/cocos/core/math/vec3.ts:329-345),Quat.invertproduces the zero quaternion (src/cocos/core/math/quat.ts:384-398), andMat4.toSRTreturns identity rotation when any scale is 0 (:894-899). Mirror matrices are handled by taking the sign into the scale: after normalizing the three column vectors,toSRTcomputesMat3.determinantand on a negative value negatess.xand column 0 beforeQuat.fromMat3(src/cocos/core/math/mat4.ts:914-927). - Capability layers, stated separately (source export / default-bundle reachable / browser-verified): the delivered bundle
build/cocosair.module.jsexportsmath,easing,bits, the ten value classes, their factory functions (v2/v3/v4/quat/mat4/rect/size/color), all of./utils(lerp,clamp,randomRange,pseudoRandom,floatToHalf, ...),EPSILON/HALF_PI/TWO_PIandValueType/MathBase, so all of them are importable from the main entry;preTransformsis engine-internal (src/cocos/core/math/index.ts:49).srgbToLinear/linearToSrgb/linearToSrgb8Bit(src/cocos/core/math/color.ts:781,:793,:800) andAffineTransform(src/cocos/core/math/affine-transform.ts:38) are source exports that are NOT in the bundle export list, andVec3.random/Vec2.randomare reachable only as statics.perlin,fp32/fp16andtoRgba8/fromRgba8are not present in the declaration. All 40examples/math-*Gallery apps are recorded PASS for Chromium indocs/evidence/examples-verified.json(measuredAt2026-10-04T05:52:32Z,candidateFingerprint2f089f6ce25eaccd) - that is captured browser evidence for that candidate, not for the bundle inbuild/today.