I’ve been making an attempt to calculate and visualize frustum in SwiftUI utilizing ARKit for a number of days now, however to no avail. I’ve been utilizing the next code
// This operate handles a faucet gesture to provoke the frustum calculation
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
guard let arView = sender?.view as? ARView,
let tapLocation = sender?.location(in: arView) else {
print("Faucet or ARView not discovered")
return
}
// Get the present digicam place from ARKit
if let currentFrame = arView.session.currentFrame {
calculateFOVAndFrustum(body: currentFrame, remodel: currentFrame.digicam.remodel, arView: arView)
}
}
func calculateFOVAndFrustum(body: ARFrame, remodel: simd_float4x4, arView: ARView) {
let intrinsics = body.digicam.intrinsics
let fx = intrinsics[0, 0]
let fy = intrinsics[1, 1]
let imageWidth = Float(body.digicam.imageResolution.width)
let imageHeight = Float(body.digicam.imageResolution.top)
let aspectRatio = imageWidth / imageHeight
// Calculate FOV
let verticalFOV = 2 * atan(imageHeight / (2 * fy))
let horizontalFOV = 2 * atan(imageWidth / (2 * fx))
let nearDistance: Float = 0.03
let farDistance: Float = 0.1
let nearHeight = 2 * tan(verticalFOV / 2) * nearDistance
let farHeight = 2 * tan(verticalFOV / 2) * farDistance
let nearWidth = nearHeight * aspectRatio
let farWidth = farHeight * aspectRatio
print("Place: (remodel.getPosition())")
print("Rotation: (remodel.getRotation())")
// Get digicam place and orientation from the remodel
let camPos = SIMD3(remodel.columns.3.x,
remodel.columns.3.y,
remodel.columns.3.z)
// Digital camera axes in ARKit (ahead is -Z)
let camForward = -SIMD3(remodel.columns.2.x,
remodel.columns.2.y,
remodel.columns.2.z)
let camUp = SIMD3(remodel.columns.1.x,
remodel.columns.1.y,
remodel.columns.1.z)
let camRight = SIMD3(remodel.columns.0.x,
remodel.columns.0.y,
remodel.columns.0.z)
print("Cam Pos: (camPos)")
print("Cam Ahead: (camForward)")
print("Cam Up: (camUp)")
print("Cam Proper: (camRight)")
// Calculate facilities (in entrance of digicam)
let nearCenter = camPos + camForward * nearDistance
let farCenter = camPos + camForward * farDistance
// Calculate frustum corners
let farTopLeft = farCenter + (camUp * (farHeight * 0.5)) - (camRight * (farWidth * 0.5))
let farTopRight = farCenter + (camUp * (farHeight * 0.5)) + (camRight * (farWidth * 0.5))
let farBottomLeft = farCenter - (camUp * (farHeight * 0.5)) - (camRight * (farWidth * 0.5))
let farBottomRight = farCenter - (camUp * (farHeight * 0.5)) + (camRight * (farWidth * 0.5))
let nearTopLeft = nearCenter + (camUp * (nearHeight * 0.5)) - (camRight * (nearWidth * 0.5))
let nearTopRight = nearCenter + (camUp * (nearHeight * 0.5)) + (camRight * (nearWidth * 0.5))
let nearBottomLeft = nearCenter - (camUp * (nearHeight * 0.5)) - (camRight * (nearWidth * 0.5))
let nearBottomRight = nearCenter - (camUp * (nearHeight * 0.5)) + (camRight * (nearWidth * 0.5))
visualizeFrustumPlanes(
corners: [nearTopLeft, nearTopRight, nearBottomLeft, nearBottomRight,
farTopLeft, farTopRight, farBottomLeft, farBottomRight],
nearCenter: nearCenter,
farCenter: farCenter,
arView: arView
)
}
// This operate creates and visualizes the frustum planes, and likewise the close to and much facilities as spheres within the AR scene
func visualizeFrustumPlanes(corners: [SIMD3], nearCenter: SIMD3, farCenter: SIMD3, arView: ARView) {
// Outline the airplane supplies
let nearPlaneMaterial = SimpleMaterial(shade: .blue, isMetallic: true) // Close to airplane (blue)
let farPlaneMaterial = SimpleMaterial(shade: .crimson, isMetallic: true) // Far airplane (crimson)
let sidePlaneMaterial = SimpleMaterial(shade: .inexperienced, isMetallic: true) // Aspect planes (inexperienced)
// Outline the sphere materials for the facilities
let centerMaterial = SimpleMaterial(shade: .yellow, isMetallic: true) // Yellow for facilities
let centerMaterial2 = SimpleMaterial(shade: .inexperienced, isMetallic: true) // Yellow for facilities
// Create a small sphere mesh for visualization
let sphereMesh = MeshResource.generateSphere(radius: 0.01)
// Create ModelEntities for close to and much facilities (visualize them as spheres)
let nearCenterEntity = ModelEntity(mesh: sphereMesh, supplies: [centerMaterial])
nearCenterEntity.place = nearCenter
let farCenterEntity = ModelEntity(mesh: sphereMesh, supplies: [centerMaterial2])
farCenterEntity.place = farCenter
let nearCenterAnchor = AnchorEntity(world: nearCenter)
nearCenterAnchor.addChild(nearCenterEntity, preservingWorldTransform: true)
let farCenterAnchor = AnchorEntity(world: farCenter)
farCenterAnchor.addChild(farCenterEntity, preservingWorldTransform: true)
arView.scene.addAnchor(nearCenterAnchor)
arView.scene.addAnchor(farCenterAnchor)
}
The issue is that when i attempt to visualize the frustum NEAR and FAR facilities, they seem accurately in middle of what my machine digicam can see when it comes to close to and much viewing distance, BUT after the primary body at any time when i attempt to change my digicam orientation the NEAR and FAR middle factors do get positioned in world area however not within the view of WHAT MY CAMERA IS SEEING.
I might actually respect if any individual may help me with it.
I anticipate the NEAR and FAR facilities of the frustum to look on the earth area however within the view of what my digicam is definitely seeing.