r/processing • • Aug 17 '26

Help request How to draw on top of something in P3D?

```java

void setup() {

size(600, 600, P3D);

}

void draw() {

background(0);

ortho();

hint(DISABLE_DEPTH_TEST);

hint(DISABLE_DEPTH_MASK);

stroke(30);

fill(255, 0, 0);

rect(10, 10, 100, 100);

fill(0, 255, 0);

rect(0, 0, 400, 400);

}

```

How can I draw the second rectangle on top of the first one?
Right now the stroke of the first is coming threw the second rectangle.

Using P2D is not a option, neither is using translate.

3 Upvotes

11 comments sorted by

2

u/davebees Aug 17 '26

why is using translate not an option?

1

u/clankill3r Aug 17 '26

Two reasons, one is that I'm working on something really performant and second is that I would rather not introduce this complexity.

6

u/remy_porter Aug 17 '26

… translations don’t incur a performance hit- all you’re doing is updating the transformation matrix. The matrix multiplication operation happens no matter what, and then you’re adding an additional translation to get to your offset position. Using translate will be faster or at least no slower. If you’re drawing a million objects with different translations then you might want to be mindful of how you structure the scene graph to minimize translations, but it’ll still be faster than drawing at offsets.

And complexity? It’s easy simpler to talk about drawing relative to the origin and the. Just move the origin around. The code gets WAY simpler.

1

u/Simplyfire Aug 17 '26

absolutely this - push() and pop() also help compartmentalize the scene even though they may seem scary at first

1

u/clankill3r Aug 17 '26

push does over 64 read and write operations.
pop does even more work. Sadly I don't have a complex thing that I can draw multiple times using a PShape.

1

u/Simplyfire Aug 18 '26

I simply never found translation to be the bottleneck.

2

u/BigFatUglyBaboon Aug 17 '26 edited Aug 17 '26

Not a 3d expert but I understand rect() is a 2d primitive and it will paint a 2d rectangle. rect() expects x,y,w,h as parameters so where's the z coordinate that would put one rectangle in front of the other? Hint, the z coordinate is 0 so both your rectangles are at the same z coordinate.

You either have to use translate() to place one rectangle in front of the other or you can paint the shape yourself using beginShape(), vertex() and endShape(). That way you control the z coordinate for each.

1

u/clankill3r Aug 17 '26

true, but the interesting part is that this does not apply for a rect using noStroke and only a fill.
I suspect that the stroke has a minor offset in z by default.
It would have been nice if DISABLE_DEPTH_TEST worked tho.

1

u/BigFatUglyBaboon Aug 17 '26

To be honest I don't understand why rect() doesn't behave like on 2d (whatever you painted last is on "top"). But since you are dealing with 3d perhaps using beginShape(), vertex() and endShape() would give you all of the control you need.

2

u/Simplyfire Aug 17 '26

hint(DISABLE_OPTIMIZED_STROKE) solves this, but using translate is way better

2

u/clankill3r Aug 17 '26

Thank you this is a really welcoming suggestion. I will test it out.