123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
- import { AddObjectCommand } from './commands/AddObjectCommand.js';
- import { RemoveObjectCommand } from './commands/RemoveObjectCommand.js';
- import { MultiCmdsCommand } from './commands/MultiCmdsCommand.js';
- import { SetMaterialValueCommand } from './commands/SetMaterialValueCommand.js';
- var MenubarEdit = function ( editor ) {
- var strings = editor.strings;
- var container = new UIPanel();
- container.setClass( 'menu' );
- var title = new UIPanel();
- title.setClass( 'title' );
- title.setTextContent( strings.getKey( 'menubar/edit' ) );
- container.add( title );
- var options = new UIPanel();
- options.setClass( 'options' );
- container.add( options );
- // Undo
- var undo = new UIRow();
- undo.setClass( 'option' );
- undo.setTextContent( strings.getKey( 'menubar/edit/undo' ) );
- undo.onClick( function () {
- editor.undo();
- } );
- options.add( undo );
- // Redo
- var redo = new UIRow();
- redo.setClass( 'option' );
- redo.setTextContent( strings.getKey( 'menubar/edit/redo' ) );
- redo.onClick( function () {
- editor.redo();
- } );
- options.add( redo );
- // Clear History
- var option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/edit/clear_history' ) );
- option.onClick( function () {
- if ( confirm( 'The Undo/Redo History will be cleared. Are you sure?' ) ) {
- editor.history.clear();
- }
- } );
- options.add( option );
- editor.signals.historyChanged.add( function () {
- var history = editor.history;
- undo.setClass( 'option' );
- redo.setClass( 'option' );
- if ( history.undos.length == 0 ) {
- undo.setClass( 'inactive' );
- }
- if ( history.redos.length == 0 ) {
- redo.setClass( 'inactive' );
- }
- } );
- // ---
- options.add( new UIHorizontalRule() );
- // Clone
- var option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/edit/clone' ) );
- option.onClick( function () {
- var object = editor.selected;
- if ( object.parent === null ) return; // avoid cloning the camera or scene
- object = object.clone();
- editor.execute( new AddObjectCommand( editor, object ) );
- } );
- options.add( option );
- // Delete
- var option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
- option.onClick( function () {
- var object = editor.selected;
- var parent = object.parent;
- if ( parent === undefined ) return; // avoid deleting the camera or scene
- editor.execute( new RemoveObjectCommand( editor, object ) );
- } );
- options.add( option );
- // Minify shaders
- var option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/edit/minify_shaders' ) );
- option.onClick( function () {
- var root = editor.selected || editor.scene;
- var errors = [];
- var nMaterialsChanged = 0;
- var path = [];
- function getPath( object ) {
- path.length = 0;
- var parent = object.parent;
- if ( parent !== undefined ) getPath( parent );
- path.push( object.name || object.uuid );
- return path;
- }
- var cmds = [];
- root.traverse( function ( object ) {
- var material = object.material;
- if ( material.isShaderMaterial ) {
- try {
- var shader = glslprep.minifyGlsl( [
- material.vertexShader, material.fragmentShader ] );
- cmds.push( new SetMaterialValueCommand( editor, object, 'vertexShader', shader[ 0 ] ) );
- cmds.push( new SetMaterialValueCommand( editor, object, 'fragmentShader', shader[ 1 ] ) );
- ++ nMaterialsChanged;
- } catch ( e ) {
- var path = getPath( object ).join( "/" );
- if ( e instanceof glslprep.SyntaxError )
- errors.push( path + ":" +
- e.line + ":" + e.column + ": " + e.message );
- else {
- errors.push( path +
- ": Unexpected error (see console for details)." );
- console.error( e.stack || e );
- }
- }
- }
- } );
- if ( nMaterialsChanged > 0 ) {
- editor.execute( new MultiCmdsCommand( editor, cmds ), 'Minify Shaders' );
- }
- window.alert( nMaterialsChanged +
- " material(s) were changed.\n" + errors.join( "\n" ) );
- } );
- options.add( option );
- options.add( new UIHorizontalRule() );
- // Set textures to sRGB. See #15903
- var option = new UIRow();
- option.setClass( 'option' );
- option.setTextContent( strings.getKey( 'menubar/edit/fixcolormaps' ) );
- option.onClick( function () {
- editor.scene.traverse( fixColorMap );
- } );
- options.add( option );
- var colorMaps = [ 'map', 'envMap', 'emissiveMap' ];
- function fixColorMap( obj ) {
- var material = obj.material;
- if ( material !== undefined ) {
- if ( Array.isArray( material ) === true ) {
- for ( var i = 0; i < material.length; i ++ ) {
- fixMaterial( material[ i ] );
- }
- } else {
- fixMaterial( material );
- }
- editor.signals.sceneGraphChanged.dispatch();
- }
- }
- function fixMaterial( material ) {
- var needsUpdate = material.needsUpdate;
- for ( var i = 0; i < colorMaps.length; i ++ ) {
- var map = material[ colorMaps[ i ] ];
- if ( map ) {
- map.encoding = THREE.sRGBEncoding;
- needsUpdate = true;
- }
- }
- material.needsUpdate = needsUpdate;
- }
- return container;
- };
- export { MenubarEdit };
|