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

/cocosair-camera

@ed38648
by oahc09oahc09/cocosairjs3 stars
2
Run it yourself

Configure Camera projection, viewports, framing and screen/world conversion. Use for split-screen, pointer rays, wrong screenToWorld results or AIR_E_ORTHO_WIDTH.

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

Nothing lands on disk. Nothing to clean up.

Fork this Skill

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

referencescontracts.md

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

Derived from the cocosair-camera 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

  • The user surface is the Camera component (@ccclass('cc.Camera'), src/cocos/misc/camera-component.ts:102), not the render-scene scene.Camera. _createCamera builds the scene camera during onLoad (:605, :712); the component's own getters/setters only mirror onto it while it exists (every setter guards if (this._camera), e.g. :317), so reads are valid before activation but writes before onLoad are held in the serializable fields and pushed on the next _createCamera (:726-743).
  • camera.camera is TYPED non-null but IS null until the component activates: the getter returns this._camera! (src/cocos/misc/camera-component.ts:202-204) and _camera is only assigned inside onLoad -> _createCamera (:713-714), which needs the node active under a scene handed to app.run. So touch camera.camera.* (calculateObliqueMat, aspect, width/height, changeTargetWindow) only after the first run frame, and null-check it in code that can run earlier - frameObject does exactly that (src/air/utils/frame-object.ts:44). The same guard silently skips work that looks like it ran: the fovAxis setter's fov conversion sits inside if (this._camera) (camera-component.ts:339-342), so a pre-activation fovAxis write converts nothing and the fov number you typed is later interpreted against the other axis. frameObject hits the same null: it substitutes aspect = 1 when camera.camera is missing (src/air/utils/frame-object.ts:44-45), so a call before the first run frame sizes the fit for a square window - harmless when the window is square or wider (the vertical half-fov governs), but on a portrait window the horizontal field is narrower than the fit assumed and the target overflows; call it after activation or increase fov.
  • Component conversion methods take the INPUT first and an optional out second, returning out: worldToScreen(worldPos, out?) (:654), screenToWorld(screenPos, out?) (:667), screenPointToRay(x, y, out?) (:641). The wrapped render-scene Camera uses the opposite order worldToScreen(out, worldPos) etc. (src/cocos/render-scene/scene/camera.ts:1336, :1282, :1243). Call the component version; with no out, screenToWorld allocates one pre-filled with a COPY of the camera node's world position (:668 -> node.getWorldPosition(), which returns a new Vec3 when called without an argument, src/cocos/scene-graph/node.ts:2666-2672) - the node's stored position is not mutated, but pass an explicit out to avoid allocating per call.
  • Screen space is physical framebuffer pixels with a LEFT-BOTTOM origin (doc comment src/cocos/misc/camera-component.ts:634, :648). camera.width/camera.height are the swapchain pixels: resize only assigns the size it is handed and derives aspect from it (src/cocos/render-scene/scene/camera.ts:984-991, called with the window size from changeTargetWindow :1220-1221), and the effective-DPR scaling happens upstream - DPR = min(devicePixelRatio, cap) with cap default 2 (src/pal/screen-adapter/web/screen-adapter.js:78-81), applied to the CSS size in windowSize (:83-89). worldToScreen returns z normalized to [0,1] (camera.ts:1355) and it is PROJECTIVE there (non-linear in world distance); screenToWorld does not invert that curve - it re-reads screenPos.z as a LINEAR near->far fraction (:1310), so the two z conventions do not round-trip: with near = 0.1, far = 1000 a point at depth 5 projects to z ~= 0.98 and 0.98 back-projects to depth ~980 on the same ray. Convert first (docs/manual/cameras.md:300, asserted in examples/render-composite/src/main.ts:232-239) or pick with screenPointToRay + an intersection. event.getLocation() already yields left-bottom physical pixels; pass it straight in.
  • Priority is a draw ORDER key, re-sorted ASCENDING EVERY FRAME: root._frameMoveEnd sorts the extracted camera list with a.priority - b.priority (src/cocos/root.ts:795) right before handing it to this._pipeline.render(cameraList) (:801), and that list is rebuilt each frame from the render windows (:761, :769-771 -> src/cocos/render-scene/core/render-window.ts:309-316). So a LOWER priority renders first (background) and a higher one over it, and a runtime camera.priority write takes effect from the NEXT frame - the render-scene setter is a bare store (src/cocos/render-scene/scene/camera.ts:501-503) because the sort is per-frame, not because writes are ignored. Default priority is 0 (src/cocos/misc/camera-component.ts:146), UI range [0, 65535] (:211). The window's own attach-time sort (render-window.ts:365-367, called only from attachCamera :331) fixes EXTRACTION order, which is what breaks ties between equal priorities (Array.prototype.sort is stable, so ties keep window push order = node activation order) - set distinct priorities whenever order matters; re-enabling the node does not change order (camera-component.ts:609-614 -> _attachToScene :749-758 -> render-scene.ts:268-271 touches the render-SCENE list, which rendering does not walk).
  • orthoWidth defaults to 0, which keeps native orthoHeight * aspect half-width; a finite non-negative value fixes the half-width independently and perspective ignores it (src/cocos/misc/camera-component.ts:383-391, src/cocos/render-scene/scene/camera.ts:1079). Assigning a negative or non-finite value throws AIR_E_ORTHO_WIDTH and keeps the old value (camera-component.ts:387, camera.ts:697).
  • The component viewport is the normalized camera.rect Rect(0,0,1,1) (src/cocos/misc/camera-component.ts:168, :481). Setting the render-scene viewport directly warns (warnID(8302), camera.ts:758-760) and is rerouted through setViewportInOrientedSpace (:1115), which flips y when screenSpaceSignY < 0 and re-rotates per surface orientation.
  • The oblique near plane lives on the render object: camera.camera.calculateObliqueMat(viewSpacePlane: Vec4) rebuilds the ordinary projection first, then rewrites the near row and inverse matrices (camera.ts:1389-1418); a non-finite/zero plane throws AIR_E_CLIP_PLANE (TypeError, :1392) and a degenerate plane against the projection throws AIR_E_CLIP_PLANE (RangeError, :1404). getClipSpaceMinz() returns the device capabilities.clipSpaceMinZ (:1421): -1 on the WebGL 2/GL path (src/cocos/gfx/base/define.ts:774), 0 only on WebGPU (src/cocos/gfx/webgpu/webgpu-device.ts:544); the WebGPU backend is not a shipped AIR target.
  • Capability layers, stated separately (source export / default-bundle reachable / browser-verified): Camera, Camera.ProjectionType/FOVAxis/ClearFlag and frameObject all resolve by symbol name in the main entry declaration build/cocosair.module.d.ts (Camera currently :24943, frameObject currently :56816). They are NOT exposed by the narrow cocosair.js entry, whose declaration has one value export - export declare function createAirApp (currently :3 in build/cocosair.module.d.ts) - plus the AirApp/AirAppOptions type re-export, nor by cocosair.js. No camera behavior here is browser-verified against the current candidate; the examples/camera-* Gallery apps exercise it, which is not current-candidate evidence.

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
[
  "switch perspective and orthographic views",
  "fit a loaded model in view",
  "stack cameras or create split-screen views",
  "convert pointer pixels to a world ray",
  "screenToWorld results or orthographic width are wrong"
]
owns
[
  "projection",
  "viewport",
  "camera-framing",
  "screen-world",
  "camera-order",
  "oblique-plane"
]
not_for
[
  "register node layer bits -> cocosair-scene-graph",
  "offscreen render targets -> cocosair-rendering",
  "physics collision queries -> cocosair-physics"
]
metadata
{
  "author": "oahc09@gmail.com",
  "version": "1.0.0",
  "last-updated": "2026-10-13",
  "description_zh": "配置相机投影、视口、可见层和多相机顺序,并进行坐标转换、射线拾取与模型取景。用于相机配置或屏幕与世界坐标问题。"
}

README badge

README badge for oahc09/cocosairjs/cocosair-camera