Index of every page on this site: llms.txt
All skills
oahc09 avatar

/cocosair-math

@ed38648
by oahc09oahc09/cocosairjs3 stars
2
Run it yourself

Compute vectors, matrices, quaternions, colors and easing. Use for out-first math, frozen Vec3.UP errors, transform composition, rotation units or Mat4.invert.

  • 9 files
  • 119.9 KB
  • MIT
  • Updated last month
  • GitHub
Use this Skill: https://skilld.dev/gh/oahc09/cocosairjs/cocosair-math

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesvalue-types.md

≈7.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Derived from src/cocos/core/math/index.ts, src/cocos/core/index.ts, src/cocos/core/algorithm/index.ts, src/cocos/core/value-types/value-type.ts, src/cocos/core/math/math-base.ts, src/cocos/core/math/type-define.ts, src/cocos/core/math/bits.ts, src/cocos/core/math/vec2.ts, src/cocos/core/math/vec3.ts, src/cocos/core/math/vec4.ts, src/cocos/core/math/quat.ts, src/cocos/core/math/mat3.ts, src/cocos/core/math/mat4.ts, src/cocos/core/math/rect.ts, src/cocos/core/math/size.ts, src/cocos/core/math/color.ts, src/cocos/core/math/utils.ts, src/cocos/core/math/affine-transform.ts, build/cocosair.module.js (as of 2026-10-09)

Namespace membership

src/cocos/core/index.ts:28 does import * as math from './math' and :36 exports it; the member list is src/cocos/core/math/index.ts:

Line Member Notes
:34 bits module namespace of src/cocos/core/math/bits.ts, also a top-level export
:35-:43 Vec2/v2, Vec3/v3, Vec4/v4, Quat/quat, Mat3, Mat4/mat4, Size/size, Rect/rect, Color/color Mat3 (:39) is the only value type without a lowercase factory
:44 export * from './utils' scalar, random, angle and half-float helpers, HALF_PI/TWO_PI/EPSILON
:45 export * from './type-define' IColorLike..IVec4Like interfaces, FloatArray, the readonly unions (type-define.ts:25-90)
:46 export * from './math-base' MATH_FLOAT_ARRAY (math-base.ts:29), MathBase (:31)
:49 preTransforms engine-internal frozen array of four frozen four-number rows, the flattened 2x2 surface-transform matrices for identity/90/180/270 rotation (mat4.ts:43-48), indexed by orientation in Mat4.perspective (:1182) and Mat4.ortho (:1231)

math.easing does NOT exist: the curve functions are exported as the separate top-level easing namespace by src/cocos/core/algorithm/index.ts:29 (imported at :25).

Delivered export layers

Layer Symbols Evidence
Importable from the main entry math, bits, easing, the ten value classes and their factories, ValueType, MathBase, preTransforms, EPSILON, HALF_PI, TWO_PI, and all utils names (lerp, clamp, clamp01, approx, equals, random, randomRange, randomRangeInt, pseudoRandom, pseudoRandomRange, pseudoRandomRangeInt, setRandGenerator, nextPow2, repeat, pingPong, inverseLerp, absMax, absMaxComponent, enumerableProps, floatToHalf, halfToFloat, toRadian, toDegree) export block of build/cocosair.module.js
Source export only, absent from that export block srgbToLinear (color.ts:781), linearToSrgb (:793), linearToSrgb8Bit (:800), AffineTransform (affine-transform.ts:38) math/index.ts:34-49 does not re-export them
Module-local, unreachable freezeVec2 (vec2.ts:50), freezeVec3 (vec3.ts:48), freezeVec4 (vec4.ts:46), freezeColor (color.ts:45), the toHalf/fromHalf closures (utils.ts:363, :387) only used to build the constants / wrappers

Construction signatures and defaults

