Home > Back-end >  RealityKit won't show more than one DirectionalLight
RealityKit won't show more than one DirectionalLight

Time:05-21

I'm trying to create a simple 3D scene in RealityKit with two lights lighting a mesh from opposite sides. Everything seems to be working but both lights won't work at once. If I comment out light01, then light02 shows up fine. Obviously from the type of project, you can tell I'm pretty new at this. What have I missed?

func makeUIView(context: Context) -> ARView {

    //Configure ARView
    let arView = ARView(frame: .zero, cameraMode: .nonAR, 
                   automaticallyConfigureSession: true)
    
    //Set background color
    arView.environment.background = .color(.black)
    
    let light01 = DirectionalLight()
    light01.light.color = .red
    light01.light.intensity = 30000
    light01.light.isRealWorldProxy = true
    light01.shadow?.maximumDistance = 10.0
    light01.shadow?.depthBias = 5.0
    light01.orientation = simd_quatf(angle: -.pi/1.5, axis: [0,1,0])
    let light01Anchor = AnchorEntity(world: [0, 20, 0])
    light01Anchor.addChild(light01)
    arView.scene.addAnchor(light01Anchor)
    
    //NOT WORKING
    let light02 = DirectionalLight()
    light02.light.color = .green
    light02.light.intensity = 20000
    light02.light.isRealWorldProxy = true
    light02.shadow?.maximumDistance = 10.0
    light02.shadow?.depthBias = 5.0
    light02.orientation = simd_quatf(angle: .pi/1.5, axis: [0,1,0])
    let light02Anchor = AnchorEntity(world: [0, 40, 0])
    light02Anchor.addChild(light02)
    arView.scene.addAnchor(light02Anchor)      
    
    //Create plane for floor
    let floorMesh = MeshResource.generatePlane(width: 10, depth: 10)
    let floorMaterial = SimpleMaterial(color: .white, isMetallic: false)
    let floorEntity = ModelEntity(mesh: floorMesh, 
                             materials: [floorMaterial])
    let floorAnchor = AnchorEntity(world: [0, 0, 0])
    floorAnchor.addChild(floorEntity)
    arView.scene.addAnchor(floorAnchor)      

    let sphereMesh = MeshResource.generateSphere(radius: 1.5)
    let sphereMaterial = SimpleMaterial(color: .white, 
                                    roughness: 0.9, 
                                   isMetallic: false)
    let sphereEntity = ModelEntity(mesh: sphereMesh, 
                              materials: [sphereMaterial])
    let sphereAnchor = AnchorEntity(world: [0, 1.5, -4])
    sphereAnchor.addChild(sphereEntity)
    arView.scene.addAnchor(sphereAnchor)
            
    //Camera
    let camera = PerspectiveCamera()
    let cameraAnchor = AnchorEntity(world: [0, 1, 1])
    cameraAnchor.addChild(camera)
    arView.scene.addAnchor(cameraAnchor)
    
    return arView
}

CodePudding user response:

About DirectionalLight in RealityKit 2.0

RealityKit scene can contain up to nine lights. Eight of them could be dynamic lights of different types – PointLights, SpotLights, and a DirectionalLight, and one of them is image-based light (IBL). However, RealityKit supports just ONE DirectionalLight (a.k.a. virtual Sun) per scene.

In addition to the above, it makes sense to note that the position of the DirectionalLight in the RealityKit scene doesn't matter and DirectionalLight has .isRealWorldProxy instance property which, if set to true, cast shadows on virtual content without illuminating anything in the scene. You can use it to create shadows on occlusion materials that accept dynamic lighting.

  • Related