How to show metallic texture in scene kit

Viewed 377

I'm an ultra-noob in sceneKit and I have a problem with the appearance of (metallic texture) in SceneKit, I used this code to show it.

 let glassesFrame = contentNode!.childNode(withName: "Metallic_Part",
                                                            recursively: true)

        glassesFrame?.geometry?.firstMaterial?.lightingModel = .physicallyBased
         glassesFrame?.geometry?.firstMaterial?.metalness.contents = 0.8
         glassesFrame?.geometry?.firstMaterial?.roughness.contents = 0.2

but the problem is that the values of (metalness, roughness) affects the Darkening of color (the greater the value of the metal, the darker the color).

Here is an example for more clarification:

metalness:0.2 roughness: 0.8 : here is the result

metalness:0.8 roughness: 0.2 : here is the result

and when I set "1" as a value for the metalness the color changed to black, here is the result

and here is the model I used: Model in Sketchfab : https://skfb.ly/6QVTQ

How can I make the metal look natural (mirror like). here is the full code:

class BlendShapeCharacter: NSObject, VirtualContentController {

    var contentNode: SCNNode?

    private var originalJawY: Float = 0

    private lazy var jawNode = contentNode!


    private lazy var jawHeight: Float = {
        let (min, max) = jawNode.boundingBox
        return max.y - min.y
    }()

    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        guard anchor is ARFaceAnchor else { return nil }

        contentNode = SCNReferenceNode(named: "Glasses")
        let glassesFrame = contentNode!.childNode(withName: "Metallic_Part",
                                                            recursively: true)

        glassesFrame?.geometry?.firstMaterial?.lightingModel = .physicallyBased
         glassesFrame?.geometry?.firstMaterial?.metalness.contents = 0.8
         glassesFrame?.geometry?.firstMaterial?.roughness.contents = 0.2


        let lens1 = contentNode!.childNode(withName: "Glass_Part",
                                                     recursively: true)

        let material = SCNMaterial()
        material.lightingModel = .phong
        material.diffuse.contents = UIColor(white: 0.2,
                                            alpha: 1)
        material.diffuse.intensity = 0.9
        material.specular.contents = UIColor(white: 1,
                                             alpha: 1)
        material.specular.intensity = 1.0
        material.reflective.contents = UIImage(named:"Models.scnassets/texture.png")
        material.reflective.intensity = 2.0
        material.transparencyMode = .dualLayer
        material.fresnelExponent = 2.2
        material.isDoubleSided = true
        material.blendMode = .alpha
        material.shininess = 100
        material.transparency.native = 0.4
        material.cullMode = .back

        lens1?.geometry?.firstMaterial = material

        originalJawY = jawNode.position.y
        return contentNode
    }

Thank you in advance

2 Answers

Has the value of environmentTexturing been set to AREnvironmentTexturingManual or AREnvironmentTexturingAutomatic?

In 3D asset rendering, environment textures are the basis for image-based lighting algorithms where surfaces can realistically reflect light from their surroundings. ARKit can generate environment textures during an AR session using camera imagery, allowing SceneKit or a custom rendering engine to provide realistic image-based lighting for virtual objects in your AR experience.

Try setting the scenes lightingEnvironment properties. If you want the reflection to look like the surrounding area you can also use an image for the scenes background contents to provide more realism. Great example found here.

let backgroundImage = UIImage(named: "ImageName")
scene.background.contents = backgroundImage

let environmentImage = UIImage(named: "ImageName")
scene.lightingEnvironment.contents = environmentImage
scene.lightingEnvironment.intensity = 2
Related