RealityKit – Adding a Material to a ModelEntity programmatically

后端 未结 1 1852
渐次进展
渐次进展 2020-12-17 04:49

The docs for RealityKit include the structs: OcclusionMaterial, SimpleMaterial, and UnlitMaterial for adding materials to a Mode

相关标签:
1条回答
  • 2020-12-17 05:17

    Updated: June 23, 2020.

    There are 4 types of materials in RealityKit 2.0 at the moment:

    • SimpleMaterial
    • UnlitMaterial
    • OcclusionMaterial
    • VideoMaterial (look at this post to find out how to setup it)

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


    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 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  –  SimpleMaterial(color: . gray,
                                                             roughness: .float(0.0),
                                                            isMetallic: true)
    
    SCNMaterial.LightingModel.constant         –  UnlitMaterial(color: .gray)
    
    0 讨论(0)
提交回复
热议问题