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.

referenceseasing-random.md

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

Derived from src/cocos/core/algorithm/easing.ts, src/cocos/core/algorithm/index.ts, src/cocos/core/index.ts, src/cocos/core/math/index.ts, src/cocos/core/math/utils.ts, src/cocos/core/math/bits.ts, src/cocos/core/math/color.ts, src/cocos/core/math/vec2.ts, src/cocos/core/math/vec3.ts, src/cocos/core/math/vec4.ts, src/cocos/tween/export-api.ts, src/cocos/tween/tween-action.ts, build/cocosair.module.d.ts, build/cocosair.module.js, docs/evidence/examples-verified.json (as of 2026-10-09)

Easing, scalar utilities, random and bit helpers

How these symbols are reached

Symbol Source Layer statement
easing namespace object src/cocos/core/algorithm/index.ts:25 (import * as easing from './easing'), exported :29, also hung on legacyCC.easing :30; re-exported through src/cocos/core/index.ts:49 (export * from './algorithm') source export and default-bundle reachable; the generated declaration shows it as export namespace easing in build/cocosair.module.d.ts (currently :21091)
The 44 individual curve names src/cocos/core/algorithm/easing.ts reachable ONLY as easing.<name> - linear, cubicOut, bounceOut, smooth, fade, constant are not top-level bundle exports of build/cocosair.module.js
math.easing does not exist; src/cocos/core/math/index.ts:34-49 has no easing re-export not present in the declaration
bits exported from the math barrel src/cocos/core/math/index.ts:34 both math.bits.log2 and a top-level bits import of the delivered bundle work
The scalar/random helpers src/cocos/core/math/index.ts:44 (export * from './utils') members of math and top-level bundle exports, e.g. import { clamp01 } from 'cocosair'

Easing curves

Every curve is (k: number) => number where k is the normalized progress; none of them clamp k except the two rows marked "clamps". constant() is the only curve that takes no argument.

Family In Out InOut Body fact as written in the source
(linear) linear easing.ts:40 returns k - - identity
(constant) constant easing.ts:31 returns 0 - - ignores progress entirely; the tween never advances through it
quad quadIn :49 k * k quadOut :58 k * (2 - k) quadInOut :67 k *= 2; 0.5 * k * k then -0.5 * (--k * (k - 2) - 1) power 2
cubic cubicIn :79 k * k * k cubicOut :88 --k * k * k + 1 cubicInOut :97 0.5 * k * k * k then 0.5 * ((k -= 2) * k * k + 2) power 3; cubicInOut(0.5) is 0.5
quart quartIn :109 k * k * k * k quartOut :118 1 - (--k * k * k * k) quartInOut :127 0.5 * k * k * k * k then -0.5 * ((k -= 2) * k * k * k - 2) power 4
quint quintIn :139 k * k * k * k * k quintOut :148 --k * k * k * k * k + 1 quintInOut :157 0.5 * k * k * k * k * k then 0.5 * ((k -= 2) * k * k * k * k + 2) power 5
sine sineIn :170 k === 1 short-circuit, else 1 - Math.cos(k * Math.PI / 2) sineOut :182 Math.sin(k * Math.PI / 2) sineInOut :191 0.5 * (1 - Math.cos(Math.PI * k)) trig half-wave
expo expoIn :199 k === 0 ? 0 : 1024 ** (k - 1) expoOut :208 k === 1 ? 1 : 1 - (2 ** (-10 * k)) expoInOut :217 explicit k === 0 and k === 1 guards, then 0.5 * 1024 ** (k - 1) / 0.5 * (-(2 ** (-10 * (k - 1))) + 2) 2^10 base, endpoints guarded
circ circIn :235 1 - Math.sqrt(1 - k * k) circOut :244 Math.sqrt(1 - (--k * k)) circInOut :253 -0.5 * (Math.sqrt(1 - k * k) - 1) / 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1) circle quadrant
elastic elasticIn :265 a = 0.1, p = 0.4, k === 0 and k === 1 guards, !a || a < 1 forces a = 1; s = p / 4, result -(a * 2 ** (10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p)) elasticOut :288 same amplitude/period constants elasticInOut :311 same constants the amplitude is a local let that the a < 1 branch always rewrites to 1, so no amplitude/period argument exists on any elastic entry point
back backIn :339 k === 1 guard, s = 1.70158, k * k * ((s + 1) * k - s) backOut :351 k === 0 guard, same s, --k * k * ((s + 1) * k + s) + 1 backInOut :363 s = 1.70158 * 1.525 overshoot: output leaves [0, 1]
bounce bounceIn :372 1 - bounceOut(1 - k) bounceOut :380 four piecewise branches on 1 / 2.75, 2 / 2.75, 2.5 / 2.75 with coefficient 7.5625 and offsets 0.75, 0.9375, 0.984375 bounceInOut :396 bounceIn(k * 2) * 0.5 / bounceOut(k * 2 - 1) * 0.5 + 0.5 bounceIn is defined from bounceOut, so bounceIn(0) is 0 only through that mirror
smooth smooth :407 clamps (k <= 0 returns 0, k >= 1 returns 1), else k * k * (3 - 2 * k) - - clamped Hermite curve
fade fade :421 clamps the same way, else k * k * k * (k * (k * 6 - 15) + 10) - - clamped quintic smoothstep

