1

I have found a few great examples how to add a directional light to my code, but not how to change the orientation as well as add it to my scene. How do I do this with my code? Here is my light class:

class Lighting: Entity, HasDirectionalLight {
    required init() {
        super.init()
        self.light = DirectionalLightComponent(color: .white,
                                           intensity: 100000,
                                    isRealWorldProxy: true)
    }
}

And here is the function that calls it:

func addTableToPlane(arView: ARView) {
        let tableAnchor = AnchorEntity(plane: .horizontal)
        let table = try! Entity.load(named: "Table_1500")
        tableAnchor.addChild(table)

        let dirLight = Lighting().light
        let shadow = Lighting().shadow
        tableAnchor.components.set(shadow!)
        tableAnchor.components.set(dirLight)
}

I'm a pretty new to ARKit, so I haven't figured out how to edit the orientation of the directional light as I have it.

Another unsuccessful method that I tried was to create a lighting function, but I haven't been able to figure out how to add it to the scene:

func addLights(arView: ARView) {
    // 1
    let directionalLight = SCNLight()
    directionalLight.type = .directional
    directionalLight.intensity = 500
    // 2
    directionalLight.castsShadow = true
    directionalLight.shadowMode = .deferred
    // 3
    directionalLight.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
    // 4
    let directionalLightNode = SCNNode()
    directionalLightNode.light = directionalLight
    directionalLightNode.rotation = SCNVector4Make(1, 0, 0, -Float.pi / 3)
    sceneView.scene.rootNode.addChildNode(directionalLightNode)
}

I then added addLights(arView: uiView) to the addTableToPlane function. I tried to add the light with:

arView.scene.rootNode.addChildNode(ambientLightNode)

but this gives the error that I don't have a childNode and so on. I guess that I'm spoiled with decent docs for Python that supply examples interspersed to help figure out problems, unlike the overly concise docs for Xcode, such as, what the heck I do with "Use the light’s look(at:from:upVector:relativeTo:) method to aim the light". Where do I put this? Where might I find answers to these simple questions? Chasing my tail for the past couple days just to rotate a light is frustrating.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
visualride
  • 35
  • 6

1 Answers1

2

Use the following code to control orientation of directional light:

Take into consideration that position of Directional Light is not important!

import ARKit
import RealityKit

class Lighting: Entity, HasDirectionalLight, HasAnchoring {

    required init() {
        super.init()

        self.light = DirectionalLightComponent(color: .green,
                                           intensity: 1000,
                                    isRealWorldProxy: true)
    }
}

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated) 

        let light = Lighting()
        light.orientation = simd_quatf(angle: .pi/8,
                                        axis: [0, 1, 0])

        let boxAnchor = try! Experience.loadBox()

        let directLightAnchor = AnchorEntity()
        directLightAnchor.addChild(light)

        boxAnchor.addChild(directLightAnchor)
        boxAnchor.steelBox!.scale = [30,30,30]
        boxAnchor.steelBox!.position.z = -3

        arView.scene.anchors.append(boxAnchor)
    }
}

If you want to know how implement directional light's orientation in SceneKit, read this post.

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220