Type Constructor overloads Factory overloads Default
Vec2 (other), (x?, y?) vec2.ts:466-468 v2(other), v2(x?, y?) vec2.ts:867-870 (0, 0)
Vec3 (v: Vec3), (x?, y?, z?) vec3.ts:898-902 v3(other), v3(x?, y?, z?) vec3.ts:1271-1274 (0, 0, 0)
Vec4 (other), (x?, y?, z?, w?) vec4.ts:582-584 v4(other), v4(x?, y?, z?, w?) vec4.ts:1005-1008 (0, 0, 0, 0)
Quat (other), (x?, y?, z?, w?) quat.ts:789-793 quat(x = 0, y = 0, z = 0, w = 1) quat.ts:939-942 identity (0, 0, 0, 1)
Mat3 named fields m00..m08 (public declare m00: number mat3.ts:690) none identity
Mat4 (other), (m00, ..., m15) mat4.ts:1665-1673 mat4(other), mat4(m00, ..., m15) mat4.ts:2284-2291 identity
Rect (other), (x?, y?, width?, height?) rect.ts:279-291 rect(rect), rect(x?, y?, w?, h?) rect.ts:512-525 (0, 0, 0, 0)
Size (other), (width?, height?) overloads at size.ts:82, :90, implementation :92-95 size(other), size(w?, h?) size.ts:180-191 (0, 0)
Color (other), (hexString), (r?, g?, b?, a?) color.ts:404-422 color(otherOrString), color(r?, g?, b?, a?) color.ts:772-775 (0, 0, 0, 255)

new Color(x) dispatches on the first argument: a string goes to this.fromHEX(r), an object to this.set(other), otherwise the numeric channels (color.ts:424-432).

The out-parameter convention

  • Canonical shape: public static add<Out extends IVec3Like>(out: Out, a: IVec3Like, b: IVec3Like): Out - Vec3.add is vec3.ts:112-117, writing out.x/y/z then return out. No static arithmetic allocates.
  • Operands may be any IXxxLike plain object (type-define.ts:25-90); the receiver must be writable.
  • out === a and out === b are supported: implementations read operands into locals first (Vec3.add vec3.ts:112-117, Mat4.multiply mat4.ts:316 with the operand locals at :317-320 and return out at :346).
  • Allocating members: Vec3.clone(a) vec3.ts:82, Vec4.clone vec4.ts:67, Mat4.clone(a) mat4.ts:69, Mat3.clone mat3.ts:51, Quat.clone(a) quat.ts:60, Color.clone(a) color.ts:71, instance clone() on Rect (rect.ts:310) and Size (size.ts:107).
  • Instance mutators write and return this: Vec3.add vec3.ts:1030, subtract :1056, multiplyScalar :1082, multiply :1097, divide :1126, negative :1151, clampf :1165, normalize :1221, lerp :1018, set :941; Color.lerp(to, ratio) color.ts:468, Rect.lerp rect.ts:369, Size.lerp size.ts:156.
  • Read-only members: Vec3.length() vec3.ts:1202 and Vec3.lengthSqr() :1212 are instance METHODS; the static equivalents are Vec3.len(a) :255 and Vec3.lengthSqr(a) :266.
  • Degenerate inputs do not throw: Vec3.normalize writes (0,0,0) for a zero vector (vec3.ts:329-345), Quat.normalize leaves a zero quaternion at (0,0,0,0) instead of identity (quat.ts:429-448), Quat.invert returns the zero quaternion for zero input (quat.ts:384-398), Vec3.angle(a, b) returns RADIANS (vec3.ts:753).

Frozen static constants

Type Members Values Mechanism Line
Vec2 ZERO ONE NEG_ONE UNIT_X UNIT_Y (0,0) (1,1) (-1,-1) (1,0) (0,1) freezeVec2 = Object.freeze(new Vec2(...)), declared Readonly<Vec2> vec2.ts:59-63, helper :50-51
Vec3 UNIT_X UNIT_Y UNIT_Z (1,0,0) (0,1,0) (0,0,1) freezeVec3, declared Readonly<Vec3> vec3.ts:57-59, helper :48-50
Vec3 RIGHT UP FORWARD (1,0,0) (0,1,0) (0,0,-1) freezeVec3 vec3.ts:60-62; FORWARD carries the inline comment "we use -z for view-dir"
Vec3 ZERO ONE NEG_ONE (0,0,0) (1,1,1) (-1,-1,-1) freezeVec3 vec3.ts:63-65
Vec4 ZERO ONE NEG_ONE UNIT_X UNIT_Y UNIT_Z UNIT_W all-equal vectors then the unit basis freezeVec4 vec4.ts:55-61, helper :46-47
Quat IDENTITY (0,0,0,1) inline Object.freeze quat.ts:54
Mat3 IDENTITY 3x3 identity inline Object.freeze mat3.ts:45
Mat4 IDENTITY 4x4 identity inline Object.freeze mat4.ts:63
Size ZERO ONE (0,0) (1,1) inline Object.freeze size.ts:36-37
Color WHITE GRAY BLACK TRANSPARENT RED GREEN BLUE CYAN MAGENTA YELLOW byte channels 255 / 127 / 0; alpha 255 everywhere except TRANSPARENT (0,0,0,0) freezeColor color.ts:56-65, helper :45-46
Rect none - - no frozen static constants in rect.ts

