Tuesday, June 21, 2016

unity - How does Unity3D lower an imported .obj vertex count?


I have a question regarding how Unity3D handles importing of .obj files. I'm importing this teapot: http://groups.csail.mit.edu/graphics/classes/6.837/F03/models/teapot.obj


The number of vertices this teapot has is 3644. I know that Unity's Mesh class needs to replicate these vertices for each face shared by the same vertex. I tried this with a Cube imported from a .obj, and using Debug.Log() to print the number of vertexes I found out that there were 24 vertices in the mesh's vertex array.


However, with the teapot the original file had 3644 vertices, so in Unity the vertex count should be 18960. Instead, when I print the length of the mesh's vertex array, it prints 3260 (even less than the original file).


The final objective for this is that I'm trying to modify an OBJ Importer script from Unity's Wiki, so that the number of vertices resulting from this script are the same as Unity's native importer. Reference: http://wiki.unity3d.com/index.php?title=ObjImporter Note: I printed the vertex count using this importer and the result was 18960.


Does anyone have an idea of how this vertex reduction might be achieved?



Answer



There are several factors that contribute to how the geometry is saved and processed in different mediums (programs, file formats, APIs). I did implement several commercial geometry analysis programs, so I have some experience with it.



First I am sure this has nothing to do with Unity simplifying anything. But in case it did there are several decimation algorithms that can be implemented but I am sure this isn't the case, since a game engine isn't supposed to change the shape (geometrical properties of a mesh) but can usually modify its connectivity to match his optimal structure.


Some of these factors are:


What makes a vertex unique?


This can greatly vary depending on how the program, api or file format deals with a vertex. A vertex can be unique if it has a unique position, but in other cases it's unique if it has any unique attribute. For example if two vertices share the same position, but have different normals they should be "duplicated" in case of OpenGL, this is especially evident in the case of the cube you mentioned.


enter image description here Hard vs Smooth edges.


Normals can dictate if an edge is considered hard or smooth, if a vertex contributes to hard edge, then it needs to be duplicate so the current rendering APIs can understand how to render(shade) this face correctly.


enter image description here


Indices


Many file formats permit a face vertex to have several indices for the vertex position/texCoords/normals, this isn't the case of current rendering APIs. A fine example of this is the Obj file, this will force Unity to rebuild the indices and often duplicate many attributes to match the vertex indices.


Connectivity



A lot of the vertices that are actually connected and are unique are saved multiple times, this has to do of how the original file writer processed and saved the mesh. Unity will reduce the number of duplicate vertices (remember if it has different attribute then it's not unique) so to make it more efficient for storage and processing without sacrificing the quality.


This has also to do of how many polygon sides the original file permits, Obj for instance permit N-side polygons, which something any sane game engine doesn't want, so it end up triangulating the mesh and recalculting many of its attributes which often change the number of vertices.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...