Tuesday, June 6, 2023
HomeiOS Developmentios - Unable to render some extent cloud in my SwiftUI app

ios – Unable to render some extent cloud in my SwiftUI app


The undertaking is constructing with none errors or warnings and after I run the app I will see the digicam view. However after I click on on the beginning scanning button the digicam view freezes and nothing occurs. It’s supposed to begin plotting crimson factors within the surroundings after I faucet the beginning scanning button and after I faucet the cease scanning button it ought to direct me to a different display to show the purpose cloud itself. The next are the totally different recordsdata in my undertaking. Please let me know what I am doing incorrectly.

ContentView.swift

import AVFoundation
import ARKit
import RealityKit
import SceneKit
import SwiftUI
import Basis

struct ContentView: View {
    @StateObject var scanner = ScannerViewController()

    var physique: some View {
        NavigationView {
            VStack {
                ARViewContainer().edgesIgnoringSafeArea(.all)

                HStack {
                    Spacer()
                    
                    Button(motion: {
                        scanner.startScanning()
                    }, label: {
                        Textual content("Begin Scanning")
                    })
                    .padding()
                    
                    Spacer()
                    
                    NavigationLink(vacation spot: PointCloudView(pointCloud: scanner.pointCloud, pointSize: 5.0)) {
                        Textual content("Cease Scanning")
                    }
                    .padding()
                    
                    Spacer()
                }
            }
            .navigationBarTitle(Textual content("Scan an Object"))
        }
    }

    struct ARViewContainer: UIViewRepresentable {
        func makeUIView(context: Context) -> ARView {
            let arView = ARView(body: .zero)
            return arView
        }
        
        func updateUIView(_ uiView: ARView, context: Context) {
            
        }
    }
}

PointCloudView.swift

import AVFoundation
import ARKit
import RealityKit
import SceneKit
import SwiftUI
import Basis

struct PointCloudView: View {
    var pointCloud: [SIMD3<Float>]
    var pointSize: CGFloat
    
    var physique: some View {
        ZStack {
            ForEach(pointCloud, id: .self) { level in
                Sphere()
                    .body(width: pointSize, top: pointSize, alignment: .middle)
                    .place(CGPoint(x: CGFloat(level.x), y: CGFloat(level.y)))
                    .foregroundColor(.crimson)
            }
        }
    }
}

struct Sphere: Form {
    func path(in rect: CGRect) -> Path {
        let middle = CGPoint(x: rect.width / 2, y: rect.top / 2)
        let radius = min(rect.width, rect.top) / 2
        let path = Path { p in
            p.addArc(middle: middle, radius: radius, startAngle: .zero, endAngle: .levels(360), clockwise: true)
        }
        return path
    }
}

ScannerViewController.swift

import AVFoundation
import ARKit
import RealityKit
import SceneKit
import SwiftUI
import Mix
import Basis

class ScannerViewController: NSObject, ObservableObject {
    personal var cancellables = Set<AnyCancellable>()

    @Printed var isScanning = false
    @Printed var pointCloud: [SIMD3<Float>] = []

    personal let arSession = ARSession()
    personal var arConfiguration: ARConfiguration {
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = .horizontal
        configuration.environmentTexturing = .computerized
        return configuration
    }

    func startScanning() {
        print("began scanning")
        isScanning = true
        arSession.run(arConfiguration)
    }

    func stopScanning() {
        print("stopped scanning")
        isScanning = false
        arSession.pause()
    }

    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        print("continusoyl referred to as")
        guard isScanning else { return }

        // Get the present ARFrame
        guard let currentFrame = self.arSession.currentFrame else { return }

        // Generate the purpose cloud knowledge from the ARFrame
        guard let pointCloud = currentFrame.rawFeaturePoints?.factors else { return }

        // Convert the purpose cloud knowledge to a format that may be displayed within the PointCloudView
        let convertedPointCloud = pointCloud.map { level in
            SIMD3<Float>(level.x, level.y, level.z)
        }

        // Go the transformed level cloud knowledge to the PointCloudView
        DispatchQueue.primary.async {
            self.pointCloud = convertedPointCloud
        }
    }
}

I’ve tried utilizing each SceneKit and Steel to render the purpose cloud and referred to apple’s documentation: https://developer.apple.com/documentation/arkit/environmental_analysis/displaying_a_point_cloud_using_scene_depth

I’ve additionally tried constructing a storyboard undertaking however storyboard and SceneKit have been creating quite a lot of errors. Utilizing metallic and swiftui builds with out errors on the very least however nonetheless does not perform.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments