ARKit Place a SCNNode facing the camera

前端 未结 6 2070
挽巷
挽巷 2021-02-02 00:15

I\'m using ARKit to display 3D objects. I managed to place the nodes in the real world in front of the user (aka the camera). But I don\'t manage to make them to fa

6条回答
  •  忘了有多久
    2021-02-02 00:55

    here's my code for the SCNNode facing the camera..hope help for someone

        let location = touches.first!.location(in: sceneView)
    
        var hitTestOptions = [SCNHitTestOption: Any]()
        hitTestOptions[SCNHitTestOption.boundingBoxOnly] = true
    
        let hitResultsFeaturePoints: [ARHitTestResult]  = sceneView.hitTest(location, types: .featurePoint)
    
        let hitTestResults = sceneView.hitTest(location)
        guard let node = hitTestResults.first?.node else {
            if let hit = hitResultsFeaturePoints.first {
                let rotate = simd_float4x4(SCNMatrix4MakeRotation(sceneView.session.currentFrame!.camera.eulerAngles.y, 0, 1, 0))
                let finalTransform = simd_mul(hit.worldTransform, rotate)
                sceneView.session.add(anchor: ARAnchor(transform: finalTransform))
            }
            return
        }
    

提交回复
热议问题