Skip to content

Commit 0de0207

Browse files
authored
Add vertex color option to collada export example (#23384)
1 parent 3496702 commit 0de0207

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

examples/misc_exporter_collada.html

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
let bFitLid;
5353
let bNonBlinn;
5454
let shading;
55+
let vertexColors;
5556
let wireMaterial, flatMaterial, gouraudMaterial, phongMaterial, texturedMaterial, normalMaterial, reflectiveMaterial;
5657

5758
let teapot, textureCube;
@@ -182,6 +183,7 @@
182183
lhue: 0.04,
183184
lsaturation: 0.01, // non-zero so that fractions will be shown
184185
llightness: 1.0,
186+
vertexColors: false,
185187

186188
// bizarrely, if you initialize these with negative numbers, the sliders
187189
// will not show any decimal places.
@@ -217,6 +219,7 @@
217219
h.add( effectController, 'hue', 0.0, 1.0, 0.025 ).name( 'hue' ).onChange( render );
218220
h.add( effectController, 'saturation', 0.0, 1.0, 0.025 ).name( 'saturation' ).onChange( render );
219221
h.add( effectController, 'lightness', 0.0, 1.0, 0.025 ).name( 'lightness' ).onChange( render );
222+
h.add( effectController, 'vertexColors' ).onChange( render );
220223

221224
// light (point)
222225

@@ -263,7 +266,8 @@
263266
effectController.body !== bBody ||
264267
effectController.fitLid !== bFitLid ||
265268
effectController.nonblinn !== bNonBlinn ||
266-
effectController.newShading !== shading ) {
269+
effectController.newShading !== shading ||
270+
effectController.vertexColors !== vertexColors ) {
267271

268272
tess = effectController.newTess;
269273
bBottom = effectController.bottom;
@@ -272,6 +276,7 @@
272276
bFitLid = effectController.fitLid;
273277
bNonBlinn = effectController.nonblinn;
274278
shading = effectController.newShading;
279+
vertexColors = effectController.vertexColors;
275280

276281
createNewTeapot();
277282

@@ -356,6 +361,37 @@
356361
shading === 'textured' ? texturedMaterial : (
357362
shading === 'normal' ? normalMaterial : reflectiveMaterial ) ) ) ) ) ); // if no match, pick Phong
358363

364+
365+
if ( effectController.vertexColors ) {
366+
367+
teapot.geometry.computeBoundingBox();
368+
const minY = teapot.geometry.boundingBox.min.y;
369+
const maxY = teapot.geometry.boundingBox.max.y;
370+
const sizeY = maxY - minY;
371+
372+
const colors = [];
373+
const position = teapot.geometry.getAttribute( 'position' );
374+
for ( let i = 0, l = position.count; i < l; i ++ ) {
375+
376+
const y = position.getY( i );
377+
const r = ( y - minY ) / sizeY;
378+
const b = 1.0 - r;
379+
380+
colors.push( r * 128, 0, b * 128 );
381+
382+
}
383+
teapot.geometry.setAttribute( 'color', new THREE.Uint8BufferAttribute( colors, 3, true ) );
384+
teapot.material.vertexColors = true;
385+
teapot.material.needsUpdate = true;
386+
387+
} else {
388+
389+
teapot.geometry.deleteAttribute( 'color' );
390+
teapot.material.vertexColors = false;
391+
teapot.material.needsUpdate = true;
392+
393+
}
394+
359395
scene.add( teapot );
360396

361397
}

0 commit comments

Comments
 (0)