Composed OutIn variants

Variant Line Definition
quadOutIn easing.ts:435 _makeOutIn(quadIn, quadOut)
cubicOutIn :441 _makeOutIn(cubicIn, cubicOut)
quartOutIn :447 _makeOutIn(quartIn, quartOut)
quintOutIn :453 _makeOutIn(quintIn, quintOut)
sineOutIn :459 _makeOutIn(sineIn, sineOut)
expoOutIn :465 _makeOutIn(expoIn, expoOut)
circOutIn :471 _makeOutIn(circIn, circOut)
elasticOutIn :477 _makeOutIn(elasticIn, elasticOut)
backOutIn :483 _makeOutIn(backIn, backOut)
bounceOutIn :489 _makeOutIn(bounceIn, bounceOut)

_makeOutIn (src/cocos/core/algorithm/easing.ts:491-498) returns (k) => k < 0.5 ? fnOut(k * 2) / 2 : fnIn(2 * k - 1) / 2 + 0.5 (:493-496), i.e. the Out half runs first and the In half second - the reading order of the name is the execution order, which is the opposite of InOut. There is no *In-first variant builder and no easeInOutCubic-style camelCase alias.

Tween integration

Fact Line
TweenEasing is a 44-member string union: linear, smooth, fade, constant, then {quad,cubic,quart,quint,sine,expo,circ,elastic,back,bounce}{In,Out,InOut,OutIn} src/cocos/tween/export-api.ts:32-43; generated declaration build/cocosair.module.d.ts (currently :36502)
ITweenOption.easing?: TweenEasing | ((k: number) => number) - a plain function is accepted next to the strings src/cocos/tween/export-api.ts:59
A string easing is first normalized by TweenEasingAdapter (legacy hyphen/camel forms such as Quad-In), then resolved by table lookup opts.easing = easing[easingName] src/cocos/tween/tween-action.ts:33, :120-122, :128-131
An unknown name is NOT an error: warnID(1031, easingName) is emitted and opts.easing becomes undefined, so the property interpolates with the default progress function src/cocos/tween/tween-action.ts:132
The normalization/lookup only runs when opts is non-null (if (opts == null) short-circuits the whole checker block) src/cocos/tween/tween-action.ts:113-117

Scalar utilities (src/cocos/core/math/utils.ts)

Helper Line Fact
HALF_PI, TWO_PI :40, :41 PI * 0.5, PI * 2.0; there is no QUARTER_PI / THREE_HALVES_PI in this file
EPSILON :43 0.000001, the single tolerance constant
equals(a, b) :55 relative form: abs(a - b) <= EPSILON * Math.max(1.0, abs(a), abs(b))
approx(a, b, maxDiff?) :67 maxDiff = maxDiff || EPSILON - passing 0 silently falls back to EPSILON, and abs(a-b) <= maxDiff is absolute
approxGE / approxLE :80, :93 a >= b - maxDiff / a <= b + maxDiff (tolerant non-strict)
approxGT / approxLT :106, :119 a > b + maxDiff / a < b - maxDiff (tolerant strict)
clamp(val, min, max) :131-139 swaps an inverted min/max pair first, so clamp(5, 10, 0) is 5
clamp01(val) :146 val < 0 ? 0 : val > 1 ? 1 : val; NaN passes through unchanged
lerp(from, to, ratio) :157 from + (to - from) * ratio; an unclamped ratio extrapolates, argument order is start, end, t
inverseLerp(from, to, value) :307 (value - from) / (to - from), unguarded - from === to yields Infinity or NaN
repeat(t, length) :279 t - Math.floor(t / length) * length; length === 0 yields NaN
pingPong(t, length) :293 repeat(t, length * 2) then length - abs(t - length); range is [0, length]
toRadian(a) / toDegree(a) :166, :175 multiply by module-local _d2r = PI / 180 / _r2d = 180 / PI (:34, :36)
nextPow2(val) :268-269 return bits.nextPow2(val) (:269) - the two names are the same function
absMaxComponent(v: IVec3Like) :317 returns the SIGNED component with the largest magnitude (can be negative)
absMax(a, b) :337 returns the signed argument whose magnitude is larger; absMax(-5, 4) is -5
enumerableProps(prototype, attrs) :353-357 Object.defineProperty(prototype, key, { enumerable: true }) per name - the mechanism that makes value-type fields show up in dumps

