vpenades / SharpGLTF

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

Is there a way to add normal information while using PrimitiveBuilder.AddTriangle method? #5

Closed Imaver closed 5 years ago

Imaver commented 5 years ago

Hi vpenades,

Could you please kindly let me know if there is a way to add normal information while using PrimitiveBuilder.AddTriangle method?

I tested code from your examples and it worked perfectly with AddTriangle method. However, when I am trying to create Vertex with VertexPositionNormal component and pass it to AddTriangle method, it is not working at the moment. 3d models look good, but normal information is not there. I also tried to add some obvious 0,0,1 vectors to check if normal information is transferred, there were no visual changes.

Please refer to the attached file for code example.

If I am doing it wrong, could you please let me know how I can specify the normal information using AddTriangle method with a somewhat similar approach? TestGltf.txt

I am not sure if this is an issue but have no other way to reach you. Your help is very much appreciated.

vpenades commented 5 years ago

Checking your code, I've seen you're declaring the MeshBuilder with Positions only.

var mesh = new MeshBuilder<VertexPosition>("mesh");

Also, I've noticed you also want to define UV coordinates, so, you need to declare the mesh builder like this:

var mesh = new MeshBuilder<VertexPositionNormal, VertexTexture1>("mesh");

you also need to declare your vertices in the same way as the mesh, like this:

var a = new Vertex<VertexPositionNormal, VertexTexture1, VertexEmpty>();
var b = new Vertex<VertexPositionNormal, VertexTexture1, VertexEmpty>();
var c = new Vertex<VertexPositionNormal, VertexTexture1, VertexEmpty>();

primitive.AddTriangle(a,b,c);

The basic rule is that you create a mesh builder with the vertex flavours you want to use, and the vertices you use later for the triangles must match the mesh builder.

There's some basic documentation here.

And another example here.

Another detail I've noticed about your code: you're creating a new MeshBuilder for every material, and then calling CreateMeshes also for every material.

A single MeshBuilder supports multiple materials, so you can create only one mesh for all your export.

Also, CreateMeshes supports multiple meshes, and if you call it just once with all the meshes you're going to use in the model, it will optimize the meshes so they can render faster.

Imaver commented 5 years ago

Thank you very much for the extensive explanation!