我建议首先查看OBJ format 和您的网格。(更多详细信息请参阅Paul Bourke's site)
您应该能够使用免费的 3D 编辑器(例如 Blender、Wings3D 等)轻松检查拓扑。
(例如mesh -> triangles or quads -> vervices)
我们的想法是清楚地了解您的文件的结构(它是使用三角形、四边形还是组合,是否有任何嵌套结构等)
一旦您了解模型的结构,就可以更轻松地访问每个面及其顶点。正如 Kevin 提到的,只需使用 Processing 的 PShape 函数来遍历网格。例如:
确保始终在遍历之前检查有多少个 PShape 子节点可用,以及有多少个顶点可用以避免 NullPointerReference 错误。
这是来自 Examples > Basics > Shape > 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);
}
}
运行演示,您可以在 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);
}
这是一个在网格中显示/隐藏顶点的预览:
请记住,这会忽略 vertexCodes()。
此外,如果您不想更改 .obj 文件本身以及它的呈现方式,您应该查看 GLSL 并阅读 Processing PShader tutorial。它的级别较低,但效率更高,并且您将在渲染过程中拥有更大的灵活性。