Skip to content

Instantly share code, notes, and snippets.

@andijakl
Created August 8, 2019 09:29
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 andijakl/32d0af94a0af7ab11fbdc0e3cfb9808e to your computer and use it in GitHub Desktop.
Save andijakl/32d0af94a0af7ab11fbdc0e3cfb9808e to your computer and use it in GitHub Desktop.
Callback after the user taps the screen. Performs a hit test using Amazon Sumerian's ArSystem, which forwards the request to the device-native AR subsystem (ARCore / ARKit).
// Touch handler.
// As the first step, performs a hit test at the currrent screen location.
performHitTest(evt) {
// Check if this script is currently in an active place mode.
// That is triggered when another component in the scene sent the
// StartPlaceWorld event.
if (!this.placeMode.get()) {
// Currently not place mode - don't proceed!
console.log("Not in place mode - not proceeding");
return;
}
// Optional debug info
console.log("Perform hit test: " + JSON.stringify(evt));
// The pixel ratio and the viewport size is needed to convert from
// the coordinates of the client area (the window) to the 3D scene coordinates.
var pixelRatio = this.internalWorld.sumerianRunner.renderer.devicePixelRatio;
// Get the touch coordinates and multiply them with the pixel ratio.
// On a phone, you'd normally get the coordinates through changedTouches.
// Especially for testing on a browser and also for improved compatibility,
// we also check for mouse coordinates as well.
var touchX;
var touchY;
if (evt.changedTouches) {
// Touch screen
touchX = evt.changedTouches[0].pageX * pixelRatio;
touchY = evt.changedTouches[0].pageY * pixelRatio;
} else {
// Mouse
touchX = evt.clientX * pixelRatio;
touchY = evt.clientY * pixelRatio;
}
// Perform the coordinate normalization using the viewport width and height
var normalizedX = touchX / this.internalWorld.sumerianRunner.renderer.viewportWidth;
var normalizedY = touchY / this.internalWorld.sumerianRunner.renderer.viewportHeight;
// Check again if the AR system is available
if (this.arSystem) {
console.log("AR System found: calling hit test callback...")
// Perform hit test and then execute the hitTestCallback with the transform (position)
// of the anchor that we will place.
this.arSystem.hitTest(normalizedX, normalizedY, (anchorTransform) => this.hitTestCallback(anchorTransform));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment