gpt4 book ai didi

ios - ARKit + SceneKit : Using reconstructed scene for physics?

转载 作者:行者123 更新时间:2023-12-05 03:42:22 26 4
gpt4 key购买 nike

我正在将 ARKit 与 SceneKit 结合使用,并想让我的 3D 对象与 reconstructed scene 物理交互由带有 LiDAR 传感器的设备创建 (config.sceneReconstruction = .mesh)。例如,让一个虚拟球从重建场景的几何体中反弹。

在 RealityKit 中,这似乎可以使用 sceneUnderstanding:

arView.environment.sceneUnderstanding.options.insert(.physics)

我怎样才能在使用 SceneKit 时达到同样的目的?

最佳答案

据我所知,没有内置支持使用 SceneKit 启用此功能。但是,您可以使用 ARMeshAnchor 相当轻松地组合自定义解决方案。由 ARKit 创建。

首先,配置 ARKit 以启用场景重建:

let config = ARWorldTrackingConfiguration()
if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
config.sceneReconstruction = .mesh
} else {
// Handle device that doesn't support scene reconstruction
}

// and enable physics visualization for debugging
sceneView.debugOptions = [.showPhysicsShapes]

然后在您的 ARSCNViewDelegate 中使用 renderer nodeFor为新创建的 ARMeshAnchor 实例创建场景包节点:

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
guard let meshAnchor = anchor as? ARMeshAnchor else {
return nil
}

let geometry = createGeometryFromAnchor(meshAnchor: meshAnchor)

// Optionally hide the node from rendering as well
geometry.firstMaterial?.colorBufferWriteMask = []

let node = SCNNode(geometry: geometry)

// Make sure physics apply to the node
// You must used concavePolyhedron here!
node.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: geometry, options: [.type: SCNPhysicsShape.ShapeType.concavePolyhedron]))

return node
}

// Taken from https://developer.apple.com/forums/thread/130599
func createGeometryFromAnchor(meshAnchor: ARMeshAnchor) -> SCNGeometry {
let meshGeometry = meshAnchor.geometry
let vertices = meshGeometry.vertices
let normals = meshGeometry.normals
let faces = meshGeometry.faces

// use the MTL buffer that ARKit gives us
let vertexSource = SCNGeometrySource(buffer: vertices.buffer, vertexFormat: vertices.format, semantic: .vertex, vertexCount: vertices.count, dataOffset: vertices.offset, dataStride: vertices.stride)

let normalsSource = SCNGeometrySource(buffer: normals.buffer, vertexFormat: normals.format, semantic: .normal, vertexCount: normals.count, dataOffset: normals.offset, dataStride: normals.stride)
// Copy bytes as we may use them later
let faceData = Data(bytes: faces.buffer.contents(), count: faces.buffer.length)

// create the geometry element
let geometryElement = SCNGeometryElement(data: faceData, primitiveType: toSCNGeometryPrimitiveType(faces.primitiveType), primitiveCount: faces.count, bytesPerIndex: faces.bytesPerIndex)

return SCNGeometry(sources: [vertexSource, normalsSource], elements: [geometryElement])
}

func toSCNGeometryPrimitiveType(_ ar: ARGeometryPrimitiveType) -> SCNGeometryPrimitiveType {
switch ar {
case .triangle: return .triangles
default: fatalError("Unknown type")
}
}

最后,每当 ARSCNViewDelegate 中重建的几何形状发生变化时更新场景节点 renderer didUpdate:功能:

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard let meshAnchor = anchor as? ARMeshAnchor else {
return
}

let geometry = SCNGeometry.fromAnchor(meshAnchor: meshAnchor, setColors: false)
geometry.firstMaterial?.colorBufferWriteMask = []

node.geometry = geometry

node.physicsBody!.physicsShape = SCNPhysicsShape(geometry: geometry, options: [.type: SCNPhysicsShape.ShapeType.concavePolyhedron])
}

您在 SceneKit 中创建的任何物理对象现在应该能够与重建的场景进行交互:

enter image description here

关于ios - ARKit + SceneKit : Using reconstructed scene for physics?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67306802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com