Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created January 20, 2020 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupamchugh/f395f8978ec1edc1e172cce77d4e2bb6 to your computer and use it in GitHub Desktop.
Save anupamchugh/f395f8978ec1edc1e172cce77d4e2bb6 to your computer and use it in GitHub Desktop.
extension ARView{
func setupGestures() {
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
self.addGestureRecognizer(tap)
}
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
guard let touchInView = sender?.location(in: self) else {
return
}
rayCastingMethod(point: touchInView)
}
func rayCastingMethod(point: CGPoint) {
guard let raycastQuery = self.makeRaycastQuery(from: point,
allowing: .existingPlaneInfinite,
alignment: .horizontal) else {
return
}
guard let result = self.session.raycast(raycastQuery).first else {return}
let transformation = Transform(matrix: result.worldTransform)
if GlobalVariable.isBox{
let box = CustomEntityA(color: .yellow)
self.installGestures(.all, for: box)
box.addCollisions(scene: self.scene)
box.transform = transformation
let raycastAnchor = AnchorEntity(raycastResult: result)
raycastAnchor.addChild(box)
self.scene.addAnchor(raycastAnchor)
}
else{
let sphere = CustomEntityB(color: .yellow)
self.installGestures(.all, for: sphere)
sphere.addCollisions(scene: self.scene)
sphere.transform = transformation
let raycastAnchor = AnchorEntity(raycastResult: result)
raycastAnchor.addChild(sphere)
self.scene.addAnchor(raycastAnchor)
}
GlobalVariable.isBox = !GlobalVariable.isBox
}
}
struct GlobalVariable {
static var isBox = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment