Writing Custom Layers

How to display any general data

Each layer's props should extend the MetacityLayerProps interface:

import { MetacityLayerProps } from 'metacitygl';

export function CustomLayer(props: MetacityLayerProps) {
    const { context, onLoaded, enableUI } = props;
    return (
        <div>
            <h1>Custom Layer</h1>
            <p>This is a custom layer</p>
        </div>
    )
}

The returned nodes will get automatically attached to the Metacity UI panel. You can make the UI look however you want.

Each layer gets these props by default:

  • context is a class encapsulating the graphics context, you can use it to modify what gets rendered

  • onLoaded is a callback you must use to signal the application that your layer is ready to display

  • enableUI is a flag that signals you whether the layer UI should get displayed or not

Parsing data

There is a recommended way how to handle any data in MetacityGL. In your custom layer, split the process into 3 parts:

  1. Getting your data is probably the easiest part. Use a library you like (e.g. axios?) and download it.

  2. Transforming the data into a renderable model can be tricky. MetacityGL provides built-in tools called Assemblers that can make your life a little easier. The Assemblers allow you to parse the data incrementally. When you are done, you can export the data from the Assembler in a form that can be directly passed into one of Metacity's Models.

  3. Create a Model - you can create a completely new one using three.js or reuse one defined in the library.

  4. Pass the created model into the graphics context.

Workers

The data-parsing steps usually require some number-crunching. It is best to isolate these steps into a separate worker process and pass the result back to the layer component.

With this worker, you can structure the layer the following way:

There is much more you could do:

  • store the reference to the Model in the layer and allow modifying it with the UI

  • you could create a pool of workers and reuse them between layers instead of creating and terminating them for better performance, etc.

See the other pages for a better grasp of Models, Assemblers, and the context.

Last updated