You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ARKit:为ARWorldTracking Session设置自定义原点及会话后调整问询

Repositioning ARKit World Origin to a Custom Point (e.g., Marker Center)

Absolutely! You can reposition the AR world origin after your session has started—this is a super common workflow when you want to anchor your AR experience to a specific reference point like a marker. Here's how to pull it off step by step:

Key Tool: session.setWorldOrigin(relativeTransform:)

ARKit gives you the setWorldOrigin(relativeTransform:) method on your ARSession instance, which lets you redefine the world origin relative to its current position. This is exactly what you need to shift the origin straight to your marker's center.

Step-by-Step Implementation

Let's walk through a scenario where you're using an image marker (ARKit's image tracking) and want to lock the origin to that marker once it's detected:

  1. Enable Image Tracking in Your Configuration
    First, update your config to let ARKit spot your marker. Make sure you've added your reference images to your Xcode asset catalog first:

    let configuration = ARWorldTrackingConfiguration()
    // Load your pre-defined reference images from the asset catalog group
    guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "Markers", bundle: nil) else {
        fatalError("Failed to load reference images—double-check your asset catalog!")
    }
    configuration.detectionImages = referenceImages
    // Keep your debug options to visualize the origin and feature points
    self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
    self.sceneView.session.run(configuration)
    
  2. Trigger Origin Shift When Marker is Detected
    Implement the session(_:didAdd:) delegate method to catch when ARKit finds your marker. Use the marker's anchor transform to reposition the world origin:

    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        guard let imageAnchor = anchors.first as? ARImageAnchor else { return }
        
        // The imageAnchor.transform maps the current origin to the marker's position
        // Inverting it gives us the transform to make the marker the new origin
        let newOriginTransform = imageAnchor.transform.inverted()
        
        // Update the world origin to the marker's center
        session.setWorldOrigin(relativeTransform: newOriginTransform)
        
        // Optional: Disable image detection after setting the origin to avoid re-triggering
        if var currentConfig = session.configuration as? ARWorldTrackingConfiguration {
            currentConfig.detectionImages = nil
            session.run(currentConfig)
        }
    }
    

Pro Tips for Smooth Results

  • Wait for Stable Tracking: For better accuracy, hold off on shifting the origin until ARKit has a solid lock on the marker. You could check the anchor's trackingState is .normal or add a short delay after detection to avoid jitter.
  • Transform Inversion Matters: The inverted() call is non-negotiable here. It flips the transform from "current origin to marker" to "marker to current origin"—telling ARKit to make the marker the new baseline.
  • Repeated Shifts Are Allowed: You can call setWorldOrigin again later if you need to reposition the origin to a new point (like another marker) later in the session.

Alternative: Custom Anchor Use Case

If you're not using image tracking (e.g., placing a origin via tap or 3D object marker), create a custom ARAnchor at your desired position, then use its transform to shift the origin the same way we did with the image anchor above.

内容的提问来源于stack exchange,提问作者Venkatesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:18:01