|
| 1 | +import type {FileRenderPlugin} from '../../modules/file-render-plugin.ts'; |
| 2 | +import {registerFileRenderPlugin} from '../../modules/file-render-plugin.ts'; |
| 3 | + |
| 4 | +/** |
| 5 | + * 3D model file render plugin |
| 6 | + * |
| 7 | + * support common 3D model file formats, use online-3d-viewer library for rendering |
| 8 | + */ |
| 9 | +export function register3DViewerPlugin(): void { |
| 10 | + // supported 3D file extensions |
| 11 | + const SUPPORTED_EXTENSIONS = [ |
| 12 | + '.3dm', '.3ds', '.3mf', '.amf', '.bim', '.brep', |
| 13 | + '.dae', '.fbx', '.fcstd', '.glb', '.gltf', |
| 14 | + '.ifc', '.igs', '.iges', '.stp', '.step', |
| 15 | + '.stl', '.obj', '.off', '.ply', '.wrl', |
| 16 | + ]; |
| 17 | + |
| 18 | + // create and register plugin |
| 19 | + const plugin: FileRenderPlugin = { |
| 20 | + name: '3d-model-viewer', |
| 21 | + |
| 22 | + // check if file extension is supported 3D file |
| 23 | + canHandle(filename: string, _mimeType: string): boolean { |
| 24 | + const ext = filename.substring(filename.lastIndexOf('.')).toLowerCase(); |
| 25 | + const canHandle = SUPPORTED_EXTENSIONS.includes(ext); |
| 26 | + return canHandle; |
| 27 | + }, |
| 28 | + |
| 29 | + // render 3D model |
| 30 | + async render(container: HTMLElement, fileUrl: string): Promise<void> { |
| 31 | + // add loading indicator |
| 32 | + container.classList.add('is-loading'); |
| 33 | + |
| 34 | + try { |
| 35 | + // dynamically load 3D rendering library |
| 36 | + const OV = await import(/* webpackChunkName: "online-3d-viewer" */'online-3d-viewer'); |
| 37 | + |
| 38 | + // configure container style |
| 39 | + container.classList.add('model3d-content'); |
| 40 | + |
| 41 | + // initialize 3D viewer |
| 42 | + const viewer = new OV.EmbeddedViewer(container, { |
| 43 | + backgroundColor: new OV.RGBAColor(59, 68, 76, 0), // transparent |
| 44 | + defaultColor: new OV.RGBColor(65, 131, 196), |
| 45 | + edgeSettings: new OV.EdgeSettings(false, new OV.RGBColor(0, 0, 0), 1), |
| 46 | + }); |
| 47 | + |
| 48 | + // load model from url |
| 49 | + viewer.LoadModelFromUrlList([fileUrl]); |
| 50 | + } catch (error) { |
| 51 | + // handle render error |
| 52 | + console.error('error rendering 3D model:', error); |
| 53 | + |
| 54 | + // add error message and download button |
| 55 | + const fallbackText = container.getAttribute('data-fallback-text') || 'View Raw File'; |
| 56 | + container.innerHTML = ` |
| 57 | + <div class="ui error message"> |
| 58 | + <div class="header">Failed to render 3D model</div> |
| 59 | + <p>The 3D model could not be displayed in the browser.</p> |
| 60 | + <a class="ui basic button" href="${fileUrl}" target="_blank">${fallbackText}</a> |
| 61 | + </div> |
| 62 | + `; |
| 63 | + } finally { |
| 64 | + // remove loading state |
| 65 | + container.classList.remove('is-loading'); |
| 66 | + } |
| 67 | + }, |
| 68 | + }; |
| 69 | + |
| 70 | + // register plugin |
| 71 | + registerFileRenderPlugin(plugin); |
| 72 | +} |
0 commit comments