Git Product home page Git Product logo

Comments (5)

ibgreen avatar ibgreen commented on May 10, 2024

Most 3D libraries offer functions like calculateFaceNormals and calculateVertexNormals etc.

I actually have a half-finished implementation of these, I will look for it and put up a PR. I was doing this in math.gl since these calculations are completely webgl independent, though we can always discuss where it should go.

We should also consider @tsherif's proposal of using shader derivatives to automatically calculate normals in the shaders.

The "derivatives extension" seems to have 100% support, although those stats don't include headless-gl: https://webglstats.com/webgl/extension/OES_standard_derivatives. We should then decide if we use such calculation as a fallback in case normals are not provided etc.

In general, my instinct would be to resist adding normal calculations directly to loaders.gl, as it seems slightly outside the scope of this library. As an alternative, if we don't go the shader path, we could have the Geometry or Model classes autogenerate these if needed using functions e.g. from math.gl.

from loaders.gl.

tsherif avatar tsherif commented on May 10, 2024

I think Luma would be a good place for the normal calculations. Mesh processing seems outside the scope of math.gl. The Geometry class seems like a good place to me.

A couple of limitations to keep in mind:

  • The derivatives version will only give you the normal of the actual face, so things will have a faceted, flat-shaded look.
  • Vertex normal calculations can't do hard edges unless there's a separate vertex for each face, so you might end up rounding hard edges.

from loaders.gl.

ibgreen avatar ibgreen commented on May 10, 2024

I think Luma would be a good place for the normal calculations. Mesh processing seems outside the scope of math.gl. The Geometry class seems like a good place to me.

Yes, it would only make sense as part of a bigger initiative. I have what I think is a good plan here, but will add a PR on math.gl to explain my thinking before pushing ahead with this, we can implement this in luma in the mean-time.

The derivatives version will only give you the normal of the actual face, so things will have a faceted, flat-shaded look.

Yes. Ideally we would support both flat shading and smooth shading as an option (MeshLayer.flatShading: true/false).

  • Flat Shading - Supplying a normal = filterNormal(normal) module in a new geometry shader module would seems like a great way to support flat shading. It would return the passed in normal unless a flatShading uniform was set, in which case it would use derivatives to calculate a normal.

Vertex normal calculations can't do hard edges unless there's a separate vertex for each face, so you might end up rounding hard edges.

(For indexed geometries, we could do calculate flat shaded normals by first expanding indexed geometries. But if we use shaders for flat shading, this is moot.)

  • Smooth shading - We now only need to generate normals for the smooth shading case. Any geometry that uses indices does have the ability to encode that vertices are shared, and by adding face normals for each face using that vertex and then normalizing we get an average which can generate good results.

Obviously it is hard to generate normals in a predictable way for generic geometries. The results will depend on how vertices are shared in geometry which can depend on e.g. the exporter used to generate that geometry. But it will probably be reasonably good in many cases.

The smooth shading is harder to support in the general case. For non-indexed geometries it would require identifying shared vertices which is computationally more complex and expensive, so we may just generate flat normals for that case.

Does this sound like a plan or do you have other solutions in mind?

from loaders.gl.

tsherif avatar tsherif commented on May 10, 2024

If the only issue is a fallback for the SimpleMeshLayer when normals aren't provided, I can just add shader-generated normals to the SimpleMeshLayer fragment shader. For now, it could just be auto-enabled if the normals are missing.

from loaders.gl.

ibgreen avatar ibgreen commented on May 10, 2024

@loaders.gl/math has the initial algorithms for this. We can revive these if the need comes up again.

from loaders.gl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.