RealityKit:定向光的阴影不正确

问题描述 投票:0回答:1

我在

RealityKit
DirectionalLights
中遇到阴影问题。

在对我的初始问题进行一些研究后,我下载了一个Xcode项目,它描述了

RealityKit
中的灯光基础知识。这很简单。

该示例项目中使用的示例之一涉及

DirectionalLight
,这对于像太阳这样的照明非常有帮助。

我查看了应用程序中的

DirectionalLight
选项,并在场景中添加了一个额外的球体:

这里是照明:

private func addDirectionalLight() {
        let directionalLight = DirectionalLight()
        directionalLight.light.intensity = 100000
        directionalLight.shadow?.maximumDistance = 5
        directionalLight.shadow?.depthBias = 1
        directionalLight.look(at: [0, 0, 0], from: [-50, 20, 0], relativeTo: nil)
        
        addLight(directionalLight)
    }

我把它的强度设置得相当高,这样你就可以很容易地看到光(白色部分)与阴影(非白色部分)。

然后我就开始了我自己的项目。

我想创建一个定向照明设置来实现类似于太阳的效果。我认为这很容易......我只需要看看示例项目是如何做到的并进行调整。

嗯...我尝试创建自己的照明设置,如下所示:

let directionalLight = DirectionalLight()
        directionalLight.light.intensity = 100000
        directionalLight.shadow?.maximumDistance = 3
        directionalLight.look(at: [0, 0, 0], from: [0, 100, 1200], relativeTo: nil)
        // directionalLight.orientation = simd_quatf(angle: .pi/2, axis: [0, 1, 0])
        
        lightAnchor.addChild(directionalLight)

其中

lightAnchor
是世界 [0, 0, 0] 处的
AnchorEntity

这是应用于对象时的结果:

你注意到什么了吗?我把它圈起来。这就像照明被应用于每个单独的对象,而不考虑另一个对象在那里的事实。

在现实生活中,除了当前存在的阴影之外,立方体顶部的球体还会投射阴影。看第一张图片,假装它是第二张照片中立方体的顶部。是的,球体背面有一个阴影,光线没有照射到的地方,但它所在的平面上也有阴影。

我非常困惑。

我唯一想到的是,第一张图片的比例是照片中心的球体大约 3 米宽……而我的第二张图片的比例要大得多。那个盒子有150米宽。

但是,这应该不会影响照明,对吧?我认为不应该。难道是我的

DirectionalLight
位置不好?考虑到它击中了盒子和球体,我不明白会怎样。

我只是想要像第一张图片中那样的阴影,但由于某种原因我无法实现这一点。

我真的需要一些帮助。如果有人可以的话那就太棒了!

另外,Vision Pro 日快乐! :)

ios xcode swiftui arkit realitykit
1个回答
0
投票

RealityKit 中的阴影设置

使用此代码调整阴影:

import SwiftUI
import RealityKit

struct ContentView : View {
    var body: some View {
        ARViewContainer().ignoresSafeArea()
    }
}

struct ARViewContainer : UIViewRepresentable {

    let arView = ARView(frame: .zero)
    let cube = ModelEntity(mesh: .generateBox(size: 1))
    let ball = ModelEntity(mesh: .generateSphere(radius: 0.3))
    let mat = SimpleMaterial(color: .gray, roughness: 1, isMetallic: false)
    
    init() {
        cube.model?.materials = [mat]
        ball.model?.materials = [mat]
    }        
    func makeUIView(context: Context) -> ARView {
        arView.cameraMode = .nonAR
        ball.position.y = 0.55
        
        let sun = DirectionalLight()
        sun.orientation = simd_quatf(angle: -.pi/4, axis: [1, 1.2, 0])
        sun.light.intensity = 12_000
        sun.shadow = DirectionalLightComponent.Shadow()
        sun.shadow?.maximumDistance = 3
        sun.shadow?.depthBias = 0.5
                
        let anchor = AnchorEntity()
        anchor.orientation = simd_quatf(angle: .pi/3, axis: [1, 0, 0])
        anchor.addChild(sun)
        anchor.addChild(cube)
        anchor.addChild(ball)
        arView.scene.anchors.append(anchor)
        return arView
    }
    func updateUIView(_ view: ARView, context: Context) { }
}
© www.soinside.com 2019 - 2024. All rights reserved.