将scnfloor与AR对象联系

发布于 2025-02-08 08:11:49 字数 2044 浏览 1 评论 0原文

我在场景视图中添加了scnfloor以显示阴影,并且有效,但是有两个问题

1-当我添加第一个对象时,地板似乎下方是对象的下方,它显示了阴影,但是如果我在现实世界距离内0.5 m之后移动对象,则地板似乎会在某个时候结束,并且在那一点之后没有阴影。该地板不会用对象

2-当我添加第二个对象时,它在其下方没有地板,而阴影仅显示,然后我将第二个对象移动到第一个对象周围,然后阴影出现。

首先在视图中加载了加载

    let light = SCNLight()
    light.type = .directional
    light.shadowColor =  UIColor(white: 0, alpha: 0.5)
    light.color = UIColor.white
    light.castsShadow = true
    light.automaticallyAdjustsShadowProjection = true
    light.shadowMode = .deferred
    let sunLightNode = SCNNode()
    sunLightNode.position = SCNVector3(x: 1_000, y: 1_000, z: 0)
    sunLightNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: .pi * 1.5)
    sunLightNode.light = light
    sceneView.scene.rootNode.addChildNode(sunLightNode)

和材料的功能

private func addFloorSCN(node: SCNNode, planeAnchor: ARPlaneAnchor) {
    let anchorX = planeAnchor.center.x
    let anchorY = planeAnchor.center.y
    let anchorZ = planeAnchor.center.z
    
    let floor = SCNFloor()
    let floorNode = SCNNode(geometry: floor)
    floorNode.position = SCNVector3(anchorX, anchorY, anchorZ)


    
    floor.length = CGFloat(planeAnchor.extent.z)
    floor.width = CGFloat(planeAnchor.extent.x)
    floor.reflectivity = 0
    floor.materials = [shadowMaterialStandard()]
    node.addChildNode(floorNode)
}


func shadowMaterialStandard() -> SCNMaterial {
    let material = SCNMaterial()
    material.lightingModel = .constant
    material.writesToDepthBuffer = true
    material.readsFromDepthBuffer = true
    material.colorBufferWriteMask = []
    return material
 }

,最后,我在renderer(_:didAdd:for:for:)中调用了addfloorscn

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard anchor is ARPlaneAnchor else { return }
    guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
    addFloorSCN(node: node, planeAnchor: planeAnchor)
}

,然后,在这里,地板 这里的问题,有人可以帮我吗?太感谢了

i added SCNFloor to the scene view to show a shadows, and it works, but there are two issues

1- When I add the first object, the floor seems below the object and it shows the shadows, but if I move the object, probably after 0.5 m in the real world distance, seems the floor will end at some point and there is no shadows after that point. That floor is not moving with the object

2- When I add a second object, there is no floor below it and the shadows only shows then I move the second object around the first object and then shadows appears.

First in the view did load

    let light = SCNLight()
    light.type = .directional
    light.shadowColor =  UIColor(white: 0, alpha: 0.5)
    light.color = UIColor.white
    light.castsShadow = true
    light.automaticallyAdjustsShadowProjection = true
    light.shadowMode = .deferred
    let sunLightNode = SCNNode()
    sunLightNode.position = SCNVector3(x: 1_000, y: 1_000, z: 0)
    sunLightNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: .pi * 1.5)
    sunLightNode.light = light
    sceneView.scene.rootNode.addChildNode(sunLightNode)

And then, here the functions for floor and material

private func addFloorSCN(node: SCNNode, planeAnchor: ARPlaneAnchor) {
    let anchorX = planeAnchor.center.x
    let anchorY = planeAnchor.center.y
    let anchorZ = planeAnchor.center.z
    
    let floor = SCNFloor()
    let floorNode = SCNNode(geometry: floor)
    floorNode.position = SCNVector3(anchorX, anchorY, anchorZ)


    
    floor.length = CGFloat(planeAnchor.extent.z)
    floor.width = CGFloat(planeAnchor.extent.x)
    floor.reflectivity = 0
    floor.materials = [shadowMaterialStandard()]
    node.addChildNode(floorNode)
}


func shadowMaterialStandard() -> SCNMaterial {
    let material = SCNMaterial()
    material.lightingModel = .constant
    material.writesToDepthBuffer = true
    material.readsFromDepthBuffer = true
    material.colorBufferWriteMask = []
    return material
 }

And Finally, I called the addFloorSCN in the renderer(_:didAdd:for:)

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard anchor is ARPlaneAnchor else { return }
    guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
    addFloorSCN(node: node, planeAnchor: planeAnchor)
}

What is the issue here, could anyone help me on that please? Thank you so much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何处潇湘 2025-02-15 08:11:49

我已经使用RealityKit更新了我的项目,您可以尝试一下,谢谢。
https://github.com/frank.com/frank-com/frank-1992/realitykit_arql

I have updated my project with RealityKit, you can have a try, thank you.
https://github.com/frank-1992/RealityKit_ARQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文