Skip to content

SpatialMapper

Cristi Petruta edited this page Apr 11, 2019 · 1 revision

Use the SpatialMapper to run spatial recognition on the surrounding environment. Call SurfaceMapper.isAvailable() to check if spatial mapping is available (currently it only works on the HoloLens).


Methods

  • start()

Start mapping withing the specified bounding cube. While mapping is running, the script received events for new, changed and deleted surface meshes.

  • stop()

Stop mapping. Since mapping consumes system resources, it should be stopped when updates are no longer needed.


Properties

  • triangleDensity

Desired triangle density (double) in triangles per meter cube. Higher density yields more detailed meshes, while lower density yields simpler meshes. The effective range is [1000-2000], with no further decimation possible under 1000 and maximum detail achieved by 2000.

  • boundingCube

The real world volume to restrict mapping to. The bounding cube is specified as origin and extents. Example: cube with the center at scene origin and 2 meter extents: {x : 0, y : 0, z : 0, extentX : 2, extentY : 2, extentZ : 2}


Events

  • onnewmesh

Triggered when a new mesh is added.

var mapper = new SurfaceMapper();
mapper.onnewmesh = function(mesh) {};
// or
mapper.addEventListener('newmesh', function(mesh) {});
  • onupdatedmesh

Triggered when a mesh is updated. Event data is the updated mesh.

  • onmeshdeleted

Triggered when a mesh was deleted. The event data consists of the deleted mesh ID.


The Mesh object

Received on new and updated mesh events contains the following fields. Fields:

  • id String. The ID of the mesh.

  • position

Float32Array. A transform matrix for the mesh to scene origin.

  • indices Uint16Array. Triangle indices.

  • vertices

Float32Array. Vertex coordinates.

  • normals

Uint8Array. Triangle normals.


Example:

if (SurfaceMapper.isAvailable() === true) {
   surfaceMapper = new SurfaceMapper();`

   // set callbacks for new, updated and deleted meshes
   surfaceMapper.onnewmesh = onNewMesh;
   surfaceMapper.onupdatedmesh = OnMeshUpdated;
   surfaceMapper.ondeletedmesh = OnMeshDeleted;

   // Set triangle density per m3.
   surfaceMapper.triangleDensity = 1000;

   // Set the bounding cube to scan
   surfaceMapper.boundingCube = {x : 0, y : 0, z : 0, extentX : 2, extentY : 2, extentZ : 2};

   // Start the mapper; when updates are no longer needed, call stop to free device resources
   surfaceMapper.start();
}

For a complete example, see sample-code/appcode/hololens-surface-mapping.js

Clone this wiki locally