Skip to content

Instantly share code, notes, and snippets.

Andreas Jakl andijakl

Block or report user

Report or block andijakl

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@andijakl
andijakl / AnchorPositioning.js
Last active Aug 30, 2019
Complete AnchorPositioning.js script that creates an AR anchor in Amazon Sumerian, based on the upcoming scripting API.
View AnchorPositioning.js
// Import Sumerian scripts
import * as s from 'module://sumerian-common/api';
import * as si from 'module://sumerian-common/internal';
export default class AnchorPositioning extends s.Action {
static get PROPERTIES() {
return {
// Optional property to set the owner of the AR anchor
anchorEntity: {
@andijakl
andijakl / AnchorPositioning.js
Created Aug 8, 2019
Subscribing to the event StartPlaceMode to enable the Augmented Reality placement mode functionality.
View AnchorPositioning.js
// Listen for the world event "StartPlaceWorld". This will be sent
// by another component in the scene when the placement mode should
// be active. When that event is received, this script sets placeMode to true,
// which actually allows this script to react to touch events.
this.startPlaceWorldEvent = ctx.world.event('StartPlaceWorld');
this.startPlaceWorldEvent.listen(ctx, data => this.placeMode.set(true));
@andijakl
andijakl / AnchorPositioning.js
Last active Aug 8, 2019
Call-back when the AR system of the device (ARCore / ARKit) sends the anchor ID to our scene, so that we can link it to the entity.
View AnchorPositioning.js
// Anchor registration callback. Sets the anchor ID of the entity's
// ArAnchorComponent. The engine's ArSystem will automatically update
// the world position and orientation of entities with a valid anchor ID.
registerAnchorCallback(anchorId) {
if (anchorId) {
this.internalAnchorEntity.getComponent('ArAnchorComponent').anchorId = anchorId;
console.log("Registered AR anchor: " + anchorId);
this.placeMode.set(false);
this.worldPlacedEvent.emit();
console.log("Quit place mode and sent WorldPlaced event");
@andijakl
andijakl / AnchorPositioning.js
Last active Aug 8, 2019
Call-back from the hit test: register an AR anchor at this position through Amazon Sumerian's ArSystem.
View AnchorPositioning.js
// Hit test callback. If the hit test was successful (i.e., detected a
// point in the real world), registers an anchor with that point.
hitTestCallback(anchorTransform) {
if (anchorTransform) {
console.log("Transform from hit test: " + anchorTransform + ". Registering anchor...");
this.arSystem.registerAnchor(anchorTransform, (anchorId) => this.registerAnchorCallback(anchorId));
} else {
console.log("No transform received from hit test");
}
}
@andijakl
andijakl / AnchorPositioning.js
Created Aug 8, 2019
Remove the event listener for the touch event in Amazon Sumerian.
View AnchorPositioning.js
// Cleanup. When this script stops, also remove the event listeners.
// This is especially important here, as Sumerian doesn't always reliably
// clean up all the runtime-registrations when you repeatedly start the scene.
ctx.onStop(
() => {
console.log("Removing touch event listener.");
this.internalWorld.sumerianRunner.renderer.domElement.removeEventListener('touchend', performHitTestCallback);
// For testing on the PC with a mouse
//this.internalWorld.sumerianRunner.renderer.domElement.removeEventListener('mouseup', performHitTestCallback);
}
@andijakl
andijakl / AnchorPositioning.js
Created Aug 8, 2019
Adding the touchend event listener through the HTML DOM within Amazon Sumerian.
View AnchorPositioning.js
// Add an event listener for touch events anywhere on the screen.
// Note: the MouseUpAction of Sumerian doesn't provide the coordinates
// -> use the HTML DOM event directly. This is accessible through
// the Renderer.
this.internalWorld.sumerianRunner.renderer.domElement.addEventListener('touchend', performHitTestCallback);
// Phones use touch events. To also develop / test on a PC (or on
// a mobile device with a mouse attached), you can also register for
// mouse events.
//this.internalWorld.sumerianRunner.renderer.domElement.addEventListener('mouseup', performHitTestCallback);
@andijakl
andijakl / AnchorPositioning.js
Created Aug 8, 2019
Registering the hit test call back. Stores it in a variable to allow removing the event listener later, and using an arrow function to preserver this.
View AnchorPositioning.js
// Listeners and Callbacks
// -------------------------------------------------------------------
// First, create a variable that stores an arrow function to the
// performHitTest method of this class. This method takes care of
// starting the hit test in the native AR system.
// We need this as an arrow function to have access to "this" in
// the class scope. Creating this function in a variable allows
// to remove the event listener once the scene closes.
var performHitTestCallback = (evt) => this.performHitTest(evt);
@andijakl
andijakl / AnchorPositioning.js
Created Aug 8, 2019
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).
View AnchorPositioning.js
// 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;
@andijakl
andijakl / AnchorPositioning.js
Created Jul 29, 2019
Get a reference to the Amazon Sumerian engine internal version of the World.
View AnchorPositioning.js
// World reference
// -------------------------------------------------------------------
// The world is essentially the loaded scene. It allows interacting
// with the scene contents and reading / setting several properties
// of it.
const publicWorld = this.anchorEntity.world;
// Same as before, we actually need the more powerful internal world
// class, not the "normal" one.
this.internalWorld = si.World.forPublicWorld(publicWorld);
@andijakl
andijakl / AnchorPositioning.js
Created Jul 29, 2019
Attach the AR Anchor component to the entity in Amazon Sumerian
View AnchorPositioning.js
// Attach AR anchor component to entity
// -------------------------------------------------------------------
// First, create the anchor component that is actually attached
// to the entity.
const arAnchorComponent = new si.ArAnchorComponent();
// To work with this component, we also need the internal entity
// that extends the 'normal' entity and provides additional functionality.
this.internalAnchorEntity = si.Entity.forPublicEntity(this.anchorEntity);
// The internal entity allows us to add an additional component
// to its definition - in this case, the ArAnchorComponent we
You can’t perform that action at this time.