将scnfloor与AR对象联系
我在场景视图中添加了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经使用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