three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Angle based vertex normal smoothing

Open GuyGood opened this issue 13 years ago • 33 comments

Hi guys,

I wanted to try and contribute to Three.js and this thread is for discussing about the stuff i wrote.

The questions that are open:

  1. Do you want to use this functionality or do you think three.js needs it?
  2. What do you think of my algorithm. I was hoping for some guys to check the functions and maybe try to optimize the functionalty in case question 1) is "yes" ;)

What i caught missing in THree.js is the functionality to smooth dynamically generated geometries based on an angle. (like in 3dsMax for example) If the angle between two face normals is smaller than 'angle' the faces get smothed and the vertex normal accounts for that smoothing.

I forked the three.js repository and created a new branch.

I provide an example in my github fork: https://github.com/GuyGood/three.js/blob/AngleBasedSmoothing/examples/webgl_geometry_smoothing.html

The example compares an extruded geometry with bevel applied. left: unsmoothed as is, middle: my angle based smoothing, 25°, right: computeVertexNormals(true) http://img109.imageshack.us/img109/3081/v0jc.png

As I put all my code into Three.GeometryUtils without rebuilding, my example directly uses GeometryUtils.js script.

New stuff to GeometryUtils briefly explained:

So, i added the function "calculateVertexNormals" (should then be renamed to sth else). It firsts creates new vector3 objects for every vertexNormal for every face of the mesh. (just for having everything at zero).

It then fills an array 'adjacentNormals' with objects so that it contains a face and the vertexNormal, and an empty array for the smoothingGroup stuffs for later on.

then there is the helper method _sortAdjacentFaces. This is because what i am going to do is iterate over all vertices and check all faces connected to one vertex , so to say in a circular manner. And if i don't sort the array beforehand, i would compare faces, connected to the vertex that do NOT share one edge, which would be no good. As a side note, the last iteration compares the last and first face of the vertex in question.

After sorting, the algorithm gets the angle between the faces and checks if it is <= the user defined angle. if it is, it will generate 'smoothing groups' . These groups are local to each vertex and do not work like in 3dsMax for example. They are just for knowing which facenormal from which face to take into account for the vertex normal calculation.

After having calculated the groups and based on them, the vertexNormal is calculated using the helper function: '_calculateVectorSumsForSmoothingGroups'

This function is much like the "computeVertexNormal" from geometries but is always area weighted, so normals are accumulated but not normalized within there.

Finally, normalize the vertex normals and we are done.


I hope i could explain some of the stuff and maybe you like the general idea. I am just proposing some code for review because although it works, there are for sure 2-3 small issues and maybe it can be greatly optimized. p.s. i know the code is for sure not nice to read but i hope you like the feature anyway and want to have a discussion about it.

Thanks in Advance

GuyGood avatar Jul 24 '13 17:07 GuyGood

If the code to do this is short I think it could be nice to include in the lib itself. But seems like it's not.

mrdoob avatar Jul 24 '13 18:07 mrdoob

No, it is a lot ot stuff, because of going through all of the faces and all the extras. I think there could be some optimizations, but i dont think it can be done with less than 100 lines, if at all. atm it is 290 lines cough in length.

What you should think about is that this code also replaces computeVertexNormals as you could enter 180° angle and have everything smoothed. I compared two completely smoothed cubes and they look the same! computeVertexNormals is 150 lines of code, too ;)

The thing is, that as Three.js is web-based i think that many people are going to create Meshes dynamically and this function would be a great addition, esspecially if you have shapes with holes and all of this.

Anyway, i just wanted to show this and thought it would be useful to many users :)

GuyGood avatar Jul 24 '13 18:07 GuyGood

hmm, so 20 days later? :) maybe anyone else has some comments about the code or how to shorten it a bit?

@mrdoob Well, what do you think, i mean by removing the original computeVertexNormals because my "new" code can do the same and one could apply a shortcut if the user wants totally smoothed stuff.

So you remove the old computeVertexNormals which is 150 lines and the new stuff would only add 200 new lines, which i don't think is a lot. Anyway, just wanted to hear your final judgement because i would remove the online code if no one is interested.

GuyGood avatar Aug 13 '13 11:08 GuyGood

An alternative solution for custom geometries is to duplicate vertices on hard edges (and, if desired, on seams). In that case, Geometry.computeVertexNormals() should be OK as is.

WestLangley avatar Aug 13 '13 18:08 WestLangley

