# Need help regarding UV mapping of Octahedron

**URL:** <https://discourse.processing.org/t/need-help-regarding-uv-mapping-of-octahedron/7114>\
**Category:** Coding Questions\
**Created:** [January 2, 2019, 7:47pm UTC](https://discourse.processing.org/t/need-help-regarding-uv-mapping-of-octahedron/7114 "2019-01-02T19:47:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sudev](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sudev](https://discourse.processing.org/u/Sudev)\
**Post date:** [January 2, 2019, 7:47pm UTC](https://discourse.processing.org/t/need-help-regarding-uv-mapping-of-octahedron/7114/1 "2019-01-02T19:47:26Z")

</div>

I need some help with UV mapping an octahedron. Here’s my code :

```auto
  let w = 0;
  let h = 0;
  let th = 400;
  
  beginShape();
  	vertex(w - side, h - side, 0, 0, 1);
  	vertex(w + side, h - side, 0, 0, 0);
  	vertex(w, h, th, 0.5, 1);

   	vertex(w + side, h - side, 0, 0, 1);
  	vertex(w + side, h + side, 0, 0.5, 0);
  	vertex(w, h, th, 0.5, 1);

  	vertex(w + side, h + side, 0, 0, 1);
  	vertex(w - side, h + side, 0, 0, 0);
  	vertex(w, h, th, 0.5, 1);

   	vertex(w - side, h - side, 0, 0, 1);
  	vertex(w - side, h + side, 0, 0, 0);
  	vertex(w, h, th, 0.5, 1);
  
  	vertex(w - side, h - side, 0, 0 , 1);
  	vertex(w + side, h - side, 0, 0, 0);
  	vertex(w, h, -th, 0.5, 0);

  	vertex(w + side, h - side, 0, 0, 1);
  	vertex(w + side, h + side, 0, 0, 0);
  	vertex(w, h, -th, 0.5, 0);

  	vertex(w + side, h + side, 0, 0, 1);
  	vertex(w - side, h + side, 0, 0, 0);
  	vertex(w, h, -th, 0.5, 0);

  	vertex(w - side, h - side, 0, 0, 1);
  	vertex(w - side, h + side, 0, 0, 0);
  	vertex(w, h, -th, 0.5, 0);
  endShape(CLOSE);

```

---

<div class="post-metadata">

**Author:** ![julian2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/julian2/32/1551_2.png) [@julian2](https://discourse.processing.org/u/julian2)\
**Post date:** [January 4, 2019, 2:28pm UTC](https://discourse.processing.org/t/need-help-regarding-uv-mapping-of-octahedron/7114/2 "2019-01-04T14:28:04Z")

</div>

this isn’t the entire / working code right? I’m currently on my phone but might have done time to check this out when I get home. 🙂

I’m assuming you want to apply a texture to the octahedron right? You could watch Shiffman’s video on this in p5js 🙂 [https://youtu.be/O1mYw-3Wl\_Q](https://youtu.be/O1mYw-3Wl_Q)

---

<div class="post-metadata">

**Author:** ![Sudev](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sudev](https://discourse.processing.org/u/Sudev)\
**Post date:** [January 4, 2019, 4:26pm UTC](https://discourse.processing.org/t/need-help-regarding-uv-mapping-of-octahedron/7114/3 "2019-01-04T16:26:21Z")

</div>

No the texture works fine. The issue is am not able to set UV mappings. As I built the octahedron using vertices, we would need to set UV points to all those vertices (4th and 5th argument to be passed in vertex()). Without it, I can’t apply material to the octahedron. Hope am clear. 😅

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 4, 2019, 4:42pm UTC](https://discourse.processing.org/t/need-help-regarding-uv-mapping-of-octahedron/7114/4 "2019-01-04T16:42:53Z")

</div>

You could take x and y coordinates you use for every point also as the UV coordinates. The `uvsize` variable is to adjust if the result texture gets too big or too small.

```auto
  let w = 0;
  let h = 0;
  let th = 400;
  
  let uvsize = 1;
  beginShape();
  	vertex(w - side, h - side, 0, (w - side)*uvsize, (h - side)*uvsize);
  	vertex(w + side, h - side, 0, (w + side)*uvsize, (h - side)*uvsize));
  	vertex(w, h, th, w*uvsize, h*uvsize);

   	vertex(w + side, h - side, 0, (w + side)*uvsize, (h - side)*uvsize));
  	vertex(w + side, h + side, 0, (w + side)*uvsize, (h + side)*uvsize));
  	vertex(w, h, th, w*uvsize, h*uvsize);

  	vertex(w + side, h + side, 0, (w + side)*uvsize, (h + side)*uvsize));
  	vertex(w - side, h + side, 0, (w - side)*uvsize, (h + side)*uvsize));
  	vertex(w, h, th, w*uvsize, h*uvsize);

   	vertex(w - side, h - side, 0, (w - side)*uvsize, (h - side)*uvsize));
  	vertex(w - side, h + side, 0, (w - side)*uvsize, (h + side)*uvsize));
  	vertex(w, h, th, w*uvsize, h*uvsize);
  
  	vertex(w - side, h - side, 0, (w - side)*uvsize, (h - side)*uvsize));
  	vertex(w + side, h - side, 0, (w + side)*uvsize, (h - side)*uvsize));
  	vertex(w, h, th, w*uvsize, h*uvsize);

  	vertex(w + side, h - side, 0, (w + side)*uvsize, (h - side)*uvsize));
  	vertex(w + side, h + side, 0, (w + side)*uvsize, (h + side)*uvsize));
  	vertex(w, h, th, w*uvsize, h*uvsize);

  	vertex(w + side, h + side, 0, (w + side)*uvsize, (h + side)*uvsize));
  	vertex(w - side, h + side, 0, (w - side)*uvsize, (h + side)*uvsize));
  	vertex(w, h, th, w*uvsize, h*uvsize);

  	vertex(w - side, h - side, 0, (w - side)*uvsize, (h - side)*uvsize));
  	vertex(w - side, h + side, 0, (w - side)*uvsize, (h + side)*uvsize));
  	vertex(w, h, -th, 0.5, 0);
  endShape(CLOSE);

```

---

<div class="post-metadata">

**Author:** ![Sudev](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sudev](https://discourse.processing.org/u/Sudev)\
**Post date:** [January 4, 2019, 4:53pm UTC](https://discourse.processing.org/t/need-help-regarding-uv-mapping-of-octahedron/7114/5 "2019-01-04T16:53:41Z")

</div>

It still doesn’t work. Here’s the entire code.

```auto

var side = 200, th = 400, tex;

function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);
  camera(0, 0, (height) / tan(PI*30.0 / 180.0), 0, 0, 0, 0, 1, 0);
}

function draw() {
  background(200);
  orbitControl();
  strokeWeight(10);
  normalMaterial();
  ambientLight(255, 255, 255);
  
  let w = 0;
  let h = 0;
  
  
  let uvsize = 1;
  beginShape();
  	vertex(w - side, h - side, 0, (w - side)*uvsize, (h - side)*uvsize);
  	vertex(w + side, h - side, 0, (w + side)*uvsize, (h - side)*uvsize);
  	vertex(w, h, th, w*uvsize, h*uvsize);

   	vertex(w + side, h - side, 0, (w + side)*uvsize, (h - side)*uvsize);
  	vertex(w + side, h + side, 0, (w + side)*uvsize, (h + side)*uvsize);
  	vertex(w, h, th, w*uvsize, h*uvsize);

  	vertex(w + side, h + side, 0, (w + side)*uvsize, (h + side)*uvsize);
  	vertex(w - side, h + side, 0, (w - side)*uvsize, (h + side)*uvsize);
  	vertex(w, h, th, w*uvsize, h*uvsize);

   	vertex(w - side, h - side, 0, (w - side)*uvsize, (h - side)*uvsize);
  	vertex(w - side, h + side, 0, (w - side)*uvsize, (h + side)*uvsize);
  	vertex(w, h, th, w*uvsize, h*uvsize);
  
  	vertex(w - side, h - side, 0, (w - side)*uvsize, (h - side)*uvsize);
  	vertex(w + side, h - side, 0, (w + side)*uvsize, (h - side)*uvsize);
  	vertex(w, h, -th, w*uvsize, h*uvsize);

  	vertex(w + side, h - side, 0, (w + side)*uvsize, (h - side)*uvsize);
  	vertex(w + side, h + side, 0, (w + side)*uvsize, (h + side)*uvsize);
  	vertex(w, h, -th, w*uvsize, h*uvsize);

  	vertex(w + side, h + side, 0, (w + side)*uvsize, (h + side)*uvsize);
  	vertex(w - side, h + side, 0, (w - side)*uvsize, (h + side)*uvsize);
  	vertex(w, h, -th, w*uvsize, h*uvsize);

  	vertex(w - side, h - side, 0, (w - side)*uvsize, (h - side)*uvsize);
  	vertex(w - side, h + side, 0, (w - side)*uvsize, (h + side)*uvsize);
  	vertex(w, h, -th, w*uvsize, h*uvsize);
  endShape(CLOSE);
  
}

```
