为AR对象而不是在每个帧中创建阴影平面一次
首先,我在Renderer(_:didAdd:for:)
中创建了阴影平面,并且在创建对象时被调用一次。它运行良好,直到您将其移动到周围为止,您将看到阴影计划在移动后的某个时候结束。 因此,我将创建的影子计划功能移至渲染器(_:didupdate:for:)一个并添加新的。它可以正常工作。但是,由于我将从Arview中获得每秒25次快照,因此我认为在每个帧中创建阴影可能会对性能造成伤害。是否可以创建阴影计划并将其绑在对象上?
这是我用于创建影子计划的功能
private func addShadowPlan(node: SCNNode, planeAnchor: ARPlaneAnchor) {
for chidNode in node.childNodes {
if let index = shadowsPlaneHashes.firstIndex(where: {$0 == chidNode.hash}) {
chidNode.removeFromParentNode()
shadowsPlaneHashes.remove(at: index)
}
}
let anchorX = planeAnchor.center.x
let anchorY: Float = 0.0
let anchorZ = planeAnchor.center.z
let floor = SCNFloor()
let floorNode = SCNNode(geometry: floor)
floorNode.position = SCNVector3(anchorX, anchorY, anchorZ)
shadowsPlaneHashes.append(floorNode.hash)
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 = .physicallyBased
material.writesToDepthBuffer = true
material.readsFromDepthBuffer = true
material.colorBufferWriteMask = []
return material
}
first I created shadow Plane in renderer(_:didAdd:for:)
, and it is called once when the object is created. It works well until you move the abject around and you will see the shadows plan will be end in some point after moving.
So I move the creating shadow plan function to renderer(_:didUpdate:for:)
, and now it is creating the the shadow plan in every frame, and each time I makes sure that I removed the last one and add the new one. It works fine. But since I will take snapshot 25 times per sec from the ARView, I think creating shadows in every frame can be harm to the performance. Is it possible to create shadows plan once and tie it to the object?
here are the function that I use for creating shadow plan
private func addShadowPlan(node: SCNNode, planeAnchor: ARPlaneAnchor) {
for chidNode in node.childNodes {
if let index = shadowsPlaneHashes.firstIndex(where: {$0 == chidNode.hash}) {
chidNode.removeFromParentNode()
shadowsPlaneHashes.remove(at: index)
}
}
let anchorX = planeAnchor.center.x
let anchorY: Float = 0.0
let anchorZ = planeAnchor.center.z
let floor = SCNFloor()
let floorNode = SCNNode(geometry: floor)
floorNode.position = SCNVector3(anchorX, anchorY, anchorZ)
shadowsPlaneHashes.append(floorNode.hash)
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 = .physicallyBased
material.writesToDepthBuffer = true
material.readsFromDepthBuffer = true
material.colorBufferWriteMask = []
return material
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论