使用 RealityKit 2.0 将自定义纹理应用于平面实体

Apply a custom texture to Plane entity using RealityKit 2.0

我想在 RealityKit 中将自定义文本(如“网格”)应用到平面实体,但我找不到更新的解决方案。 到目前为止,我已经尝试过这段代码:

func createBoard() {
    let planeMesh = MeshResource.generatePlane(width: 1, 
                                              height: 1, 
                                        cornerRadius: 0.2)
    var myMaterial = SimpleMaterial()
    myMaterial.baseColor = try! .texture(.load(named: "texture.png")) 

    //'baseColor' was deprecated in iOS 15.0: use `color` property instead
    myMaterial.metallic = MaterialScalarParameter(floatLiteral: 0.5)
    myMaterial.roughness = MaterialScalarParameter(floatLiteral: 0.5)
    myMaterial.tintColor = UIColor.blue

    //'tintColor' was deprecated in iOS 15.0: use `color` property instead
    let modelEntity = ModelEntity(mesh: planeMesh, materials: [myMaterial])
}

但是我收到警告说 base color 和 tintcolor 在 iOS 15 上已被弃用,我找不到解决这个问题的方法。

感谢您的帮助。

您必须实现全新的参数而不是弃用的参数:

func createBoard() {
    
    let planeMesh = MeshResource.generatePlane(width: 1,
                                              height: 1,
                                        cornerRadius: 0.05)
    
    var material = SimpleMaterial()
    
    material.color = try! .init(tint: .white,
                             texture: .init(.load(named: "img", in: nil)))
    material.metallic = .init(floatLiteral: 1.0)
    material.roughness = .init(floatLiteral: 0.5)

    let modelEntity = ModelEntity(mesh: planeMesh,
                             materials: [material])
    
    let anchor = AnchorEntity()
    anchor.addChild(modelEntity)
    arView.scene.anchors.append(anchor)
}

此外,您可以使用以下语法:

var material = SimpleMaterial()
material.color.texture = .init(try! .load(named: "img", in: nil))

如果您需要模式详细信息,请阅读 this post