swift - 如何在 RealityKit 中以编程方式将 Material 添加到 ModelEntity?

标签 swift augmented-reality arkit realitykit reality-composer

RealityKit 的文档包括以下结构:OcclusionMaterialSimpleMaterialUnlitMaterial,用于将 Material 添加到模型实体

或者,您可以加载附加 Material 的模型。

我想以编程方式将自定义 Material /纹理 添加到ModelEntity。如果不将 Material 添加到 Reality Composer 或其他一些 3D 软件 中的模型,我如何才能即时实现这一点?

最佳答案

更新时间:2022 年 6 月 14 日

RealityKit Material

RealityKit 2.0RealityFoundation 目前有 6 种 Material :

要应用这些 Material ,请使用以下逻辑:

import Cocoa
import RealityKit

class ViewController: NSViewController {        
    @IBOutlet var arView: ARView!
    
    override func awakeFromNib() {
        let box = try! Experience.loadBox()
        
        var simpleMat = SimpleMaterial()
        simpleMat.color = .init(tint: .blue, texture: nil)
        simpleMat.metallic = .init(floatLiteral: 0.7)
        simpleMat.roughness = .init(floatLiteral: 0.2)
        
        var pbr = PhysicallyBasedMaterial()
        pbr.baseColor = .init(tint: .green, texture: nil) 

        let mesh: MeshResource = .generateBox(width: 0.5, 
                                             height: 0.5, 
                                              depth: 0.5, 
                                       cornerRadius: 0.02, 
                                         splitFaces: true)

        let boxComponent = ModelComponent(mesh: mesh,
                                     materials: [simpleMat, pbr])

        box.steelBox?.children[0].components.set(boxComponent)
        box.steelBox?.orientation = Transform(pitch: .pi/4, 
                                                yaw: .pi/4, 
                                               roll: 0).rotation
        arView.scene.anchors.append(box)
    }
}

enter image description here

阅读this post了解如何为 RealityKit 的着色器加载纹理。


如何创建类似于 SceneKit 着色器的 RealityKit 着色器

我们知道在 SceneKit 中有 5 种不同的着色模型,所以我们可以使用 RealityKit 的 SimpleMaterialPhysicallyBasedMaterialUnlitMaterial 来生成所有这些我们已经习惯的五个着色器。

让我们看看它的样子:

SCNMaterial.LightingModel.blinn           – SimpleMaterial(color: . gray,
                                                        roughness: .float(0.5),
                                                       isMetallic: false)

SCNMaterial.LightingModel.lambert         – SimpleMaterial(color: . gray,
                                                        roughness: .float(1.0),
                                                       isMetallic: false)
  
SCNMaterial.LightingModel.phong           – SimpleMaterial(color: . gray,
                                                        roughness: .float(0.0),
                                                       isMetallic: false)

SCNMaterial.LightingModel.physicallyBased – PhysicallyBasedMaterial()


// all three shaders (`.constant`, `UnlitMaterial` and `VideoMaterial `) 
// don't depend on lighting
SCNMaterial.LightingModel.constant        – UnlitMaterial(color: .gray)
                                          – VideoMaterial(avPlayer: avPlayer)

关于swift - 如何在 RealityKit 中以编程方式将 Material 添加到 ModelEntity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56828648/

相关文章:

ios - 无法添加 UIImageView Xcode Swift

arrays - 从数组中检索每个值并将它们相加,代码不能遍历整个数组?

android - Google Sceneform SDK 到底是什么?

ios - 定义 SCNNode 的位置

ios - 如何提高 ARKit 测量两个 SCNNode 之间距离的准确性?

xcode - 在 map 上选择一个位置会给出错误的坐标

ios - NavigationView 和 NavigationLink 在转换后不删除 View

iphone - iPhone 中的增强现实可以显示 3d 模型

swift - 如何提高 ARKit 中的相机质量

ios - 如何将 ARAnchor 大小转换为 SpriteKit 等效大小