# Polygon from 2d to 3d

**URL:** <https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791>\
**Category:** Questions\
**Created:** [February 25, 2020, 11:00pm UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791 "2020-02-25T23:00:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [February 25, 2020, 11:00pm UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791/1 "2020-02-25T23:00:51Z")

</div>

I am coming from 2d world, trying now to learn 3d, first thing I found was babylon.js, could someone please explain how a polygon is created in babylon? For example in 2d I can create a shape with points

```auto
let points = [
{x: 150, y: 50},
{x: 250, y: 50},
{x: 250, y: 150},
{x: 150, y: 50},
{x: 150, y: 150},
{x: 50, y: 250}
]

```

I understand that I have to add z point in my points array, but I am a bit confused about x,y, could someone please help me to understand how can I create above 2d points polygon to 3d polygon and create an extrude, lets say z=10? an example code or playground will be big help.

I tried to create here but it doesn’t show anything [https://www.babylonjs-playground.com/#PTTMVI#157](https://www.babylonjs-playground.com/#PTTMVI#157)

Thanks

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [February 26, 2020, 1:02am UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791/2 "2020-02-26T01:02:17Z")

</div>

Hi @Dshah_H and welcoming me to the forum. You may find this way more straightforward [Create Parametric Shapes - Babylon.js Documentation](https://doc.babylonjs.com/how_to/parametric_shapes#extruded-non-regular-polygon)

If you prefer to stick to PolygonMeshBuilder [Use PolygonMeshBuilder - Babylon.js Documentation](https://doc.babylonjs.com/how_to/polygonmeshbuilder)

Note carefully the difference between construction methods and parameters. The first uses Vec3s the second Vec2s.

---

<div class="post-metadata">

**Author:** ![pjbaron](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pjbaron/32/6983_2.png) [@pjbaron](https://forum.babylonjs.com/u/pjbaron)\
**Post date:** [February 26, 2020, 3:52am UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791/3 "2020-02-26T03:52:03Z")

</div>

```
        var polygon = this.createPolygon(0, pointList2D, material);
        polygon.parent = this.regions;
        polygon.position = new BABYLON.Vector3(0,0,0);
        polygon.rotation = new BABYLON.Vector3(Math.PI / 2, 0, 0);

createPolygon: function( _id, _points, _material )
{
    var corners = [];
    for(var i = 0, l = _points.length; i < l; i++)
        corners.push(new BABYLON.Vector2(_points[i].x, _points[i].y));
    var mb = new BABYLON.PolygonMeshBuilder("polygon" + _id, corners, this.scene);
    var polygon = mb.build();
    polygon.material = _material;
    return polygon;
},

```

I use this, it creates flat polygons (no extrusions) with the shape determined by the 2d point list.  
When the shape is created I parent it to another mesh and spin it to face the camera.

Hope this helps a little!

---

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [February 26, 2020, 11:59am UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791/4 "2020-02-26T11:59:31Z")

</div>

@JohnK Thank you for welcoming and linking to resources, and @pjbaron thanks for the snippet. , while I am understanding a bit on how all this is working I am still a bit confused on how I could convert my 2d points to BABYLON.Vector3 points, The above points I wrote:  
let points = [  
{x: 150, y: 50},  
{x: 250, y: 50},  
{x: 250, y: 150},  
{x: 150, y: 50},  
{x: 150, y: 150},  
{x: 50, y: 250}  
]

creates this image in 2d : ![Screenshot 2020-02-26 at 1.32.45 PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/f/f8e9ced9f292e15b32ded8ddf4b7221cff5f755a.png)

Now I need to take the same points and add extrude for 3d, so I thought I could just divide all x,y to 100 and gets a point like this

```
let points = [
        { x: 150, y: 50, z: 1 },
        { x: 250, y: 50, z: 1 },
        { x: 250, y: 150, z: 1 },
        { x: 150, y: 50, z: 2 },
        { x: 150, y: 150, z: 1 },
        { x: 50, y: 250, z: 1 }
    ];
    var shape = [];
    points.forEach(function (point, i) {
     // dividing by 100
        shape.push(new BABYLON.Vector3((point.x / 100), (point.y / 100), point.z));
    });

```

but the shape is being created is not same, I am confused on how I can use the same points array and move into 3d, Here is a playground example [https://playground.babylonjs.com/##WP4T1H](https://playground.babylonjs.com/##WP4T1H) , could you please update the playground ? Thanks

edit: I also tried to move y,z positions [Babylon.js Playground](https://playground.babylonjs.com/#GD5WQ0), it gave a bit similar 2d image look.

---

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [February 26, 2020, 12:16pm UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791/5 "2020-02-26T12:16:51Z")

</div>

ok, I think I get it now, this is the correct shape [https://playground.babylonjs.com/#GD5WQ0#2](https://playground.babylonjs.com/#GD5WQ0#2)

I just moved the camera distant and also switched y position with z,

` shape.push(new BABYLON.Vector3((point.x ), point.z, (point.y )));`

hopefully it is the right way?

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [February 26, 2020, 12:24pm UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791/6 "2020-02-26T12:24:59Z")

</div>

A couple of problems.

Here hopefully is as you want it [Babylon.js Playground](https://playground.babylonjs.com/#WL3F9C)

In Babylon.js Y axis increases upwards not downwards

From the Docs

All vectors for shape and holes are Vector3 and should be in the XoZ plane, ie of the form BABYLON.Vector3(x, 0, z) y should be zero

in counter clockwise order (see line 23, shape.reverse()

---

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [February 26, 2020, 12:33pm UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791/7 "2020-02-26T12:33:10Z")

</div>

Thank you so so much, Now it all has started to make sense to me, I just started with babylon.js yesterday and already falling in love with it, and the supporting guy like yourself, made it easier to adopt. I will start reading docs and practice.

Thanks once again.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [February 26, 2020, 12:40pm UTC](https://forum.babylonjs.com/t/polygon-from-2d-to-3d/8791/8 "2020-02-26T12:40:07Z")

</div>

Keep playing with babylon.js and keep asking questions. Have fun.
