Created
December 3, 2019 19:43
-
-
Save digitallysavvy/6555fad6936dadad180e87abcc0ccd42 to your computer and use it in GitHub Desktop.
A snippet showing how to add a rootNode for every new touch received within the ARSupportBroadcastViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case "touch-start": | |
print("touch-start msg recieved") | |
// add root node for points received | |
guard let pointOfView = self.sceneView.pointOfView else { return } | |
let transform = pointOfView.transform // transformation matrix | |
let orientation = SCNVector3(-transform.m31, -transform.m32, -transform.m33) // camera rotation | |
let location = SCNVector3(transform.m41, transform.m42, transform.m43) // location of camera frustum | |
let currentPostionOfCamera = orientation + location // center of frustum in world space | |
DispatchQueue.main.async { | |
let touchRootNode : SCNNode = SCNNode() // create an empty node to serve as our root for the incoming points | |
touchRootNode.position = currentPostionOfCamera // place the root node ad the center of the camera's frustum | |
touchRootNode.scale = SCNVector3(1.25, 1.25, 1.25)// touches projected in Z will appear smaller than expected - increase scale of root node to compensate | |
guard let sceneView = self.sceneView else { return } | |
sceneView.scene.rootNode.addChildNode(touchRootNode) // add the root node to the scene | |
let constraint = SCNLookAtConstraint(target: self.sceneView.pointOfView) // force root node to always face the camera | |
constraint.isGimbalLockEnabled = true // enable gimbal locking to avoid issues with rotations from LookAtConstraint | |
touchRootNode.constraints = [constraint] // apply LookAtConstraint | |
self.touchRoots.append(touchRootNode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment