forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc_exporter_gltf.html
543 lines (400 loc) · 15.8 KB
/
misc_exporter_gltf.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - exporter - gltf</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - gltf
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
import { GLTFExporter } from 'three/addons/exporters/GLTFExporter.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
function exportGLTF( input ) {
const gltfExporter = new GLTFExporter();
const options = {
trs: params.trs,
onlyVisible: params.onlyVisible,
binary: params.binary,
maxTextureSize: params.maxTextureSize
};
gltfExporter.parse(
input,
function ( result ) {
if ( result instanceof ArrayBuffer ) {
saveArrayBuffer( result, 'scene.glb' );
} else {
const output = JSON.stringify( result, null, 2 );
console.log( output );
saveString( output, 'scene.gltf' );
}
},
function ( error ) {
console.log( 'An error happened during parsing', error );
},
options
);
}
const link = document.createElement( 'a' );
link.style.display = 'none';
document.body.appendChild( link ); // Firefox workaround, see #6594
function save( blob, filename ) {
link.href = URL.createObjectURL( blob );
link.download = filename;
link.click();
// URL.revokeObjectURL( url ); breaks Firefox...
}
function saveString( text, filename ) {
save( new Blob( [ text ], { type: 'text/plain' } ), filename );
}
function saveArrayBuffer( buffer, filename ) {
save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
}
let container;
let camera, object, object2, material, geometry, scene1, scene2, renderer;
let gridHelper, sphere, waltHead;
const params = {
trs: false,
onlyVisible: true,
binary: false,
maxTextureSize: 4096,
exportScene1: exportScene1,
exportScenes: exportScenes,
exportSphere: exportSphere,
exportHead: exportHead,
exportObjects: exportObjects,
exportSceneObject: exportSceneObject
};
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
// Make linear gradient texture
const data = new Uint8ClampedArray( 100 * 100 * 4 );
for ( let y = 0; y < 100; y ++ ) {
for ( let x = 0; x < 100; x ++ ) {
const stride = 4 * ( 100 * y + x );
data[ stride ] = Math.round( 255 * y / 99 );
data[ stride + 1 ] = Math.round( 255 - 255 * y / 99 );
data[ stride + 2 ] = 0;
data[ stride + 3 ] = 255;
}
}
const gradientTexture = new THREE.DataTexture( data, 100, 100, THREE.RGBAFormat );
gradientTexture.minFilter = THREE.LinearFilter;
gradientTexture.magFilter = THREE.LinearFilter;
gradientTexture.needsUpdate = true;
scene1 = new THREE.Scene();
scene1.name = 'Scene1';
// ---------------------------------------------------------------------
// 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 );
// ---------------------------------------------------------------------
// Ambient light
// ---------------------------------------------------------------------
const ambientLight = new THREE.AmbientLight( 0xffffff, 0.2 );
ambientLight.name = 'AmbientLight';
scene1.add( ambientLight );
// ---------------------------------------------------------------------
// DirectLight
// ---------------------------------------------------------------------
const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
dirLight.target.position.set( 0, 0, - 1 );
dirLight.add( dirLight.target );
dirLight.lookAt( - 1, - 1, 0 );
dirLight.name = 'DirectionalLight';
scene1.add( dirLight );
// ---------------------------------------------------------------------
// Grid
// ---------------------------------------------------------------------
gridHelper = new THREE.GridHelper( 2000, 20, 0x888888, 0x444444 );
gridHelper.position.y = - 50;
gridHelper.name = 'Grid';
scene1.add( gridHelper );
// ---------------------------------------------------------------------
// Axes
// ---------------------------------------------------------------------
const axes = new THREE.AxesHelper( 500 );
axes.name = 'AxesHelper';
scene1.add( axes );
// ---------------------------------------------------------------------
// Simple geometry with basic material
// ---------------------------------------------------------------------
// Icosahedron
const mapGrid = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
mapGrid.wrapS = mapGrid.wrapT = THREE.RepeatWrapping;
material = new THREE.MeshBasicMaterial( {
color: 0xffffff,
map: mapGrid
} );
object = new THREE.Mesh( new THREE.IcosahedronGeometry( 75, 0 ), material );
object.position.set( - 200, 0, 200 );
object.name = 'Icosahedron';
scene1.add( object );
// Octahedron
material = new THREE.MeshBasicMaterial( {
color: 0x0000ff,
wireframe: true
} );
object = new THREE.Mesh( new THREE.OctahedronGeometry( 75, 1 ), material );
object.position.set( 0, 0, 200 );
object.name = 'Octahedron';
scene1.add( object );
// Tetrahedron
material = new THREE.MeshBasicMaterial( {
color: 0xff0000,
transparent: true,
opacity: 0.5
} );
object = new THREE.Mesh( new THREE.TetrahedronGeometry( 75, 0 ), material );
object.position.set( 200, 0, 200 );
object.name = 'Tetrahedron';
scene1.add( object );
// ---------------------------------------------------------------------
// Buffered geometry primitives
// ---------------------------------------------------------------------
// Sphere
material = new THREE.MeshStandardMaterial( {
color: 0xffff00,
metalness: 0.5,
roughness: 1.0,
flatShading: true
} );
material.map = gradientTexture;
sphere = new THREE.Mesh( new THREE.SphereGeometry( 70, 10, 10 ), material );
sphere.position.set( 0, 0, 0 );
sphere.name = 'Sphere';
scene1.add( sphere );
// Cylinder
material = new THREE.MeshStandardMaterial( {
color: 0xff00ff,
flatShading: true
} );
object = new THREE.Mesh( new THREE.CylinderGeometry( 10, 80, 100 ), material );
object.position.set( 200, 0, 0 );
object.name = 'Cylinder';
scene1.add( object );
// TorusKnot
material = new THREE.MeshStandardMaterial( {
color: 0xff0000,
roughness: 1
} );
object = new THREE.Mesh( new THREE.TorusKnotGeometry( 50, 15, 40, 10 ), material );
object.position.set( - 200, 0, 0 );
object.name = 'Cylinder';
scene1.add( object );
// ---------------------------------------------------------------------
// Hierarchy
// ---------------------------------------------------------------------
const mapWood = new THREE.TextureLoader().load( 'textures/hardwood2_diffuse.jpg' );
material = new THREE.MeshStandardMaterial( { map: mapWood, side: THREE.DoubleSide } );
object = new THREE.Mesh( new THREE.BoxGeometry( 40, 100, 100 ), material );
object.position.set( - 200, 0, 400 );
object.name = 'Cube';
scene1.add( object );
object2 = new THREE.Mesh( new THREE.BoxGeometry( 40, 40, 40, 2, 2, 2 ), material );
object2.position.set( 0, 0, 50 );
object2.rotation.set( 0, 45, 0 );
object2.name = 'SubCube';
object.add( object2 );
// ---------------------------------------------------------------------
// Groups
// ---------------------------------------------------------------------
const group1 = new THREE.Group();
group1.name = 'Group';
scene1.add( group1 );
const group2 = new THREE.Group();
group2.name = 'subGroup';
group2.position.set( 0, 50, 0 );
group1.add( group2 );
object2 = new THREE.Mesh( new THREE.BoxGeometry( 30, 30, 30 ), material );
object2.name = 'Cube in group';
object2.position.set( 0, 0, 400 );
group2.add( object2 );
// ---------------------------------------------------------------------
// THREE.Line Strip
// ---------------------------------------------------------------------
geometry = new THREE.BufferGeometry();
let numPoints = 100;
let positions = new Float32Array( numPoints * 3 );
for ( let i = 0; i < numPoints; i ++ ) {
positions[ i * 3 ] = i;
positions[ i * 3 + 1 ] = Math.sin( i / 2 ) * 20;
positions[ i * 3 + 2 ] = 0;
}
geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
object.position.set( - 50, 0, - 200 );
scene1.add( object );
// ---------------------------------------------------------------------
// THREE.Line Loop
// ---------------------------------------------------------------------
geometry = new THREE.BufferGeometry();
numPoints = 5;
const radius = 70;
positions = new Float32Array( numPoints * 3 );
for ( let i = 0; i < numPoints; i ++ ) {
const 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.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
object = new THREE.LineLoop( geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ) );
object.position.set( 0, 0, - 200 );
scene1.add( object );
// ---------------------------------------------------------------------
// THREE.Points
// ---------------------------------------------------------------------
numPoints = 100;
const pointsArray = new Float32Array( numPoints * 3 );
for ( let i = 0; i < numPoints; i ++ ) {
pointsArray[ 3 * i ] = - 50 + Math.random() * 100;
pointsArray[ 3 * i + 1 ] = Math.random() * 100;
pointsArray[ 3 * i + 2 ] = - 50 + Math.random() * 100;
}
const pointsGeo = new THREE.BufferGeometry();
pointsGeo.setAttribute( 'position', new THREE.BufferAttribute( pointsArray, 3 ) );
const pointsMaterial = new THREE.PointsMaterial( { color: 0xffff00, size: 5 } );
const pointCloud = new THREE.Points( pointsGeo, pointsMaterial );
pointCloud.name = 'Points';
pointCloud.position.set( - 200, 0, - 200 );
scene1.add( pointCloud );
// ---------------------------------------------------------------------
// Ortho camera
// ---------------------------------------------------------------------
const cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0.1, 10 );
scene1.add( cameraOrtho );
cameraOrtho.name = 'OrthographicCamera';
material = new THREE.MeshLambertMaterial( {
color: 0xffff00,
side: THREE.DoubleSide
} );
object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
object.position.set( 200, 0, - 400 );
scene1.add( object );
object = new THREE.Mesh( new THREE.RingGeometry( 10, 50, 20, 5, 0, Math.PI * 2 ), material );
object.position.set( 0, 0, - 400 );
scene1.add( object );
object = new THREE.Mesh( new THREE.CylinderGeometry( 25, 75, 100, 40, 5 ), material );
object.position.set( - 200, 0, - 400 );
scene1.add( object );
//
const points = [];
for ( let 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 );
object.position.set( 200, 0, 400 );
scene1.add( object );
// ---------------------------------------------------------------------
// Big red box hidden just for testing `onlyVisible` option
// ---------------------------------------------------------------------
material = new THREE.MeshBasicMaterial( {
color: 0xff0000
} );
object = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), material );
object.position.set( 0, 0, 0 );
object.name = 'CubeHidden';
object.visible = false;
scene1.add( object );
// ---------------------------------------------------------------------
//
//
const loader = new OBJLoader();
loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
waltHead = obj;
waltHead.scale.multiplyScalar( 1.5 );
waltHead.position.set( 200, - 40, - 200 );
scene1.add( waltHead );
} );
// ---------------------------------------------------------------------
// 2nd THREE.Scene
// ---------------------------------------------------------------------
scene2 = new THREE.Scene();
object = new THREE.Mesh( new THREE.BoxGeometry( 100, 100, 100 ), material );
object.position.set( 0, 0, 0 );
object.name = 'Cube2ndScene';
scene2.name = 'Scene2';
scene2.add( object );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize );
const gui = new GUI();
let h = gui.addFolder( 'Settings' );
h.add( params, 'trs' ).name( 'Use TRS' );
h.add( params, 'onlyVisible' ).name( 'Only Visible Objects' );
h.add( params, 'binary' ).name( 'Binary (GLB)' );
h.add( params, 'maxTextureSize', 2, 8192 ).name( 'Max Texture Size' ).step( 1 );
h = gui.addFolder( 'Export' );
h.add( params, 'exportScene1' ).name( 'Export Scene 1' );
h.add( params, 'exportScenes' ).name( 'Export Scene 1 and 2' );
h.add( params, 'exportSphere' ).name( 'Export Sphere' );
h.add( params, 'exportHead' ).name( 'Export Head' );
h.add( params, 'exportObjects' ).name( 'Export Sphere With Grid' );
h.add( params, 'exportSceneObject' ).name( 'Export Scene 1 and Object' );
gui.open();
}
function exportScene1() {
exportGLTF( scene1 );
}
function exportScenes() {
exportGLTF( [ scene1, scene2 ] );
}
function exportSphere() {
exportGLTF( sphere );
}
function exportHead() {
exportGLTF( waltHead );
}
function exportObjects() {
exportGLTF( [ sphere, gridHelper ] );
}
function exportSceneObject() {
exportGLTF( [ scene1, gridHelper ] );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
const timer = Date.now() * 0.0001;
camera.position.x = Math.cos( timer ) * 800;
camera.position.z = Math.sin( timer ) * 800;
camera.lookAt( scene1.position );
renderer.render( scene1, camera );
}
</script>
</body>
</html>