vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
470 stars 75 forks source link

Add normals to a MeshBuilder<VertexPosition> #25

Closed tongbong closed 4 years ago

tongbong commented 4 years ago

Hello and thank you for this library,

I am trying to convert a STL file into a GLTF file.

So I create a mesh like this :

            var mesh = new MeshBuilder<VertexPosition>();
            var prim = mesh.UsePrimitive(material, 3);

            foreach (var stlMesh in reader.ReadFile())
            {
                var vertice1 = VERTEX.Create(new System.Numerics.Vector3(stlMesh.vert1.x, stlMesh.vert1.y, stlMesh.vert1.z));
                var vertice2 = VERTEX.Create(new System.Numerics.Vector3(stlMesh.vert2.x, stlMesh.vert2.y, stlMesh.vert2.z));
                var vertice3 = VERTEX.Create(new System.Numerics.Vector3(stlMesh.vert3.x, stlMesh.vert3.y, stlMesh.vert3.z));
                prim.AddTriangle(vertice1, vertice2, vertice3);
            }

So far so good, the conversion is ok and I am able to see the 3D object in a gltf viewer (https://gltf-viewer.donmccurdy.com/).

But now I would like to add normals to the mesh and I am stuck here. Is there any way to do it?

vpenades commented 4 years ago

Yes, you can use VertexPositionNormal instead of VertexPosition

If you check the Geometry/VertexTypes namespace, you'll see there's many vertex combinations already defined.

Furthermore, if at some point you need vertex colors or texture coodinates, you define them like this:

new MeshBuilder<VertexPositionNormal, VertexColor1Texture1>();
tongbong commented 4 years ago

Yes, I saw that, but I came to understand it just now! Thanks for the quick answer.