misc_exporter_gltf.html 13.6 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
		</div>
F
Fernando Serrano 已提交
32 33 34 35 36 37 38 39

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

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

		<script>

40
			function exportGLTF( input ) {
F
Fernando Serrano 已提交
41

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

44
				gltfExporter.parse( input, function( result ) {
F
Fernando Serrano 已提交
45

46 47 48
					var output = JSON.stringify( result, null, 2 );
					console.log( output );
					saveString( output, 'scene.gltf' );
F
Fernando Serrano 已提交
49

50
				} );
F
Fernando Serrano 已提交
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
			}

			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 ] );

76
			} );
F
Fernando Serrano 已提交
77

78 79 80 81 82 83 84
			document.getElementById( 'export_scene_object' ).addEventListener( 'click', function () {

				exportGLTF( [ scene1, gridHelper ] );

			} );


85 86 87
			var link = document.createElement( 'a' );
			link.style.display = 'none';
			document.body.appendChild( link ); // Firefox workaround, see #6594
F
Fernando Serrano 已提交
88

89
			function save( blob, filename ) {
F
Fernando Serrano 已提交
90

91 92 93
				link.href = URL.createObjectURL( blob );
				link.download = filename || 'data.json';
				link.click();
F
Fernando Serrano 已提交
94

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

97 98 99
			}

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

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

103
			}
F
Fernando Serrano 已提交
104 105 106

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

107
			var container;
F
Fernando Serrano 已提交
108

109 110
			var camera, scene1, scene2, renderer;
			var gridHelper, sphere;
F
Fernando Serrano 已提交
111 112 113 114 115 116 117 118 119

			init();
			animate();

			function init() {

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

120 121
				scene1 = new THREE.Scene();
				scene1.name = 'Scene1';
F
Fernando Serrano 已提交
122

123 124 125 126 127 128 129 130
				// ---------------------------------------------------------------------
				// 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 已提交
131

132 133 134
				// ---------------------------------------------------------------------
				// Ambient light
				// ---------------------------------------------------------------------
135
				scene1.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
F
Fernando Serrano 已提交
136

137 138 139
				// ---------------------------------------------------------------------
				// DirectLight
				// ---------------------------------------------------------------------
140
				var light = new THREE.DirectionalLight( 0xffffff, 1 );
F
Fernando Serrano 已提交
141
				light.position.set( 1, 1, 0 );
F
Fernando Serrano 已提交
142
				scene1.add( light );
F
Fernando Serrano 已提交
143

144 145 146
				// ---------------------------------------------------------------------
				// Grid
				// ---------------------------------------------------------------------
147
				gridHelper = new THREE.GridHelper( 2000, 20 );
F
Fernando Serrano 已提交
148
				gridHelper.position.y = -50;
149 150
				gridHelper.name = "Grid";
				scene1.add( gridHelper );
F
Fernando Serrano 已提交
151

152 153 154 155 156 157
				// ---------------------------------------------------------------------
				// Axis
				// ---------------------------------------------------------------------
				var axis = new THREE.AxisHelper(500);
				axis.name = "AxisHelper";
				scene1.add( axis );
F
Fernando Serrano 已提交
158 159 160 161 162

				// ---------------------------------------------------------------------
				// Simple geometry with basic material
				// ---------------------------------------------------------------------
				// Icosahedron
163 164
				var mapGrid = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
				mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
165
				var material = new THREE.MeshBasicMaterial( {
166 167
					color: 0xffffff,
					map: mapGrid
168
				} );
F
Fernando Serrano 已提交
169

170
				object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
F
Fernando Serrano 已提交
171
				object.position.set( -200, 0, 200 );
172 173
				object.name = 'Icosahedron';
				scene1.add( object );
F
Fernando Serrano 已提交
174

F
Fernando Serrano 已提交
175 176 177 178 179
				// Octahedron
				material = new THREE.MeshBasicMaterial( {
					color: 0x0000ff,
					wireframe: true
				} );
180
				object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
F
Fernando Serrano 已提交
181
				object.position.set( 0, 0, 200 );
182 183
				object.name = 'Octahedron';
				scene1.add( object );
F
Fernando Serrano 已提交
184

F
Fernando Serrano 已提交
185 186
				// Tetrahedron
				material = new THREE.MeshBasicMaterial( {
187 188 189
					color: 0xff0000,
					transparent: true,
					opacity: 0.5
F
Fernando Serrano 已提交
190 191
				} );

F
Fernando Serrano 已提交
192 193
				object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
				object.position.set( 200, 0, 200 );
194 195
				object.name = 'Tetrahedron';
				scene1.add( object );
F
Fernando Serrano 已提交
196

F
Fernando Serrano 已提交
197 198 199
				// ---------------------------------------------------------------------
				// Buffered geometry primitives
				// ---------------------------------------------------------------------
F
Fernando Serrano 已提交
200 201
				// Sphere
				material = new THREE.MeshStandardMaterial( {
F
Fernando Serrano 已提交
202
					color: 0xffff00,
F
Fernando Serrano 已提交
203
					metalness: 0.5,
F
Fernando Serrano 已提交
204 205
					roughness: 1.0,
					flatShading: true
F
Fernando Serrano 已提交
206
				} );
207 208 209 210
				sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 70, 10, 10 ), material );
				sphere.position.set( 0, 0, 0 );
				sphere.name = "Sphere";
				scene1.add( sphere );
