This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmisc_exporter_collada.html
458 lines (317 loc) · 14.6 KB
/
misc_exporter_collada.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webgl - exporter - collada</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://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> webgl - exporter - collada
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
import { OrbitControls } from 'v3d/addons/controls/OrbitControls.js';
import { ColladaExporter } from 'v3d/addons/exporters/ColladaExporter.js';
import { TeapotGeometry } from 'v3d/addons/geometries/TeapotGeometry.js';
////////////////////////////////////////////////////////////////////////////////
// Utah/Newell Teapot demo
////////////////////////////////////////////////////////////////////////////////
/*global window */
let camera, scene, renderer;
let cameraControls;
let effectController;
const teapotSize = 100; // vertical height of the teapot is about 2x this, we scale it to millimeters.
let ambientLight, light;
let tess = -1; // force initialization
let bBottom;
let bLid;
let bBody;
let bFitLid;
let bNonBlinn;
let shading;
let vertexColors;
let wireMaterial, flatMaterial, gouraudMaterial, phongMaterial, texturedMaterial, normalMaterial, reflectiveMaterial;
let teapot, textureCube;
// allocate these just once
const diffuseColor = new v3d.Color();
const specularColor = new v3d.Color();
init();
render();
function init() {
const container = document.createElement('div');
document.body.appendChild(container);
const canvasWidth = window.innerWidth;
const canvasHeight = window.innerHeight;
// CAMERA
camera = new v3d.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20000);
camera.position.set(- 150, 137.5, 325);
// LIGHTS
ambientLight = new v3d.AmbientLight(0x333333); // 0.2
light = new v3d.DirectionalLight(0xFFFFFF, 1.0);
// direction is set in GUI
// RENDERER
renderer = new v3d.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(canvasWidth, canvasHeight);
renderer.outputEncoding = v3d.sRGBEncoding;
container.appendChild(renderer.domElement);
// EVENTS
window.addEventListener('resize', onWindowResize);
// CONTROLS
cameraControls = new OrbitControls(camera, renderer.domElement);
cameraControls.addEventListener('change', render);
// TEXTURE MAP
const loader = new v3d.TextureLoader();
const textureMap = loader.load('textures/uv_grid_opengl.jpg');
textureMap.wrapS = textureMap.wrapT = v3d.RepeatWrapping;
textureMap.anisotropy = 16;
textureMap.encoding = v3d.sRGBEncoding;
// NORMAL MAP
const diffuseMap = loader.load('textures/floors/FloorsCheckerboard_S_Diffuse.jpg');
const normalMap = loader.load('textures/floors/FloorsCheckerboard_S_Normal.jpg');
// REFLECTION MAP
const path = 'textures/cube/pisa/';
const urls = [
path + 'px.png', path + 'nx.png',
path + 'py.png', path + 'ny.png',
path + 'pz.png', path + 'nz.png'
];
textureCube = new v3d.CubeTextureLoader().load(urls);
textureCube.encoding = v3d.sRGBEncoding;
// MATERIALS
const materialColor = new v3d.Color();
materialColor.setRGB(1.0, 1.0, 1.0);
wireMaterial = new v3d.MeshBasicMaterial({ color: 0xFFFFFF, wireframe: true });
flatMaterial = new v3d.MeshPhongMaterial({ color: materialColor, specular: 0x000000, flatShading: true, side: v3d.DoubleSide });
gouraudMaterial = new v3d.MeshLambertMaterial({ color: materialColor, side: v3d.DoubleSide });
phongMaterial = new v3d.MeshPhongMaterial({ color: materialColor, side: v3d.DoubleSide });
texturedMaterial = new v3d.MeshPhongMaterial({ color: materialColor, map: textureMap, side: v3d.DoubleSide });
normalMaterial = new v3d.MeshPhongMaterial({ color: materialColor, map: diffuseMap, normalMap: normalMap, side: v3d.DoubleSide });
reflectiveMaterial = new v3d.MeshPhongMaterial({ color: materialColor, envMap: textureCube, side: v3d.DoubleSide });
// scene itself
scene = new v3d.Scene();
scene.background = new v3d.Color(0xAAAAAA);
scene.add(ambientLight);
scene.add(light);
// GUI
setupGui();
}
// EVENT HANDLERS
function onWindowResize() {
const canvasWidth = window.innerWidth;
const canvasHeight = window.innerHeight;
renderer.setSize(canvasWidth, canvasHeight);
camera.aspect = canvasWidth / canvasHeight;
camera.updateProjectionMatrix();
render();
}
function setupGui() {
effectController = {
shininess: 40.0,
ka: 0.17,
kd: 0.51,
ks: 0.2,
metallic: true,
hue: 0.121,
saturation: 0.73,
lightness: 0.66,
lhue: 0.04,
lsaturation: 0.01, // non-zero so that fractions will be shown
llightness: 1.0,
vertexColors: false,
// bizarrely, if you initialize these with negative numbers, the sliders
// will not show any decimal places.
lx: 0.32,
ly: 0.39,
lz: 0.7,
newTess: 15,
bottom: true,
lid: true,
body: true,
fitLid: false,
nonblinn: false,
newShading: 'glossy',
export: exportCollada
};
let h;
const gui = new GUI();
// material (attributes)
h = gui.addFolder('Material control');
h.add(effectController, 'shininess', 1.0, 400.0, 1.0).name('shininess').onChange(render);
h.add(effectController, 'kd', 0.0, 1.0, 0.025).name('diffuse strength').onChange(render);
h.add(effectController, 'ks', 0.0, 1.0, 0.025).name('specular strength').onChange(render);
h.add(effectController, 'metallic').onChange(render);
// material (color)
h = gui.addFolder('Material color');
h.add(effectController, 'hue', 0.0, 1.0, 0.025).name('hue').onChange(render);
h.add(effectController, 'saturation', 0.0, 1.0, 0.025).name('saturation').onChange(render);
h.add(effectController, 'lightness', 0.0, 1.0, 0.025).name('lightness').onChange(render);
h.add(effectController, 'vertexColors').onChange(render);
// light (point)
h = gui.addFolder('Lighting');
h.add(effectController, 'lhue', 0.0, 1.0, 0.025).name('hue').onChange(render);
h.add(effectController, 'lsaturation', 0.0, 1.0, 0.025).name('saturation').onChange(render);
h.add(effectController, 'llightness', 0.0, 1.0, 0.025).name('lightness').onChange(render);
h.add(effectController, 'ka', 0.0, 1.0, 0.025).name('ambient').onChange(render);
// light (directional)
h = gui.addFolder('Light direction');
h.add(effectController, 'lx', -1.0, 1.0, 0.025).name('x').onChange(render);
h.add(effectController, 'ly', -1.0, 1.0, 0.025).name('y').onChange(render);
h.add(effectController, 'lz', -1.0, 1.0, 0.025).name('z').onChange(render);
h = gui.addFolder('Tessellation control');
h.add(effectController, 'newTess', [2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50]).name('Tessellation Level').onChange(render);
h.add(effectController, 'lid').name('display lid').onChange(render);
h.add(effectController, 'body').name('display body').onChange(render);
h.add(effectController, 'bottom').name('display bottom').onChange(render);
h.add(effectController, 'fitLid').name('snug lid').onChange(render);
h.add(effectController, 'nonblinn').name('original scale').onChange(render);
// shading
gui.add(effectController, 'newShading', ['wireframe', 'flat', 'smooth', 'glossy', 'textured', 'normal', 'reflective']).name('Shading').onChange(render);
h = gui.addFolder('Export');
h.add(effectController, 'export').name('Export Collada');
}
//
function render() {
if (effectController.newTess !== tess ||
effectController.bottom !== bBottom ||
effectController.lid !== bLid ||
effectController.body !== bBody ||
effectController.fitLid !== bFitLid ||
effectController.nonblinn !== bNonBlinn ||
effectController.newShading !== shading ||
effectController.vertexColors !== vertexColors) {
tess = effectController.newTess;
bBottom = effectController.bottom;
bLid = effectController.lid;
bBody = effectController.body;
bFitLid = effectController.fitLid;
bNonBlinn = effectController.nonblinn;
shading = effectController.newShading;
vertexColors = effectController.vertexColors;
createNewTeapot();
}
// We're a bit lazy here. We could check to see if any material attributes changed and update
// only if they have. But, these calls are cheap enough and this is just a demo.
phongMaterial.shininess = effectController.shininess;
texturedMaterial.shininess = effectController.shininess;
normalMaterial.shininess = effectController.shininess;
diffuseColor.setHSL(effectController.hue, effectController.saturation, effectController.lightness);
if (effectController.metallic) {
// make colors match to give a more metallic look
specularColor.copy(diffuseColor);
} else {
// more of a plastic look
specularColor.setRGB(1, 1, 1);
}
diffuseColor.multiplyScalar(effectController.kd);
flatMaterial.color.copy(diffuseColor);
gouraudMaterial.color.copy(diffuseColor);
phongMaterial.color.copy(diffuseColor);
texturedMaterial.color.copy(diffuseColor);
normalMaterial.color.copy(diffuseColor);
specularColor.multiplyScalar(effectController.ks);
phongMaterial.specular.copy(specularColor);
texturedMaterial.specular.copy(specularColor);
normalMaterial.specular.copy(specularColor);
// Ambient's actually controlled by the light for this demo
ambientLight.color.setHSL(effectController.hue, effectController.saturation, effectController.lightness * effectController.ka);
light.position.set(effectController.lx, effectController.ly, effectController.lz);
light.color.setHSL(effectController.lhue, effectController.lsaturation, effectController.llightness);
// skybox is rendered separately, so that it is always behind the teapot.
if (shading === 'reflective') {
scene.background = textureCube;
} else {
scene.background = null;
}
renderer.render(scene, camera);
}
// Whenever the teapot changes, the scene is rebuilt from scratch (not much to it).
function createNewTeapot() {
if (teapot !== undefined) {
teapot.geometry.dispose();
scene.remove(teapot);
}
const teapotGeometry = new TeapotGeometry(teapotSize,
tess,
effectController.bottom,
effectController.lid,
effectController.body,
effectController.fitLid,
! effectController.nonblinn);
teapot = new v3d.Mesh(
teapotGeometry,
shading === 'wireframe' ? wireMaterial : (
shading === 'flat' ? flatMaterial : (
shading === 'smooth' ? gouraudMaterial : (
shading === 'glossy' ? phongMaterial : (
shading === 'textured' ? texturedMaterial : (
shading === 'normal' ? normalMaterial : reflectiveMaterial)))))); // if no match, pick Phong
if (effectController.vertexColors) {
teapot.geometry.computeBoundingBox();
const minY = teapot.geometry.boundingBox.min.y;
const maxY = teapot.geometry.boundingBox.max.y;
const sizeY = maxY - minY;
const colors = [];
const position = teapot.geometry.getAttribute('position');
for (let i = 0, l = position.count; i < l; i++) {
const y = position.getY(i);
const r = (y - minY) / sizeY;
const b = 1.0 - r;
colors.push(r * 128, 0, b * 128);
}
teapot.geometry.setAttribute('color', new v3d.Uint8BufferAttribute(colors, 3, true));
teapot.material.vertexColors = true;
teapot.material.needsUpdate = true;
} else {
teapot.geometry.deleteAttribute('color');
teapot.material.vertexColors = false;
teapot.material.needsUpdate = true;
}
scene.add(teapot);
}
const exporter = new ColladaExporter();
function exportCollada() {
const result = exporter.parse(teapot, undefined, { upAxis: 'Y_UP', unitName: 'millimeter', unitMeter: 0.001 });
let materialType = 'Phong';
if (shading === 'wireframe') {
materialType = 'Constant';
}
if (shading === 'smooth') {
materialType = 'Lambert';
}
saveString(result.data, 'teapot_' + shading + '_' + materialType + '.dae');
result.textures.forEach(tex => {
saveArrayBuffer(tex.data, `${ tex.name }.${ tex.ext }`);
});
}
function save(blob, filename) {
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
}
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);
}
const link = document.createElement('a');
link.style.display = 'none';
document.body.appendChild(link);
</script>
</body>
</html>