RealityKit –以编程方式将材料添加到ModelEntity
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?
更新时间:2020年6月23日.
目前RealityKit 2.0中有4种类型的材料:
- SimpleMaterial
- UnlitMaterial
- 遮挡材料
- 视频资料(请查看
- SimpleMaterial
- UnlitMaterial
- OcclusionMaterial
- VideoMaterial (look at this post to find out how to setup it)
因此,您可以将以下代码与
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中只有4种方法可以创建简单的3D图元:
And at the moment there are only 4 methods in RealityKit to create simple 3D primitives:
-
generateBox()
-
generateSphere()
-
generatePlane()
-
generateText()
generateBox()
generateSphere()
generatePlane()
generateText()
所以我们使用其中之一:
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
和UnlitMaterial
生成我们习惯的所有这五个着色器.We know that in SceneKit there are 5 different shading models, so we can use RealityKit's
SimpleMaterial
andUnlitMaterial
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 – SimpleMaterial(color: . gray, roughness: .float(0.0), isMetallic: true) SCNMaterial.LightingModel.constant – UnlitMaterial(color: .gray)