Position a SceneKit object in front of SCNCamera's current orientation

后端 未结 5 886
梦如初夏
梦如初夏 2020-12-04 13:10

I would like to create a new SceneKit node when the user taps the screen, and have it appear directly in front of the camera at a set distance. For testing, this will be a S

5条回答
  •  情话喂你
    2020-12-04 13:29

    Making it simple:

    let constraint = SCNTransformConstraint(inWorldSpace: true) { node, transform -> SCNMatrix4 in
        let distance: Float = 1.0
    
        let newNode = SCNNode()
        let worldFront = self.sceneView!.pointOfView!.simdWorldFront * distance
    
        newNode.transform = self.sceneView!.pointOfView!.transform
        newNode.transform = SCNMatrix4Translate(newNode.transform, worldFront.x, worldFront.y, worldFront.z)
    
        return newNode.transform
    }
    

提交回复
热议问题