Forum

Flat shading for dynamic geometry and vertex arrays

10 January 2018 20:33
Hello Blend4Web guys, and thumbs up for the good work with blend4web!

context
I am prototyping an online model viewer for 3D meshes in an exotic format (which is used in my university for running simulations, and based on which the lab I work in has developed a few codes).

To do so, I just have created a blender scene with my desired render options, and load the user object on runtime by parsing the user file and overriding an existing cube (set to flat shading) geometry (using IBOs and VBOs, which are the natural structure for our 3D file format).

So far so good…

problem
However, the meshes I load that way are automatically "smoothed", although I don't provide any normal buffer, and I would like to be able to have a flat shading by default in my app (mechanical pieces, performance reasons, ease of coding…).

how I deal with it
I manage to do so with creating new IBO and VBO by extracting the 3 vertices for every triangle of my mesh, computing the triangle normal, and storing the same computed normal for each vertex of every face.
This low level approach is quite tedious, and some apps I may develop could use other vertex information (simulation results which might be 3d vectors for instance…).

The scene object which geometry is overridden is a simple cube, which is set to flat shading before the export.
I saw in an other post (june 2016) that this seems to be the classic method for shapekeys.

why I am not happy
Although this method works, it makes the code way more complicated, the memory usage exponentially increases, and the computing time (which is a trouble here, since the objects used for numerical simulation can be quite heavy - 100k+ faces - as they often are unstructured triangular meshes) at the same time…

help needed!
After this long explanation (probably for nothing), here comes my question:
Is there a way to use the override_geometry method with indexed IBOs and corresponding VBOs, and still benefit from a flat shading? (playing on the "smooth_normals" parameter does not fix the problem)

Thank you in advance for any help!
11 January 2018 17:49
why I am not happy
Although this method works, it makes the code way more complicated, the memory usage exponentially increases, and the computing time (which is a trouble here, since the objects used for numerical simulation can be quite heavy - 100k+ faces - as they often are unstructured triangular meshes) at the same time…

The memory usage can't increase exponentially in this case. It will increase linearly ~3*n. Currently flat shading is implemented with simple separation of each triangle.
But technically it is possible to implement special Flat Shader using derivatives dFdx, dFdy, or with flat qualifier on WebGL 2. It could work slower then simple separation of triangles and could cause issues in shaders that use Normals. Also Editing of Normals will not be available - there will be pure Flat Shading where normal is orthogonal to triangle. However, I think that this functionality could be useful.

We will decide soon whether we add this feature and in which form it will be represented.
Alexander (Blend4Web Team)
twitter
11 January 2018 20:34
Hello Alexander, and thank you very much for your fast answer!

Disclaimer: By saying that I was not happy, I was of course saying that I was sad, not unhappy with blend4web of course!

By exponentialy I meant "drastically" (even is a factor of 3 or 4 on vertex attributes is still not a factor 100, but starts to impact quite a bit loading times and memory usage on large meshes), but thank you for the precision though.

For my application, a pure flat shading is exactly what I am looking for as the objects do not necessarily have additional normal information. Judging by your answer, I deduce that there is no "straightforward" way to obtain a flat shading with dynamic geometry, and will therefore stick to triangle separation for the time being.

However, if the other ways to implement this feature work quite the same and won't significantly improve the performances, I'm perfectly fine with this approach .

IMHO, I think that this functionnality could mainly be useful for model viewers or equivalent apps, which should not require normal editing I guess, and would therefore vote for an additional parameter in the override_geometry method… My 2 cents .

Thank you anyway for your help!
12 January 2018 10:54
Actually for WebGL 2 with flat qualifier the performance may be increased, because there will no normal interpolation. But in case of dynamic geometry there will be some complication: you will have to set correct normal for each vertex which will correspond to some triangle, and you will have to sort its indices.
Anyway, dynamic geometry API is one of the most important targets for our engine. And we are going to significantly improve the engine in this area
Alexander (Blend4Web Team)
twitter
12 January 2018 17:30
Anyway, dynamic geometry API is one of the most important targets for our engine. And we are going to significantly improve the engine in this area
Good to know!

Thank you for the input on this topic, "case closed" for me!
 
Please register or log in to leave a reply.