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

如何在ARView中使用setWorldOrigin?RealityKit世界原点设置的认知误区与问题排查

Understanding setWorldOrigin in RealityKit: Why Your World Origin Shift Isn't Working as Expected

Let's break down what's going on here—your confusion comes from a common misunderstanding of how setWorldOrigin(relativeTransform:) works, plus a key quirk of using non-AR mode.

First: Your Core Misconception

You assumed that setWorldOrigin(relativeTransform:) directly shifts the coordinate system so your AnchorEntity(world: .zero) lands at the new origin position. But here's the critical detail:

  • The relativeTransform parameter defines where the new world origin sits relative to the old world origin.
  • When you set relativeTransform.columns.3.y = -1, you're telling the session: "Place the new world origin 1 unit below the old origin."
  • That means your AnchorEntity(world: .zero) should sit at this new origin (old origin's y=-1), which should be lower. But it's not working—why?

The Non-AR Mode Problem

Your code uses cameraMode: .nonAR, and this is the missing piece: ARSession.setWorldOrigin() is built for active AR tracking scenarios (where the session maps real-world space). In non-AR mode, the session doesn't perform any spatial tracking, so calls to setWorldOrigin() are effectively ignored. The coordinate system stays fixed to the default origin, which is why you don't see your cube move.

The Correct Approach for Your Goal

Your need is to redefine the coordinate mapping so (0,0,0) points to a new location. Here's how to do it properly, depending on your mode:

For Non-AR Mode (Your Current Setup)

Instead of relying on setWorldOrigin, create a parent anchor to act as your custom new origin, then attach your cube to it:

let arView = ARView(frame: .zero, cameraMode: .nonAR)
arView.environment.background = .color(.white)

// Create a parent anchor at your desired "new origin" (1 unit below the default origin)
let newOriginAnchor = AnchorEntity(world: SIMD3(x: 0, y: -1, z: 0))
arView.scene.addAnchor(newOriginAnchor)

// Add your cube to this anchor—now its local (0,0,0) maps to the new origin
let material = SimpleMaterial(color: .gray, isMetallic: false)
let entity = ModelEntity(mesh: .generateBox(size: 0.3), materials: [material])
newOriginAnchor.addChild(entity)

For AR Mode (If You Switch Later)

If you move to AR mode, setWorldOrigin works as intended—just make sure you grasp the transform direction. The code you wrote would work here, since the AR session actively uses the origin shift:

let arView = ARView(frame: .zero, cameraMode: .ar)
arView.environment.background = .color(.white)

var relativeTransform = matrix_identity_float4x4
relativeTransform.columns.3.y = -1
arView.session.setWorldOrigin(relativeTransform: relativeTransform)

let material = SimpleMaterial(color: .gray, isMetallic: false)
let entity = ModelEntity(mesh: .generateBox(size: 0.3), materials: [material])
let anchor = AnchorEntity(world: .zero)
anchor.addChild(entity)
arView.scene.addAnchor(anchor)

Quick Recap

  • setWorldOrigin only affects AR sessions with active tracking (non-AR mode ignores it entirely).
  • The relativeTransform parameter defines the new origin's position relative to the old origin—not the other way around.
  • For non-AR coordinate redefinition, use a parent anchor as your custom origin.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 15:42:34