问题
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.
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?
回答1:
Updated : 21st October 2019.
As you said, there are 3 types of materials in RealityKit 1.0 at the moment: `
SimpleMaterial
UnlitMaterial
OcclusionMaterial
So you can use the following code with a SimpleMaterial()
class:
@IBOutlet var arView: ARView!
let sceneObjects = try! Experience.loadMy3dObjects()
var simpleMaterial = SimpleMaterial()
simpleMaterial.baseColor = try! MaterialColorParameter.texture(
TextureResource.load(named: "image.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))
*/
At the moment there are 4 methods in RealityKit to create simple 3D primitives:
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)
print("\(component.mesh.bounds)")
print("\(component.materials.count)")
来源:https://stackoverflow.com/questions/56828648/adding-a-material-to-a-modelentity-programmatically