如何在 RealityKit 中以编程方式将材质添加到 ModelEntity?

问题描述 投票:0回答:1

RealityKit
的文档包括结构体:
OcclusionMaterial
SimpleMaterial
UnlitMaterial
,用于将材质添加到
ModelEntity

或者,您可以加载带有附加材质的模型。

我想以编程方式将自定义材质/纹理添加到

ModelEntity
。如何在不将材质添加到 Reality Composer 或其他一些 3D 软件中的模型的情况下即时实现此目标?

swift swiftui arkit realitykit reality-composer
1个回答
13
投票

更新:2024 年 1 月 7 日

RealityKit 材料

目前RealityKit中有八种材质:

SwiftUI 版本

这里我使用了两个 macOS 实现(SwiftUI 和 Cocoa)来演示如何以编程方式分配 RealityKit 材质。

import SwiftUI
import RealityKit

struct VRContainer : NSViewRepresentable {        
    let arView = ARView(frame: .zero)
    let anchor = AnchorEntity()
    
    func makeNSView(context: Context) -> ARView {                    
        var smpl = SimpleMaterial()
        smpl.color.tint = .blue
        smpl.metallic = 0.7
        smpl.roughness = 0.2
                
        var pbr = PhysicallyBasedMaterial()
        pbr.baseColor.tint = .green

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

        let box = ModelEntity(mesh: mesh, materials: [smpl, pbr])    
        box.orientation = Transform(pitch: .pi/4,
                                      yaw: .pi/4, roll: 0.0).rotation
        anchor.addChild(box)
        arView.scene.anchors.append(anchor)
        arView.environment.background = .color(.black)
        return arView
    }
    func updateNSView(_ view: ARView, context: Context) { }
}

struct ContentView: View {
    var body: some View {
        VRContainer().ignoresSafeArea()
    }
}

可可版

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)
    }
}

阅读这篇文章,了解如何为 RealityKit 着色器加载纹理。


RealityKit 着色器与 SceneKit 着色器

我们知道,SceneKit 中有 5 种不同的着色模型,因此我们可以使用 RealityKit 的

SimpleMaterial
PhysicallyBasedMaterial
UnlitMaterial
来生成我们已经习惯的这五种着色器。

让我们看看它是什么样子:

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)
© www.soinside.com 2019 - 2024. All rights reserved.