-
Notifications
You must be signed in to change notification settings - Fork 1.2k
FB_ngon_encoding extension. #1620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # FB_ngon_encoding | ||
|
|
||
| ## Contributors | ||
|
|
||
| * Pär Winzell, Facebook, [zell@fb.com](mailto:zell@fb.com) | ||
| * Michael Bunnell, Facebook | ||
|
|
||
| ## Status | ||
|
|
||
| Draft | ||
|
|
||
| ## Dependencies | ||
|
|
||
| Written against the glTF 2.0 spec. | ||
|
|
||
| ## Overview | ||
|
|
||
| While glTF can only deliver a polygon mesh after it's been decomposed into triangles, there are cases where access to the source topology is still useful. One common such case is [Catmull-Clarke subdivision surfaces](https://en.wikipedia.org/wiki/Catmull%E2%80%93Clark_subdivision_surface), which work best with quadrilaterals. | ||
|
|
||
|
|
||
| This extension contains **no additional data**: | ||
| ``` | ||
| "meshes": [ | ||
| { | ||
| "name": "Apple", | ||
| "primitives": [ | ||
| { | ||
| "indices": 0, | ||
| "attributes": { | ||
| "POSITION": 1, | ||
| "NORMAL": 2, | ||
| "COLOR_0": 3, | ||
| "TEXCOORD_0": 4 | ||
| }, | ||
| "material": 0, | ||
| "mode": 4, | ||
| "extensions": { | ||
| "FB_ngon_encoding": {} | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| ``` | ||
|
|
||
| Rather, it marks the use of an implicit scheme where the **order of triangles and per-triangle vertex indices** holds all the information needed to reconstruct the original structure, and can be generated via a simple triangulation process: | ||
|
|
||
| - For each polygon `p`, choose one identifying vertex `v(p)`, | ||
| - Break the polygon into a fan of triangles, all anchored in that same `v(p)`, | ||
| - (It's obvious that this can be done for convex polygons, e.g. quads, but the true constraint is a bit less strict.) | ||
| - Proceed with the next polygon `p'` where, importantly, `v(p') != v(p)`. | ||
|
|
||
| This lays each polygon's constituent triangles down sequentially, grouped by sharing the same `triangle.vertices[0]`. Given such a sequence, it's then trivial to decode the original structure with linear iteration. A sketch of a possible implementation can be found [below](#Resources). | ||
|
|
||
| Advantages of this scheme: | ||
| - The meshes remain valid glTF 2.0, | ||
| - Asset size is unchanged, since we add no new data, | ||
| - The scheme works with arbitrary mixtures of triangles, quads, and so on, | ||
| - Very simple to implement. | ||
|
|
||
| There are a few geometric concerns for non-quad cases: | ||
| - Polygons that can't be decomposed into a triangle fan must first be cut into ones that can, | ||
| - Even polygons that are technically convex can yield triangles narrow enough to be nearly degenerate. | ||
|
|
||
| On a pragmatic ecosystem note, we must also remember that glTF 2.0 normally allows reordering of triangles and triangle vertices (in fact, such operations are common optimisations), and so tools unfamiliar with this extension would likely destroy the structure we rely on here. Then again, they would presumably not preserve the extension in their output, so at worst one ends up with glTF that's unextended yet still valid. | ||
|
|
||
|
|
||
| ## glTF Schema Updates | ||
|
|
||
| * **JSON schema**: [glTF.FB_ngon_encoding.schema.json](schema/glTF.FB_ngon_encoding.schema.json) | ||
|
|
||
| ## Known Implementations | ||
|
|
||
| * [FBX2glTF](https://github.com/facebookincubator/FBX2glTF) (shortly) | ||
| * Internal Facebook AR/VR development | ||
|
|
||
| ## Resources | ||
|
|
||
| A sketch of an implementation. Output each polygon as a fan of individual triangles. Make sure that the triangles for a polygon do not start with the same vertex index as the triangles of the preceding polygon. | ||
| ``` | ||
| // convert mesh to triangles | ||
| int startIndex = -1; // invalid vertex index | ||
| for (int p = 0; p < numPolygons; p++) { | ||
| int start = startIndex == polygon[p].vertex[0] ? 1 : 0; | ||
| startIndex = polygon[p].vertex[start]; | ||
| int n = polygon[p].numVertices; | ||
| for (int v = start+2; v < start+n; v++) | ||
| EmitTriangle(startIndex, polygon[p].vertex[v-1], polygon[p].vertex[v == n ? 0 : v]); | ||
| } | ||
| ``` | ||
|
|
||
| Reconstruction reverses the process: | ||
| ``` | ||
| // convert triangles back to original polygons | ||
| int startIndex = -1; | ||
| for (int t = 0; t < numTriangles; t++) { | ||
| if (triangle[t].vertex[0] != startIndex) { | ||
| if (startIndex >= 0) | ||
| EndPolygon(); | ||
| BeginPolygon(); | ||
| startIndex = triangle.vertex[0]; | ||
| EmitVertex(startIndex); | ||
| EmitVertex(triangle[t].vertex[1]; | ||
| } | ||
| EmitVertex(triangle[t].vertex[2]); | ||
| } | ||
| if (startIndex >= 0) | ||
| EndPolygon(); | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,15 @@ | ||||||||||||||||
| { | ||||||||||||||||
| "$schema": "http://json-schema.org/draft-04/schema", | ||||||||||||||||
| "title": "FB_ngon_encoding glTF extension", | ||||||||||||||||
|
Comment on lines
+1
to
+3
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update schema with new glTF schema version, add ID, and adjust title. The file should be renamed to
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aaronfranke I recommend taking this under omi or godot namespaces and doing the updates. There is a vrm blender addon that has ngon support. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||
| "type": "object", | ||||||||||||||||
| "description": "glTF extension for reconstruction general polygonal meshes", | ||||||||||||||||
| "allOf": [ | ||||||||||||||||
| { | ||||||||||||||||
| "$ref": "glTFProperty.schema.json" | ||||||||||||||||
| } | ||||||||||||||||
| ], | ||||||||||||||||
| "properties": { | ||||||||||||||||
| "extensions": {}, | ||||||||||||||||
| "extras": {} | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The true constraint is a star-shaped polygon: https://en.wikipedia.org/wiki/Star-shaped_polygon
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The true constraint is more strict than "star-shaped", and it doesn't have a name of which I'm aware. For example, here are a couple of star-shaped polygons that don't work:

@devernay earlier said:
But actually all pentagons are fine (see the beginning of https://math.stackexchange.com/a/249070/102988 for explanation): so apparently you need n>=6 (e.g. the pink non-convex star-shaped hexagon shown above) for there to be a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@donhatch Those work fine, if you add one more vertex in the middle for all triangles to share:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aaronfranke true, but not relevant, unless I completely misunderstood your claim. If I understood correctly, you claimed that the class of polygons for which the described scheme (which doesn't include adding a vertex) works is the star-shaped polygons. I'm saying that claim is wrong, as shown by these two counterexamples (star-shaped polygons for which the scheme doesn't work).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@donhatch Star-shaped doesn't just mean "shaped like a star", it is specific math terminology: https://en.wikipedia.org/wiki/Star-shaped_polygon
FB_ngon_encoding only works when all triangles share a starting vertex, which means all triangles share a vertex, which is an equivalent condition to requiring that a point exists within the shape from which all other points are visible (since those lines can form the edges of the triangles). To be clear, this does mean that you would need to add a vertex to encode the examples you gave using FB_ngon_encoding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Randomly stumbling over this:
I asked a related question on Math.SE 12 years ago: https://math.stackexchange.com/q/856399 And even though this referred to general domains (and not polygons), I also hoped that it there was a description that referred to a specific (given) point. Right now, it only says: "It is a shape for which such a point exists", and ... what are computer scientists supposed to do with such a claim...?
However, I think that one could describe the condition here analogously to the definition from Wikipedia.
For Star-Shaped:
For what is needed here
Right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aaronfranke Thanks, yes, I understand what the mathematical term "star-shaped" means.
The intent of my comment was to try to make sure there is no misunderstanding.
Your earlier comment sounded, to me and perhaps to others, like you were offering "star-shaped" as a name for the condition referred to as "the true constraint" for which the described procedure can be done, in the README. But my reading of that passage is that it's talking about a procedure, central to the spirit of this extension, that does not include changing the vertex arrays. So "star-shaped" doesn't work as a name for what it's talking about.
This nuance looks important to me in light of @andybak's earlier comment:
Yes, it should certainly be renamed; FB_ngon_encoding is a indeed quite a bad name and would cause damage. What would be a better name for the extension? I don't know, but I do want to nip in the bud any possible suggestion that it be called FB_star_shaped_ngon_encoding since that would be incorrect/misleading/chaotic too, since "star-shaped" isn't the right term.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javagl wrote:
That sounds right to me. Unfortunately I don't know a good clear name for this concept and can't think of a good short one.
For the README, I suggest "star-shaped with respect to some vertex"; that seems good, clear, and concise to me. But that's a bit long to be used in the extension name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In G4MF, I called this
"polytopeCells", intending to imply that multiple simplex cells (ex: triangles) combine together to form a polytope (ex: polygon), as opposed to other non-cell (non-triangle) ways to encode polytopes (polygons). So perhaps some variant ofpolygon_triangles,ngon_triangles,ngon_tris,polygon_tris, etc. The "encoding" part of the extension name doesn't do much work, so to speak.