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

/cocosair-performance

@ed38648
by oahc09oahc09/cocosairjs3 stars
2
Run it yourself

Optimize draw calls: static merge, instancing, dynamic atlases and GPU morph fields. Frame budgets, on-demand rendering, AIR_E_MESH_CAPACITY, AIR_E_PIXEL_RATIO_IMPORT_ORDER.

  • 12 files
  • 84.2 KB
  • MIT
  • Updated last month
  • GitHub
Use this Skill: https://skilld.dev/gh/oahc09/cocosairjs/cocosair-performance

Nothing lands on disk. Nothing to clean up.

Fork this Skill

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

referencesinstance-batches.md

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

Explicit instance batches

Use createAirInstanceBatch for many copies of a fixed indexed mesh without one Node per copy. Native USE_INSTANCING still serves ordinary shared-material MeshRenderers; it is a different stream owner. For shape-to-shape GPU interpolation use morph fields.

Check the actual consumer's exports/declarations first: older installed bundles may not include these helpers. This route requires initialized WebGL2. Package examples import cocosair.js; the repository examples' cocosair import-map alias is not an npm package name.

Minimal batch

After createAirApp(), with a Scene and visible Camera already prepared:

import { createAirInstanceBatch, utils, primitives, Mat4, Vec3, Vec4 } from 'cocosair.js';

const mesh = utils.createMesh(primitives.box());
const batch = createAirInstanceBatch({ parent: scene, mesh, capacity: 128, count: 3 });
for (let i = 0; i < 3; i++) {
    batch.setMatrix(i, Mat4.fromTranslation(new Mat4(), new Vec3(i * 2 - 2, 0, 0)));
    batch.setColor(i, new Vec4(i === 0 ? 1 : 0, i === 1 ? 1 : 0, i === 2 ? 1 : 0, 1));
}
batch.commit();

Each batch uses one native Node/Model and shares the original Mesh buffers. Each submesh/pass still needs a draw; do not promise one draw for arbitrary multi-pass or multi-submesh assets. Default material is opaque, double-sided, linear-color unlit; it is not a PBR or shadow material.

Publish and capacity

setMatrix/setColor stage CPU edits. commit(start?, count?) uploads the selected instance range; omitted arguments commit the dirty span. Other staged edits stay pending. Bounds, getMatrix() and raycast() consume committed matrices, not unpublished values. info.lastUploadBytes reports that instance update, not uniform/driver traffic or GPU completion time.

reserve(n) grows capacity while preserving committed and pending data; it does not change the visible count. Commit new data before setCount(n) exposes it. setCount(0) disables drawing rather than falling back to an ordinary non-instanced draw. Capacity is explicit; the 1..1,048,576 guard is not a throughput promise.

Custom materials must declare all four AIR_INSTANCE_ATTRIBUTES as instanced RGBA32F inputs at locations 8..11: three matrix columns with translation in their W components, followed by linear RGBA. Do not also enable native USE_INSTANCING. The helper rejects incompatible streams, excess attributes and unsupported device limits.

Input is fixed indexed TRIANGLE_LIST geometry with finite bounds. Do not reset/destroy/re-upload borrowed mesh buffers while the batch is alive. Dynamic meshes and native Mesh Morph data are not accepted by this helper.

Bounds, picking and release

getWorldBounds() includes committed instance and parent transforms. raycast(ray) is a CPU triangle test, returns the closest instanceIndex, and is not GPU picking; its cost grows with the instances considered. Form the ray with native Camera framebuffer coordinates, converting DOM CSS pixels and flipping Y first.

batch.dispose() is idempotent; direct destruction of batch.node also retires owned buffers. The borrowed Mesh and optional user Material remain caller-owned. The helper releases its references and destroys only its own default material. For a mesh created solely for this feature, dispose the batch before mesh.destroy(); do not destroy a shared mesh just because one consumer exits.

In a checkout, examples/instance-batch/ and tools/verify/instance-batch.cjs cover actual indexed draw counts, range updates, zero count, bounds/picking and GPU deletion. A field assignment or profiler FPS alone is not proof. Local Chromium reports are subsets, not full browser/release acceptance.

Canonical checkout guide/source: docs/guides/instance-batches.md, src/air/rendering/instance-batch.ts and instance-data.ts. These paths are optional source references; this copied skill supplies neither the SDK nor browser tooling.

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
[
  "draw calls equal the object count",
  "batch many static meshes or UI sprites; createAirInstanceBatch/createAirMorphField point/voxel",
  "measure actual draw counts and frame budget",
  "stop rendering until the next requested frame",
  "high pixel density makes the device too slow"
]
owns
[
  "batching",
  "instancing",
  "dynamic-atlas",
  "profiler",
  "on-demand-rendering",
  "pixel-ratio"
]
not_for
[
  "procedural geometry authoring -> cocosair-primitives-mesh",
  "startup admission and app lifecycle -> cocosair-application",
  "material property types -> cocosair-materials-effects"
]
metadata
{
  "author": "oahc09@gmail.com",
  "version": "1.0.0",
  "last-updated": "2026-10-13",
  "description_zh": "选择静态合批、动态图集和实例化,并检查性能统计、剔除和按需渲染。用于大量对象、动画或绘制性能优化。"
}

README badge

README badge for oahc09/cocosairjs/cocosair-performance