Computing normals for UTF8Loader
I noticed that the UTF8Loader does not compute normals. We are receiving high resolution scans from an external vendor in the form of OBJ files, very large OBJ files. I am using the obj2utf8 executable to generate the UTF8 and JSON files. When the asset is loaded with UTF8Loader it comes in flat black.
I ran an experiment to see how it would work if either the vendor included normals with the OBJ or we generated them internally by importing the OBJ into Maya and re-exporting with normals enabled. What I found is that the OBJ was more than twice the size (from 450MB to roughly 1.2GB in one case), and that obj2utf8 conversion time goes from minutes to quite literally hours.
So I then did a test in the BufferGeometryCreator.create() where I added an integer variable to the loop that builds the normal array and store the sum of all the normal vectors.
var testNormals = 0;
for( i = offset; i < end; i += stride ) {
x = attribArray[ i ];
y = attribArray[ i + 1 ];
z = attribArray[ i + 2 ];
testNormals += x + y + z;
normalArray[ j++ ] = x;
normalArray[ j++ ] = y;
normalArray[ j++ ] = z;
}
and at the end added the computeVertexNormals() call on the geometry if that variable is 0
if ( testNormals == 0 ) geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
At least for our work flow this is solves the issue of normals. Not sure if you want this computation in the loader so I didn't make a PR. Also if there is interest I don't think my implementation may be exactly the right choice, but wanted to share anyways to get an opinion or 2 on this.
Have you considered using ctm? I think it is smaller and faster than utf8?
No. I needed something that will convert an OBJ directly and the UTF8 tools already had that (on a deadline and not a lot of time to be compiling libraries and writing converters). It isn't like the UTF8 format, and loader, are doing a bad job. It does reduce a 450MB file to under 50MB and people using up to date chrome report good loading times. In fact both loading time and fps in the browser (with a turntable rig, 3 point light setup, and shadows enabled) are yielding better results than if the asset was brought into Maya.
The only hiccup was that computing vertex normals was not handled in the loader, and I just looked at the CTMLoader and it does compute them. Just seems like one would want similar behavior in the loaders to maintain some consistency.
Yep. Sounds good to me. I was just curious if utf8 performed better than ctm.
Good question. If I can get a model in both formats I certainly would be interested in conducting speed tests, for the sake of clarity. Any feedback on the implementation before I submit a PR?
Side note: I think you would be happy to hear that the largest test I performed was with over 6 million triangles and that the geometry buffer handled it quite nicely.
The implementation seems a bit weird. Isn't it a way to know if the utf8 data contains normals or not? Looking at your implementation, looks like utf8 has an array filled with 0s if there are no normals?
Oh, and yes... 6 million triangles! \o/
Correct, for whatever reason the attribArray is being filled with 0s. To be completely honest I have no clue if this is happening when the decompression of the utf8 data happens or if the obj converter is populating the utf8 data with null values that are translating to 0. Yes my implementation is a bit weird and not really addressing the root of the problem. I guess since this loader probably isn't a priority, at least concerning assets without normals, then this can be a slow cook issue until it is addressed properly. I have my fix that is working so I'm not blocked but I did want to bring it to your attention.
And now it looks like there are 2 issues to address: 1 - what is causing (0,0,0) vectors 2 - how to determine no normals are defined and compute them
/ping @won3d
@repsac Is there any chance I could get a sample? That obj2utf8 performance sounds pretty atrocious.
Anyway, IIRC without the OBJ having normals, the encoder will assume you didn't want them, so you end up with the 0s. If you want to compute vertex normals, you can do a fair reconstruction based on triangle normals. In fact, that is what is happening here: http://webgl-loader.googlecode.com/svn/trunk/samples/walt.html
See computeNormals: https://code.google.com/p/webgl-loader/source/browse/trunk/samples/walt.js
@mrdoob CTM is faster? That's surprising. I guess they did something to improve performance. Anyway, I'm actually spending my IRL 20% time on mesh compression again, so there's that.
@won3d The assets this occurred on are all internal at my job, but I do have the Blender open movie DVDs and I can look at creating a dense test asset that I can recreate the performance problem with so there is something I can openly share
@won3d sorry for the delay but I have a test case to share with you. It is with an asset with 1.3 million triangles.
Without normals: 184.533 seconds With normals: 29472.317 or 8.187 hours
https://dl.dropboxusercontent.com/u/2907790/obj2utf8_test.zip
@repsac It happens I will have time tomorrow to take a look at this.
Oh and there is absolutely no urgency on this whatsoever. The obj files we get from the vendor don't have normals anyway and I hacked the UTF8Loader to compute them for us. Prior to this I tried bringing the obj into Maya and re-exporting with normals on and that is when I noticed the huge performance hit. Clearly the margin between no normals and normals is larger than one would expect so I thought I would mention it because if I was you this is something I would want to be aware of.
Again this is all at your leisure so don't feel obligated.
@repsac Well, thanks for the sample, it was very helpful. I'm actually working on something that hopefully will replace the old compression stuff, and this test case already shook out some defects. The new stuff also doesn't seem to have this performance issue, so yay to that.
Good to hear. Look forward to seeing what you come up with next!