Search code examples
iosswiftscenekitarkitlighting

How to make natural lighting in ARKit?


I want lighting of added projects in my ARKit project to be similar to real-world objects. Please explain how to achieve this? Thank you


Solution

  • You can add lighting to an SCNMaterial by choosing from one of the lightingModel parameters e.g:

    enter image description here

    To add one of these to an SCNMaterial all you need to do is the following:

    material.lightingModel = .constant 
    

    You can also make objects appear more realistic by making use of the following variable of an SCNView:

    var autoenablesDefaultLighting: Bool { get set }
    

    autoEnablesDefaultLighting is simply a Boolean value that determines whether SceneKit automatically adds lights to a scene or not.

    By default this is set as false meaning that:

    the only light sources SceneKit uses for rendering a scene are those contained in the scene graph.

    If on the other hand, this is set to true:

    SceneKit automatically adds and places an omnidirectional light source when rendering scenes that contain no lights or only contain ambient lights.

    To apply this setting to an SCNView therefore, all you need to do is use the following:

    augmentedRealityScene.autoenablesDefaultLighting = true
    

    In addition to these suggestions, you can also create different types of lights to add to your scene e.g:

    enter image description here

    func createDirectionalLight(){
    
            let spotLight = SCNNode()
            spotLight.light = SCNLight()
            spotLight.scale = SCNVector3(1,1,1)
            spotLight.light?.intensity = 1000
            spotLight.castsShadow = true
            spotLight.position = SCNVector3Zero
            spotLight.light?.type = SCNLight.LightType.directional
            spotLight.light?.color = UIColor.white
    }
    

    Hope this helps...