123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- 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 Project
- const newProjectSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/file/new' ) ).addClass( 'option' ).addClass( 'submenu-title' );
- newProjectSubmenuTitle.onMouseOver( function () {
- const { top, right } = this.dom.getBoundingClientRect();
- const { paddingTop } = getComputedStyle( this.dom );
- newProjectSubmenu.setLeft( right + 'px' );
- newProjectSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
- newProjectSubmenu.setDisplay( 'block' );
- } );
- newProjectSubmenuTitle.onMouseOut( function () {
- newProjectSubmenu.setDisplay( 'none' );
- } );
- options.add( newProjectSubmenuTitle );
- const newProjectSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
- newProjectSubmenuTitle.add( newProjectSubmenu );
- // New Project / Empty
- let option = new UIRow().setTextContent( strings.getKey( 'menubar/file/new/empty' ) ).setClass( 'option' );
- option.onClick( function () {
- if ( confirm( strings.getKey( 'prompt/file/open' ) ) ) {
- editor.clear();
- }
- } );
- newProjectSubmenu.add( option );
- //
- newProjectSubmenu.add( new UIHorizontalRule() );
- // New Project / ...
- const examples = [
- { title: 'menubar/file/new/Arkanoid', file: 'arkanoid.app.json' },
- { title: 'menubar/file/new/Camera', file: 'camera.app.json' },
- { title: 'menubar/file/new/Particles', file: 'particles.app.json' },
- { title: 'menubar/file/new/Pong', file: 'pong.app.json' },
- { title: 'menubar/file/new/Shaders', file: 'shaders.app.json' }
- ];
- const loader = new THREE.FileLoader();
- for ( let i = 0; i < examples.length; i ++ ) {
- ( function ( i ) {
- const example = examples[ i ];
- const option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( example.title ) );
- option.onClick( function () {
- if ( confirm( strings.getKey( 'prompt/file/open' ) ) ) {
- loader.load( 'examples/' + example.file, function ( text ) {
- editor.clear();
- editor.fromJSON( JSON.parse( text ) );
- } );
- }
- } );
- newProjectSubmenu.add( option );
- } )( i );
- }
- // Open
- const openProjectForm = document.createElement( 'form' );
- openProjectForm.style.display = 'none';
- document.body.appendChild( openProjectForm );
- const openProjectInput = document.createElement( 'input' );
- openProjectInput.multiple = false;
- openProjectInput.type = 'file';
- openProjectInput.accept = '.json';
- openProjectInput.addEventListener( 'change', async function () {
- const file = openProjectInput.files[ 0 ];
- if ( file === undefined ) return;
- try {
- const json = JSON.parse( await file.text() );
- async function onEditorCleared() {
- await editor.fromJSON( json );
- editor.signals.editorCleared.remove( onEditorCleared );
- }
- editor.signals.editorCleared.add( onEditorCleared );
- editor.clear();
- } catch ( e ) {
- alert( strings.getKey( 'prompt/file/failedToOpenProject' ) );
- console.error( e );
- } finally {
- form.reset();
- }
- } );
- openProjectForm.appendChild( openProjectInput );
- option = new UIRow()
- .addClass( 'option' )
- .setTextContent( strings.getKey( 'menubar/file/open' ) )
- .onClick( function () {
- if ( confirm( strings.getKey( 'prompt/file/open' ) ) ) {
- openProjectInput.click();
- }
- } );
- options.add( option );
- // Save
- option = new UIRow()
- .addClass( 'option' )
- .setTextContent( strings.getKey( 'menubar/file/save' ) )
- .onClick( function () {
- const json = editor.toJSON();
- const blob = new Blob( [ JSON.stringify( json ) ], { type: 'application/json' } );
- editor.utils.save( blob, 'project.json' );
- } );
- 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 );
- // Export
- const fileExportSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/file/export' ) ).addClass( 'option' ).addClass( 'submenu-title' );
- fileExportSubmenuTitle.onMouseOver( function () {
- const { top, right } = this.dom.getBoundingClientRect();
- const { paddingTop } = getComputedStyle( this.dom );
- fileExportSubmenu.setLeft( right + 'px' );
- fileExportSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
- fileExportSubmenu.setDisplay( 'block' );
- } );
- fileExportSubmenuTitle.onMouseOut( function () {
- fileExportSubmenu.setDisplay( 'none' );
- } );
- options.add( fileExportSubmenuTitle );
- const fileExportSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
- fileExportSubmenuTitle.add( fileExportSubmenu );
- // Export DRC
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( 'DRC' );
- option.onClick( async function () {
- const object = editor.selected;
- if ( object === null || object.isMesh === undefined ) {
- alert( strings.getKey( 'prompt/file/export/noMeshSelected' ) );
- 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' );
- } );
- fileExportSubmenu.add( option );
- // Export GLB
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( '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 } );
- } );
- fileExportSubmenu.add( option );
- // Export GLTF
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( '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 } );
- } );
- fileExportSubmenu.add( option );
- // Export OBJ
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( 'OBJ' );
- option.onClick( async function () {
- const object = editor.selected;
- if ( object === null ) {
- alert( strings.getKey( 'prompt/file/export/noObjectSelected' ) );
- return;
- }
- const { OBJExporter } = await import( 'three/addons/exporters/OBJExporter.js' );
- const exporter = new OBJExporter();
- saveString( exporter.parse( object ), 'model.obj' );
- } );
- fileExportSubmenu.add( option );
- // Export PLY (ASCII)
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( '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' );
- } );
- } );
- fileExportSubmenu.add( option );
- // Export PLY (BINARY)
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( '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 } );
- } );
- fileExportSubmenu.add( option );
- // Export STL (ASCII)
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( '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' );
- } );
- fileExportSubmenu.add( option );
- // Export STL (BINARY)
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( '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' );
- } );
- fileExportSubmenu.add( option );
- // Export USDZ
- option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( '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' );
- } );
- fileExportSubmenu.add( option );
- //
- function getAnimations( scene ) {
- const animations = [];
- scene.traverse( function ( object ) {
- animations.push( ... object.animations );
- } );
- return animations;
- }
- return container;
- }
- export { MenubarFile };
|