# Line thickness in P3D

**URL:** <https://discourse.processing.org/t/line-thickness-in-p3d/12995>\
**Category:** Coding Questions\
**Created:** [July 26, 2019, 9:45am UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995 "2019-07-26T09:45:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [July 26, 2019, 9:45am UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/1 "2019-07-26T09:45:51Z")

</div>

Hello everyone!

Been a little while, and I enjoy exploring what Processing 3 has brought us. Have a rather simple question though transitioning from the old OpenGL way of thinking to the new P3D rendering – one being line thickness in 3D. The classic way OpenGL rendered lines was without an actual thickness in the 3D sense of the word; a line going away from you would have a constant thickness and not get thinner as it was farther away (which I admit, in most cases would be desirable). Now the line thickness does scale according to distance from the camera by default.

My question is whether the classic behaviour is still possible to enable, without having to do funky post processing/screenX kind of tricks?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 26, 2019, 9:54am UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/2 "2019-07-26T09:54:32Z")

</div>

1. 

just for completeness sake I want to mention (what you already know) that there is a 2D version and a 3D version of the `line` command. The latter has 6 parameters: `x1,y1,z1 , x2,y2,z2`. See reference line(): [https://www.processing.org/reference/line\_.html](https://www.processing.org/reference/line_.html). Unfortunately, line() is not mentioned in the _3D Primitives_ section of the reference (same with `point()`).

1. 

There is a fancy 3D line function that is easy to handle. (I think 1 or 2 things are not correct but it looks cool)

see: [https://github.com/Kango/Processing-snippets/blob/master/ThreeDLine/ThreeDLine.pde](https://github.com/Kango/Processing-snippets/blob/master/ThreeDLine/ThreeDLine.pde)

---

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [July 26, 2019, 10:19am UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/3 "2019-07-26T10:19:40Z")

</div>

Thanks for you reply! Upon re-reading my post I realised my ‘simple question’ wasn’t actually worded all that clearly. What I am after is indeed when using the 3D version of the line, and creating say a line that starts very far away and ends up at the camera, for that line _not_ to appear thicker when it is close, but instead be always exactly the 1px thick as set by lineThickness(), no matter how far away from the camera it is.

The context in this case would be a 3D grid that I’m drawing, that I want to have behave with the lines always being ‘hairlines’, like they are in C4D etc.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 26, 2019, 10:27am UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/4 "2019-07-26T10:27:20Z")

</div>

> [@rapatski](#):
>
> line thickness in 3D

I wasn’t reading your post properly, just read “line thickness in 3D” and - click! - I was writing. Sorry.

I don’t know whether that’s possible. Maybe using a 2D line with previously stored `screenX` and `screenY` or with a transparent PImage… very complicate…

---

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [July 26, 2019, 1:48pm UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/5 "2019-07-26T13:48:17Z")

</div>

I know right! The “odd” thing is that point() does still behave like it use to; zooming out or into a scene does not change the size of a point. It simply is whatever you set the strokeWidth to be.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 26, 2019, 1:57pm UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/6 "2019-07-26T13:57:00Z")

</div>

Or use set perspective orthogonal or so?

Can’t remember

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 31, 2019, 5:21pm UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/7 "2019-07-31T17:21:57Z")

</div>

> [@rapatski](#):
>
> when using the 3D version of the line, and creating say a line that starts very far away and ends up at the camera, for that line _not_ to appear thicker when it is close

I’m not sure if that is possible with P3D.

I do know that if you have the 3D coordinates for a line (float[6]) then you can retrieve the 2D coordinates using screenX / screenY.

```auto
float[] screenLine(float[] ln) {
  return screenLine(ln[0], ln[1], ln[2], ln[3], ln[4], ln[5]);
}
float[] screenLine(float x1, float y1, float z1, float x2, float y2, float z2) {
  float[] result = new float[]{
    screenX(x1, y1, z1), 
    screenY(x1, y1, z1), 
    screenX(x2, y2, z2), 
    screenY(x2, y2, z2)
  };
  return result;
}

```

So, you could enter a 3D scene with translations and rotations, express a bunch of 3D lines, and, rather than drawing them, save them to a float array. Then after you exit (popMatrix), you can loop over your line data and render as 2D lines.

Here is a simple example with one line to inspect what is going on when you draw a 3D line inside a transform, then draw it again in 2D (flat) outside the transform.

```auto
/**
 * Flat 2D Line from 3D Line
 * 2019-07-31 Processing 3.4
 * note that screenX/Y can return bad values if the 3D points
 * are outside the view. You can view this bug by increasing
 * the line dim=500.
 */

PVector rot;
float dim = 50;

void setup() {
  size(400, 400, P3D);
  rot = new PVector(-1, 0, 0);
  frameRate(30);
}

void draw() {
  background(192);

  pushMatrix();

  // center and pulse in-out
  translate(width/2.0, height/2.0,sin((frameCount/120.0)%4000)*200);
  // increment rotation and apply
  rot.add(0.01, 0.02, 0.03);
  rotateX(rot.x);
  rotateY(rot.y);
  rotateZ(rot.z);

  pushStyle();
  // draw 3D line
  stroke(0);
  strokeWeight(1);
  sphere(30);
  float[] ln = new float[]{-dim, -dim, -dim, dim, dim, dim};
  line(ln[0], ln[1], ln[2], ln[3], ln[4], ln[5]);
  // save 3D line as 2D coordinates
  float[] flatln = screenLine(ln);
  popStyle();

  // leave transform
  popMatrix();

  // draw 2D line with captured coordinates
  pushStyle();
  strokeWeight(20);
  stroke(255, 0, 0, 32);
  line(flatln[0], flatln[1], flatln[2], flatln[3]);
  println(flatln[0], flatln[1], flatln[2], flatln[3]);
  popStyle();
}

float[] screenLine(float[] ln) {
  return screenLine(ln[0], ln[1], ln[2], ln[3], ln[4], ln[5]);
}
float[] screenLine(float x1, float y1, float z1, float x2, float y2, float z2) {
  float[] result = new float[]{
    screenX(x1, y1, z1), 
    screenY(x1, y1, z1), 
    screenX(x2, y2, z2), 
    screenY(x2, y2, z2)
  };
  return result;
}

```

---

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [August 1, 2019, 9:36am UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/8 "2019-08-01T09:36:28Z")

</div>

Hi Jeremy,

Thank you - I appreciate the effort you put into the reply and yes, that would create the effect I’m after. That said I was hoping to find a way to achieve it without having to add so much computation to the sketch, but afraid you might be right.

That said, it is strange how P3D seems to render _points_ still the same way it used to when directly using OpenGL as a renderer?

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [August 3, 2019, 11:54pm UTC](https://discourse.processing.org/t/line-thickness-in-p3d/12995/9 "2019-08-03T23:54:26Z")

</div>

I’m not sure. Perhaps check the P3D / PGraphics 3D source code on Github? I had assumed it related to OpenGL internals, but I don’t know that code well.