Random helpers

Helper Line Fact
random() src/cocos/core/math/utils.ts:182-183 returns _random() (:183) where let _random = Math.random (:38) is module state
setRandGenerator(func) :191-193 _random = func (:192) replaces the generator process-wide; every consumer of utils.random inherits it, including Vec3.random (src/cocos/core/math/vec3.ts:456, which calls the imported random - vec3.ts:32), Vec2.random (src/cocos/core/math/vec2.ts:353) and Vec4.random (src/cocos/core/math/vec4.ts:426)
randomRange(min, max) :203 random() * (max - min) + min; max is exclusive in practice, reversed bounds produce a reversed open interval
randomRangeInt(min, max) :214 Math.floor(randomRange(min, max)), so randomRangeInt(1, 6) yields 1..5
pseudoRandom(seed) :227 LCG seed = (seed * 9301 + 49297) % 233280; return seed / 233280.0 (:228-229); JS % takes the sign of the dividend, so the SIGN OF THE RESULT VARIES WITH THE ARGUMENT: pseudoRandom(-1) is 0.1714506172839506 (positive) while pseudoRandom(-100000) is -0.8428626543209876 (negative). Non-negative input gives [0, 1); any input gives (-1, 1), so a negative value is not a failure signal, it is a valid output
pseudoRandomRange(seed, min, max) :243 pseudoRandom(seed) * (max - min) + min - the seed is the only state, so calling it twice with the same seed returns the same number
pseudoRandomRangeInt(seed, min, max) :255 Math.floor(pseudoRandomRange(seed, min, max))

setRandGenerator has no reset API and no per-object generator: to restore the default you must call setRandGenerator(Math.random). Nothing in this file offers a hash-noise or gradient-noise generator.

Bit helpers (src/cocos/core/math/bits.ts)

All of them take and return number, and most start with an implicit integer coercion, so non-integer and out-of-range input is silently reinterpreted rather than rejected.

Helper Line Fact
INT_BITS, INT_MAX, INT_MIN :40, :47, :54 32, 0x7fffffff, -1 << 31
sign(v) :60 (v > 0) - (v < 0); sign(0) is 0, sign(NaN) is 0
abs(v) :68 mask is v >> (INT_BITS - 1), so the argument is ToInt32-coerced before the sign trick: abs(-2.5) is 2 and abs(5000000000) is 705032704
min / max :77, :85 y ^ ((x ^ y) & -(x < y)) / x ^ ((x ^ y) & -(x < y)) XOR-select forms, so both arguments are ToInt32-coerced: min(2.5, 1.5) is 1
isPow2(v) :93 !(v & (v - 1)) && (!!v) - isPow2(0) is false, but the coercion makes isPow2(2.5) true
log2(v) :101-108 v >>>= r ladder, i.e. ToUint32 then integer compare: log2(0)/log2(1) are 0, log2(2.9) is 1, log2(1024) is 10 - never fractional, and a negative input produces a huge value (log2(-1) is 131071) instead of failing
log10(v) :115 comparison chain returning 0..9 only
popCount(v) :125 Hamming weight of the ToUint32 form
countTrailingZeros(v) :135 v &= -v then mask ladder; countTrailingZeros(0) is 32, countTrailingZeros(8) is 3
nextPow2(v) :151 --v (:152), >>> fill ladder and + 1 - nextPow2(0) is 0, nextPow2(1) is 1, nextPow2(3) is 4, nextPow2(17) is 32; a fractional argument is coerced first, so nextPow2(2.5) is 2 rather than 4
prevPow2(v) :165 fill ladder then v - (v >>> 1) - prevPow2(0) is 0, prevPow2(17) is 16
parity(v) :178 fold-xor plus 0x6996 lookup, returns 0 or 1
reverse(v) :206 byte-reverse through a module-local 256-entry REVERSE_TABLE built at load time
interleave2(x, y) / deinterleave2(v, n) :217, :237 16-bit Morton key; the deinterleave ends with (v << 16) >> 16, so lane values are sign-extended
interleave3(x, y, z) / deinterleave3(v, n) :250, :277 10-bit-per-lane Morton key, ends with (v << 22) >> 22
nextCombination(v) :290 next larger integer with the same popcount

