# How to extrude from 2D points with BufferGeometry?

**URL:** https://discourse.threejs.org/t/how-to-extrude-from-2d-points-with-buffergeometry/40967
**Category:** Questions
**Tags:** geometry
**Created:** [August 2, 2022, 2:01pm UTC](https://discourse.threejs.org/t/how-to-extrude-from-2d-points-with-buffergeometry/40967 "2022-08-02T14:01:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Bossie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/bossie/32/14371_2.png) [@Bossie](https://discourse.threejs.org/u/Bossie)
#### Post date: [August 2, 2022, 2:01pm UTC](https://discourse.threejs.org/t/how-to-extrude-from-2d-points-with-buffergeometry/40967/1 "2022-08-02T14:01:58Z")

</div>

I used the function below to create custom geometry from 2D points.

```javascript
/**
 * Code from: https://stackoverflow.com/a/29969348
 * @param points
 * @param depth
 * @returns {PlaneGeometry}
 */
extrudePath(points, depth) {
    const geometry = new THREE.PlaneGeometry(0, 0, points.length - 1, 1);
    const vertices = geometry.vertices;

    for (let i = 0, l = points.length, p; i < l; i++) {
        p = points[i];
        vertices[i].x = vertices[i + l].x = p[0];
        vertices[i].y = vertices[i + l].y = p[1];
        vertices[i].z = p[2];
        vertices[i + l].z = p[2] + depth;
    }

    geometry.computeFaceNormals();
    return geometry;
}

```

Plain and simple, and much needed in the configurators I’m creating.

However this function doesn’t work anymore using a newer version of Three which only supports BufferGeometry.

How do I change the function to support BufferGeometry?

I am aware that I can extrude a shape defined by 2D points, but it seems that the shape always wants to close itself.

---

<div class="post-metadata">

### Author: ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)
#### Post date: [August 2, 2022, 2:31pm UTC](https://discourse.threejs.org/t/how-to-extrude-from-2d-points-with-buffergeometry/40967/2 "2022-08-02T14:31:57Z")

</div>

Hi!  
You can do it this way: [https://codepen.io/prisoner849/pen/VwXMmvZ?editors=0010](https://codepen.io/prisoner849/pen/VwXMmvZ?editors=0010)

 ![изображение](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/c/c/cca70ca6b7e9e520f7cd0e22fc733c8139f6124a.png)

```javascript
function extrudePath(points, depth) {
  let geometry = new THREE.PlaneGeometry(0, 0, points.length - 1, 1);
  let pos = geometry.attributes.position;

  for (let i = 0, l = points.length, p; i < l; i++) {
    let p = points[i];
    pos.setXYZ(i, p.x, p.y, p.z + depth);
    pos.setXYZ(i + points.length, p.x, p.y, p.z)
  }
  
  geometry.computeVertexNormals();
  return geometry;
}

```

---

<div class="post-metadata">

### Author: ![Bossie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/bossie/32/14371_2.png) [@Bossie](https://discourse.threejs.org/u/Bossie)
#### Post date: [August 2, 2022, 2:39pm UTC](https://discourse.threejs.org/t/how-to-extrude-from-2d-points-with-buffergeometry/40967/3 "2022-08-02T14:39:05Z")

</div>

Thanks a lot for the quick answer, works like a charm!

---

<div class="post-metadata">

### Author: ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)
#### Post date: [August 2, 2022, 2:39pm UTC](https://discourse.threejs.org/t/how-to-extrude-from-2d-points-with-buffergeometry/40967/4 "2022-08-02T14:39:36Z")

</div>

You’re welcome 🍻 🤝

---

<div class="post-metadata">

### Author: ![Bossie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/bossie/32/14371_2.png) [@Bossie](https://discourse.threejs.org/u/Bossie)
#### Post date: [August 3, 2022, 6:57am UTC](https://discourse.threejs.org/t/how-to-extrude-from-2d-points-with-buffergeometry/40967/5 "2022-08-03T06:57:44Z")

</div>

Another question, the shading looks weird, see screenshot:  
How should I fix this?

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/e/7/e733d04a8ec327fdf05a9ddff162d26960370a56.jpeg)

---

<div class="post-metadata">

### Author: ![Bossie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/bossie/32/14371_2.png) [@Bossie](https://discourse.threejs.org/u/Bossie)
#### Post date: [August 3, 2022, 12:24pm UTC](https://discourse.threejs.org/t/how-to-extrude-from-2d-points-with-buffergeometry/40967/6 "2022-08-03T12:24:57Z")

</div>

Ok this took me a while to figure out, but I fixed it by converting the new geometry to non-indexed geometry using geometry.toNonIndexed(), this allows for “hard corners” for as far as I think I understand normals 😃

And after that calling geometry.computeVertexNormals()
