-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
FB_ngon_encoding extension. #1620
Open
zellski
wants to merge
3
commits into
KhronosGroup:main
Choose a base branch
from
zellski:spec/fb-ngon-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
15 changes: 15 additions & 0 deletions
15
extensions/2.0/Vendor/FB_ngon_encoding/schema/glTF.FB_ngon_encoding.schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
"type": "object", | ||
"description": "glTF extension for reconstruction general polygonal meshes", | ||
"allOf": [ | ||
{ | ||
"$ref": "glTFProperty.schema.json" | ||
} | ||
], | ||
"properties": { | ||
"extensions": {}, | ||
"extras": {} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Update schema with new glTF schema version, add ID, and adjust title. The file should be renamed to
mesh.primitive.FB_ngon_encoding.schema.json
to clarify that this extends glTF mesh primitives.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 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 comment
The reason will be displayed to describe this comment to others. Learn more.
See saturday06/VRM-Addon-for-Blender#101