Lens Scripting API

    Class WorldQueryModuleWearable Only

    Provides access to various APIs which can perform hit test for real surfaces to sample the depth and normal at a certain location.

    // import required modules
    const WorldQueryModule = require("LensStudio:WorldQueryModule")
    const EPSILON = 0.01;
    const RAY_LENGTH = 1000;

    @component
    export class NewScript extends BaseScriptComponent {

    private hitTestSession;
    private transform: Transform;
    private cameraTransform: Transform;

    @input
    targetObject: SceneObject;

    @input
    camera: Camera;

    @input
    filterEnabled: boolean;

    onAwake() {
    // create new hit session
    this.hitTestSession = this.createHitTestSession(this.filterEnabled);

    this.transform = this.targetObject.getTransform();
    this.cameraTransform = this.camera.getSceneObject().getTransform();
    // disable target object when surface is not detected
    this.targetObject.enabled = false;
    // create update event
    this.createEvent("UpdateEvent").bind(this.onUpdate.bind(this));
    }


    createHitTestSession(filterEnabled) {
    // create hit test session with options
    let options = HitTestSessionOptions.create();
    options.filter = filterEnabled;


    let session = WorldQueryModule.createHitTestSessionWithOptions(options);
    return session;
    }


    onHitTestResult(results) {
    if (results === null) {
    this.targetObject.enabled = false;
    } else {
    this.targetObject.enabled = true;
    // get hit information
    const hitPosition = results.position;
    const hitNormal = results.normal;


    // identifying the direction the object should look at based on the normal of the hit location.

    let lookDirection;
    if (1 - Math.abs(hitNormal.normalize().dot(vec3.up())) < EPSILON) {
    lookDirection = vec3.forward();
    } else {
    lookDirection = hitNormal.cross(vec3.up());
    }


    const toRotation = quat.lookAt(lookDirection, hitNormal);
    // set position and rotation
    this.targetObject.getTransform().setWorldPosition(hitPosition);
    this.targetObject.getTransform().setWorldRotation(toRotation);
    }
    }


    onUpdate() {
    let rayStart = this.cameraTransform.getWorldPosition();
    let rayEnd = rayStart.add(this.cameraTransform.forward.uniformScale(RAY_LENGTH));
    this.hitTestSession.hitTest(rayStart, rayEnd, this.onHitTestResult.bind(this));
    }
    }

    Hierarchy (View Summary, Expand)

    Index

    Constructors

    Properties

    name: string

    The name of the Asset in Lens Studio.

    uniqueIdentifier: string

    Methods

    • Returns the name of this object's type.

      Returns string

    • Returns true if the object matches or derives from the passed in type.

      Parameters

      • type: string

      Returns boolean

    • Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

      Parameters

      Returns boolean

    MMNEPVFCICPMFPCPTTAAATR