211

F
Fernando Serrano 已提交
212
				// Cylinder
F
Fernando Serrano 已提交
213 214 215 216
				material = new THREE.MeshStandardMaterial( {
					color: 0xff00ff,
					flatShading: true
				} );
F
Fernando Serrano 已提交
217 218 219
				object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 10, 80, 100 ), material );
				object.position.set( 200, 0, 0 );
				object.name = "Cylinder";
220
				scene1.add( object );
F
Fernando Serrano 已提交
221

F
Fernando Serrano 已提交
222
				// TorusKnot
F
Fernando Serrano 已提交
223
				material = new THREE.MeshStandardMaterial( {
224 225
					color: 0xff0000,
					roughness: 1
F
Fernando Serrano 已提交
226
				} );
227
				object = new THREE.Mesh( new THREE.TorusKnotGeometry( 50, 15, 40, 10 ), material );
F
Fernando Serrano 已提交
228 229
				object.position.set( -200, 0, 0 );
				object.name = "Cylinder";
230
				scene1.add( object );
F
Fernando Serrano 已提交
231

F
Fernando Serrano 已提交
232

F
Fernando Serrano 已提交
233 234 235 236 237
				// ---------------------------------------------------------------------
				// Hierarchy
				// ---------------------------------------------------------------------
				var mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
				material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
F
Fernando Serrano 已提交
238

F
Fernando Serrano 已提交
239 240
				object = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 100, 100 ), material );
				object.position.set( -200, 0, 400 );
F
Fernando Serrano 已提交
241
				object.name = "Cube";
242
				scene1.add( object );
F
Fernando Serrano 已提交
243

F
Fernando Serrano 已提交
244
				object2 = new THREE.Mesh( new THREE.BoxBufferGeometry( 40, 40, 40, 2, 2, 2 ), material );
F
Fernando Serrano 已提交
245
				object2.position.set( 0, 0, 50 );
F
Fernando Serrano 已提交
246
				object2.rotation.set( 0, 45, 0 );
F
Fernando Serrano 已提交
247
				object2.name = "SubCube";
F
Fernando Serrano 已提交
248
				object.add( object2 );
249

F
Fernando Serrano 已提交
250 251 252 253

				// ---------------------------------------------------------------------
				// Groups
				// ---------------------------------------------------------------------
F
Fernando Serrano 已提交
254 255
				group1 = new THREE.Group();
				group1.name = "Group";
256
				scene1.add( group1 );
F
Fernando Serrano 已提交
257 258 259

				group2 = new THREE.Group();
				group2.name = "subGroup";
F
Fernando Serrano 已提交
260
				group2.position.set( 0, 50, 0);
F
Fernando Serrano 已提交
261
				group1.add( group2 );
262 263 264

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

F
Fernando Serrano 已提交
268
				// ---------------------------------------------------------------------
269
				// Triangle Strip
F
Fernando Serrano 已提交
270
				// ---------------------------------------------------------------------
