123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
- function MenubarFile( editor ) {
- const strings = editor.strings;
- const saveArrayBuffer = editor.utils.saveArrayBuffer;
- const saveString = editor.utils.saveString;
- const container = new UIPanel();
- container.setClass( 'menu' );
- const title = new UIPanel();
- title.setClass( 'title' );
- title.setTextContent( strings.getKey( 'menubar/file' ) );
- container.add( title );
- const options = new UIPanel();
- options.setClass( 'options' );
- container.add( options );
- // New
- let option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/new' ) );
- option.onClick( function () {
- if ( confirm( 'Any unsaved data will be lost. Are you sure?' ) ) {
- editor.clear();
- }
- } );
- options.add( option );
- //
- options.add( new UIHorizontalRule() );
- // Import
- const form = document.createElement( 'form' );
- form.style.display = 'none';
- document.body.appendChild( form );
- const fileInput = document.createElement( 'input' );
- fileInput.multiple = true;
- fileInput.type = 'file';
- fileInput.addEventListener( 'change', function () {
- editor.loader.loadFiles( fileInput.files );
- form.reset();
- } );
- form.appendChild( fileInput );
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/import' ) );
- option.onClick( function () {
- fileInput.click();
- } );
- options.add( option );
- //
- options.add( new UIHorizontalRule() );
- // Export DRC
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/drc' ) );
- option.onClick( async function () {
- const object = editor.selected;
- if ( object === null || object.isMesh === undefined ) {
- alert( 'No mesh selected' );
- return;
- }
- const { DRACOExporter } = await import( 'three/addons/exporters/DRACOExporter.js' );
- const exporter = new DRACOExporter();
- const options = {
- decodeSpeed: 5,
- encodeSpeed: 5,
- encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
- quantization: [ 16, 8, 8, 8, 8 ],
- exportUvs: true,
- exportNormals: true,
- exportColor: object.geometry.hasAttribute( 'color' )
- };
- // TODO: Change to DRACOExporter's parse( geometry, onParse )?
- const result = exporter.parse( object, options );
- saveArrayBuffer( result, 'model.drc' );
- } );
- options.add( option );
- // Export GLB
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/glb' ) );
- option.onClick( async function () {
- const scene = editor.scene;
- const animations = getAnimations( scene );
- const optimizedAnimations = [];
- for ( const animation of animations ) {
- optimizedAnimations.push( animation.clone().optimize() );
- }
- const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
- const exporter = new GLTFExporter();
- exporter.parse( scene, function ( result ) {
- saveArrayBuffer( result, 'scene.glb' );
- }, undefined, { binary: true, animations: optimizedAnimations } );
- } );
- options.add( option );
- // Export GLTF
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/gltf' ) );
- option.onClick( async function () {
- const scene = editor.scene;
- const animations = getAnimations( scene );
- const optimizedAnimations = [];
- for ( const animation of animations ) {
- optimizedAnimations.push( animation.clone().optimize() );
- }
- const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
- const exporter = new GLTFExporter();
- exporter.parse( scene, function ( result ) {
- saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
- }, undefined, { animations: optimizedAnimations } );
- } );
- options.add( option );
- // Export OBJ
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/obj' ) );
- option.onClick( async function () {
- const object = editor.selected;
- if ( object === null ) {
- alert( 'No object selected.' );
- return;
- }
- const { OBJExporter } = await import( 'three/addons/exporters/OBJExporter.js' );
- const exporter = new OBJExporter();
- saveString( exporter.parse( object ), 'model.obj' );
- } );
- options.add( option );
- // Export PLY (ASCII)
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/ply' ) );
- option.onClick( async function () {
- const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
- const exporter = new PLYExporter();
- exporter.parse( editor.scene, function ( result ) {
- saveArrayBuffer( result, 'model.ply' );
- } );
- } );
- options.add( option );
- // Export PLY (Binary)
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/ply_binary' ) );
- option.onClick( async function () {
- const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
- const exporter = new PLYExporter();
- exporter.parse( editor.scene, function ( result ) {
- saveArrayBuffer( result, 'model-binary.ply' );
- }, { binary: true } );
- } );
- options.add( option );
- // Export STL (ASCII)
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/stl' ) );
- option.onClick( async function () {
- const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
- const exporter = new STLExporter();
- saveString( exporter.parse( editor.scene ), 'model.stl' );
- } );
- options.add( option );
- // Export STL (Binary)
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/stl_binary' ) );
- option.onClick( async function () {
- const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
- const exporter = new STLExporter();
- saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
- } );
- options.add( option );
- // Export USDZ
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/file/export/usdz' ) );
- option.onClick( async function () {
- const { USDZExporter } = await import( 'three/addons/exporters/USDZExporter.js' );
- const exporter = new USDZExporter();
- saveArrayBuffer( await exporter.parseAsync( editor.scene ), 'model.usdz' );
- } );
- options.add( option );
- //
- function getAnimations( scene ) {
- const animations = [];
- scene.traverse( function ( object ) {
- animations.push( ... object.animations );
- } );
- return animations;
- }
- return container;
- }
- export { MenubarFile };
|