RealityKit
的文档包括以下结构:OcclusionMaterial
、SimpleMaterial
和 UnlitMaterial
,用于将 Material 添加到模型实体
。
或者,您可以加载附加 Material 的模型。
我想以编程方式将自定义 Material /纹理 添加到ModelEntity
。如果不将 Material 添加到 Reality Composer 或其他一些 3D 软件 中的模型,我如何才能即时实现这一点?
最佳答案
更新时间:2022 年 6 月 14 日
RealityKit Material
RealityKit 2.0 和 RealityFoundation 目前有 6 种 Material :
- SimpleMaterial
- UnlitMaterial
- OcclusionMaterial (阅读这篇文章以了解如何设置 SceneKit occlusion shader )
- VideoMaterial (查看 this post 了解如何设置)
- PhysicallyBasedMaterial
- CustomMaterial
要应用这些 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)
}
}
阅读this post了解如何为 RealityKit 的着色器加载纹理。
如何创建类似于 SceneKit 着色器的 RealityKit 着色器
我们知道在 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)
关于swift - 如何在 RealityKit 中以编程方式将 Material 添加到 ModelEntity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56828648/