@futureverse/swappable-viewer-react
TypeScript icon, indicating that this package has built-in type declarations

0.9.1 • Public • Published

Futureverse Swappable Viewer React

Provides a wrapper around the Futureverse Asset Register. It exposes hooks to query what is currently equipped / linked to the parent collection along with token metadata and other information.

Installation

NPM:

npm install @futureverse/swappable-viewer-react --save

Yarn:

yarn add @futureverse/swappable-viewer-react

Providers

SwappableEngineProvider

Provides hooks and utilty to querying collections, metadata and what is equipped to your swappable. This is not strictly required if you are just looking to implement the 3D viewer then the ViewerProvider is all that is required.

Queries use react-query. In order to use the provider you must make sure it is a child of a QueryClientProvider.

Setup

environement When using testnets such as Porcini use "development" otherwise "production"

<SwappableEngineProvider
  environment="production"
  config={swappableConfig}
>

Example Swappable Config

This config provides the SwappableProvider with context as to which on-chain collections you are interested in retrieving data for. Locations are on chain destinations which you are free to specify multiple locations.

type CollectionParts =
  | 'Chasis'
  | 'Engines'
  | 'Wheels'
  | 'Exhausts'
  | 'Accessories';

const swappableConfig: SwappableCollectionConfig<CollectionParts> = {
  parent: {
    name: 'Chasis',
    locations: ['7672:root:<chasis_root_collection_id>'],
  },
  linked: [
    { name: 'Engines', locations: ['7672:root:<engines_root_collection_id>'] },
    { name: 'Wheels', locations: ['7672:root:<wheels_root_collection_id>'] },
    {
      name: 'Exhausts',
      locations: ['7672:root:<exhausts_root_collection_id>'],
    },
    {
      name: 'Accessories',
      locations: ['7672:root:<accessories_root_collection_id>'],
    },
  ],
};

Hooks

Query what is equipped for FuturePass address and token

import { useQueryEquipped } from '@futureverse/swappable-viewer-react';
const { data: equipped } = useQueryEquipped<CollectionParts>(
  ['<FuturePassAddress>'],
  '<token_id>'
);

Query all parts owned by this address for "Engines"

import { useQueryCollection } from '@futureverse/swappable-viewer-react';
const { data: engines } = useQueryCollection<RaicersParts>('Engines', [
  '<FuturePassAddress>',
]);

ViewerProvider

Renders the unity application in the client. It is recommended to put this where you would like to view your 3D model / avatar / scene. The ViewerProvider provides utility hooks to command the viewer through event messages.

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      refetchOnMount: false,
      refetchOnWindowFocus: false,
    },
  },
});


const swappableConfig: SwappableCollectionConfig = {
....
};

export default function Index() {
  return (
    <QueryClientProvider client={queryClient}>
      <SwappableEngineProvider
        environment="development"
        config={swappableConfig}
      >
        <ViewerProvider
          buildUrl="<your-unity-3d-viewer-build-url>"
          buildDir="viewer/WebGL"
          streamingAssetsDir="StreamingAssets"
          compression="br"
          style={{ width: '100%', height: '100vh' }}
        >
          ...
        </ViewerProvider>
      </SwappableEngineProvider>
    </QueryClientProvider>
  );
}

Hooks

Sending events through the event orchestration handler. The events largely come down to what is implemented in the Unity build. It is up to the developer to decide which events they wish to support. The following is an example of updating a 3D model to display based on metadata.

import { useViewer } from '@futureverse/swappable-viewer-react';
const { data: equipped } = useQueryEquipped<CollectionParts>(
  ['<FuturePassAddress>'],
  '<token_id>'
);
const { sendEvent } = useViewer();

...

sendEvent('update_model', equipped.engine.attributes)

Readme

Keywords

none

Package Sidebar

Install

npm i @futureverse/swappable-viewer-react

Weekly Downloads

91

Version

0.9.1

License

none

Unpacked Size

39.9 kB

Total Files

24

Last publish

Collaborators

  • aidan-starke
  • driferose
  • rmafv
  • chris_futureverse
  • fv-philip.roigard
  • taylor_tran
  • kingsley.wang
  • jcsanpedro
  • nick95550
  • garethdainesnpm
  • developer-darpan
  • tknff
  • mj.liang
  • hlissnake
  • chris.czurylo
  • felixschl
  • pramodya-centrapass
  • admin-futureverse