Welcome to OGeek Q&A 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 Reply

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...