Vec3 exposes no BACK, DOWN or LEFT. Object.freeze is shallow and does not touch the prototype, so methods remain callable on a frozen instance and throw only when they assign.

ValueType family

src/cocos/core/value-types/value-type.ts is the base of every value type: clone() :48 (base logs errorID(100) and returns this), equals(other) :64 (base returns false), set(other) :78, toString() :91. MathBase extends ValueType (math-base.ts:31) adds static createFloatArray(size) :32, the array getter :40 and the internal _array field :44, allocated from MATH_FLOAT_ARRAY :29, which resolves to Float64Array because JSB is false in this Web build.

Family member Static form (Vec3) Instance form Notes
set Vec3.set(out, x, y, z) vec3.ts:101 set(other) / set(x?, y?, z?) vec3.ts:929-941 instance set() with no arguments zeroes the vector
copy Vec3.copy(out, a) vec3.ts:90 copy(other) returns this
clone Vec3.clone(a): Vec3 vec3.ts:82 clone() the only allocating member
lerp Vec3.lerp(out, a, b, t) vec3.ts:373 Vec3.lerp(to, ratio) vec3.ts:1018 t/ratio are not clamped - outside [0,1] they extrapolate
equals Vec3.equals(a, b, epsilon = EPSILON) vec3.ts:739 Vec3.equals(other, epsilon = EPSILON) vec3.ts:961 APPROXIMATE by default, EPSILON = 0.000001 (utils.ts:43)
strictEquals Vec3.strictEquals(a, b) Vec3.strictEquals(other) vec3.ts:987 exact field comparison
toArray / fromArray Vec3.toArray(out, v, ofs = 0) vec3.ts:707, Vec3.fromArray(out, arr, ofs = 0) :720 toArray(out, ofs) / fromArray(arr, ofs) Mat4.toArray mat4.ts:1377, Quat.toArray quat.ts:691, Color.toArray color.ts:258
non-vector variants Rect.lerp(out, from, to, ratio) rect.ts:77, Size.lerp size.ts:49, Color.lerp(out, from, to, ratio) color.ts:240 instance variants above
enumerableProps(prototype, attrs) utils.ts:353 - sets enumerable: true on the named accessor keys through Object.defineProperty; changes no value

Type-specific surface

