【问题标题】:Apply a custom texture to Plane entity using RealityKit 2.0使用 RealityKit 2.0 将自定义纹理应用于平面实体
【发布时间】:2021-12-05 13:44:23
【问题描述】:

我想将“网格”之类的自定义文本应用于 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])
}

但我收到警告说基本颜色和色调颜色在 iOS 15 上已弃用,我找不到解决此问题的方法。

感谢您的帮助。

【问题讨论】:

    标签: swift augmented-reality arkit realitykit


    【解决方案1】:

    您必须实现全新的参数而不是不推荐使用的参数:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-06
      • 2016-04-28
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多