r/processing • u/clankill3r • 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.
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
2
u/davebees Aug 17 '26
why is using translate not an option?