Half-float encoding

Helper Line Fact
floatToHalf(val) src/cocos/core/math/utils.ts:417-419 wrapper over the module-local toHalf closure (:363-385), which reinterprets a Float32Array/Int32Array pair (:365-366) and returns an integer half bit pattern
halfToFloat(val) :421-423 wrapper over fromHalf (:387-415), same reinterpretation pair (:388-389)
encode: denormals :375 h = (em < (113 << 23)) ? 0 : h; // denormals-as-zero - floatToHalf(1e-8) is 0
encode: overflow :377 h = (em >= (143 << 23)) ? 0x7c00 : h; // overflow - floatToHalf(Infinity) is 0x7c00 (31744)
encode: NaN :379 h = (em > (255 << 23)) ? 0x7e00 : h; // NaN - floatToHalf(NaN) is 0x7e00 (32256)
decode: specials are NOT restored :398, :403-404, :407 if (h !== 0x7c00) (:398) compares the ALREADY-SHIFTED em << 13 value against the raw half pattern, and no em satisfies em << 13 === 0x7c00, so the 0x7f800000 write (:407) is unreachable: halfToFloat(0x7c00) is 65536, halfToFloat(0xfc00) is -65536 and halfToFloat(0x7e00) is 98304 - all finite. Only the em === 0x7fff branch (:403-404) yields NaN, so halfToFloat(0x7fff) is NaN
decode: working cases :395, :399, :401-402 halfToFloat(0x3c00) is 1 and halfToFloat(0x0001) is 0.000030547380447387695 (em is 1, so both take the exponent-adjust path :395 + :399); the em === 0 mantissa-shift branch (:401-402) only fires for 0x0000/0x8000, where it yields halfToFloat(0) = 0
round trip - finite values round trip (halfToFloat(floatToHalf(1.5)) is 1.5); Infinity and NaN do NOT - they come back as finite garbage, so do not use this pair for a sentinel-carrying channel

RGBA packing and colour-space helpers

Symbol Line Layer statement
Color.toUint32(color) (static) src/cocos/core/math/color.ts:308-309 (color.a << 24 | color.b << 16 | color.g << 8 | color.r) >>> 0 - alpha in the highest byte, RED in the lowest, so the layout is 0xAABBGGRR, not 0xRRGGBBAA
Color.fromUint32(out, uint32) (static) src/cocos/core/math/color.ts:289-298 uint32 >>>= 0 first (:291), then out.r = uint32 & 0xff, g >> 8, b >> 16, a >> 24 (:293-296) - the exact inverse of toUint32
Color.fromHEX / toHEX :150-174, :561 string and number forms are both accepted (:151-167) and the number form reads out.r = hexNumber >>> 24 (:168), i.e. 0xRRGGBBAA - do not feed a toUint32 result to fromHEX
srgbToLinear / linearToSrgb / linearToSrgb8Bit :781, :793, :800 source exports of src/cocos/core/math/color.ts that are NOT in the delivered bundle export list, so import { srgbToLinear } from 'cocosair' does not resolve
fp32 / fp16 / toRgba8 / fromRgba8 / perlin - not present in the declaration at all; Color.toUint32/fromUint32 are the RGBA8-style packers and floatToHalf/halfToFloat are the 16-bit encoders

Browser evidence pointer

The captured Chromium run records the comparison helpers actually executing: entry math-approx-compare in docs/evidence/examples-verified.json (results[]; the entry carries its own candidateFingerprint 2f089f6ce25eaccd and result PASS, while measuredAt 2026-10-04T05:52:32.537Z is a FILE-level field, not a per-entry one) lists math.equals, math.approx, math.approxGE, math.approxGT, math.approxLE, math.approxLT and math.EPSILON as executed with result PASS. All 40 examples/math-* Gallery apps are PASS in the same file. That is browser evidence for that recorded candidate, not for the bytes currently in build/.

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