Adding a material to a ModelEntity programmatically

陌路散爱 提交于 2019-12-01 13:02:12

As you said, there are 3 types of materials in RealityKit at the moment: SimpleMaterial, UnlitMaterial and OcclusionMaterial.

So you can try the following code using SimpleMaterial class:

var material = SimpleMaterial()

material.baseColor = try! .texture(.load(named: "image.jpg"))
material.metallic = MaterialScalarParameter(floatLiteral: 0.9)
material.roughness = MaterialScalarParameter(floatLiteral: 0.1)

/*

material.baseColor = MaterialColorParameter.color(UIColor(red: 0.7,
                                                        green: 0.5,
                                                         blue: 0.2,
                                                        alpha: 1.0))

*/

At the moment there are 4 methods in RealityKit to create simple 3D primitives: generateBox(), generateSphere(), generatePlane() and generateText().

let mesh: MeshResource = .generateBox(size: 2.5)
let component = ModelComponent(mesh: mesh, materials: [material])

print("\(component.mesh.bounds)")
print("\(component.materials.count)")

P.S. At the moment I have no opportunity to test this code in iOS 13 but I hope it's OK.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!