com.unity.cloud.gltfast
com.unity.cloud.gltfast copied to clipboard
Primitive Mode TRIANGLE_STRIP and TRIANGLE_FAN implementation, LINE_LOOP render fix
According to these two issues;
- https://github.com/atteneder/glTFast/issues/316
- https://github.com/atteneder/glTFast/issues/689
These primitive modes are not implemented:
-
TRIANGLE_STRIP
-
TRIANGLE_FAN
And this primitive mode is not rendered correctly:
-
LINE_LOOP
TRIANGLE_STRIP
and TRIANGLE_FAN
implementation
First of all, we need example assets for the two primitive mode implementations (TRIANGLE_STRIP
, TRIANGLE_FAN
)
We can find those assets from these two links;
- https://github.com/KhronosGroup/glTF-Sample-Assets/tree/main/Models/MeshPrimitiveModes
- https://github.com/KhronosGroup/glTF-Asset-Generator/tree/main/Output/Positive/Mesh_PrimitiveMode
First link includes a gltf file with all the primitive modes embedded in one file. Second one has seperate gltf files where each one is a different primitive mode.
In order to correctly implement these triangle primitive modes gltf-2.0 specification documentation of Khronos Group was used;
- https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html
From this documentation these two formulas was used;
Triangle Strips One triangle primitive is defined by each vertex and the two vertices that follow it, according to the equation:
- pi = {vi, vi+(1+i%2), vi+(2-i%2)}
Triangle Fans Triangle primitives are defined around a shared common vertex, according to the equation:
- pi = {vi+1, vi+2, v0}
Since it was necessary to create the indices from scratch this change was necessary; before
var usage = (
primitive.mode == DrawMode.Triangles
|| primitive.mode == DrawMode.TriangleStrip
|| primitive.mode == DrawMode.TriangleFan
)
? AccessorUsage.IndexFlipped
: AccessorUsage.Index;
after
var usage = ( primitive.mode == DrawMode.Triangles )
? AccessorUsage.IndexFlipped
: AccessorUsage.Index;
In order to check if the results was correct gltf viewer of Khronos group was used;
- https://github.khronos.org/glTF-Sample-Viewer-Release/
Since I couldn't find any other example assets I used blender to generate them. These are the steps to do so;
- Import or draw any shape in Blender (I used Blender 4.2)
- Export using gltf seperate option
- Add primitive mode to the glft file (or change the existing from 4
TRIANGLE
to 5TRIANGLE_STRIP
or 6TRIANGLE_FAN
) - Import the asset to Unity and gltf viewer of Khronos group, and compare them
- The results were looking the same even though the topology was messed up
I am not sure if using blender was correct way to check that but the original assets were correctly imported.
LINE_LOOP
render display fix
As stated in the issue it was needed to add one more index to the end of the original index list. In line 3661 Array.Copy
was used (I don't know if Job system was supposed to be used here too)
Unity version 2022.3.38f1 was used in this PR