misc_exporter_gltf.html 14.5 KB
Newer Older
F
Fernando Serrano 已提交
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
F
Fernando Serrano 已提交
4
		<title>three.js webgl - gltf exporter</title>
F
Fernando Serrano 已提交
5 6 7 8 9 10 11 12 13
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				margin: 0px;
				overflow: hidden;
			}
14
			#info {
15
				color: #ccc;
16 17 18 19 20
				text-align: center;
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}
F
Fernando Serrano 已提交
21 22 23
		</style>
	</head>
	<body>
24
		<div id="info">
25
			GLTF Exporter<br/>
26 27 28 29 30
			<button id="export_scene">Export Scene1</button>
			<button id="export_scenes">Export Scene1 and Scene 2</button>
			<button id="export_object">Export Sphere</button>
			<button id="export_objects">Export Sphere and Grid</button>
			<button id="export_scene_object">Export Scene1 and Sphere</button>
31
			<br/>
32 33
			<label><input id="option_trs" name="trs" type="checkbox"/>TRS</label>
			<label><input id="option_visible" name="visible" type="checkbox" checked="checked"/>Only Visible</label>
34
		</div>
F
Fernando Serrano 已提交
35 36 37 38 39 40 41 42

		<script src="../build/three.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/exporters/GLTFExporter.js"></script>

		<script>

43
			function exportGLTF( input ) {
F
Fernando Serrano 已提交
44

45
				var gltfExporter = new THREE.GLTFExporter();
F
Fernando Serrano 已提交
46

47
				var options = {
48 49
					trs: document.getElementById('option_trs').checked,
					onlyVisible: document.getElementById('option_visible').checked,
50
				}
51
				gltfExporter.parse( input, function( result ) {
F
Fernando Serrano 已提交
52

53 54 55
					var output = JSON.stringify( result, null, 2 );
					console.log( output );
					saveString( output, 'scene.gltf' );
F
Fernando Serrano 已提交
56

57
				}, options );
F
Fernando Serrano 已提交
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
			}

			document.getElementById( 'export_scene' ).addEventListener( 'click', function () {

				exportGLTF( scene1 );

			} );

			document.getElementById( 'export_scenes' ).addEventListener( 'click', function () {

				exportGLTF( [ scene1, scene2 ] );

			} );

			document.getElementById( 'export_object' ).addEventListener( 'click', function () {

				exportGLTF( sphere );

			} );

			document.getElementById( 'export_objects' ).addEventListener( 'click', function () {

				exportGLTF( [ sphere, gridHelper ] );

83
			} );
F
Fernando Serrano 已提交
84

85 86 87 88 89 90 91
			document.getElementById( 'export_scene_object' ).addEventListener( 'click', function () {

				exportGLTF( [ scene1, gridHelper ] );

			} );


92 93 94
			var link = document.createElement( 'a' );
			link.style.display = 'none';
			document.body.appendChild( link ); // Firefox workaround, see #6594
F
Fernando Serrano 已提交
95

96
			function save( blob, filename ) {
F
Fernando Serrano 已提交
97

98 99 100
				link.href = URL.createObjectURL( blob );
				link.download = filename || 'data.json';
				link.click();
F
Fernando Serrano 已提交
101

102
				// URL.revokeObjectURL( url ); breaks Firefox...
F
Fernando Serrano 已提交
103

104 105 106
			}

			function saveString( text, filename ) {
F
Fernando Serrano 已提交
107

108
				save( new Blob( [ text ], { type: 'text/plain' } ), filename );
F
Fernando Serrano 已提交
109

110
			}
F
Fernando Serrano 已提交
111 112 113

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

114
			var container;
F
Fernando Serrano 已提交
115

116 117
			var camera, scene1, scene2, renderer;
			var gridHelper, sphere;
F
Fernando Serrano 已提交
118 119 120 121 122 123 124 125 126

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

127 128
				scene1 = new THREE.Scene();
				scene1.name = 'Scene1';
F
Fernando Serrano 已提交
129

130 131 132 133 134 135 136 137
				// ---------------------------------------------------------------------
				// Perspective Camera
				// ---------------------------------------------------------------------
				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.set(600, 400, 0);

				camera.name = "PerspectiveCamera";
				scene1.add( camera );
F
Fernando Serrano 已提交
138

139 140 141
				// ---------------------------------------------------------------------
				// Ambient light
				// ---------------------------------------------------------------------
142
				scene1.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
F
Fernando Serrano 已提交
143

