-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgltfExporter.html
More file actions
410 lines (324 loc) · 13.4 KB
/
gltfExporter.html
File metadata and controls
410 lines (324 loc) · 13.4 KB
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
<!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">
<meta name="description"
content="Final project of the Interactive Graphics 19-20 course @ Sapienza University of Rome">
<meta name="keywords" content="">
<meta name="author" content="Ivan Fardin, Francesco Ottaviani and Samuele Olivieri Pennesi">
</head>
<body>
<div id="info">
<button id="export_scene">Export Scene</button>
<button id="export_object">Export Object</button>
<br/><br/>
<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>
<label><input id="option_drawrange" name="visible" type="checkbox" checked="checked"/>Truncate drawRange</label><br/>
<label><input id="option_binary" name="visible" type="checkbox">Binary (<code>.glb</code>)</label>
<label><input id="option_forcepot" name="visible" type="checkbox">Force POT textures</label>
<label><input id="option_maxsize" name="maxSize" type="number" value="4096" min="2" max="8192" step="1"> Max texture size</label>
</div>
<script type="module">
import * as THREE from "https://unpkg.com/three@0.118.3/build/three.module.js";
import { OBJLoader } from "https://unpkg.com/three@0.118.3/examples/jsm/loaders/OBJLoader.js";
import { GLTFLoader } from "https://unpkg.com/three@0.118.3/examples/jsm/loaders/GLTFLoader.js";
import { GLTFExporter } from "https://unpkg.com/three@0.118.3/examples/jsm/exporters/GLTFExporter.js";
import { OrbitControls } from "https://unpkg.com/three@0.118.3/examples/jsm/controls/OrbitControls.js";
import { Utils } from "./js/Utils.js";
function exportGLTF( input ) {
const gltfExporter = new GLTFExporter();
const options = {
trs: document.getElementById( 'option_trs' ).checked,
onlyVisible: document.getElementById( 'option_visible' ).checked,
truncateDrawRange: document.getElementById( 'option_drawrange' ).checked,
binary: document.getElementById( 'option_binary' ).checked,
forcePowerOfTwoTextures: document.getElementById( 'option_forcepot' ).checked,
maxTextureSize: Number( document.getElementById( 'option_maxsize' ).value ) || Infinity // To prevent NaN value
};
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' );
}
}, options );
}
document.getElementById( 'export_scene' ).addEventListener( 'click', function () {
exportGLTF( scene );
} );
document.getElementById( 'export_object' ).addEventListener( 'click', function () {
exportGLTF( object );
} );
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 camera, object, scene, renderer;
init();
animate();
function init() {
const container = document.createElement( 'div' );
document.body.appendChild( container );
scene = new THREE.Scene();
scene.name = 'Scene';
// ---------------------------------------------------------------------
// Perspective Camera
// ---------------------------------------------------------------------
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.001, 100000 );
camera.position.set( 600, 400, 0 );
camera.name = "PerspectiveCamera";
scene.add( camera );
// ---------------------------------------------------------------------
// Ambient light
// ---------------------------------------------------------------------
let light = new THREE.AmbientLight( 0xffffff, 0.2 );
light.name = 'AmbientLight';
scene.add( light );
// ---------------------------------------------------------------------
// DirectLight
// ---------------------------------------------------------------------
light = new THREE.DirectionalLight( 0xffffff, 1 );
light.target.position.set( 0, 0, - 1 );
light.add( light.target );
light.lookAt( - 1, - 1, 0 );
light.name = 'DirectionalLight';
scene.add( light );
// ---------------------------------------------------------------------
// Grid
// ---------------------------------------------------------------------
const gridHelper = new THREE.GridHelper( 2000, 20 );
gridHelper.position.y = - 50;
gridHelper.name = "Grid";
scene.add( gridHelper );
// ---------------------------------------------------------------------
// Axes
// ---------------------------------------------------------------------
const axes = new THREE.AxesHelper( 500 );
axes.name = "AxesHelper";
scene.add( axes );
const model = {
/*url: 'src/environment/city/street/sidewalk_corner/scene.gltf',
position: [0, 0, 0],
scale: [1, 1, 1],
rotation: [0, -Math.PI/2, 0]*/
/*url: 'src/environment/city/street/neon_signs_billboards/scene.gltf',
position: [-2000, 0, 0],
scale: [1, 1, 1],
rotation: [0, 0, 0]*/
/*url: 'src/environment/country - desert/buildings/motel_lone_star/scene.gltf',
position: [0, 0, 0],
scale: [1, 1, 1],
rotation: [0, 0, 0]*/
/*url: 'src/environment/city/street/sidewalk_straight/scene.gltf',
position: [26.5, 0, 0],
scale: [1, 1, 1],
rotation: [0, 0, 0],*/
url: 'src/environment/highway/road/jersey_barrier/scene.gltf',
position: [0, 0, 0],
scale: [1.2, 1.2, 3],
rotation: [0, 0, 0]
};
// GLTF loader for removing meshes
/*const gltfLoader = new GLTFLoader();
gltfLoader.load( model.url, function ( gltf ) {
object = gltf.scene;
object.position.set(...model.position);
object.scale.set(...model.scale)
object.rotation.set(...model.rotation);
console.log(Utils.dumpObject(object).join('\n'));
// For removing the corner of the sidewalk
/*let rmObj = null;
object.traverse( o => {
if (o.name === "CORNER")
rmObj = o;
} );
if (rmObj)
rmObj.parent.remove(rmObj);*/
// For removing the unused neon billboards
/*let rmObjs = [];
object.traverse( o => {
if (o.name === "takoyaki-ya" || o.name === "takoyaki-ta" ||
o.name === "takoyaki-ko" || o.name === "takoyaki-ki" ||
o.name === "cube-letter" || o.name === "cube-letter001" ||
o.name === "cube-letter002" || o.name === "cube-letter003" ||
o.name === "2-takoyaki-ya" || o.name === "2-takoyaki-ta" ||
o.name === "2-takoyaki-ko" || o.name === "2-takoyaki-ki" ||
o.name === "cube-letter004" || o.name === "cube-letter005" ||
o.name === "cube-letter006" || o.name === "cube-letter007" ||
o.name === "takoyaky-ta" || o.name === "takoyaky-ta001" ||
o.name === "takoyaky-ta002" || o.name === "jote" ||
o.name === "crock-pot" || o.name === "donkey" ||
o.name === "text-shibuya" || o.name === "text-shibuya001" ||
o.name === "text-shibuya002" || o.name === "text-shibuya003"
)
rmObjs.push(o);
} );
rmObjs.forEach( o => o.parent.remove(o) );*/
// For removing the ground of the lone star motel
/*let rmObjs = [];
object.traverse( o => {
if (o.name === "ground" || o.name === "bush" ||
o.name === "grass" || o.name === "grass_plane")
rmObjs.push(o);
} );
rmObjs.forEach( o => o.parent.remove(o) );
console.log(Utils.dumpObject(object).join('\n'));
scene.add( object );
},
undefined,
function ( error ) {
console.error(error);
alert("Error during the loading of the model!\nTry to refresh the page");
} );//*/
// GLTF loader for creating a unique object from several clones
const gltfLoader = new GLTFLoader();
gltfLoader.load( model.url, function ( gltf ) {
object = new THREE.Group(); // Final object result
const obj = gltf.scene; // Used to create clones
// For sidewalks
/*object.name = "Big sidewalks";
const n = 50, nHalf = 25, dist = 25.3;
// Create n clones, nHalf to the right and nHalf to the left
for (let i=0; i<n; i++) {
const clone = obj.clone();
let posX = model.position[0], posZ = 0, rad = model.rotation[1];
if (i < nHalf)
posZ = 0 - (dist * i);
else {
posX = -posX;
posZ = 19 - (dist * (i - nHalf)); // 19 is a offset due to rotation
rad += Math.PI;
}
const modelClone = {
position: [posX, 0, posZ],
scale: [1, 1, 1],
rotation: [0, rad, 0],
};
console.log("posX: " + posX + " posZ: " + posZ + " rad: " + rad);
clone.position.set(...modelClone.position);
clone.scale.set(...modelClone.scale)
clone.rotation.set(...modelClone.rotation);
clone.traverse(o => {
if (o.isMesh) {
o.castShadow = true;
o.receiveShadow = true;
}
});
//scene.add(clone);
object.add(clone);
}*/
// For jerseys
object.name = "Big jerseys";
const n = 50, dist = 15; //6;
// Create n clones, nHalf to the right and nHalf to the left
for (let i=0; i<n; i++) {
const clone = obj.clone();
let posX = model.position[0], posZ = 367 - (dist * i), rad = model.rotation[1];
const modelClone = {
position: [posX, 0, posZ],
scale: model.scale,
rotation: [0, rad, 0],
};
console.log("posX: " + posX + " posZ: " + posZ + " rad: " + rad);
clone.position.set(...modelClone.position);
clone.scale.set(...modelClone.scale)
clone.rotation.set(...modelClone.rotation);
clone.traverse(o => {
if (o.isMesh) {
o.castShadow = true;
o.receiveShadow = true;
}
});
//scene.add(clone);
object.add(clone);
}
scene.add(object);
console.log(Utils.dumpObject(object).join('\n'));
},
undefined,
function ( error ) {
console.error(error);
alert("Error during the loading of the model!\nTry to refresh the page");
} );
/*const loader = new OBJLoader();
loader.load( model.url, function ( obj ) {
object = obj;
object.scale.multiplyScalar( 1.5 );
object.position.set( 400, 0, 0 );
scene.add( object );
} );*/
/*const models = {
gasStation: { url: 'src/environment/city/buildings/gas_station/scene.gltf',
position: [0, 0, 0],
scale: [1, 1, 1],
rotation: [0, 0, 0]
},
gasStationAd: { url: 'src/environment/city/street/gas_station_ad/scene.gltf',
position: [900, 0, 1900],
scale: [24, 24, 24],
rotation: [0, 0, 0]
},
gasStationAdClone: { url: 'src/environment/city/street/gas_station_ad/scene.gltf',
position: [900, 0, -1900],
scale: [24, 24, 24],
rotation: [0, 0, 0]
}
};
const gltfLoader = new GLTFLoader();
for (const model of Object.values(models)) {
gltfLoader.load(model.url, function (gltf) {
object = gltf.scene;
object.position.set(...model.position);
object.scale.set(...model.scale)
object.rotation.set(...model.rotation);
scene.add( object );
},
undefined,
function (error) {
console.error(error);
alert("Error during the loading of the models!\nTry to refresh the page");
});
}*/
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
// Controls for zooming and moving around the scene
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 5, 0);
controls.update();
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() {
renderer.render( scene, camera );
}
</script>
</body>
</html>