THREE.IcosahedronGeometry has no vertices array

Viewed 85

I was following Advanced Creative Coding with WebGL & Shaders course https://frontendmasters.com/courses/webgl-shaders/ by Matt DesLauriersd and one video he extracted the vertices array for icosahedronGeometry object by doing this:


const geometry = new THREE.SphereGeometry(1, 32, 16);
  const baseGeometry = new THREE.IcosahedronGeometry(1, 1);


  const points = baseGeometry.vertices;
  console.log(points)

but when I do it it is undifined

link to jsfiddle : https://jsfiddle.net/18hd3jge/

1 Answers

The tutorial is outdated. Since r125 all geometry generators like SphereGeometry produce BufferGeometry. The same release also removed the former Geometry from the library.

If you want to access the vertices of a buffer geometry, you have to work with the position buffer attribute. Typical code for this looks like so:

const geometry = new THREE.SphereGeometry(1, 32, 16);
const positionAttribute = geometry.getAttribute( 'position' );

const vertex = new THREE.Vector3();

for ( let vertexIndex = 0; vertexIndex < positionAttribute.count; vertexIndex ++ ) {

    vertex.fromBufferAttribute( positionAttribute, vertexIndex );

    // now do something with vertex

}

More information about the removal of Geometry here: https://discourse.threejs.org/t/three-geometry-will-be-removed-from-core-with-r125/22401

Related