144 145 146
				// ---------------------------------------------------------------------
				// DirectLight
				// ---------------------------------------------------------------------
147
				var light = new THREE.DirectionalLight( 0xffffff, 1 );
F
Fernando Serrano 已提交
148
				light.position.set( 1, 1, 0 );
F
Fernando Serrano 已提交
149
				scene1.add( light );
F
Fernando Serrano 已提交
150

151 152 153
				// ---------------------------------------------------------------------
				// Grid
				// ---------------------------------------------------------------------
154
				gridHelper = new THREE.GridHelper( 2000, 20 );
F
Fernando Serrano 已提交
155
				gridHelper.position.y = -50;
156 157
				gridHelper.name = "Grid";
				scene1.add( gridHelper );
F
Fernando Serrano 已提交
158

159 160 161 162 163 164
				// ---------------------------------------------------------------------
				// Axis
				// ---------------------------------------------------------------------
				var axis = new THREE.AxisHelper(500);
				axis.name = "AxisHelper";
				scene1.add( axis );
F
Fernando Serrano 已提交
165 166 167 168 169

				// ---------------------------------------------------------------------
				// Simple geometry with basic material
				// ---------------------------------------------------------------------
				// Icosahedron
170 171
				var mapGrid = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
				mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
172
				var material = new THREE.MeshBasicMaterial( {
173 174
					color: 0xffffff,
					map: mapGrid
175
				} );
F
Fernando Serrano 已提交
176

177
				object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
F
Fernando Serrano 已提交
178
				object.position.set( -200, 0, 200 );
179 180
				object.name = 'Icosahedron';
				scene1.add( object );
F
Fernando Serrano 已提交
181

F
Fernando Serrano 已提交
182 183 184 185 186
				// Octahedron
				material = new THREE.MeshBasicMaterial( {
					color: 0x0000ff,
					wireframe: true
				} );
187
				object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
F
Fernando Serrano 已提交
188
				object.position.set( 0, 0, 200 );
189 190
				object.name = 'Octahedron';
				scene1.add( object );
F
Fernando Serrano 已提交
191

F
Fernando Serrano 已提交
192 193
				// Tetrahedron
				material = new THREE.MeshBasicMaterial( {
194 195 196
					color: 0xff0000,
					transparent: true,
					opacity: 0.5
F
Fernando Serrano 已提交
197 198
				} );

F
Fernando Serrano 已提交
199 200
				object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
				object.position.set( 200, 0, 200 );
201 202
				object.name = 'Tetrahedron';
				scene1.add( object );
F
Fernando Serrano 已提交
203

F
Fernando Serrano 已提交
204 205 206
				// ---------------------------------------------------------------------
				// Buffered geometry primitives
				// ---------------------------------------------------------------------
F
Fernando Serrano 已提交
207 208
				// Sphere
				material = new THREE.MeshStandardMaterial( {
F
Fernando Serrano 已提交
209
					color: 0xffff00,
F
Fernando Serrano 已提交
210
					metalness: 0.5,
F
Fernando Serrano 已提交
211 212
					roughness: 1.0,
					flatShading: true
F
Fernando Serrano 已提交
213
				} );
214 215 216 217
				sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 70, 10, 10 ), material );
				sphere.position.set( 0, 0, 0 );
				sphere.name = "Sphere";
				scene1.add( sphere );
218

F
Fernando Serrano 已提交
219
				// Cylinder
F
Fernando Serrano 已提交
220 221 222 223
				material = new THREE.MeshStandardMaterial( {
					color: 0xff00ff,
					flatShading: true
				} );
F
Fernando Serrano 已提交
224 225 226
				object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 10, 80, 100 ), material );
				object.position.set( 200, 0, 0 );
				object.name = "Cylinder";
227
				scene1.add( object );
F
Fernando Serrano 已提交
228

F
Fernando Serrano 已提交
229
				// TorusKnot
F
Fernando Serrano 已提交
230
				material = new THREE.MeshStandardMaterial( {
231 232
					color: 0xff0000,
					roughness: 1
F
Fernando Serrano 已提交
233
				} );
234
				object = new THREE.Mesh( new THREE.TorusKnotGeometry( 50, 15, 40, 10 ), material );
F
Fernando Serrano 已提交
235 236
				object.position.set( -200, 0, 0 );
				object.name = "Cylinder";
237
				scene1.add( object );
F
Fernando Serrano 已提交
238

F
Fernando Serrano 已提交
239

F
Fernando Serrano 已提交
240 241 242 243 244
				// ---------------------------------------------------------------------
				// Hierarchy
				// ---------------------------------------------------------------------
				var mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
				material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
