Tea Tree
Tea Tree

Reputation: 984

How can I display text always in front on a 3D plot?

I have a complex 3D plot with some text on rollover. It's already a bit cluttered so I'd like to have the text always be in front of all the plot parts. Like here:

textfront

Right now, when I hover over some points in the background, the text is sometimes hidden behind plot components, like here:

textbehind

How can I force the text to always be in the front and facing the camera (which it already is right now).?

Upvotes: 0

Views: 178

Answers (1)

George Profenza
George Profenza

Reputation: 51837

Use pushMatrix()/popMatrix() to isolate coordinate spaces: one for the 3D graphics, another for the 2D elements on top:

void setup(){
  size(300, 300, P3D);
  sphereDetail(9);
  noFill();
  stroke(255);
  textSize(21);
  textAlign(CENTER);
}

void draw(){
  background(0);
  // 3D
  pushMatrix();
    translate(width * 0.5, height * 0.5, 0);
    rotateX(map(mouseY, 0, height, -PI, PI));
    rotateY(map(mouseX, 0, width, PI, -PI));
    sphere(150);
  popMatrix();
  // 2D
  pushMatrix();
    text((int)frameRate, mouseX, mouseY);
  popMatrix();
}

(The indenting is not required, just visually emphasising coordinate space nesting. The last push/pop may not be needed for such as simple example, but is useful if you have more 2D/3D things happening on top of what's already been drawn)

For more info checkout the 2D Transformations Tutorial.

Additionally you might find the PeasyCam library useful. To draw 2D text on top you'd do something like:

camera.beginHUD();
// draw 2D overlays here
camera.endHUD(); // back to 3D

Upvotes: 3

Related Questions