Hey WestLangley, cool that you are joining in. Does your answer imply that you, too, don't see a necessity for angle based vertex normal smoothing and would opt for another approach but whatever it is, not include it in three.js? Because as you proposed, a user would have to somehow find seams/hard edges and then duplicate those specific vertices? :)

GuyGood avatar Aug 13 '13 22:08 GuyGood

three.js is primarily a rendering library, not a modeling library, and as such, does not have sophisticated modeling tools. It is up to @mrdoob to decide if he want to include features like the one you proposed.

I think it would be great if you kept your utility available on github for others to use. I have seen this feature requested in the past.

WestLangley avatar Aug 13 '13 22:08 WestLangley

ok. Well, my main points of thought were that as three.js is web-based, 1) there could often be dynamically created content and 2) it influences the display of content, so it is definetly connected to rendering.

Anyway, may I ask you 1 question: How deep did you check my code or read my explanation? Do you think one could shorten the source code or optimize it? Anyway, just a question :) Thanks for participating in this at all :)

GuyGood avatar Aug 13 '13 22:08 GuyGood

@GuyGood few questions -

from the screenshot alone, the smoothing does seems to give better appearance, and could be a feature well desired, but how well does it apply to other different kind of geometries? does the angle need to be selected intelligently, or is that an angle (like 20 degrees perhaps) that work well in general? also, do you have any links to reference on the approach you or others have implemented here?

lastly, if computeVertexNormals is replaced with your function, how much penalty in performance (since I guess this probably does more calculations..)

zz85 avatar Aug 17 '13 06:08 zz85

hey zz85, great to have a response at all :)
warning: sorry, long reply :D

"how well does it apply to other different kind of geometries?"

Well, i did not test a lot, yet. Mainly because I wanted to know if this feature is wanted at all and for my use case works. I am generating some kind of steel plate with holes and bevel and the smoothing looks pretty good so that i am fine with it. The circle shape in the screenshot was just for showing the audience, so to say :) I am not sure if there is any kind of geometry edge case that would not work except of course for those geometries where an angle-weighted approach is not useful. What i can think of is deformed planeGeometries that i have not tested yet but i think they should smooth too as the user wants.

"does the angle need to be selected intelligently"

Definitely. It all depends on the bevelling parameters or whatever the user generated or what look he/she wants to achieve.

also, do you have any links to reference on the approach you or others have implemented here?

No, unfortunately not. I found a gamedev-thread which i don't have at hand right now but the people all had some different ideas and I was just implementing a stupid straight forward approach which seems to work but is not quite tested and maybe has some edge case problems. I also posted here because I thought you guys would give me some input too about what you think how it can be done or done faster.

I will search some more stuff, for example i found this one:

http://docs.autodesk.com/3DSMAX/15/ENU/3ds-Max-SDK-Programmer-Guide/index.html?url=files/GUID-0FCB4578-77F8-4F05-99CD-349E85F13639.htm,topicNumber=d30e22921

But i did not have the time to go into more detail as the problem is not quite that easy after a second look. You can divide the problem in two parts:

  1. One would have to check for faster ways (or shorter to program ways) to find adjacent faces/edges.
  2. Maybe there is even a better way to generate those "smoothing groups".

Well, I thought if mrDoob or others liked the feature, they would chime in or give tips about how to find and cache adjacent faces/edges and so on but I am willing to do this, too!! Right now i am fine but i would love to shorten the code with some nice math and javascript magic and i would invest more time in finding papers on this problem!

lastly, if computeVertexNormals is replaced with your function, how much penalty in performance

Yes, it does a lot more calculus and you can't do this in the render loop but why wouldn't you at all. I measured around 30ms on an old core2 duo desktop CPU and for the first run a bit more ( i guess the browser does some caching here and there). Although i already changed the functions a bit and moved some variable declaration stuff outside of some loops which seemed to help improve performance.

Anyway, as the area weighted averaging is taken from the now-used computeVertexNormals function, one could write a really short bypass via a boolean flag if no angle is provided for example and just iterate over all faces the way it is done now. Then, performance would be the same as it is now and the results are the same, too.

GuyGood avatar Aug 17 '13 18:08 GuyGood

I stumbled on this issue today, as I was looking to use the creaseAngle property from a VRML IndexedFaceSet to attain a smoothed curved surface. From what I have found so far, I can only choose between smooth nothing and smooth everything, am I right @mrdoob @WestLangley ? Both solutions (everything and nothing smoothed) look horrible in this particular use case (a bent shape with holes in it).

