|
52 | 52 | let bFitLid; |
53 | 53 | let bNonBlinn; |
54 | 54 | let shading; |
| 55 | + let vertexColors; |
55 | 56 | let wireMaterial, flatMaterial, gouraudMaterial, phongMaterial, texturedMaterial, normalMaterial, reflectiveMaterial; |
56 | 57 |
|
57 | 58 | let teapot, textureCube; |
|
182 | 183 | lhue: 0.04, |
183 | 184 | lsaturation: 0.01, // non-zero so that fractions will be shown |
184 | 185 | llightness: 1.0, |
| 186 | + vertexColors: false, |
185 | 187 |
|
186 | 188 | // bizarrely, if you initialize these with negative numbers, the sliders |
187 | 189 | // will not show any decimal places. |
|
217 | 219 | h.add( effectController, 'hue', 0.0, 1.0, 0.025 ).name( 'hue' ).onChange( render ); |
218 | 220 | h.add( effectController, 'saturation', 0.0, 1.0, 0.025 ).name( 'saturation' ).onChange( render ); |
219 | 221 | h.add( effectController, 'lightness', 0.0, 1.0, 0.025 ).name( 'lightness' ).onChange( render ); |
| 222 | + h.add( effectController, 'vertexColors' ).onChange( render ); |
220 | 223 |
|
221 | 224 | // light (point) |
222 | 225 |
|
|
263 | 266 | effectController.body !== bBody || |
264 | 267 | effectController.fitLid !== bFitLid || |
265 | 268 | effectController.nonblinn !== bNonBlinn || |
266 | | - effectController.newShading !== shading ) { |
| 269 | + effectController.newShading !== shading || |
| 270 | + effectController.vertexColors !== vertexColors ) { |
267 | 271 |
|
268 | 272 | tess = effectController.newTess; |
269 | 273 | bBottom = effectController.bottom; |
|
272 | 276 | bFitLid = effectController.fitLid; |
273 | 277 | bNonBlinn = effectController.nonblinn; |
274 | 278 | shading = effectController.newShading; |
| 279 | + vertexColors = effectController.vertexColors; |
275 | 280 |
|
276 | 281 | createNewTeapot(); |
277 | 282 |
|
|
356 | 361 | shading === 'textured' ? texturedMaterial : ( |
357 | 362 | shading === 'normal' ? normalMaterial : reflectiveMaterial ) ) ) ) ) ); // if no match, pick Phong |
358 | 363 |
|
| 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 | + |
359 | 395 | scene.add( teapot ); |
360 | 396 |
|
361 | 397 | } |
|
0 commit comments