一键导入
spectacles-spatial-persistence
Learn how to persist and restore AR content across sessions using World Anchors.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Learn how to persist and restore AR content across sessions using World Anchors.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | spectacles-spatial-persistence |
| description | Learn how to persist and restore AR content across sessions using World Anchors. |
Spectacles support persisting digital content (anchors) across sessions using the Spatial Anchors API. This enables users to place an object in their physical room, close the lens or reboot the device, and have the object reappear in the exact same physical location when they return.
Official docs: Spectacles Home · Spatial Anchors
[!WARNING] The DeviceTracking component's mode MUST be set to
Worldfor Spatial Anchors to function.
Persisting content is a two-step process: first you create a local World Anchor using AnchorSession, then you explicitly save it to the device using AnchorSession.saveAnchor().
import { AnchorSession, AnchorSessionOptions } from "Spatial Anchors.lspkg/AnchorSession";
import { AnchorModule } from "Spatial Anchors.lspkg/AnchorModule";
import { State } from "Spatial Anchors.lspkg/Anchor";
// 1. Get the AnchorModule
const anchorModule = global.scene.getRootObject(0).getComponent(AnchorModule.getTypeName());
// 2. Configure Options
const options = new AnchorSessionOptions();
options.area = "MyRoomConfig"; // Group anchors by an area ID
options.scanForWorldAnchors = true;
// 3. Open Session
const session = await anchorModule.openSession(options);
// 4. Create the World Anchor in front of the camera
const spawnTransform = mat4.compose(myWorldPos, myWorldRot, vec3.one());
session.createWorldAnchor(spawnTransform).then(async (anchor) => {
// 5. Save the anchor persistently
const savedAnchor = await session.saveAnchor(anchor);
// Check if the physical anchor was immediately recognized by tracking
if (savedAnchor.state === State.Found) {
print(`Anchor saved and found! ID: ${savedAnchor.id}`);
}
});
When users reload the Lens, you need to use the same area string to scan for previously saved anchors.
// After opening the session with scanForWorldAnchors = true
session.onAnchorNearby.add((anchor) => {
print(`Found an anchor matching ID: ${anchor.id}`);
// Wait until the tracking system locks onto the physical location
anchor.onFound.add(() => {
print(`Anchor is physically localized at: ${anchor.toWorldFromAnchor.column3}`);
// Spawn/move your preserved 3D content to this matrix transform
myPrefab.getTransform().setWorldTransform(anchor.toWorldFromAnchor);
});
});
To clear persistence state, you can reset the entire space:
await session.reset();
The logic snippet below is extracted directly from the official Spatial Persistence sample, demonstrating an AnchorManager singleton that handles session generation and tracking events cleanly.
See the reference guide for details.
Reference guide for 2D UI and screen-space interaction in Lens Studio — covering ScreenTransform anchors/offsets/size/pivot and coordinate conversions (localPointToScreenPoint, screenPointToLocalPoint, localPointToWorldPoint), ScreenImage (texture, stretch mode, color tint), Text component (content, font, alignment, color, size), ScreenRegionComponent for defining tap/touch areas, TouchComponent with TouchStartEvent/TouchMoveEvent/TouchEndEvent, tap input for phone lenses, LSTween UI animations (colorTo, moveTo on screen elements), multi-swatch color picker pattern, undo stack pattern, and common gotchas. Use this skill whenever a lens needs a 2D UI panel, tap interaction, on-screen buttons, text labels, color pickers, swipeable menus, or undo/redo — covering Drawing, Quiz, TappableQuestion, MemeSticker, MusicVideo, and HighScore examples.
Learn how to programmatically request and manage raw camera textures and crops.
Reference guide for face and body AR tracking in Lens Studio — covering FaceTrackingComponent setup (multi-face, faceIndex), FaceInset and FaceMask effects, 2D and 3D Face Attachments (hat/mouth/left_eye/right_eye anchors), Face Mesh UV texturing, Face Landmarks (68 keypoints), Face Expression weights for mouth-open/eye-blink detection, Eye Tracking component (left/right eye direction), Upper Body Tracking 3D asset (hips/spine/shoulder attachment points), Upper Body Mesh for seamless selfie occlusion, and Face Retouch/Eye Color/Face Liquify/Face Stretch effects. Use this skill for any phone or front-camera lens involving faces, selfie effects, makeup, face masks, 3D head ornaments, body tracking, or expression-driven animations — covering the vast majority of Snapchat lens content.
Learn how to dynamically instantiate prefabs algorithmically in grid or spherical patterns.
Implement proximity-based triggers and smooth tethering behaviors for interactive objects.
Learn how to track image markers and detach the tracked content into World Space in AR.