Type Member (line) Fact
Vec2 cross overloads vec2.ts:319-326 Vec2.cross(a, b) returns a NUMBER (2D pseudo cross product); Vec2.cross(out: Vec3, a, b) writes and returns a Vec3
Vec2 str(a) vec2.ts:389, angle(a, b) :438 str is the string form; angle is RADIANS
Vec3 random(out, scale?) vec3.ts:456 consumes the shared generator behind math.random (utils.ts:182)
Vec3 angle(a, b) vec3.ts:753, signedAngle :791 RADIANS; signedAngle takes an axis reference vector
Vec3 projectOnPlane(out, a, n) vec3.ts:807, project(out, a, b) :817, moveTowards(out, current, target, maxStep) :833, generateOrthogonal(out, n) :862 generateOrthogonal accepts a Readonly<IVec3Like> operand
Vec3 min vec3.ts:178, max :189, negate :277, invert :288 componentwise, out-first
Vec4 fromColor(out, color) vec4.ts:101 copies the BYTE accessors: out.x = color.r (:102) then color.g/b/a (:103-105), and r/g/b/a read _data directly (color.ts:346-387), so a Color(255,128,0,255) arrives as Vec4(255,128,0,255), not fractions. Use Color.toVec4 (color.ts:113) for the 0-1 x/y/z/w view (:390-397)
Rect fromMinMax(out, v1, v2) rect.ts:56, intersection(out, one, other) :97, union(out, one, other) :121, equals(a, b) :145 intersection (:97-114) writes NEGATIVE width/height when the rects are disjoint; test overlap with intersects(other) :399, contains(point: Vec2) :414, containsRect(other) :428
Size lerp(out, from, to, ratio) size.ts:49, equals(a, b) :62 width/height only
Color storage private _data = new Uint8ClampedArray(4) color.ts:340, channel accessors :346-387 channels are 0-255 BYTES; writes clamp out-of-range values, coerce NaN to 0 and round halves to even (0.5 to 0, 2.5 to 2, 127.5 to 128)
Color set(r?, g?, b?, a?) color.ts:721-743 per-channel defaults 0, 0, 0, 255 via ??
Color x/y/z/w accessors color.ts:390-397 the 0-1 view: getter multiplies by toFloat = 1/255 (:36), setter multiplies by 255
Color fromHEX(out, hex) :150-174, instance fromHEX(hex) :519 # optional; 6 digits get FF appended, 3 digits are doubled then FF, 4 digits are doubled; a NUMBER below 0x1000000 is shifted << 8 and + 0xff; channels are read back as r = hexNumber >>> 24
Color toCSS(opt = 'rgba') :497, toHEX(fmt = '#rrggbb') :561, hex(a) :336, toRGBValue() :591 toCSS('rgba') prints byte integers for rgb and a fixed(2) 0-1 alpha, e.g. rgba(255,128,0,1.00); the '#rgb'/'rgb' forms are lossy
Color toUint32(color) :308, fromUint32(out, uint32) :289 alpha in the MOST significant byte, red in the least (0xAABBGGRR); fromUint32 does uint32 >>>= 0 first (:291) - this is the opposite byte order from the number form of fromHEX
Color toVec4(color, out?) :113, fromVec4(value, out?) :131 divide / multiply by toFloat
Color add :180, subtract :192, multiply :204, divide :216, scale :228, lerp :240 all arithmetic is byte-space and clamps on write
Color fromHSV(h, s, v) :608, toHSV() :683 s/v are fractions, h is an angle
Color static equals(a, b, epsilon = EPSILON) :324 vs instance equals(other) :452 the static is approximate, the instance compares bytes exactly
Color srgbToLinear :781, linearToSrgb :793, linearToSrgb8Bit :800, SRGB_8BIT_TO_LINEAR :812 gamma conversion helpers on scalars; not reachable through the bundle export list

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No third-party reports yet.

Signed by skilld at ed38648. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated last month
layer
subsystem
All 1 allowed tools
Read Edit Write Bash
Other metadata
compatibility
Cocos AIR source checkout or installed cocosair.js package. Browser rendering requires WebGL 2; source tooling requires Node.js >=18. Use the host-provided tools and user-authorized workspace.
when_to_use
[
  "normalize or project a direction",
  "compose or decompose a transform matrix",
  "choose quaternion rotation and angle units",
  "convert colors or calculate easing and deterministic random",
  "writing a frozen vector constant throws"
]
owns
[
  "vector-math",
  "matrix-transforms",
  "quaternion",
  "color-values",
  "easing",
  "deterministic-random"
]
not_for
[
  "screen and world camera conversion -> cocosair-camera",
  "native tween playback -> cocosair-animation-tween",
  "node parenting and lifecycle -> cocosair-scene-graph"
]
metadata
{
  "author": "oahc09@gmail.com",
  "version": "1.0.0",
  "last-updated": "2026-10-13",
  "description_zh": "使用 Cocos 数学类型进行向量、矩阵、四元数、颜色和插值计算。用于坐标变换、旋转、颜色处理、缓动或确定性随机。"
}

README badge

README badge for oahc09/cocosairjs/cocosair-math