Line thickness in P3D

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.

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.

/**
 * 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;
}
1 Like