使用Processing PeasyCam制作3D点网格

问题描述 投票:0回答:1

我正在尝试使用 PeasyCam 进行处理来创建盒子中的 3D 点网格,以可视化盒子。为了制作点网格,我使用了一种在 2D 中成功使用过的方法。

import peasy.*;
PeasyCam cam;

void setup(){
 size(1000, 800, P3D);
 background(0);
 frameRate(60);
 
  cam = new PeasyCam(this, 1200);
  cam.lookAt(boxwidth/2, boxheight/2, boxdepth/2);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(2000);
  
 
 //initialize grid of points of interest
   float count = 0;
  
  grid_spacingx = grid_width / (Nx-1); //one fewer spacings than points
  grid_spacingy = grid_height / (Ny-1);
  grid_spacingz = grid_depth / (Nz-1);
  
   for(int i = 0; i<N; i++){
     px[i] = (count*grid_spacingx);
     count++;
     if(count%Nx == 0){
       count = 0;
     }
   }
   for(int i = 0; i<N; i++){
     py[i] = (count*grid_spacingy);
     count++;
        if(count%Ny == 0){
       count = 0;
     }
   } 
  
   for(int i = 0; i<N; i++){
     pz[i] = (count*grid_spacingz);
     count++;
        if(count%Nz == 0){
       count = 0;
     }
   }   
   
}


int boxwidth = 1000;
int boxheight = 1000;
int boxdepth = 1000;

//size of arrays
int Nx = 26; //number of horizontal points (makes N-1 spacings)
int Ny = 26; //number of vertical points
int Nz = 26; //number of vertical points
int N = Nx * Ny; //total number of points

float grid_width = 1000;
float grid_height = 1000;
float grid_depth = 1000;

float grid_spacingx;
float grid_spacingy;
float grid_spacingz;

//position of point of interest
float[] px = new float[N];
float[] py = new float[N];
float[] pz = new float[N];

void draw() {
background(0);

   

colorMode(RGB, 255);

fill(255);
strokeWeight(2);
drawBox();
drawFieldArrows();

}

//******************************************************//

void drawFieldArrows(){

  fill(255);
  for(int i = 0; i<N; i++){
  pushMatrix();
  translate(px[i], py[i], pz[i]);
  point(0, 0, 0); 
  popMatrix(); 
  

  }



}

//******************************************************//

void drawBox(){
  stroke(255);
  noFill();

  line(0, 0, 0, boxwidth, 0, 0);
  line(0, 0, 0, 0, boxheight, 0);
  line(0, 0, 0, 0, 0, boxdepth);
  line(0, 0, boxdepth, boxwidth, 0, boxdepth);
  line(0, 0, boxdepth, 0, boxheight, boxdepth);
  
  line(boxwidth, boxheight, boxdepth, boxwidth, boxheight, 0);
  line(boxwidth, boxheight, boxdepth, 0, boxheight, boxdepth);  
  line(boxwidth, boxheight, boxdepth, boxwidth, 0, boxdepth);   

  line(boxwidth, 0, 0, boxwidth, boxheight, 0);   
  line(boxwidth, 0, 0, boxwidth, 0, boxdepth); 
  
  line(0, boxheight, 0, boxwidth, boxheight, 0);   
  line(0, boxheight, 0, 0, boxheight, boxdepth);     
  
  //fill(135,206,250, 100);
  //rect(0, 0, boxwidth, boxheight);
  //translate(0, 0, boxdepth);
  //rect(0, 0, boxwidth, boxheight);  
}

由于 3D 中的某些原因,它会生成点的单个对角线,而不是完整的网格。任何见解将不胜感激!

我复制了在 2D 中完美运行的代码(仅限 x、y),并且尝试添加 z 维度,但无济于事。

graphics 3d processing
1个回答
0
投票

以下源代码使用放置在框内的二维点网格,然后在“for”循环中通过 z 轴连续平移以到达框的另一侧,创建 3 维点网格。 我使用了以下参考:https://processing.org/tutorials/p3d

import peasy.*;
PeasyCam cam;

int boxwidth = 1000;
int boxheight = 1000;
int boxdepth = 1000;

//size of arrays
int Nx = 26; //number of horizontal points (makes N-1 spacings)
int Ny = 26; //number of vertical points
int Nz = 26; //number of vertical points
int N = Nx * Ny * Nz; //total number of points

float grid_width = 1000;
float grid_height = 1000;
float grid_depth = 1000;

float grid_spacingx;
float grid_spacingy;
float grid_spacingz;

//position of point of interest
float[] px = new float[N];
float[] py = new float[N];
float[] pz = new float[N];

void setup() {
  size(1000, 800, P3D);
  background(0);
  frameRate(60);

  cam = new PeasyCam(this, 1200);
  cam.lookAt(boxwidth/2, boxheight/2, boxdepth/2);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(2000);

  //initialize grid of points of interest
  float count = 0;

  grid_spacingx = grid_width / (Nx-1); //one fewer spacings than points
  grid_spacingy = grid_height / (Ny-1);
  grid_spacingz = grid_depth / (Nz-1);

  for (int i = 0; i<N; i++) {
    px[i] = (count*grid_spacingx);
    count++;
    if (count%Nx == 0) {
      count = 0;
    }
  }
  for (int i = 0; i < N; i++) {
    py[i] = (count*grid_spacingy);
    count++;
    if (count%Ny == 0) {
      count = 0;
    }
  }

  for (int i = 0; i<N; i++) {
    pz[i] = (count*grid_spacingz);
    count++;
    if (count%Nz == 0) {
      count = 0;
    }
  }
}

void draw() {
  background(0);
  colorMode(RGB, 255);
  fill(255);
  strokeWeight(3);
  drawBox();
  drawFieldArrows();
}

//******************************************************//

void pointGrid(int colGap, int rowGap) {
  int _numRows = 26;
  int _numCols = 26;
  for (int k = 0; k < _numRows; k++) {
    for (int j = 0; j < _numCols; j++) {
      int x = j*(colGap);
      int y = k*(rowGap);
      stroke(0, 0, 255);
      strokeWeight(3);
      point(x, y);
    }
  }
}

void drawFieldArrows() {
  fill(255);

  for (int i = 0; i < 40; i++) {
    pushMatrix();
    translate(0, 0, 26*i);
    pointGrid(40, 40);
    popMatrix();
  }
}

//******************************************************//

void drawBox() {
  stroke(255);
  noFill();

  line(0, 0, 0, boxwidth, 0, 0);
  line(0, 0, 0, 0, boxheight, 0);
  line(0, 0, 0, 0, 0, boxdepth);
  line(0, 0, boxdepth, boxwidth, 0, boxdepth);
  line(0, 0, boxdepth, 0, boxheight, boxdepth);

  line(boxwidth, boxheight, boxdepth, boxwidth, boxheight, 0);
  line(boxwidth, boxheight, boxdepth, 0, boxheight, boxdepth);
  line(boxwidth, boxheight, boxdepth, boxwidth, 0, boxdepth);

  line(boxwidth, 0, 0, boxwidth, boxheight, 0);
  line(boxwidth, 0, 0, boxwidth, 0, boxdepth);

  line(0, boxheight, 0, boxwidth, boxheight, 0);
  line(0, boxheight, 0, 0, boxheight, boxdepth);

}

© www.soinside.com 2019 - 2024. All rights reserved.