OBJ解析

博客详细介绍了如何使用WebGL加载OBJ模型,包括vertices、vertexNormals、textures和indices等属性的作用。vertices表示顶点位置,vertexNormals表示顶点法线,textures表示纹理坐标,而indices则用于指定绘制三角面的顺序。通过示例展示了正方体的OBJ文件格式,帮助理解模型数据结构。
摘要由CSDN通过智能技术生成

参考: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
  •  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值