@homee-ai/3d-space-viewer
v0.0.30-alpha
Published
This package is a react package for rendering 3D space for viewing
Readme
Homee 3D Space Viewer
This viewer enables the rendering of 3D spaces, enhancing visual detail through precise spatial information (e.g., dimensions, object coordinates). It offers features for navigating and adjusting the camera view within the space. Additionally, this library includes a chat UI component that interacts with an AI service, allowing users to communicate with a custom AI designed to understand the rendered space. Interaction between the chat and the viewer is facilitated through a context function provided by the library.
Installation
npm install @homee-ai/3d-space-viewerQuick Start
To initiate the viewer:
import { HomeeViewer, HomeeViewerProvider } from '@homee-ai/3d-space-viewer'
const Viewer = () => {
return <HomeeViewer source={{ scene: 'homee-scene.ply', model: 'homee-room.glb' }} />
}
const App = () => {
return (
<HomeeViewerProvider>
<Viewer />
</HomeeViewerProvider>
)
}
export default AppAPI
HomeeViewerProvider
<HomeeViewerProvider>
{children}
</HomeeViewerProvider>useHomeeViewer
// pattern for getting global states under `HomeeViewerProvider`
const initViewer = useHomeeViewerContext(state => state.initViewer)
const disposeViewer = useHomeeViewerContext(state => state.disposeViewer)Returns
isDimensionShown: boolean- when
trueindicate space dimensions are visible in the viewer. - when
falseindicates space dimensions are not visible in the viewer.
- when
isPreviewMode: boolean- when
trueindicate space is in preview mode and mouse movement is disabled. - when
falseindicates that space is not in preview mode.
- when
initViewer: ({ ref?: HTMLDivElement, sharedMemoryForWorkers?: boolean, preview?: boolean }) => Promise<void>- bind the viewer to div element.
- if no
refis provided, by default the viewer will bind todocument.body. sharedMemoryForWorkerstells the viewer to use shared memory via aSharedArrayBuffer(for better performance) to transfer data to and from the sorting web worker. Refer to the CORS section below when setting this field totrue.- default
false
- default
previewwhen set totrueit will enable preview mode which will disable mouse movement and only allow user to rotate the view angle of the space on a fixed point.- default
false
- default
disposeViewer: () => Promise<void>- require viewer to be init before calling.
- dispose the viewer
loadSpace: (arg: { sceneSrc: string, modelSrc: string }) => Promise<void>- require viewer to be init before calling.
sceneSrcshould be either a.plyor.kspatsource, used for loading the 3D scene.modelSrcshould be a.glbsource, used to map to the 3D scene on load to provide accurate coordinates and dimensions for interactions.
setCameraHeight: (level: number) => voidmin:0, max:1- require viewer to be init before calling.
- adjust the viewer's camera height (
y).
moveCamera: (arg: { position: { x: number, z: number }, target?: { x: number, z: number } | null }) => void- require viewer to be init before calling.
positionwill move the camera position toxandz.targetwill move the camera view toxandz.- height (
y) of the camera will remain the same when moving camera.
toggleDimensionView: (bool? boolean) => void- if
boolis provided it will setisDimensionShownto value, else it will toggleisDimensionShownvalue.
- if
HomeeViewer
<HomeeViewer source={{ scene: 'homee-scene.ply', model: 'homee-plan.glb' }} />Props
source: { scene: string, model: string }requiredsceneshould be either a.plyor.kspatsource, used for loading the 3D scene.modelshould be a.glbsource, used to map to the 3D scene on load to provide accurate coordinates and dimensions for interactions.
sharedMemoryForWorkers?: booleanoptional (default: false)- tells the viewer to use shared memory via a
SharedArrayBuffer(for better performance) to transfer data to and from the sorting web worker. Refer to the CORS section below when setting this field totrue.
preview?: booleanoptional (default: false)- when set to
trueit will enable preview mode which will disable mouse movement and only allow user to rotate the view angle of the space on a fixed point.
CORS issue and SharedArrayBuffer
By default, the Viewer class uses shared memory (via a typed array backed by a SharedArrayBufffer) to communicate with the web worker to enhance the performance. This mechanism presents a potential security issue that is outlined here: https://web.dev/articles/cross-origin-isolation-guide. Shared memory can be disabled by passing false for the sharedMemoryForWorkers parameter to the constructor for Viewer, but if you want to leave it enabled, a couple of extra CORS HTTP headers need to be present in the response from the server that is sent when loading the application.
