Menubar.File.js 7.4 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

5
Menubar.File = function ( editor ) {
M
Mr.doob 已提交
6

7 8 9 10 11 12 13 14 15 16
	var NUMBER_PRECISION = 6;

	function parseNumber( key, value ) {

		return typeof value === 'number' ? parseFloat( value.toFixed( NUMBER_PRECISION ) ) : value;

	}

	//

M
Mr.doob 已提交
17 18
	var config = editor.config;

19 20
	var container = new UI.Panel();
	container.setClass( 'menu' );
M
Mr.doob 已提交
21

22 23 24 25
	var title = new UI.Panel();
	title.setClass( 'title' );
	title.setTextContent( 'File' );
	container.add( title );
M
Mr.doob 已提交
26

27 28 29
	var options = new UI.Panel();
	options.setClass( 'options' );
	container.add( options );
M
Mr.doob 已提交
30

31
	// New
32

M
Mr.doob 已提交
33
	var option = new UI.Row();
34 35 36
	option.setClass( 'option' );
	option.setTextContent( 'New' );
	option.onClick( function () {
37

M
Mr.doob 已提交
38
		if ( confirm( 'Any unsaved data will be lost. Are you sure?' ) ) {
39

M
Mr.doob 已提交
40
			editor.clear();
M
Mr.doob 已提交
41 42

		}
43

44 45
	} );
	options.add( option );
M
Mr.doob 已提交
46

47
	//
48

49
	options.add( new UI.HorizontalRule() );
50

51
	// Import
52

53 54 55 56
	var form = document.createElement( 'form' );
	form.style.display = 'none';
	document.body.appendChild( form );

57 58 59
	var fileInput = document.createElement( 'input' );
	fileInput.type = 'file';
	fileInput.addEventListener( 'change', function ( event ) {
60

61
		editor.loader.loadFile( fileInput.files[ 0 ] );
62
		form.reset();
M
Mr.doob 已提交
63

64
	} );
65
	form.appendChild( fileInput );
M
Mr.doob 已提交
66

M
Mr.doob 已提交
67
	var option = new UI.Row();
68 69 70
	option.setClass( 'option' );
	option.setTextContent( 'Import' );
	option.onClick( function () {
M
Mr.doob 已提交
71

72
		fileInput.click();
M
Mr.doob 已提交
73

74 75
	} );
	options.add( option );
76

77
	//
78

79
	options.add( new UI.HorizontalRule() );
80

81
	// Export Geometry
82

M
Mr.doob 已提交
83
	var option = new UI.Row();
84 85 86
	option.setClass( 'option' );
	option.setTextContent( 'Export Geometry' );
	option.onClick( function () {
87

88
		var object = editor.selected;
89

90
		if ( object === null ) {
91

92 93
			alert( 'No object selected.' );
			return;
94

95
		}
96

97 98 99 100 101 102
		var geometry = object.geometry;

		if ( geometry === undefined ) {

			alert( 'The selected object doesn\'t have geometry.' );
			return;
103

104
		}
105

106
		var output = geometry.toJSON();
M
Mr.doob 已提交
107

M
makc 已提交
108
		try {
M
Mr.doob 已提交
109

110
			output = JSON.stringify( output, parseNumber, '\t' );
M
makc 已提交
111
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
112

M
Mr.doob 已提交
113
		} catch ( e ) {
M
Mr.doob 已提交
114

M
makc 已提交
115
			output = JSON.stringify( output );
M
Mr.doob 已提交
116

M
makc 已提交
117
		}
118

M
Mr.doob 已提交
119
		saveString( output, 'geometry.json' );
120

121 122 123 124
	} );
	options.add( option );

	// Export Object
M
Mr.doob 已提交
125

M
Mr.doob 已提交
126
	var option = new UI.Row();
127 128 129
	option.setClass( 'option' );
	option.setTextContent( 'Export Object' );
	option.onClick( function () {
130

131 132 133
		var object = editor.selected;

		if ( object === null ) {
134 135 136 137 138 139

			alert( 'No object selected' );
			return;

		}

140
		var output = object.toJSON();
M
Mr.doob 已提交
141

M
makc 已提交
142
		try {
M
Mr.doob 已提交
143

144
			output = JSON.stringify( output, parseNumber, '\t' );
M
makc 已提交
145
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
146

M
Mr.doob 已提交
147
		} catch ( e ) {
M
Mr.doob 已提交
148

M
makc 已提交
149
			output = JSON.stringify( output );
M
Mr.doob 已提交
150

M
makc 已提交
151
		}
152

M
Mr.doob 已提交
153
		saveString( output, 'model.json' );
154

155 156 157 158
	} );
	options.add( option );

	// Export Scene
159

M
Mr.doob 已提交
160
	var option = new UI.Row();
161 162 163
	option.setClass( 'option' );
	option.setTextContent( 'Export Scene' );
	option.onClick( function () {
164

165
		var output = editor.scene.toJSON();
M
Mr.doob 已提交
166

M
makc 已提交
167
		try {
M
Mr.doob 已提交
168

169
			output = JSON.stringify( output, parseNumber, '\t' );
M
makc 已提交
170
			output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
M
Mr.doob 已提交
171

M
Mr.doob 已提交
172
		} catch ( e ) {
M
Mr.doob 已提交
173

M
makc 已提交
174
			output = JSON.stringify( output );
M
Mr.doob 已提交
175

M
makc 已提交
176
		}
177

M
Mr.doob 已提交
178
		saveString( output, 'scene.json' );
179

180 181
	} );
	options.add( option );
M
Mr.doob 已提交
182

M
Mr.doob 已提交
183 184 185 186
	//

	options.add( new UI.HorizontalRule() );

M
Mr.doob 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199
	// Export GLB

	var option = new UI.Row();
	option.setClass( 'option' );
	option.setTextContent( 'Export GLB' );
	option.onClick( function () {

		var exporter = new THREE.GLTFExporter();

		exporter.parse( editor.scene, function ( result ) {

			saveArrayBuffer( result, 'scene.glb' );

200 201 202
			// forceIndices: true, forcePowerOfTwoTexture: true
			// to allow compatibility with facebook
		}, { binary: true, forceIndices: true, forcePowerOfTwoTexture: true } );
203
		
M
Mr.doob 已提交
204 205 206
	} );
	options.add( option );

M
Mr.doob 已提交
207
	// Export GLTF
208

M
Mr.doob 已提交
209
	var option = new UI.Row();
210
	option.setClass( 'option' );
M
Mr.doob 已提交
211
	option.setTextContent( 'Export GLTF' );
212
	option.onClick( function () {
213

M
Mr.doob 已提交
214
		var exporter = new THREE.GLTFExporter();
215

M
Mr.doob 已提交
216
		exporter.parse( editor.scene, function ( result ) {
217

M
Mr.doob 已提交
218
			saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
219

M
Mr.doob 已提交
220
		} );
221

222

223 224 225
	} );
	options.add( option );

M
Mr.doob 已提交
226
	// Export OBJ
F
Fernando Serrano 已提交
227 228 229

	var option = new UI.Row();
	option.setClass( 'option' );
M
Mr.doob 已提交
230
	option.setTextContent( 'Export OBJ' );
F
Fernando Serrano 已提交
231 232
	option.onClick( function () {

M
Mr.doob 已提交
233
		var object = editor.selected;
F
Fernando Serrano 已提交
234

M
Mr.doob 已提交
235
		if ( object === null ) {
F
Fernando Serrano 已提交
236

M
Mr.doob 已提交
237 238
			alert( 'No object selected.' );
			return;
F
Fernando Serrano 已提交
239

M
Mr.doob 已提交
240
		}
F
Fernando Serrano 已提交
241

M
Mr.doob 已提交
242 243 244
		var exporter = new THREE.OBJExporter();

		saveString( exporter.parse( object ), 'model.obj' );
F
Fernando Serrano 已提交
245 246 247 248

	} );
	options.add( option );

249
	// Export STL
250

M
Mr.doob 已提交
251
	var option = new UI.Row();
252 253 254
	option.setClass( 'option' );
	option.setTextContent( 'Export STL' );
	option.onClick( function () {
M
Mr.doob 已提交
255

M
Mr.doob 已提交
256
		var exporter = new THREE.STLExporter();
257

M
Mr.doob 已提交
258
		saveString( exporter.parse( editor.scene ), 'model.stl' );
M
Mr.doob 已提交
259

260 261 262
	} );
	options.add( option );

M
Mr.doob 已提交
263
	//
264 265

	options.add( new UI.HorizontalRule() );
M
Mr.doob 已提交
266

267
	// Publish
268

M
Mr.doob 已提交
269
	var option = new UI.Row();
270
	option.setClass( 'option' );
271
	option.setTextContent( 'Publish' );
272
	option.onClick( function () {
273

M
Mr.doob 已提交
274 275 276 277
		var zip = new JSZip();

		//

M
Mr.doob 已提交
278
		var output = editor.toJSON();
M
Mr.doob 已提交
279 280 281
		output.metadata.type = 'App';
		delete output.history;

282 283
		var vr = output.project.vr;

284
		output = JSON.stringify( output, parseNumber, '\t' );
M
Mr.doob 已提交
285 286
		output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

M
Mr.doob 已提交
287
		zip.file( 'app.json', output );
M
Mr.doob 已提交
288 289 290

		//

M
Mr.doob 已提交
291 292
		var title = config.getKey( 'project/title' );

M
Mr.doob 已提交
293 294
		var manager = new THREE.LoadingManager( function () {

M
Mr.doob 已提交
295
			save( zip.generate( { type: 'blob' } ), ( title !== '' ? title : 'untitled' ) + '.zip' );
M
Mr.doob 已提交
296 297 298

		} );

299
		var loader = new THREE.FileLoader( manager );
300 301
		loader.load( 'js/libs/app/index.html', function ( content ) {

M
Mr.doob 已提交
302 303
			content = content.replace( '<!-- title -->', title );

304 305 306 307
			var includes = [];

			if ( vr ) {

M
Mr.doob 已提交
308
				includes.push( '<script src="js/WebVR.js"></script>' );
309 310 311 312 313

			}

			content = content.replace( '<!-- includes -->', includes.join( '\n\t\t' ) );

314 315
			var editButton = '';

M
Mr.doob 已提交
316
			if ( config.getKey( 'project/editable' ) ) {
317

M
Mr.doob 已提交
318 319 320 321 322 323 324 325 326 327
				editButton = [
					'',
					'			var button = document.createElement( \'a\' );',
					'			button.href = \'https://threejs.org/editor/#file=\' + location.href.split( \'/\' ).slice( 0, - 1 ).join( \'/\' ) + \'/app.json\';',
					'			button.style.cssText = \'position: absolute; bottom: 20px; right: 20px; padding: 12px 14px; color: #fff; border: 1px solid #fff; border-radius: 4px; text-decoration: none;\';',
					'			button.target = \'_blank\';',
					'			button.textContent = \'EDIT\';',
					'			document.body.appendChild( button );',
					''
				].join( '\n' );
328 329
			}

M
Mr.doob 已提交
330
			content = content.replace( '\n\t\t\t/* edit button */\n', editButton );
331

332 333 334
			zip.file( 'index.html', content );

		} );
M
Mr.doob 已提交
335
		loader.load( 'js/libs/app.js', function ( content ) {
M
Mr.doob 已提交
336

M
Mr.doob 已提交
337
			zip.file( 'js/app.js', content );
M
Mr.doob 已提交
338 339

		} );
M
Mr.doob 已提交
340
		loader.load( '../build/three.min.js', function ( content ) {
M
Mr.doob 已提交
341

M
Mr.doob 已提交
342
			zip.file( 'js/three.min.js', content );
M
Mr.doob 已提交
343 344

		} );
345

346
		if ( vr ) {
347

M
Mr.doob 已提交
348
			loader.load( '../examples/js/vr/WebVR.js', function ( content ) {
M
Mr.doob 已提交
349 350 351 352 353

				zip.file( 'js/WebVR.js', content );

			} );

354
		}
355

356 357
	} );
	options.add( option );
358

359
	//
360

M
Mr.doob 已提交
361 362 363 364
	var link = document.createElement( 'a' );
	link.style.display = 'none';
	document.body.appendChild( link ); // Firefox workaround, see #6594

M
Mr.doob 已提交
365
	function save( blob, filename ) {
366

M
Mr.doob 已提交
367
		link.href = URL.createObjectURL( blob );
M
Mr.doob 已提交
368
		link.download = filename || 'data.json';
M
Mr.doob 已提交
369 370 371 372 373 374
		link.click();

		// URL.revokeObjectURL( url ); breaks Firefox...

	}

M
Mr.doob 已提交
375 376 377 378 379 380
	function saveArrayBuffer( buffer, filename ) {

		save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );

	}

M
Mr.doob 已提交
381
	function saveString( text, filename ) {
G
gero3 已提交
382

M
Mr.doob 已提交
383
		save( new Blob( [ text ], { type: 'text/plain' } ), filename );
384

M
Mr.doob 已提交
385
	}
386

387
	return container;
M
Mr.doob 已提交
388

389
};