Skip to content

Instantly share code, notes, and snippets.

@PaulWall43
Created April 22, 2018 23:03
Show Gist options
  • Select an option

  • Save PaulWall43/78d01cc70ded8082169231fece1a6f33 to your computer and use it in GitHub Desktop.

Select an option

Save PaulWall43/78d01cc70ded8082169231fece1a6f33 to your computer and use it in GitHub Desktop.
//Add the sofa to the plane if possible when the screen is tapped
@IBAction func screenTapped(_ sender: UITapGestureRecognizer) {
//Get the location of the tap on screen
let location = sender.location(in: sceneView)
//Make sure that we do the follow stuff on a background queue as to make sure not to block the main queue
DispatchQueue.global(qos: .background).async {
//Perform a hit test using the location of tap to see if it is on a plane
let results = self.sceneView.hitTest(location, types: ARHitTestResult.ResultType.existingPlane)
//If the hit test is successful then we can add our object to the plane
if let result = results.first {
//Get the xyz of the plane that we found
let x = result.worldTransform.columns.3.x
let y = result.worldTransform.columns.3.y
let z = result.worldTransform.columns.3.z
//Create the scene with our 3D object in it
guard let sofaScene = SCNScene(named: "art.scnassets/sofa-travis.scn") else {return}
//Get the specific node from our scene that we want to add to our own scene graph
guard let sofaNode = sofaScene.rootNode.childNode(withName: "Sofa", recursively: true) else {return}
//Set it's position (this doesn't really matter as of now)
sofaNode.position = SCNVector3(x, y + 0.55, z)
//Give the node something to be identified by in case we need to check for its existence in the future
sofaNode.name = "sofa"
//Add the node to our scene graph
self.sceneView.scene.rootNode.addChildNode(sofaNode)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment