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

如何在macOS的SceneKit场景中显示WorldOrigin坐标轴?

替代showWorldOrigin在macOS显示世界坐标轴的内置方案

方法1:手动创建坐标轴节点(基于SceneKit内置API)

这是最稳妥的内置方案,利用SCNCylinder和SCNNode直接构建X/Y/Z轴节点,无需外部依赖,代码可完全自定义样式:

func createWorldOriginAxes(length: CGFloat = 1.0, radius: CGFloat = 0.02) -> SCNNode {
    let axesRoot = SCNNode()
    
    // X轴(红色)
    let xAxis = SCNCylinder(radius: radius, height: length)
    xAxis.firstMaterial?.diffuse.contents = NSColor.red
    let xNode = SCNNode(geometry: xAxis)
    xNode.eulerAngles = SCNVector3(0, 0, .pi/2)
    xNode.position = SCNVector3(length/2, 0, 0)
    axesRoot.addChildNode(xNode)
    
    // Y轴(绿色)
    let yAxis = SCNCylinder(radius: radius, height: length)
    yAxis.firstMaterial?.diffuse.contents = NSColor.green
    let yNode = SCNNode(geometry: yAxis)
    yNode.position = SCNVector3(0, length/2, 0)
    axesRoot.addChildNode(yNode)
    
    // Z轴(蓝色)
    let zAxis = SCNCylinder(radius: radius, height: length)
    zAxis.firstMaterial?.diffuse.contents = NSColor.blue
    let zNode = SCNNode(geometry: zAxis)
    zNode.eulerAngles = SCNVector3(.pi/2, 0, 0)
    zNode.position = SCNVector3(0, 0, length/2)
    axesRoot.addChildNode(zNode)
    
    return axesRoot
}

// SwiftUI中使用示例
struct ContentView: View {
    private let scene: SCNScene = {
        let scene = SCNScene()
        // 添加坐标轴到场景根节点
        let axesNode = createWorldOriginAxes()
        scene.rootNode.addChildNode(axesNode)
        // 可添加其他场景内容
        return scene
    }()
    
    var body: some View {
        SceneView(scene: scene, options: [.allowsCameraControl])
    }
}

方法2:私有API调试(仅限开发阶段)

注意:此方法依赖未公开API,无法通过App Store审核,仅适合本地调试使用:

extension SCNView {
    func showWorldOrigin() {
        perform(Selector(("showWorldOrigin")))
    }
}

// 在SceneView中调用
SceneView(scene: scene)
    .onAppear { view in
        (view as? SCNView)?.showWorldOrigin()
    }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:30:29