@inweb/viewer-three
Version:
JavaScript library for rendering CAD and BIM files in a browser using Three.js
104 lines (98 loc) • 3.91 kB
text/typescript
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2025, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// protected by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
// This application incorporates Open Design Alliance software pursuant to a
// license agreement with Open Design Alliance.
// Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance.
// All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////
import { ILoadersRegistry, loadersRegistry } from "@inweb/viewer-core";
import { GLTFFileLoader } from "./GLTFFileLoader";
import { GLTFCloudDynamicLoader } from "./GLTFCloudDynamicLoader";
/**
* Viewer loaders registry. Use this registry to register custom loaders.
*
* To implement custom loader:
*
* 1. Define a loader class implements {@link ILoader}.
* 2. Define a constructor with a `viewer` parameter.
* 3. Override {@link ILoader.isSupport} and сheck if the loader can load the specified file.
* 4. Override {@link ILoader.load} and define the logic for loading the scene from the file.
*
* The loader should do:
*
* - Load raw data from file and convert it to the `Three.js` scene.
* - Add scene to the viewer `scene`.
* - Create `ModelImpl` for the scene and to the viewer `models` list.
* - Synchronize viewer options and overlay.
* - Update the viewer.
*
* The loader must emit events:
*
* - `geometryprogress` - during loading. If progress is not supported, emit it once with a value of 100%
* after the load is complete.
* - `databasechunk` - when scene is loaded and ready to render.
* 5. Override {@link ILoader.dispose} and release loader resources, if required.
* 6. Register loader provider in the loaders registry by calling the {@link loaders.registerLoader}.
*
* @example Implementing a custom loader.
*
* ```javascript
* import { loaders, Loader, ModelImpl, Viewer } from "@inweb/viewer-three";
*
* class MyLoader extends Loader {
* public viewer: Viewer;
*
* constructor(viewer: Viewer) {
* super();
* this.viewer = viewer;
* }
*
* override isSupport(file, format): Boolean {
* // place custom logic here
* return ...;
* }
*
* override load(file, format, params): Promise<this> {
*
* // place custom loading logic here
* const scene = ...;
*
* const modelImpl = new ModelImpl(scene);
* modelImpl.loader = this;
* modelImpl.viewer = this.viewer;
*
* this.viewer.scene.add(scene);
* this.viewer.models.push(modelImpl);
*
* this.viewer.syncOptions();
* this.viewer.syncOverlay();
* this.viewer.update();
*
* this.viewer.emitEvent({ type: "databasechunk", data: scene, file });
*
* return Promise.resove(this);
* };
* }
*
* loaders.registerLoader("MyLoader", (viewer) => new MyLoader(viewer));
* ```
*/
export const loaders: ILoadersRegistry = loadersRegistry("threejs");
// build-in loaders
loaders.registerLoader("gltf-file", (viewer: any) => new GLTFFileLoader(viewer));
loaders.registerLoader("gltf-cloud", (viewer: any) => new GLTFCloudDynamicLoader(viewer));