Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
Complex algorithms, I'm not sure what unity uses but you can find them online. –  MickLH Nov 26 '13 at 17:39

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
    
Oh I get it now! In the .obj importer I used, the vertices are always duplicated for each face they are used in, and that's why I always get hard edges on my teapot (haven't mentioned this before because I thought it was a different problem). Great answer! –  VitorOliveira Nov 27 '13 at 9:48

Obj formats are strange in particular because the positions, normals and texture coordinates are separated and each face can choose whatever index from each stream. When you refer to vertex count you think that the vertex positions and normals count is not the same and you can't really refer to the whole count.

During loading all these three attributes must be merged together so one vertex position could end duplicated because the face associated to that vertex position is associated to two normals. This can happen in reverse also so most loaders have a step to optimize the obj but in case of your cube the optimization will not work since the cube has hard edges and each vertex has a different normal(or texture coordinate). This is why you counted 6 vertex positions but unity duplicated those positions resulting in 24 vertices.

To load a mesh you can duplicate positions/normals or texture coordinates so optimizing this is not something simple. It boils down to choosing the minimum number of duplicates from those three streams.

share|improve this answer
    
This answer was a great complement of what concept3d said, and it helped me understand this problem. So I guess for now my .obj importer will always have hard edges, but if I want to improve it I should search for algorithms to help me know which vertices should be duplicated or not, in order to have a smooth edged model with minimum (or at least optimized) vertex count. –  VitorOliveira Nov 27 '13 at 9:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.