OBJ解析

参考:GitHub - frenchtoast747/webgl-obj-loader: A simple OBJ model loader to help facilitate the learning of WebGL.

Attributes:

  • vertices: an array containing the vertex values that correspond to each unique face index. The array is flat in that each vertex's component is an element of the array. For example: with verts = [1, -1, 1, ...]verts[0] is xverts[1] is y, and verts[2] is z. Continuing on, verts[3] would be the beginning of the next vertex: its x component. This is in preparation for using gl.ELEMENT_ARRAY_BUFFER for the gl.drawElements call.
    • Note that the vertices attribute is the Geometric Vertex and denotes the position in 3D space.
  • vertexNormals: an array containing the vertex normals that correspond to each unique face index. It is flat, just like vertices.
  • textures: an array containing the s and t (or u and v) coordinates for this mesh's texture. It is flat just like vertices except it goes by groups of 2 instead of 3.
  • indices: an array containing the indicies to be used in conjunction with the above three arrays in order to draw the triangles that make up faces. See below for more information on element indices.
  • Element Index

    The indices attribute is a list of numbers that represent the indices of the above vertex groups. For example, the Nth index, mesh.indices[N], may contain the value 38. This points to the 39th (zero indexed) element. For Mesh classes, this points to a unique group vertex, normal, and texture values. However, the verticesnormals, and textures attributes are flattened lists of each attributes' components, e.g. the vertices list is a repeating pattern of [X, Y, Z, X, Y, Z, ...], so you cannot directly use the element index in order to look up the corresponding vertex position. That is to say mesh.vertices[38] does not point to the 39th vertex's X component. The following diagram illustrates how the element index under the hood:

  • 一个长宽高2的正方体的OBJ例子如下:
     o my_cube.obj
                v 1 1 1
                v -1 1 1
                v -1 -1 1
                v 1 -1 1
                v 1 1 -1
                v -1 1 -1
                v -1 -1 -1
                v 1 -1 -1
                vn 0 0 1
                vn 1 0 0
                vn -1 0 0
                vn 0 0 -1
                vn 0 1 0
                vn 0 -1 0
                f 1//1 2//1 3//1
                f 3//1 4//1 1//1
                f 5//2 1//2 4//2
                f 4//2 8//2 5//2
                f 2//3 6//3 7//3
                f 7//3 3//3 2//3
                f 7//4 8//4 5//4
                f 5//4 6//4 7//4
                f 5//5 6//5 2//5
                f 2//5 1//5 5//5
                f 8//6 4//6 3//6
                f 3//6 7//6 8//6

猜你喜欢

转载自blog.csdn.net/weixin_44345862/article/details/123307429