我有 SceneKit 代码,可生成存储为 SCNGeometry 对象的 3D 网格:
geometry = SCNGeometry(sources: [vertexSource, normalSource, texCoordSource], elements: elements)
我正在将我的代码移植到使用RealityKit。令我惊讶的是,我找不到任何类型的适配器或转换器,因此我可以在 RealityKit 中使用 SceneKit 网格;例如,从 SCNGeometry
对象创建
MeshResource,目标是创建 ModelEntity。 Model I/O 框架 没有提到 RealityKit:
“模型 I/O 可以与 MetalKit、GLKit 和 SceneKit 框架可帮助您加载、处理和渲染 3D 资源 高效。”
RealityKit 怎么样?
我也无法找到任何自动化工具将应用程序从 SceneKit 移植到 RealityKit。我必须手动完成。使用最新版本的 RealityKit(来自 WWDC2023),这是我生成四面体视图的代码:
struct Tetrahedron: View {
var tetrahedronEntity = TetrahedronEntity()
var body: some View {
let modelEntity: ModelEntity = tetrahedronEntity.createModelEntity()
RealityView { content in
modelEntity.scale = [ 0.25, 0.25, 0.25 ]
content.add(modelEntity)
}
}
}
final class TetrahedronEntity: Entity {
let vertices: [SIMD3<Float>] = [ // A tetrahedron has 4 vertices.
SIMD3( x: 1.0, y: -1.0, z: 1.0 ), // vertex = 0
SIMD3( x: 1.0, y: 1.0, z: -1.0 ), // vertex = 1
SIMD3( x: -1.0, y: 1.0, z: 1.0 ), // vertex = 2
SIMD3( x: -1.0, y: -1.0, z: -1.0 ) // vertex = 3
]
let counts: [UInt8] = [3,3,3,3] // A tetrahedron has 4 faces, each with 3 corners
let indices: [UInt32] = [
0, 1, 2, // indices to vertices array for face = 0
1, 0, 3, // indices to vertices array for face = 1
2, 3, 0, // indices to vertices array for face = 2
3, 2, 1, // indices to vertices array for face = 3
]
func createModelEntity() -> ModelEntity {
var meshDescriptor = MeshDescriptor()
meshDescriptor.positions = MeshBuffers.Positions(vertices)
meshDescriptor.primitives = .polygons(counts, indices)
let mesh: MeshResource = try! .generate(from: [meshDescriptor])
let material = SimpleMaterial(color: .red, isMetallic: false)
let modelEntity = ModelEntity(mesh: mesh, materials: [material])
return modelEntity
}
}
不幸的是,这个更新版本的 RealityKit 目前仅受 VisionOS 支持,因此您必须在 Vision Pro 模拟器上运行 Xcode 应用程序。