Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

swift - RealityKit – Adding a Material to a ModelEntity programmatically

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Updated: June 08, 2021.

There are 6 types of materials in RealityKit 2.0 and RealityFoundation at the moment:

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))
*/

And at the moment there are 5 methods in RealityKit 2.0 to create 3D primitives:

  • 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)


How to generate SceneKit's shaders in RealityKit

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.

Let's see how it looks like:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...