Skip to main content

WorldQueryModule

Class

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

  • Snapchat: not supported
  • SPECS: fully supported
  • Camera Kit Web: not supported
  • Camera Kit Mobile: not supported
  • SnapchatNot supported — SPECS only
  • SPECSFully supported
  • Camera Kit WebNot supported — SPECS only
  • Camera Kit MobileNot supported — SPECS only

Platform details

  • Snapchat — Not supported — SPECS only.

  • SPECS — Fully supported.

  • Camera Kit Web — Not supported — SPECS only.

  • Camera Kit Mobile — Not supported — SPECS only.

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

Index

Example

// 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));
}
}
//

## Extends

- [`Asset`](./Asset.mdx)

## Properties

<a id="name" className="api-member api-member--property"></a>

### name

> **name**: `string`

The name of the Asset in Lens Studio.

#### Default Value

```ts
""

Inherited from

Asset.name


uniqueIdentifier

readonly uniqueIdentifier: string

Since

Lens Scripting Version 176

Inherited from

Asset.uniqueIdentifier

Methods

createHitTestSession()

createHitTestSession(): HitTestSession

SPECS only

Create a HitTestSession with default options.

Returns

HitTestSession

Since

Lens Scripting Version 270


createHitTestSessionWithOptions()

createHitTestSessionWithOptions(options): HitTestSession

SPECS only

Create a new HitTestSession with options.

Parameters

ParameterType
optionsHitTestSessionOptions

Returns

HitTestSession

Since

Lens Scripting Version 270


getTypeName()

static getTypeName(): TypeName<WorldQueryModule>

SPECS only

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<WorldQueryModule>

Overrides

Asset.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

Asset.getTypeName


isOfType()

isOfType(type): boolean

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

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

Asset.isOfType


isSame()

isSame(other): boolean

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

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

Asset.isSame

See also

World Query Module guide.