271
				var geometry = new THREE.BufferGeometry();
F
Fernando Serrano 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
				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,
				]);
289 290

				geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
F
Fernando Serrano 已提交
291
				geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
292
				object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: THREE.VertexColors } ) );
F
Fernando Serrano 已提交
293
				object.position.set( 140, -40, -250);
294
				object.setDrawMode( THREE.TriangleStripDrawMode );
F
Fernando Serrano 已提交
295
				object.name = 'Custom buffered';
296 297
				object.userData = { data: 'customdata', list: [ 1,2,3,4 ] };

F
Fernando Serrano 已提交
298
				scene1.add( object );
299 300


F
Fernando Serrano 已提交
301
				// ---------------------------------------------------------------------
302
				// Line Strip
F
Fernando Serrano 已提交
303
				// ---------------------------------------------------------------------
304 305 306 307 308 309 310 311 312 313 314 315
				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 已提交
316 317
				object.position.set(-50, 0, -200);
				scene1.add( object );
318 319


F
Fernando Serrano 已提交
320
				// ---------------------------------------------------------------------
321
				// Line Loop
F
Fernando Serrano 已提交
322
				// ---------------------------------------------------------------------
323 324
				var geometry = new THREE.BufferGeometry();
				var numPoints = 5;
F
Fernando Serrano 已提交
325
				var radius = 70;
326 327 328 329 330 331 332 333 334 335 336
				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 已提交
337
				object.position.set(0, 0, -200);
338

F
Fernando Serrano 已提交
339
				scene1.add( object );
340

F
Fernando Serrano 已提交
341
				// ---------------------------------------------------------------------
F
Fernando Serrano 已提交
342
				// Points
F
Fernando Serrano 已提交
343
				// ---------------------------------------------------------------------
344 345 346 347
				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 已提交
348
					pointsArray[ 3 * i + 1 ] = Math.random() * 100;
349
					pointsArray[ 3 * i + 2 ] = -50 + Math.random() * 100;
F
Fernando Serrano 已提交
350 351 352
				}

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

F
Fernando Serrano 已提交
355
				var pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
F
Fernando Serrano 已提交
356 357
				var points = new THREE.Points( pointsGeo, pointsMaterial );
				points.name = "Points";
358 359
				points.position.set( -200, 0, -200);
				scene1.add( points );
F
Fernando Serrano 已提交
360

361 362 363
				// ---------------------------------------------------------------------
				// Ortho camera
				// ---------------------------------------------------------------------
F
Fernando Serrano 已提交
364
				var cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10, 10 );
365
				scene1.add( cameraOrtho );
F
Fernando Serrano 已提交
366
				cameraOrtho.name = 'OrthographicCamera';
F
Fernando Serrano 已提交
367

368
				material = new THREE.MeshLambertMaterial( {
369 370 371 372
					color: 0xffff00,
					side: THREE.DoubleSide
				} );

F
Fernando Serrano 已提交
373
				object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
F
Fernando Serrano 已提交
374
				object.position.set( 200, 0, -400 );
375
				scene1.add( object );
F
Fernando Serrano 已提交
376 377

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

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

F
Fernando Serrano 已提交
385 386 387 388 389 390 391 392 393 394
				//
				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 );
395
				object.position.set( 200, 0, 400 );
396
				scene1.add( object );
F
Fernando Serrano 已提交
397

F
Fernando Serrano 已提交
398

F
Fernando Serrano 已提交
399 400 401
				// ---------------------------------------------------------------------
				// 2nd Scene
				// ---------------------------------------------------------------------
402
				scene2 = new THREE.Scene();
403 404 405 406 407 408
				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 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
				//

				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 已提交
444
				var timer = Date.now() * 0.0001;
F
Fernando Serrano 已提交
445

F
Fernando Serrano 已提交
446 447
				camera.position.x = Math.cos( timer ) * 800;
				camera.position.z = Math.sin( timer ) * 800;
F
Fernando Serrano 已提交
448

449 450
				camera.lookAt( scene1.position );
				renderer.render( scene1, camera );
F
Fernando Serrano 已提交
451 452 453 454 455 456 457

			}

		</script>

	</body>
</html>