RealityKit – 以编程方式向 ModelEntity 添加材质 [英] RealityKit – Adding a Material to a ModelEntity programmatically
问题描述
RealityKit 的文档包括结构体:OcclusionMaterial
、SimpleMaterial
和 UnlitMaterial
,用于将材料添加到 ModelEntity
>.
The docs for RealityKit include the structs: OcclusionMaterial
, SimpleMaterial
, and UnlitMaterial
for adding materials to a ModelEntity
.
或者,您可以加载附加了材料的模型.
Alternatively you can load in a model with a material attached to it.
我想以编程方式向 ModelEntity
添加自定义材质/纹理.如何在不将材质添加到 Reality Composer 或其他一些 3D 软件中的模型的情况下即时实现这一目标?
I want to add a custom material/texture to a ModelEntity
programmatically. How can I achieve this on the fly without adding the material to a model in Reality Composer or some other 3D software?
推荐答案
更新时间:2021 年 6 月 8 日.
目前 RealityKit 2.0 和 RealityFoundation 中有 6 种材质:
- 简单材料
- UnlitMaterial
- 遮挡材料
- VideoMaterial(查看 这篇文章 了解如何设置)
- PhysicallyBasedMaterial
- CustomMaterial
- SimpleMaterial
- UnlitMaterial
- OcclusionMaterial
- VideoMaterial (look at this post to find out how to setup it)
- PhysicallyBasedMaterial
- CustomMaterial
因此您可以将以下代码与 SimpleMaterial()
类一起使用:
So you can use the following code with a SimpleMaterial()
class:
@IBOutlet var arView: ARView!
let sceneObjects = try! Experience.loadAllMyObjects()
var simpleMaterial = SimpleMaterial()
simpleMaterial.baseColor = try! MaterialColorParameter.texture(
TextureResource.load(named: "img.jpg"))
simpleMaterial.metallic = MaterialScalarParameter(floatLiteral: 0.9)
simpleMaterial.roughness = MaterialScalarParameter(floatLiteral: 0.1)
simpleMaterial.tintColor = UIColor.yellow
/*
simpleMaterial.baseColor = MaterialColorParameter.color(UIColor(red: 0.7,
green: 0.5,
blue: 0.2,
alpha: 1.0))
*/
目前在 RealityKit 2.0 中有 5 种方法来创建 3D 基元:
And at the moment there are 5 methods in RealityKit 2.0 to create 3D primitives:
generateBox(...)
generateSphere(...)
generatePlane(...)
generateText(...)
generate(...)
– 从网格描述符列表中创建网格资源
generateBox(...)
generateSphere(...)
generatePlane(...)
generateText(...)
generate(...)
– Create a mesh resource from a list of mesh descriptors
所以让我们使用其中之一:
So let's use one of them:
let mesh: MeshResource = .generateBox(size: 2.5)
let component = ModelComponent(mesh: mesh,
materials: [simpleMaterial])
sceneObjects.components.set(component)
arView.scene.anchors.append(sceneObjects)
我们知道SceneKit中有5种不同的着色模型,所以我们可以使用RealityKit的SimpleMaterial
、PhysicallyBasedMaterial
和UnlitMaterial
来生成所有这些五个我们已经习惯的着色器.
We know that in SceneKit there are 5 different shading models, so we can use RealityKit's SimpleMaterial
, PhysicallyBasedMaterial
and UnlitMaterial
to generate all these five shaders that we've been accustomed to.
让我们看看它的样子:
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()
SCNMaterial.LightingModel.constant – UnlitMaterial(color: .gray)
这篇关于RealityKit – 以编程方式向 ModelEntity 添加材质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!