Created
January 20, 2020 14:15
-
-
Save anupamchugh/f395f8978ec1edc1e172cce77d4e2bb6 to your computer and use it in GitHub Desktop.
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
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