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

RealityKit中.generateBox方法无法工作问题求助

问题解决:RealityKit中.generateBox创建的盒子无法显示

问题分析

你用ModelEntity结合.generateBox()创建盒子时画布为空,但其他形状能正常显示,核心原因是你使用的UnlitMaterial默认颜色为黑色,和RealityKit预览画布的黑色背景完全重合,导致盒子实际存在但肉眼无法分辨。

解决方案

方案1:给UnlitMaterial设置可见颜色

修改材质代码,指定一个和背景区分开的颜色,比如红色:

RealityView { content in
    let model = ModelEntity(mesh: .generateBox(size: 2, cornerRadius: 0),
                           materials: [UnlitMaterial(color: .red)])
    content.add(model)
}

方案2:使用默认物理材质

替换为PhysicallyBasedMaterial,它默认带有灰色基础色,能在场景中正常显示:

RealityView { content in
    let model = ModelEntity(mesh: .generateBox(size: 2, cornerRadius: 0),
                           materials: [PhysicallyBasedMaterial()])
    content.add(model)
}

验证说明

修改后运行代码就能看到画布上的盒子。如果还是不显示,可以检查盒子位置(默认在原点,预览相机可覆盖),或调整size参数确保尺寸合适。

内容的提问来源于stack exchange,提问作者Code on the Rocks

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:22:06