Create a viewer instance for using the webgi viewer SDK.
ReadonlyactionsThe actions of the viewer and its plugins (ViewerAction): the commands that a person runs from a key, a
toolbar, a menu or a command palette. Plugins register theirs here; a UI lists and runs them, and the app changes
their keys (ViewerActions.setShortcut) or turns the keys off (ViewerActions.enabled).
ReadonlyassetReadonlydebugIf the viewer is enabled. Set this false to disable RAF loop.
It is also false while the WebGL context is lost. A change dispatches viewerEnabled or viewerDisabled.
Specifies how many frames to render in a single request animation frame. Keep to 1 for realtime rendering. Note: should be max (screen refresh rate / animation frame rate) like 60Hz / 30fps
Readonlyobject3dReadonlypluginsEnable or disable all rendering. When disabled, the animation loop is still running, but nothing is rendered. This can be used to pause rendering when not needed, for example when the viewer is not visible. Note: this does not pause plugins or the timeline, use enabled for that.
ReadonlyrenderNumber of times to run composer render. If set to more than 1, preRender and postRender events will also be called multiple times.
OptionalrenderHelper to track and visualize rendering performance while in debug mode.
ReadonlyresizeThe ResizeObserver observing the canvas element. Add more elements to this observer to resize viewer on their size change.
plugins that are not serialized/deserialized with the viewer from config. useful when loading files exported from the editor, etc (runtime only, not serialized itself)
ReadonlytimelineMain timeline for the viewer.
It's a WIP, API might change.
ReadonlytypeStatic ReadonlyConfigStaticConsoleStaticDialogStaticShadowThe material properties that three.js reads when it draws the shadow maps (WebGLShadowMap.getDepthMaterial).
The maps are drawn again only after renderManager.resetShadows(), so a materialUpdate with one of these as
its change, or with no change, resets them. Add a key for a material property or extension that changes
the shadow casters.
Static ReadonlyVERSIONStaticViewerIf any of the viewers are in debug mode, this will be true. This is required for debugging/logging in some cases.
Get the HTML Canvas Element where the viewer is rendering
Create and get a three.js CanvasTexture from the viewer's canvas.
Get the HTML Element containing the canvas
The renderer for the viewer that's attached to the canvas. This is wrapper around WebGLRenderer and EffectComposer and manages post-processing passes and rendering logic
Scene with object hierarchy used for rendering
Protected_Protected_Protected_Add a plugin to the viewer.
Add multiple plugins to the viewer.
List of plugin instances or classes
Add multiple plugins to the viewer(sync).
List of plugin instances or classes
Add a plugin to the viewer(sync).
Add an object to the scene model root. If an imported scene model root is passed, it will be loaded with viewer configuration, unless importConfig is false
Optionaloptions: AddObjectOptionsDeserialize all the plugins and their settings from a preset. Used in fromJSON.
The output of serializePlugins.
Optionalmeta: SerializationMetaType
The meta object.
Disposes the viewer and frees up all resource and events. Do not use the viewer after calling dispose.
NOTE - If you want to reuse the viewer, set viewer.enabled to false instead, then set it to true again when required.
To dispose all the objects, materials in the scene, but not the viewer itself, use viewer.scene.disposeSceneModels()
Optionalfunc: (...args: any[]) => TRetExports an object/mesh/material/texture/render-target/plugin-preset/viewer to a blob. If no object is given, a glb is exported with the current viewer state.
Optionalobj: Optionaloptions: ExportFileOptionsUses the FileTransferPlugin to export a Blob/File. If the plugin is not available, it will download the blob. FileTransferPlugin can be configured by other plugins to export the blob to a specific location like local file system, cloud storage, etc.
The blob or file to export/download
Optionalname: string
name of the file, if not provided, the name of the file is used if it's a file.
Serialize all the viewer and plugin settings.
Indicate that the output will be converted and saved as binary data. (default: false)
OptionalpluginFilter: string[]
List of PluginType to include. If empty, no plugins will be serialized. If undefined, all plugins will be serialized.
Serialize a single plugin settings.
Optionalplugin: Serialize multiple plugin settings.
Optionalfilter: string[]
List of PluginType to include. If empty, no plugins will be serialized. If undefined, all plugins will be serialized.
Export the scene to a file (default: glb with viewer config) and return a blob
Optionaloptions: ExportFileOptionsuses the AssetExporterPlugin if available. This is useful to use the options configured by the user in the plugin.
Fits the camera to frame the selected objects (or the entire scene if none).
When duration is 0, the view is set instantly without animation.
Optionalselected: objects to fit (Object3D, materials, textures, or geometries). Falls back to entire scene.
padding multiplier (default 1.5)
Optionalduration: number
animation duration in ms. Pass 0 for instant. Defaults to CameraViewPlugin.animDuration (1000ms).
Optionalease: easing function or name
Can be used to "subscribe" to plugins.
Optionalunmount: (p: T) => voidOptionalthisPlugin: AViewerPlugin<AViewerPluginEventMap, ThreeViewer, boolean>Deserialize all the viewer and plugin settings. NOTE - use async ThreeViewer.importConfig to import a json/config exported with ThreeViewer.exportConfig or ThreeViewer.toJSON.
The serialized JSON object returned from toJSON.
Optionalmeta: SerializationMetaType
The meta object, see SerializationMetaType
use assetManager instead. Gets the Asset manager, contains useful functions for managing, loading and inserting assets.
Get the Plugin by a constructor type or add a new plugin of the specified type if it doesn't exist.
Get the Plugin by a constructor type or add a new plugin to the viewer of the specified type if it doesn't exist(sync).
Get the Plugin by a constructor type or by the string type. Use string type if the plugin is not a dependency, and you don't want to bundle the plugin.
Get the Plugin by the string type.
Returns a blob with the screenshot of the canvas. If CanvasSnapshotPlugin is added, it will be used, otherwise canvas.toBlob will be used directly. When rendering is paused (renderEnabled false) or the viewer is disabled (enabled false, also after a lost WebGL context), no frame is rendered, so the canvas is read as it is (also with the plugin added).
default image/jpeg
Returns a data URL with the screenshot of the canvas, after the next frame. When rendering is paused (renderEnabled) or the viewer is disabled (enabled, also after a lost WebGL context), the canvas is read as it is, also when the viewer is disabled before the next frame.
default image/jpeg
Imports an object/model/material/texture/viewer-config/plugin-preset/... to the viewer scene from url or an IAsset object. Same as AssetImporter.importSingle
Optionaloptions: ImportAssetOptionsDeserialize and import all the viewer and plugin settings, exported with exportConfig.
The serialized JSON object returned from exportConfig or toJSON.
Optionalmeta: SerializationMetaTypeDeserialize and import a single plugin settings. Can also use ThreeViewer.importConfig to import only plugin config.
Optionalplugin: IViewerPlugin<ThreeViewer, boolean>Add an object/model/material/viewer-config/plugin-preset/... to the viewer scene from url or an IAsset object. Same as AssetManager.addAssetSingle
Optionaloptions: ImportAddOptionsOptionalextraResources: Partial<SerializationResourcesType>Waits for the next frame (the postFrame event). The wait also ends when the viewer is disabled (enabled,
also when the WebGL context is lost) or disposed before the frame, since the animation loop sends no postFrame
then. A viewer that is disabled already does not wait.
The code after the await runs in the same animation frame, before the browser shows it, so it can read the canvas
of that frame. A frame can come while rendering is paused (renderEnabled): check it to know if the frame
rendered.
true after a frame, false when the wait ended without one
Remove a plugin instance or a plugin class. Works similar to ThreeViewer.addPlugin
Remove a plugin instance or a plugin class(sync). Works similar to ThreeViewer.addPluginSync
Mark that the canvas is resized. If the size is changed, the renderer and all render targets are resized. This happens before the render of the next frame.
Serialize all the plugins and their settings to save or create presets. Used in toJSON.
The meta object.
Optionalfilter: string[]
List of PluginType for to include. If empty, no plugins will be serialized. If undefined, all plugins will be serialized.
Set the background image of the scene from url or an IAsset object.
Set the environment map of the scene from the same map.
Set the viewer to dirty and trigger render of the next frame.
This also triggers the 'update' event on the viewer. Note - update event might be triggered multiple times in a single frame, use preFrame or preRender events to get notified only once per frame.
Optionalsource: any
The source of the dirty event. like plugin or 3d object
Optionalevent: Event<string, unknown>
The event that triggered the dirty event.
Set the environment map of the scene from url or an IAsset object.
Set the background image of the scene from the same map.
Set the render size of the viewer to fit in the container according to the specified mode, maintaining aspect ratio. Changes the renderScale accordingly. Note: the canvas needs to be centered in the container to work properly, this can be done with the following css on the container:
display: flex;
justify-content: center;
align-items: center;
or in js:
viewer.container.style.display = 'flex';
viewer.container.style.justifyContent = 'center';
viewer.container.style.alignItems = 'center';
Modes: 'contain': The canvas is scaled to fit within the container while maintaining its aspect ratio. The canvas will be fully visible, but there may be empty space around it. 'cover': The canvas is scaled to fill the entire container while maintaining its aspect ratio. Part of the canvas may be clipped to fit the container. 'fill': The canvas is stretched to completely fill the container, ignoring its aspect ratio. 'scale-down': The canvas is scaled down to fit within the container while maintaining its aspect ratio, but it won't be scaled up if it's smaller than the container. 'none': container size is ignored, but devicePixelRatio is used
Check the example for more details - https://threepipe.org/examples/#viewer-render-size/
The size to set the render to. The canvas will render to this size.
'contain', 'cover', 'fill', 'scale-down' or 'none'. Default is 'contain'.
typically set to window.devicePixelRatio, or Math.min(1.5, window.devicePixelRatio) for performance. Use this only when size is derived from dom elements.
OptionalcontainerSize: { height: number; width: number }
(optional) The size of the container, if not passed, the bounding client rect of the container is used.
Set size of the canvas and update the renderer. If no size or width/height is passed, canvas is set to 100% of the container.
See also ThreeViewer.setRenderSize to set the size of the render target by automatically calculating the renderScale and fitting in container.
Note: Apps using this should ideally set max-width: 100% for the canvas in css.
Optionalsize: { height?: number; width?: number }Serialize all the viewer and plugin settings and versions.
Indicate that the output will be converted and saved as binary data. (default: true)
OptionalpluginFilter: string[]
List of PluginType to include. If empty, no plugins will be serialized. If undefined/not-passed, all plugins will be serialized.
Optionalmeta: SerializationMetaTypeTraverse all objects in scene model root.
Three Viewer
The ThreeViewer is the main class in the framework to manage a scene, render and add plugins to it.