bartmcleod avatar Jan 08 '17 15:01 bartmcleod

@bartmcleod The constant THREE.SmoothShading was poorly-named. See the discussion in #10383.

You need to fix your geometry. Duplicate vertices on hard edges and specify proper vertex normals for each vertex.

WestLangley avatar Jan 08 '17 18:01 WestLangley

@WestLangley Thanks a ton for your quick reply. I found similar answers by you on Stackoverflow earlier, but I couldn't really believe it. It sounds cumbersome and smelly, as a workaround-more than solid design-to have to duplicate hard edges.

From your older answers I understood that it should be enough to duplicate the hard edges. From your current answer, I understand that we need not only duplicate the edges, but also specify vertex normals. This would imply that simply calling computeVertexNormals() would not be sufficient, correct?

Since in my project, I am loading legacy geometry from VRML files, I would have to compute the duplications and the normals, which should be possible once I fully understand the principles.

bartmcleod avatar Jan 08 '17 19:01 bartmcleod

For anyone who finds this with the same question: A small test shows that with duplicate edges, it works just fine when calling only computeVertexNormals() to generate the normals automatically. Of course this does not respect any specific creaseAngle, just some default angle. Make sure not to call mergeVertices(), because this will get rid of any duplicate vertices you created to cater for the duplicate edges. Like I said, this was in a small test, with only three square faces in an IndexedFaceSet, which will result in 6 faces after parsing the original VRML.

bartmcleod avatar Jan 10 '17 17:01 bartmcleod

@GuyGood The image you linked too no longer exists in that location.

bartmcleod avatar Jan 31 '17 06:01 bartmcleod

@WestLangley When I look at a standard ThreeJs Cylinder, rendered with computeVertexNormals(), it gets smooth sides, but the angle between the sides and top and bottom appears to be smooth too. image

Also, the green cube is an IndexedFaceSet with duplicated edges where sharp. It is triangulated and it shows with the lighting. How can this be avoided? The diagonal edge is not duplicated, so it should appear as smooth I think.

bartmcleod avatar Jan 31 '17 17:01 bartmcleod

@bartmcleod Sorry, this is not the proper forum for help. Perhaps you can try stackoverflow.

WestLangley avatar Jan 31 '17 21:01 WestLangley

Yeah, ok.

bartmcleod avatar Jan 31 '17 21:01 bartmcleod

bart,

What are you looking for? If you can edit the normals of your VRML object in a dedicated Digital Content Creation Software like Blender/3dsMax/Maya whatever, then you should do so.

If you cant, you can try out the method that I wrote from my fork. Or does this not work anymore? I havn't been using ThreeJS for a long long time. Concerning the image, i don't know what it displayed. I could only guess, sorry. But it was for sure some kind of bevel on a cylinder that was correctly smoothed. Like this: http://imgur.com/a/APXgh

GuyGood avatar Jan 31 '17 21:01 GuyGood

GuyGood, how nice of you to respond on such an old issue.

I have now picked up this thread on Stackoverflow to discuss my issue further, as it might turn out to be just a need for help: http://stackoverflow.com/questions/27055644/three-js-maintaining-creases-when-smooth-shading-custom-geometry

I think the simplest use case that describes my problem is a Cylinder with computeVertexNormals(). It does not appear to have sharp edges. The top and bottom seem to bulge outward. I understand that it comes from averaging the vertexnormals, which makes sense. What I do not understand is that a default cylinder does not have the necessary duplicated edges to support computeVertexNormals() in a way that produces an expected result.

The same holds true for a standard cube. It also does not seem to have sharp edges and it lights up in a weird way, to say the least. I am getting even more confused, if I build a cube as an IndexedFaceSet, with duplicated edges where necessary (based on creaseAngle). It renders very similar if not identical to a default Cube. Also, a vague line is visible where the triangles are co-planar and should be completely smoothed out.

Of course, I can be totally wrong on all of theses topics, if I missed a point somewhere. I would be happy to try out your code by the way.

bartmcleod avatar Jan 31 '17 22:01 bartmcleod

👍 for the original suggestion of @GuyGood to have a way so smooth out areas based on a crease angle. I have written code now that can duplicate edges if the angle between two face normals is larger than a given crease angle, to keep that angle sharp, as @WestLangley suggested. This works well, but there is an impact on load time. I'm currently working on making this independent on how the geometry is loaded. The solution I have only works on triangulated faces currently, not on polygons.

bartmcleod avatar Feb 13 '17 19:02 bartmcleod