处理:如何使Box()在3D模式下显示固体(非透明)

发布于 2025-01-27 01:33:26 字数 1134 浏览 1 评论 0原文

我正在尝试在处理中创建3D框的层。我希望它们看起来坚固,以便您看不到其他盒子后面的框,但是它们显示的方式使它们看起来透明。您可以看到其他盒子后面的盒子。我如何使它们显得坚固?

// number of boxes
int numBox = 300;

// width of each box
int boxWidth = 30;

// number of boxes per row
float numPerRow;

void setup() {
  size(800, 800, P3D);
  pixelDensity(1);
  colorMode(HSB, 360, 100, 100, 100);
  background(40, 6, 85);
  stroke(216, 0, 55);
  smooth(4);
  fill(0, 0, 90, 100);
  
  numPerRow = width / boxWidth;
  
}

void draw() {
  background(40, 6, 85);
  
  translate((boxWidth / 2), 100);
  rotateX(-PI/6);
  rotateY(PI/8);
  
  for (int i = 0; i < numBox; i++) {
    drawBox(i);
    
    if (i == numBox - 1) {
      noLoop();
    }
  }
}

void drawBox(int i) {
  if ((i % 2) == 0) {
    pushMatrix();
    translate(((boxWidth / 2) * i) % width, 20 * floor(i / (2 * numPerRow)));
    translate(0, -((i % 30) / 2));
    box(boxWidth, i % 30, boxWidth);
    popMatrix();
  };
}

盒子的显示方式的特写:

”与作为步骤层出现的立方体相交的行”

I'm trying to create layers of 3d boxes in Processing. I want them to appear solid, so that you can't see the boxes "behind" other boxes, but the way they're displaying makes them seem transparent; you can see the stroke of boxes behind other boxes. How do I make them appear solid?

// number of boxes
int numBox = 300;

// width of each box
int boxWidth = 30;

// number of boxes per row
float numPerRow;

void setup() {
  size(800, 800, P3D);
  pixelDensity(1);
  colorMode(HSB, 360, 100, 100, 100);
  background(40, 6, 85);
  stroke(216, 0, 55);
  smooth(4);
  fill(0, 0, 90, 100);
  
  numPerRow = width / boxWidth;
  
}

void draw() {
  background(40, 6, 85);
  
  translate((boxWidth / 2), 100);
  rotateX(-PI/6);
  rotateY(PI/8);
  
  for (int i = 0; i < numBox; i++) {
    drawBox(i);
    
    if (i == numBox - 1) {
      noLoop();
    }
  }
}

void drawBox(int i) {
  if ((i % 2) == 0) {
    pushMatrix();
    translate(((boxWidth / 2) * i) % width, 20 * floor(i / (2 * numPerRow)));
    translate(0, -((i % 30) / 2));
    box(boxWidth, i % 30, boxWidth);
    popMatrix();
  };
}

Close-up of how the boxes are being displayed:

intersecting rows of cubes that appear as layers of steps

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2025-02-03 01:33:26

问题在于,这些盒子是相交的,这些相交框的笔触使外观“ see taby”。

我注意到您正在使用X和Y翻译,但不使用Z。
如果您不打算增加x,y间距避免交叉点,则可以轻松地在z轴上偏移行,以便彼此前面出现一排盒子。

这是您的代码的一个稍微修改的版本,说明了此想法:(

// number of boxes
int numBox = 300;

// width of each box
int boxWidth = 30;

// number of boxes per row
float numPerRow;

void setup() {
  size(800, 800, P3D);
  pixelDensity(1);
  colorMode(HSB, 360, 100, 100, 100);
  background(40, 6, 85);
  stroke(216, 0, 55);
  smooth(4);
  fill(0, 0, 90, 100);

  numPerRow = width / boxWidth;
}

void draw() {
  background(40, 6, 85);

  translate((boxWidth / 2), 100);
  if(mousePressed){
    rotateX(map(mouseY, 0, height, -PI, PI));
    rotateY(map(mouseX, 0, width, PI, -PI));
  }else{
    rotateX(-PI/6);
    rotateY(PI/8);
  }

  for (int i = 0; i < numBox; i++) {
    drawBox(i);

    //if (i == numBox - 1) {
    //  noLoop();
    //}
  }
}

void drawBox(int i) {
  if ((i % 2) == 0) {
    pushMatrix();
    float x = ((boxWidth / 2) * i) % width;
    float y = 20 * floor(i / (2 * numPerRow));
    float z = y * 1.5;
    translate(x, y, z);
    translate(0, -((i % 30) / 2));
    box(boxWidth, i % 30, boxWidth);
    popMatrix();
  };
}

单击并拖动以旋转并观察Z偏移量。

随意使Z作为您的兴趣,您需要它。

构图和颜色不错!
(框架(窗口大小)可以使用一些迭代/调整,但我猜这是WIP))

< img src =“ https://i.sstatic.net/tlexx.jpg” alt =“踏板立方的行深度偏移以避免交叉点”>

The issue is that the boxes are intersecting and the strokes of these intersecting boxes are what give the appearance of "see through".

I'm noticing you are using x and y translation, but not z.
If you don't plan to increase x, y spacing to avoid intersections, you can easily offset rows on the z axis so rows of boxes appear in front of each other.

Here's a slightly modified version of your code illustrating this idea:

// number of boxes
int numBox = 300;

// width of each box
int boxWidth = 30;

// number of boxes per row
float numPerRow;

void setup() {
  size(800, 800, P3D);
  pixelDensity(1);
  colorMode(HSB, 360, 100, 100, 100);
  background(40, 6, 85);
  stroke(216, 0, 55);
  smooth(4);
  fill(0, 0, 90, 100);

  numPerRow = width / boxWidth;
}

void draw() {
  background(40, 6, 85);

  translate((boxWidth / 2), 100);
  if(mousePressed){
    rotateX(map(mouseY, 0, height, -PI, PI));
    rotateY(map(mouseX, 0, width, PI, -PI));
  }else{
    rotateX(-PI/6);
    rotateY(PI/8);
  }

  for (int i = 0; i < numBox; i++) {
    drawBox(i);

    //if (i == numBox - 1) {
    //  noLoop();
    //}
  }
}

void drawBox(int i) {
  if ((i % 2) == 0) {
    pushMatrix();
    float x = ((boxWidth / 2) * i) % width;
    float y = 20 * floor(i / (2 * numPerRow));
    float z = y * 1.5;
    translate(x, y, z);
    translate(0, -((i % 30) / 2));
    box(boxWidth, i % 30, boxWidth);
    popMatrix();
  };
}

(Click and drag to rotate and observe the z offset.

Feel free to make z as interestersting as you need it it.

Nice composition and colours!
(framing (window size) could use some iteration/tweaking, but I'm guessing this is WIP))

rows of stepped cubes offset in depth to avoid intersections

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文