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

visionOS中AnchorEntity存在时Portal无法锚定墙面的问题求助

visionOS 锚定墙面时Portal不显示问题

我遇到了visionOS中Portal锚定墙面的问题:当把Portal作为AnchorEntity的子节点添加时,Portal完全不显示;如果直接将Portal添加到RealityView的content中,虽然能看到Portal,但无法实现墙面锚定的效果。

以下是我的原始代码:

import SwiftUI
import RealityKit
import RealityKitContent

struct PortalView : View {
    var body: some View {
        RealityView { content in
            let anchor = AnchorEntity(.plane(.vertical,
                              classification: .wall,
                               minimumBounds: [1, 1]))

            let world = makeWorld()
            let portal = makePortal(world: world)
            
            anchor.addChild(portal)
            content.add(world)
            content.add(anchor)
            let _ = content.subscribe(to: SceneEvents.AnchoredStateChanged.self,
                                                  on: anchor) { event in
                            print("Anchored State Changed")
                        }
        }
    }

    @MainActor func makeWorld() -> Entity {
        let world = Entity()
        world.components[WorldComponent.self] = .init()

        let environment = try! EnvironmentResource.load(named: "Sunlight")
        world.components[ImageBasedLightComponent.self] = .init(source: .single(environment), intensityExponent: 12)
        world.components[ImageBasedLightReceiverComponent.self] = .init(imageBasedLight: world)

        let earth = try! ModelEntity.load(named: "Earth", in: realityKitContentBundle)
        earth.position = SIMD3<Float>(x: -0.1, y: 0, z: -0.4)
        world.addChild(earth)

        return world
    }

    @MainActor func makePortal(world: Entity) -> Entity {
        let portal = Entity()

        
        portal.orientation = simd_quatf(angle: -.pi/2, axis: [1,0,0]) // rotate CW

        portal.components[ModelComponent.self] = .init(mesh: .generatePlane(width: 1,
                                                                            height: 1,
                                                                            cornerRadius: 0.5),
                                                       materials: [PortalMaterial()])
        portal.components[PortalComponent.self] = .init(target: world)

        return portal
    }
}

#Preview {
    PortalView()
}

当我修改body函数为以下代码时:

var body: some View {
    RealityView { content in
        let anchor = AnchorEntity(.plane(.vertical,
                          classification: .wall,
                           minimumBounds: [1, 1]))

        let world = makeWorld()
        let portal = makePortal(world: world)
        
        //anchor.addChild(portal)
        content.add(world)
        content.add(portal)
        let _ = content.subscribe(to: SceneEvents.AnchoredStateChanged.self,
                                              on: anchor) { event in
                                print("Anchored State Changed")
                            }
    }
}

此时能在屏幕上看到Portal,但它无法锚定到墙面:
未锚定到墙面的Portal

调用代码如下:

import SwiftUI

@main
struct <AppName>: App {

    var body: some Scene {
        WindowGroup {
            PortalView()
        }
    }
}

恳请提供解决方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 22:50:08