# 3D

## Shapes

<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td>plane() - Plane</td><td></td><td></td><td><a href="3d/plane">plane</a></td><td><a href="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2Fpje1BB8HSfnMMhD9FlvF%2Fplane.PNG?alt=media&#x26;token=068c25cc-0875-4c4f-a672-33ed49094896">plane.PNG</a></td></tr><tr><td>box() - box</td><td></td><td></td><td><a href="3d/box">box</a></td><td><a href="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2F9MqhmPbrYEAdCxUYQRxw%2Fbox.PNG?alt=media&#x26;token=4cbd6805-011a-4247-ab9a-581c4ab30867">box.PNG</a></td></tr><tr><td>sphere() - Sphere</td><td></td><td></td><td><a href="3d/sphere">sphere</a></td><td><a href="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2FdExDi1nBQXaeM8D01lpw%2Fsphere.PNG?alt=media&#x26;token=22a3e1df-ca99-49a0-9cd7-88f65d055096">sphere.PNG</a></td></tr><tr><td>cone() - Cone</td><td></td><td></td><td><a href="3d/cone">cone</a></td><td><a href="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2FydorO28MJjaNBobCpUsK%2Fcone.PNG?alt=media&#x26;token=f49e8bfe-989e-4b28-b02c-40f3ee17cfff">cone.PNG</a></td></tr><tr><td>cylinder() - Cylinder</td><td></td><td></td><td><a href="3d/cylinder">cylinder</a></td><td><a href="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2FXILGUXrpP6Yibfuew3qi%2Fcylinder.PNG?alt=media&#x26;token=9cff7ebf-0ee7-4a81-b33a-2f50a87a0b30">cylinder.PNG</a></td></tr><tr><td>ellipsoid() - Deformed Sphere</td><td></td><td></td><td><a href="3d/ellipsoid">ellipsoid</a></td><td><a href="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2FFX9IbWYm9dCckTTwVsBs%2Fellipsoid.PNG?alt=media&#x26;token=7e3e5be2-34d7-40db-93fc-662feded48aa">ellipsoid.PNG</a></td></tr><tr><td>torus() - Donut-like Shape</td><td></td><td></td><td><a href="3d/torus">torus</a></td><td><a href="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2F7gAwDQhmjRBJEFSgpXpr%2Ftorus.PNG?alt=media&#x26;token=bf8283fa-ba81-4a52-b8fb-1409ab92cac2">torus.PNG</a></td></tr></tbody></table>

#### To be able to draw in 3D, `WEBGL` mode needs to be activated on your canvas.

`createCanvas(width, height,`` `**`WEBGL`**`)`

## How are 3D Shapes drawn?

In WEBGL mode, 3D shapes are constructed using **triangular faces**. Each face is defined by three vertices that specify a **triangular face** in space, and each face is **assigned a vector** that indicates the normal direction of the face (resulting in an upper and a lower face). This enables the face to interact with incoming light and be shaded accurately.

<figure><img src="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2Fhe0ilw4Rd1sbMJxKPFmN%2Fimage.png?alt=media&#x26;token=11f43fbe-ee37-4f74-8311-1c88eb6a2e07" alt=""><figcaption></figcaption></figure>

<figure><img src="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2Ft17hCBjAeBFg0SSgp2SL%2Flightinteractionexample.gif?alt=media&#x26;token=90892c7f-81e7-4646-8450-c905d0444d15" alt=""><figcaption></figcaption></figure>

## Details

Because a 3D shape like a sphere is constructed from triangular faces, it is not perfectly curved. The level of curvature depends on the number of faces used. In all the functions used to create 3D shapes, there are two optional parameters: `detailX` and `detailY`. These parameters specify the number of faces that the shape is divided into in the X or Y direction.

```python
sphere([radius], [detailX], [detailY])
```

<div><figure><img src="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2FZg3Jc36uHKFDlSyypTwd%2Fspheredetailx.gif?alt=media&#x26;token=c86f1c99-176f-4bc4-95f3-563b0c0bcaea" alt=""><figcaption><p>maximum = 24</p></figcaption></figure> <figure><img src="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2FYlP5NqxKQgLKOKChPQsJ%2Fspheredetaily.gif?alt=media&#x26;token=0323c2c4-1e8b-4278-ac02-e6cde66781c9" alt=""><figcaption><p>maximum = 24</p></figcaption></figure></div>

```python
cone([radius], [height], [detailX], [detailY], [cap])
```

<div><figure><img src="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2F4kLwcsGUT6KSquBaVf7R%2Fconedetaily.gif?alt=media&#x26;token=d2b3d3e4-7073-419d-b2d5-00b2d8e1d0b6" alt=""><figcaption><p>maximum = 16</p></figcaption></figure> <figure><img src="https://768248463-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fqbs7pgU3AVBY06Rl9M31%2Fuploads%2Fyj7cGXXcTDMyEb8XxByn%2Fconedetailx.gif?alt=media&#x26;token=78872305-5669-4ae8-aad8-79ac4137c232" alt=""><figcaption><p>maximum = 24</p></figcaption></figure></div>

## Translated Shapes

Methods introduced in this following pages all draw the shapes **at the origin** `(0, 0, 0)` by default, and they don't have parameters to specify their position. \
The common practice is to [translate()](https://learnpython.strivemath.com/p5-python-web/reference/built-in-functions/translate) **the origin to where you want to the shape to be drawn**, and draw it at that new origin. To not disturb the positions of other shapes, use push() and pop().

Hence, a common block of code in `WEBGL` mode looks like

```python
push()
translate(100, 0, 0)
sphere(50)
pop()
```

{% hint style="info" %}
Find these documents helpful? Let the people who made them help your child learn to code at [**Strivemath**](https://www.strivemath.com/)<mark style="color:blue;">**!**</mark>

We'd love to hear your Feedback/Comments [here](https://docs.google.com/forms/d/e/1FAIpQLSeqorBAGTya-YBRI-VFjJxtgQtCz3ucGDI96K96sNyuaGuvdw/viewform?usp=sf_link).
{% endhint %}
