3d 处理OBJ提取顶点

3d 处理OBJ提取顶点,3d,processing,3d,Processing,如何在处理时获取和打印OBJ的顶点 pshapex; void setup(){size(600600,P3D);x=loadShape(“x.obj”); }作废提款(){ 对于(inti=0;i三角形或四边形->三角板) 这样做的目的是要清楚地了解文件的结构(是否使用三角形、四边形或组合,是否有嵌套结构等) 一旦了解了模型的结构,访问每个面及其顶点就会容易得多。正如Kevin提到的,只需使用Processing的函数遍历网格即可。例如: 计算PShape子实例的数量(便于遍历深度嵌套的树结

如何在处理时获取和打印OBJ的顶点

pshapex;
void setup(){size(600600,P3D);x=loadShape(“x.obj”);
}作废提款(){
对于(inti=0;i
来自:

根据文档,
getVertexCount()
getVertex()
函数仅适用于通过调用
vertex()
函数添加的顶点,而不是从文件加载的形状

但是有一种方法(至少有时)可以到达形状文件中的顶点:首先必须循环遍历形状文件的子级,然后从这些子级获取顶点

PShape形状=载荷形状(“x.obj”);
PShape child=shape.getChild(0);
PVector vertex=child.getVertex(0);
有关允许您在每个子图形上循环的函数,请参阅。
PShape
,然后在每个子图形的顶点上循环。

来自:

根据文档,
getVertexCount()
getVertex()
函数仅适用于通过调用
vertex()
函数添加的顶点,而不是从文件加载的形状

但是有一种方法(至少有时)可以到达形状文件中的顶点:首先必须循环遍历形状文件的子级,然后从这些子级获取顶点

PShape形状=载荷形状(“x.obj”);
PShape child=shape.getChild(0);
PVector vertex=child.getVertex(0);

有关允许您在每个子图形上循环的函数,请参阅。

我建议您首先查看网格和网格。(有关的更多详细信息)

您应该能够使用免费的3D编辑器(例如,等)轻松检查拓扑。 (例如,
网格->三角形或四边形->三角板

这样做的目的是要清楚地了解文件的结构(是否使用三角形、四边形或组合,是否有嵌套结构等)

一旦了解了模型的结构,访问每个面及其顶点就会容易得多。正如Kevin提到的,只需使用Processing的函数遍历网格即可。例如:

  • 计算PShape子实例的数量(便于遍历深度嵌套的树结构)
  • 在给定实例中检索PShape子实例
  • 在PShape实例中计算顶点的步骤
  • 获取给定(有效索引)处的PShape实例顶点
确保在遍历之前始终检查有多少PShape子对象可用,以及有多少顶点可用,以避免出现NullPointerReference错误

以下是示例>基础>形状>LoadDisplayOBJ

/**
 * Load and Display an OBJ Shape. 
 * 
 * The loadShape() command is used to read simple SVG (Scalable Vector Graphics)
 * files and OBJ (Object) files into a Processing sketch. This example loads an
 * OBJ file of a rocket and displays it to the screen. 
 */


PShape rocket;

float ry;

int startFaceIndex = 0;
int stopFaceIndex;

int maxFaceIndex;

public void setup() {
  size(640, 360, P3D);

  rocket = loadShape("rocket.obj");

  maxFaceIndex = rocket.getChildCount();
  stopFaceIndex = maxFaceIndex;

  println("total faces:",rocket.getChildCount());
  println("faces[0] vertices count:",rocket.getChild(0).getVertexCount());
  println("faces[0] vertices[0]",rocket.getChild(0).getVertex(0));
  println("faces[0] vertices[1]",rocket.getChild(0).getVertex(1));
  println("faces[0] vertices[2]",rocket.getChild(0).getVertex(2));
}

public void draw() {
  background(0);
  lights();

  translate(width/2, height/2 + 100, -200);
  rotateZ(PI);
  rotateY(ry);
  //shape(rocket);
  drawFaceSelection();

  ry += 0.02;
}

void drawFaceSelection(){
  beginShape(TRIANGLES);
  for(int i = startFaceIndex; i < stopFaceIndex; i++){
    PShape face = rocket.getChild(i);
    int numVertices = face.getVertexCount();
    for(int j = 0; j < numVertices; j++){
      vertex(face.getVertexX(j),face.getVertexY(j),face.getVertexZ(j));
    }
  }
  endShape();
}

void mouseDragged(){
  if(keyPressed){
    startFaceIndex = (int)map(mouseX,0,width,0,maxFaceIndex-1);
    startFaceIndex = constrain(startFaceIndex,0,maxFaceIndex-1);
  }else{
    stopFaceIndex = (int)map(mouseX,0,width,1,maxFaceIndex-1);
    stopFaceIndex = constrain(stopFaceIndex,0,maxFaceIndex-1);
  }
}
/**
*加载并显示OBJ形状。
* 
*loadShape()命令用于读取简单SVG(可缩放矢量图形)
*将文件和OBJ(对象)文件合并到处理草图中。此示例加载一个
*火箭的OBJ文件,并将其显示在屏幕上。
*/
形状火箭;
浮雕;
int startFaceIndex=0;
int stopFaceIndex;
int-maxFaceIndex;
公共作废设置(){
尺寸(640、360、P3D);
rocket=loadShape(“rocket.obj”);
maxFaceIndex=rocket.getChildCount();
stopFaceIndex=maxFaceIndex;
println(“总面:”,rocket.getChildCount());
println(“面[0]顶点计数:”,rocket.getChild(0.getVertexCount());
println(“面[0]顶点[0]”,rocket.getChild(0.getVertex(0));
println(“面[0]顶点[1]”,rocket.getChild(0.getVertex(1));
println(“面[0]顶点[2]”,rocket.getChild(0.getVertex(2));
}
公众抽签(){
背景(0);
灯光();
平移(宽度/2,高度/2+100,-200);
rotateZ(PI);
rotateY(ry);
//形状(火箭);
drawFaceSelection();
ry+=0.02;
}
void drawFaceSelection(){
beginShape(三角形);
对于(int i=startFaceIndex;i
运行演示程序时,可以在X轴上拖动鼠标,以控制绘制.obj网格的面数:

如果您不关心面/结构,只想访问顶点(将它们渲染为四边形或任何您喜欢的形状),可以编写递归函数来遍历PShape及其子对象,并将所有顶点附加到平面列表中:

/**
 * Load and Display an OBJ Shape. 
 * 
 * The loadShape() command is used to read simple SVG (Scalable Vector Graphics)
 * files and OBJ (Object) files into a Processing sketch. This example loads an
 * OBJ file of a rocket and displays it to the screen. 
 */


PShape rocket;

float ry;

ArrayList<PVector> vertices = new ArrayList<PVector>();
int startVertexIndex = 0;

public void setup() {
  size(640, 360, P3D);
  strokeWeight(9);

  rocket = loadShape("rocket.obj");

  getVertices(rocket,vertices);
  println(vertices);
}

/*
* recursively retrieves vertices from a PShape
* @arg PShape shape - the PShape instance to traverse (must be not null)
* @arg ArrayList<PVector> vertices - the list of vertices to add values to
*/
void getVertices(PShape shape,ArrayList<PVector> vertices){
  //for each face in current mesh
  for(int i = 0 ; i < shape.getChildCount(); i++){
    //get each child element
    PShape child = shape.getChild(i);
    int numChildren = child.getChildCount();
    //if has nested elements, recurse
    if(numChildren > 0){
      for(int j = 0 ; j < numChildren; j++){
        getVertices(child.getChild(j),vertices);
      } 
    }
    //otherwise append child's vertices
    else{
      //get each vertex and append it
      for(int j = 0 ; j < child.getVertexCount(); j++){
        vertices.add(child.getVertex(j));
      }
    }
  }
}

public void draw() {
  background(255);
  lights();

  translate(width/2, height/2 + 100, -200);
  rotateZ(PI);
  rotateY(ry);
  //shape(rocket);
  drawVerticesSelection();

  ry += 0.02;
}

void drawVerticesSelection(){
  beginShape(POINTS);
  for(int i = startVertexIndex; i < vertices.size(); i++){
    PVector v = vertices.get(i);
    vertex(v.x,v.y,v.z);
  }
  endShape();
}

void mouseDragged(){
  startVertexIndex = (int)map(mouseX,0,width,0,vertices.size()-1);
  startVertexIndex = constrain(startVertexIndex,0,vertices.size()-1);
}
/**
*加载并显示OBJ形状。
* 
*loadShape()命令用于读取简单SVG(可缩放矢量图形)
*将文件和OBJ(对象)文件合并到处理草图中。此示例加载一个
*火箭的OBJ文件,并将其显示在屏幕上。
*/
形状火箭;
浮雕;
ArrayList顶点=新建ArrayList();
int startVertexIndex=0;
公共作废设置(){
尺寸(640、360、P3D);
冲程重量(9);
rocket=loadShape(“rocket.obj”);
获取顶点(火箭,顶点);
println(顶点);
}
/*
*递归地从PShape检索顶点
*@arg PShape shape-要遍历的PShape实例(必须不为null)
*@arg ArrayList顶点-要向其添加值的顶点列表
*/
void获取顶点(PShape形状、阵列列表顶点){
//对于当前网格中的每个面
对于(int i=0;i0){