F
Fernando Serrano 已提交
245

F
Fernando Serrano 已提交
246 247
				object = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 100, 100 ), material );
				object.position.set( -200, 0, 400 );
F
Fernando Serrano 已提交
248
				object.name = "Cube";
249
				scene1.add( object );
F
Fernando Serrano 已提交
250

F
Fernando Serrano 已提交
251
				object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 40, 40, 2, 2, 2 ), material );
F
Fernando Serrano 已提交
252
				object2.position.set( 0, 0, 50 );
F
Fernando Serrano 已提交
253
				object2.rotation.set( 0, 45, 0 );
F
Fernando Serrano 已提交
254
				object2.name = "SubCube";
F
Fernando Serrano 已提交
255
				object.add( object2 );
256

F
Fernando Serrano 已提交
257 258 259 260

				// ---------------------------------------------------------------------
				// Groups
				// ---------------------------------------------------------------------
F
Fernando Serrano 已提交
261 262
				group1 = new THREE.Group();
				group1.name = "Group";
263
				scene1.add( group1 );
F
Fernando Serrano 已提交
264 265 266

				group2 = new THREE.Group();
				group2.name = "subGroup";
F
Fernando Serrano 已提交
267
				group2.position.set( 0, 50, 0);
F
Fernando Serrano 已提交
268
				group1.add( group2 );
269 270 271

				object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 30, 30, 30 ), material );
				object2.name = "Cube in group";
F
Fernando Serrano 已提交
272
				object2.position.set( 0, 0, 400 );
273 274
				group2.add( object2 );

F
Fernando Serrano 已提交
275
				// ---------------------------------------------------------------------
276
				// Triangle Strip
F
Fernando Serrano 已提交
277
				// ---------------------------------------------------------------------
278
				var geometry = new THREE.BufferGeometry();
F
Fernando Serrano 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
				var positions = new Float32Array([
					0, 0, 0,
					0, 80, 0,
					80, 0, 0,
					80, 80, 0,
					80, 0, 80,
					80, 80, 80,
				]);

				var colors = new Float32Array([
					1, 0, 0,
					1, 0, 0,
					1, 1, 0,
					1, 1, 0,
					0, 0, 1,
					0, 0, 1,
				]);
296 297

				geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
F
Fernando Serrano 已提交
298
				geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
299
				object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
F
Fernando Serrano 已提交
300
				object.position.set( 140, -40, -250);
301
				object.setDrawMode( THREE.TriangleStripDrawMode );
F
Fernando Serrano 已提交
302
				object.name = 'Custom buffered';
303 304
				object.userData = { data: 'customdata', list: [ 1,2,3,4 ] };

F
Fernando Serrano 已提交
305
				scene1.add( object );
306 307


F
Fernando Serrano 已提交
308
				// ---------------------------------------------------------------------
309
				// Line Strip
F
Fernando Serrano 已提交
310
				// ---------------------------------------------------------------------
311 312 313 314 315 316 317 318 319 320 321 322
				var geometry = new THREE.BufferGeometry();
				var numPoints = 100;
				var positions = new Float32Array( numPoints * 3 );

				for (var i = 0; i < numPoints; i++ ) {
					positions[ i * 3 ] = i;
					positions[ i * 3 + 1 ] = Math.sin( i / 2 ) * 20;
					positions[ i * 3 + 2 ] = 0;
				}

				geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
				object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
F
Fernando Serrano 已提交
323 324
				object.position.set(-50, 0, -200);
				scene1.add( object );
325 326


F
Fernando Serrano 已提交
327
				// ---------------------------------------------------------------------
328
				// Line Loop
F
Fernando Serrano 已提交
329
				// ---------------------------------------------------------------------
330 331
				var geometry = new THREE.BufferGeometry();
				var numPoints = 5;
F
Fernando Serrano 已提交
332
				var radius = 70;
333 334 335 336 337 338 339 340 341 342 343
				var positions = new Float32Array( numPoints * 3 );

				for (var i = 0; i < numPoints; i++ ) {
					var s = i * Math.PI * 2 / numPoints;
					positions[ i * 3 ] = radius * Math.sin ( s );
					positions[ i * 3 + 1 ] = radius * Math.cos ( s );
					positions[ i * 3 + 2 ] = 0;
				}

				geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
				object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
F
Fernando Serrano 已提交
344
				object.position.set(0, 0, -200);
345

F
Fernando Serrano 已提交
346
				scene1.add( object );
