简体   繁体   中英

SceneKit. Place nodes in one "surface"

The users draw a line on drawing views and then I need to translate these points into 3d world, but place these points in one "surface". For this, I map the array of points into vectors (I use hitTest with.featurePoint) and then filter this array for the further one

func didEndDrawing(with points: [CGPoint]) {

    guard let transform = sceneView.pointOfView?.transform else { return }
    let cameraVectror = SCNVector3(transform.m31, transform.m32, transform.m33)

    let farthestVector = points
        .reduce((vector: SCNVector3(0, 0, 0), distance: CGFloat.zero)) { result, point in
                    
        guard let vector = getVector(for: point) else { return result }
        let distance: CGFloat = cameraVectror.distance(to: vector)
        return  distance > result.distance ? (vector, distance) : result
    }
        .vector
}
let parentNode = SCNNode()
parentNode.position = farthestVector

How can I adjust coordinates (I guess z position) to have all the child nodes at the same distance from the point of view? The idea of the app is freehand drawing in AR.

Update

With Voltan's help I was able to solve it


points.forEach { point in
    let scenePoint = sceneView.unprojectPoint(SCNVector3(point.x, point.y, CGFloat(projectedPoint.z)))
    let sphere = SCNSphere(radius: 0.01)
    let material = SCNMaterial()
    material.diffuse.contents = UIColor.green
    sphere.materials = [material]
    let node = SCNNode(geometry: sphere)
    node.position = scenePoint
    sceneView.scene.rootNode.addChildNode(node)
}

If I'm understanding correctly, you want some kind of tap/drag combination - get the points from the 2D world and translate to a 3D world. This is some game code for a missile command type game, maybe it will help you with unprojectPoint stuff. There are some timers that aren't included, but hopefully you will get the idea.

@objc func handleTap(recognizer: UITapGestureRecognizer)
    {
        if(data.gameState == .endGame)
        {
            endGameAnimates.stop()
            let _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: false, block: { _ in self.dismiss(animated: false, completion: nil) })
            
            return
        }
        
        if(gameControl.isWaveComplete == true || gNodes.gameNodes.isPaused == true) { return }
        
        currentLocation = recognizer.location(in: gameScene)
        let projectedPoint = gameScene.projectPoint(SCNVector3(0, 0, 0))
        let scenePoint = gameScene.unprojectPoint(SCNVector3(currentLocation.x, currentLocation.y, CGFloat(projectedPoint.z)))
        
        if(data.gameState == .endGame) // Allow animations to finish, otherwise they will show up next round
        {
            DispatchQueue.main.async { self.endGameAnimates.stop() }
            let _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: false, block: { _ in self.dismiss(animated: false, completion: nil) })
            return
        }
        
        if(data.missilesAvailable <= 0)
        {
            sound.playSoundType(vSoundType: .defenseFails)
            hudControl.notify()
        }
        else
        {
            gameControl.defenseMissileShoot(vPosition: scenePoint, soundType: 0)
            sound.playSoundType(vSoundType: .defenseFires)
        }
    }
    //**************************************************************************
    @objc func handlePan(recognizer: UIPanGestureRecognizer)
    {
        currentLocation = recognizer.location(in: gameScene)
        let projectedPoint = gameScene.projectPoint(SCNVector3(0, 0, 0))
        let scenePoint = gameScene.unprojectPoint(SCNVector3(currentLocation.x, currentLocation.y, CGFloat(projectedPoint.z)))
        
        if(gameControl.isWaveComplete == true || gNodes.gameNodes.isPaused == true) { return }
        
        switch recognizer.state
        {
        case UIGestureRecognizer.State.began:
            gameControl.defenseMissileShoot(vPosition: scenePoint, soundType: 1)
            SNDdefenseSoundCount = 0
            if(data.missilesAvailable <= 0) { sound.playSoundType(vSoundType: .defenseFails); hudControl.notify() }
            beginLocation.x = currentLocation.x
            break
        case UIGestureRecognizer.State.changed:
            if(currentLocation.x > beginLocation.x + dragDistance) 
            {
                beginLocation.x = currentLocation.x
                if(data.missilesAvailable > 0) { gameControl.defenseMissileShoot(vPosition: scenePoint, soundType: 2) }
                SNDdefenseSoundCount += 1
            }
            if(currentLocation.x < beginLocation.x - dragDistance)
            {
                beginLocation.x = currentLocation.x
                if(data.missilesAvailable > 0) { gameControl.defenseMissileShoot(vPosition: scenePoint, soundType: 2) }
                SNDdefenseSoundCount += 1
            }
            break
        case UIGestureRecognizer.State.ended:
            if(data.missilesAvailable > 0)
            {
                if(SNDdefenseSoundCount < 2) { sound.playSoundType(vSoundType: .defenseFires) }
                else                         { sound.playSoundType(vSoundType: .defensePans)  }
            }
            break
        default:
            break
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM