gitea源码

3d-viewer.ts 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import type {FileRenderPlugin} from '../plugin.ts';
  2. import {extname} from '../../utils.ts';
  3. // support common 3D model file formats, use online-3d-viewer library for rendering
  4. /* a simple text STL file example:
  5. solid SimpleTriangle
  6. facet normal 0 0 1
  7. outer loop
  8. vertex 0 0 0
  9. vertex 1 0 0
  10. vertex 0 1 0
  11. endloop
  12. endfacet
  13. endsolid SimpleTriangle
  14. */
  15. export function newRenderPlugin3DViewer(): FileRenderPlugin {
  16. // Some extensions are text-based formats:
  17. // .3mf .amf .brep: XML
  18. // .fbx: XML or BINARY
  19. // .dae .gltf: JSON
  20. // .ifc, .igs, .iges, .stp, .step are: TEXT
  21. // .stl .ply: TEXT or BINARY
  22. // .obj .off .wrl: TEXT
  23. // So we need to be able to render when the file is recognized as plaintext file by backend.
  24. //
  25. // It needs more logic to make it overall right (render a text 3D model automatically):
  26. // we need to distinguish the ambiguous filename extensions.
  27. // For example: "*.obj, *.off, *.step" might be or not be a 3D model file.
  28. // So when it is a text file, we can't assume that "we only render it by 3D plugin",
  29. // otherwise the end users would be impossible to view its real content when the file is not a 3D model.
  30. const SUPPORTED_EXTENSIONS = [
  31. '.3dm', '.3ds', '.3mf', '.amf', '.bim', '.brep',
  32. '.dae', '.fbx', '.fcstd', '.glb', '.gltf',
  33. '.ifc', '.igs', '.iges', '.stp', '.step',
  34. '.stl', '.obj', '.off', '.ply', '.wrl',
  35. ];
  36. return {
  37. name: '3d-model-viewer',
  38. canHandle(filename: string, _mimeType: string): boolean {
  39. const ext = extname(filename).toLowerCase();
  40. return SUPPORTED_EXTENSIONS.includes(ext);
  41. },
  42. async render(container: HTMLElement, fileUrl: string): Promise<void> {
  43. // TODO: height and/or max-height?
  44. const OV = await import(/* webpackChunkName: "online-3d-viewer" */'online-3d-viewer');
  45. const viewer = new OV.EmbeddedViewer(container, {
  46. backgroundColor: new OV.RGBAColor(59, 68, 76, 0),
  47. defaultColor: new OV.RGBColor(65, 131, 196),
  48. edgeSettings: new OV.EdgeSettings(false, new OV.RGBColor(0, 0, 0), 1),
  49. });
  50. viewer.LoadModelFromUrlList([fileUrl]);
  51. },
  52. };
  53. }