347

F
Fernando Serrano 已提交
348
				// ---------------------------------------------------------------------
F
Fernando Serrano 已提交
349
				// Points
F
Fernando Serrano 已提交
350
				// ---------------------------------------------------------------------
351 352 353 354
				var numPoints = 100;
				var pointsArray = new Float32Array( numPoints * 3 );
				for ( var i = 0; i < numPoints; i++ ) {
					pointsArray[ 3 * i ] = -50 + Math.random() * 100;
F
Fernando Serrano 已提交
355
					pointsArray[ 3 * i + 1 ] = Math.random() * 100;
356
					pointsArray[ 3 * i + 2 ] = -50 + Math.random() * 100;
F
Fernando Serrano 已提交
357 358 359
				}

				pointsGeo = new THREE.BufferGeometry();
F
Fernando Serrano 已提交
360
				pointsGeo.addAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
F
Fernando Serrano 已提交
361

F
Fernando Serrano 已提交
362
				var pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
F
Fernando Serrano 已提交
363 364
				var points = new THREE.Points( pointsGeo, pointsMaterial );
				points.name = "Points";
365 366
				points.position.set( -200, 0, -200);
				scene1.add( points );
F
Fernando Serrano 已提交
367

368 369 370
				// ---------------------------------------------------------------------
				// Ortho camera
				// ---------------------------------------------------------------------
F
Fernando Serrano 已提交
371
				var cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10, 10 );
372
				scene1.add( cameraOrtho );
F
Fernando Serrano 已提交
373
				cameraOrtho.name = 'OrthographicCamera';
F
Fernando Serrano 已提交
374

375
				material = new THREE.MeshLambertMaterial( {
376 377 378 379
					color: 0xffff00,
					side: THREE.DoubleSide
				} );

F
Fernando Serrano 已提交
380
				object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
F
Fernando Serrano 已提交
381
				object.position.set( 200, 0, -400 );
382
				scene1.add( object );
F
Fernando Serrano 已提交
383 384

				object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
F
Fernando Serrano 已提交
385
				object.position.set( 0, 0, -400 );
386
				scene1.add( object );
F
Fernando Serrano 已提交
387 388

				object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
F
Fernando Serrano 已提交
389
				object.position.set( -200, 0, -400 );
390
				scene1.add( object );
391

F
Fernando Serrano 已提交
392 393 394 395 396 397 398 399 400 401
				//
				var points = [];

				for ( var i = 0; i < 50; i ++ ) {

					points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * Math.sin( i * 0.1 ) * 15 + 50, ( i - 5 ) * 2 ) );

				}

				object = new THREE.Mesh( new THREE.LatheGeometry( points, 20 ), material );
402
				object.position.set( 200, 0, 400 );
403
				scene1.add( object );
F
Fernando Serrano 已提交
404

405 406 407 408 409 410 411 412 413 414 415
				// ---------------------------------------------------------------------
				// Big red box hidden just for testing `onlyVisible` option
				// ---------------------------------------------------------------------
				material = new THREE.MeshBasicMaterial( {
					color: 0xff0000
				} );
				object = new THREE.Mesh( new THREE.BoxBufferGeometry( 200, 200, 200 ), material );
				object.position.set( 0, 0, 0 );
				object.name = "CubeHidden";
				object.visible = false;
				scene1.add( object );
F
Fernando Serrano 已提交
416

F
Fernando Serrano 已提交
417 418 419
				// ---------------------------------------------------------------------
				// 2nd Scene
				// ---------------------------------------------------------------------
420
				scene2 = new THREE.Scene();
421 422 423 424 425 426
				object = new THREE.Mesh( new THREE.BoxBufferGeometry( 100, 100, 100 ), material );
				object.position.set( 0, 0, 0 );
				object.name = "Cube2ndScene";
				scene2.name = 'Scene2';
				scene2.add(object);

F
Fernando Serrano 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
				//

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );

				container.appendChild( renderer.domElement );

				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();

			}

			function render() {

F
Fernando Serrano 已提交
462
				var timer = Date.now() * 0.0001;
F
Fernando Serrano 已提交
463

F
Fernando Serrano 已提交
464 465
				camera.position.x = Math.cos( timer ) * 800;
				camera.position.z = Math.sin( timer ) * 800;
F
Fernando Serrano 已提交
466

467 468
				camera.lookAt( scene1.position );
				renderer.render( scene1, camera );
F
Fernando Serrano 已提交
469 470 471 472 473 474 475

			}

		</script>

	</body>
</html>