misc_exporter_gltf.html 13.8 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 32
			<br/>
			<input id="option_trs" type="checkbox" value="trs"/> TRS
33
		</div>
F
Fernando Serrano 已提交
34 35 36 37 38 39 40 41

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

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

		<script>

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

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

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

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

55
				}, options );
F
Fernando Serrano 已提交
56

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

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

81
			} );
F
Fernando Serrano 已提交
82

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

				exportGLTF( [ scene1, gridHelper ] );

			} );


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

94
			function save( blob, filename ) {
F
Fernando Serrano 已提交
95

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

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

102 103 104
			}

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

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

108
			}
F
Fernando Serrano 已提交
109 110 111

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

112
			var container;
F
Fernando Serrano 已提交
113

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

			init();
			animate();

			function init() {

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

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

128 129 130 131 132 133 134 135
				// ---------------------------------------------------------------------
				// 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 已提交
136

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

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

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

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

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

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

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

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

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

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

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

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

F
Fernando Serrano 已提交
237

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

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

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

F
Fernando Serrano 已提交
255 256 257 258

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

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

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

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

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

F
Fernando Serrano 已提交
303
				scene1.add( object );
304 305


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


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

F
Fernando Serrano 已提交
344
				scene1.add( object );
345

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

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

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

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

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

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

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

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

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

F
Fernando Serrano 已提交
403

F
Fernando Serrano 已提交
404 405 406
				// ---------------------------------------------------------------------
				// 2nd Scene
				// ---------------------------------------------------------------------
407
				scene2 = new THREE.Scene();
408 409 410 411 412 413
				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 已提交
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 444 445 446 447 448
				//

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

F
Fernando Serrano 已提交
451 452
				camera.position.x = Math.cos( timer ) * 800;
				camera.position.z = Math.sin( timer ) * 800;
F
Fernando Serrano 已提交
453

454 455
				camera.lookAt( scene1.position );
				renderer.render( scene1, camera );
F
Fernando Serrano 已提交
456 457 458 459 460 461 462

			}

		</script>

	</body>
</html>